1

I have a .csv file with a bunch of android tokens. For all of these tokens I want to check if they're still valid. In the end I want a .csv file with an extra column that's called "valid" which can be either 0 or 1. (0 if it's not, 1 if it is).

I want to check each of the tokens by the following command:

curl --header "Authorization: key=[my API key]" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"[my token]\"]}"

If the response is

{  
   "multicast_id":[  
      id
   ],
   "success":0,
   "failure":1,
   "canonical_ids":0,
   "results":[  
      {  
         "error":"InvalidRegistration"
      }
   ]
}

the token is invalid and I want the column "valid" to be set to 0.

How can I set up a script that automatically checks each token in the .csv separately and updates the column 'valid' with the right value?

Inian
  • 80,270
  • 14
  • 142
  • 161
QueryJoe
  • 21
  • 1
  • 2

2 Answers2

2

Assuming you can use GNU awk (that'll simplify CSV parsing), and your tokens are stored in the second column (notice $2 in awk line) of the tokens.csv:

#!/bin/bash

is_token_invalid() {
    args=(
        -s
        -H 'Authorization: key=[my API key]'
        -H 'Content-Type: application/json'
        -d '{"registration_ids": ["'"$1"'"]}'
        'https://android.googleapis.com/gcm/send'
    )
    curl "${args[@]}" | grep -q InvalidRegistration
}

awk -v FPAT='[^,]*|"[^"]+"' '{print $2}' tokens.csv | 
    while read token; do
        is_token_invalid "$token" && echo 1 || echo 0
    done | paste -d, tokens.csv -
randomir
  • 17,989
  • 1
  • 40
  • 55
1

Without knowing the format of your CSV file, it's difficult to give a solution. Using a mock up CSV file with 4 columns, you could do something very crudely like this:

$ cat test.csv
blah,blah,token1,blah
blah,blah,token2,blah
blah,blah,token3,blah
$ ./test.sh < test.csv
blah,blah,token1,blah,1
blah,blah,token2,blah,1
blah,blah,token3,blah,1

Code:

#!/usr/bin/env bash

set -euo pipefail

invalid_response () {
  cat << 'EOF'
{
   "multicast_id":[
      id
   ],
   "success":0,
   "failure":1,
   "canonical_ids":0,
   "results":[
      {
         "error":"InvalidRegistration"
      }
   ]
}
EOF
}

is_invalid_token () {
  declare token="${1:-}"
  [[ "$(curl -sSL \
    --header "Authorization: key=[my API key]" \
    --header Content-Type:"application/json" \
    https://android.googleapis.com/gcm/send \
    -d "{\"registration_ids\":[\"$token\"]}")" == "$(invalid_response)" ]]
}

main () {
  declare token="" valid=""
  while IFS="," read -r col1 col2 token col4 valid ; do
    valid=1
    if is_invalid_token "$token" ; then
      valid=0
    fi
    printf "$col1,$col2,$token,$col4,$valid\n"
  done
}

main "$@"

This isn't a great solution as it re-constructs every CSV line in its entirety, and it is comparing the JSON response against your literal string example.

It would better to know precisely what part of the response should be taken to mean valid or invalid, and use a tool like jq to parse the response for the specific data you want to test against.