我正在尝试从第三方API收集数据,对其进行解析,并将每个游戏的数据点存储在firestore集合中。
到目前为止,我已经得到了下面的代码,它可以工作,但只写入最后一个游戏的数据,而不是提要中可用的所有游戏的数据。
我检查了API调用和解析代码,它返回了正确的数据。所以我认为这与代码中的写到firebase部分有关。
需要为现有文档更新数据,或者如果游戏数据尚未在集合中,则需要创建新文档。API将每天调用几次,以确保数据是最新的。
任何帮助都将不胜感激。谢谢
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)
使用for循环时,确保所有需要的操作都正确缩进,并确保它在其中。doc.set()
与for循环内联,使for循环首先完成,并且只编写或更新最后一个游戏。请参阅下面的固定代码:
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)