-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameId.py
102 lines (92 loc) · 2.83 KB
/
gameId.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
##############################################
### Get gameId from API. Save it to gameId.txt
##############################################
# import modules
import websocket
import json
import time
from functions import read, write, reset_score, initial_call
initial_call()
# setup variables
path = 'data/'
url = read('apiURL.txt')
gameId = ''
ignorList =[]
singleCourt = True
# make subscribe message
subscribe = {}
subscribe['apiName'] = "TvFeedApiV3"
subscribe['apiCommand'] = "subscribe"
subscribe['apiKey'] = read('apiKey.txt')
subscribe['requestId'] = "test connection"
subscribe['eventId'] = read('eventId.txt')
# setup WebSocket
ws = websocket.WebSocket()
# start ws connection
ws.connect(url)
# subscribe to feed
ws.send(json.dumps(subscribe))
# initialize files
write('Home', path + 'homeTeamName.txt')
write('Away', path + 'awayTeamName.txt')
reset_score()
# main loop
while(True):
# get data
ans = ws.recv()
try:
data = json.loads(ans)['data']
except KeyError:
print(ans)
break
# get team info
try:
for i in data:
if data[i]['dataType'] == 'game' and i not in ignorList:
game_info = data[i]
gameName = game_info['gameName']
homeTeamId = game_info['homeTeamId']
homeTeamName = data[homeTeamId]['teamName']
awayTeamId = game_info['awayTeamId']
awayTeamName = data[awayTeamId]['teamName']
print('New game found: {}, {} vs. {}'.format(gameName, homeTeamName, awayTeamName))
if singleCourt:
print('*** New game selected ***')
else:
input_var = input('Make this new game the current game? No will ignor this game. [Y/n]')
if singleCourt:
write(i, path + 'gameId.txt')
homeTeamId = game_info['homeTeamId']
awayTeamId = game_info['awayTeamId']
write(homeTeamId, path + 'homeTeamId.txt')
write(awayTeamId, path + 'awayTeamId.txt')
write(gameName, path + 'gameName.txt')
try:
write(data[homeTeamId]['teamName'], path + 'homeTeamName.txt')
except UnicodeEncodeError:
pass
try:
write(data[awayTeamId]['teamName'], path + 'awayTeamName.txt')
except UnicodeEncodeError:
pass
reset_score()
elif input_var.lower() == 'y':
write(i, path + 'gameId.txt')
homeTeamId = game_info['homeTeamId']
awayTeamId = game_info['awayTeamId']
write(homeTeamId, path + 'homeTeamId.txt')
write(awayTeamId, path + 'awayTeamId.txt')
write(gameName, path + 'gameName.txt')
try:
write(data[homeTeamId]['teamName'], path + 'homeTeamName.txt')
except UnicodeEncodeError:
pass
try:
write(data[awayTeamId]['teamName'], path + 'awayTeamName.txt')
except UnicodeEncodeError:
pass
reset_score()
elif input_var.lower() == 'n':
ignorList.append(i)
except (KeyError, NameError):
pass