JSON – Pull #FIRSTInspires data from @thebluealliance API

| | | | | |

Working on a couple tasks that need team information so I registered a read-only API key and started pulling down data for an offline app.

Initially populating data using bash and jq. This may be cleaned up in time but it works for my purpose for now.

Currently I’m just creating the create and update SQL files so I can spot check them but I plan to get them directly into a database in time.

#!/bin/sh

#  FRC_Team_List.sh
#
#
#  Created by David Kittell on 4/16/18.
#

function UpdateTeamData
{
# Variables - Start
  TeamNum=$1
  apiURL="https://www.thebluealliance.com/api/v3/team/frc"
  apiURL="$apiURL$TeamNum"
  apiToken="<your API key>"
# Variables - Done

# Get Data - Start
  data=$(curl -s --request GET --url "${apiURL}" --header "X-TBA-Auth-Key: ${apiToken}" --header 'accept: application/json')
  #echo $data
# Get Data - Done

# Use Data - Start
  nickname=$(echo $data | jq '.nickname')
  if [ ! -z "$nickname" ]
  then
    sql="UPDATE teams SET nickname = $nickname WHERE team_number = $TeamNum;"
    echo $sql >> UpdateTeamRecord.sql
  fi

  city=$(echo $data | jq '.city')
  if [ ! -z "$city" ]
  then
    sql="UPDATE teams SET city = $city WHERE team_number = $TeamNum;"
    echo $sql >> UpdateTeamRecord.sql
  fi

  state_prov=$(echo $data | jq '.state_prov')
  if [ ! -z "$state_prov" ]
  then
    sql="UPDATE teams SET state_prov = $state_prov WHERE team_number = $TeamNum;"
    echo $sql >> UpdateTeamRecord.sql
  fi

  country=$(echo $data | jq '.country')
  if [ ! -z "$country" ]
  then
    sql="UPDATE teams SET country = $country WHERE team_number = $TeamNum;"
    echo $sql >> UpdateTeamRecord.sql
  fi

  postal_code=$(echo $data | jq '.postal_code')
  if [ ! -z "$postal_code" ]
  then
    sql="UPDATE teams SET postal_code = $postal_code WHERE team_number = $TeamNum;"
    echo $sql >> UpdateTeamRecord.sql
  fi

  rookie_year=$(echo $data | jq '.rookie_year')
  if [ ! -z "$rookie_year" ]
  then
    sql="UPDATE teams SET rookie_year = $rookie_year WHERE team_number = $TeamNum;"
    echo $sql >> UpdateTeamRecord.sql
  fi

  website=$(echo $data | jq '.website')
  if [ ! -z "$website" ]
  then
    sql="UPDATE teams SET website = $website WHERE team_number = $TeamNum;"
    echo $sql >> UpdateTeamRecord.sql
  fi

# Use Data - Done
}

function InsertTeamData
{
# Variables - Start
  TeamNum=$1
  apiURL="https://www.thebluealliance.com/api/v3/teams/"
  apiURL="$apiURL$TeamNum"
  #echo $apiURL
  apiToken="<your API key>"
# Variables - Done

# Get Data - Start
#data=$(curl -s --request GET --url "${apiURL}" --header "X-TBA-Auth-Key: ${apiToken}" --header 'accept: application/json')
#echo $data
# Get Data - Done

# Use Data - Start

  curl -s --request GET --url "${apiURL}" --header "X-TBA-Auth-Key: ${apiToken}" --header 'accept: application/json' > data.json
  for tnum in `jq -r ".[] | .team_number" data.json`; do
    echo "INSERT IGNORE INTO teams (team_number) VALUES ($tnum);"
    UpdateTeamData $tnum
  done
# Use Data - Done
}

InsertTeamData 0 > CreateTeamRecord.sql
InsertTeamData 1 >> CreateTeamRecord.sql
InsertTeamData 2 >> CreateTeamRecord.sql
InsertTeamData 3 >> CreateTeamRecord.sql
InsertTeamData 4 >> CreateTeamRecord.sql
InsertTeamData 5 >> CreateTeamRecord.sql
InsertTeamData 6 >> CreateTeamRecord.sql
InsertTeamData 7 >> CreateTeamRecord.sql
InsertTeamData 8 >> CreateTeamRecord.sql

Originally Posted on April 16, 2018
Last Updated on March 9, 2020
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.