I am trying to collect data from a 3rd party API, parse it and store the data points for each game in firestore collection.
So far I have got the code below which works but only writes the data for the last game instead of all the games available in the feed.
I have checked the API call and parsing code and it returns the correct data. So I assume it has something to do with the write to firebase section of the code.
The data needs to be updated for existing documents, or a new one created if the game data is not already in the collection. The API will be called a few times a day to ensure data is up to date.
any help would be appreciated. thanks.
import requests
from google.cloud import firestore
import json
client = firestore.Client(project='PROJECTNAME')
def bluebet_nba_odds(bluebet_nba_oods):
# MAKE VARIABLES GLOBAL
global away_team_win_odds, away_team, home_team_win_odds, home_team, start_time, event_title, event_id, competition
# API CALL
link = 'https://xxxxxxxxxx.com.au/json/reply/MasterCategoryRequest?EventTypeID=107&WithLevelledMarkets' \
'=true&WithLevelledMarkets=true '
# Request data from link as 'str'
nbadata = requests.get(link).text
# convert 'str' to Json
nbadata = json.loads(nbadata)
# JSON PARSE
for nba_odds in nbadata['MasterCategories'][2]['Categories'][0]['MasterEvents']:
competition = nba_odds['CategoryName']
event_id = nba_odds['MasterEventId']
event_title = nba_odds['MasterEventName']
start_time = nba_odds['MaxAdvertisedStartTime']
home_team = nba_odds['Markets'][0]['OutcomeName']
home_team_win_odds = nba_odds['Markets'][0]['Price']
away_team = nba_odds['Markets'][1]['OutcomeName']
away_team_win_odds = nba_odds['Markets'][1]['Price']
# WRITE TO FIRESTORE
doc = client.collection('bluebet_nba_season_odds').document()
doc.set({
'competition': competition,
'event_id': event_id,
'event_title': event_title,
'start_time': start_time,
'home_team': home_team,
'home_team_win_odds': home_team_win_odds,
'away_team': away_team,
'away_team_win_odds': away_team_win_odds,
})
if __name__ == "__main__":
bluebet_nba_odds(bluebet_nba_odds)
CodePudding user response:
When using for loop, ensure that all the needed actions are properly indented and make sure it's inside of it. The doc.set()
is inline with the for loop which makes the for loop finish first and only the last game is being written or updated. See the fixed code below:
import requests
from google.cloud import firestore
import json
client = firestore.Client(project='PROJECTNAME')
def bluebet_nba_odds(bluebet_nba_oods):
# MAKE VARIABLES GLOBAL
global away_team_win_odds, away_team, home_team_win_odds, home_team, start_time, event_title, event_id, competition
# API CALL
link = 'https://xxxxxxxxxx.com.au/json/reply/MasterCategoryRequest?EventTypeID=107&WithLevelledMarkets' \
'=true&WithLevelledMarkets=true '
# Request data from link as 'str'
nbadata = requests.get(link).text
# convert 'str' to Json
nbadata = json.loads(nbadata)
# JSON PARSE
for nba_odds in nbadata['MasterCategories'][2]['Categories'][0]['MasterEvents']:
competition = nba_odds['CategoryName']
event_id = nba_odds['MasterEventId']
event_title = nba_odds['MasterEventName']
start_time = nba_odds['MaxAdvertisedStartTime']
home_team = nba_odds['Markets'][0]['OutcomeName']
home_team_win_odds = nba_odds['Markets'][0]['Price']
away_team = nba_odds['Markets'][1]['OutcomeName']
away_team_win_odds = nba_odds['Markets'][1]['Price']
# WRITE TO FIRESTORE
# This would write to Firestore on every iteration instead of completing the loop first and then writing.
doc = client.collection('bluebet_nba_season_odds').document()
doc.set({
'competition': competition,
'event_id': event_id,
'event_title': event_title,
'start_time': start_time,
'home_team': home_team,
'home_team_win_odds': home_team_win_odds,
'away_team': away_team,
'away_team_win_odds': away_team_win_odds,
})
if __name__ == "__main__":
bluebet_nba_odds(bluebet_nba_odds)