/
match_data.py
117 lines (91 loc) · 3.91 KB
/
match_data.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import boto3
from league_api import Summoner, Game, NotFoundException
import pickle
import time
from queue import Queue
table_name = 'league_data'
attribute_name = 'match_id'
class DataCollector:
def __init__(self, summoner_name):
self.summoner_name = summoner_name.lower().replace(' ', '')
self.game = Game()
self.dynamo_controller = DynamoData()
try:
self.table = self.dynamo_controller.get_table(self.summoner_name)
except ValueError:
self.table = self.dynamo_controller.setup_table(self.summoner_name, attribute_name)
self.output = Queue()
def start_tracking(self):
summoner = Summoner()
summoner_info = summoner.get_summoners_info_by_names([self.summoner_name])
summoner_id = summoner_info[self.summoner_name]['id']
current_match_id = None
while True:
print('Tracking outer loop')
try:
current_match = self.game.get_current_match(summoner_id)
self.track_in_game(current_match, summoner_id)
except NotFoundException:
time.sleep(600)
def track_in_game(self, current_match, summoner_id):
def player_mapper(p_id, participant):
player = {'summonerId': participant['summonerId'],
'summonerName': participant['summonerName'],
'profileIcon': participant['profileIconId']}
return {'participantId': p_id, 'player': player}
match_id = current_match['gameId']
match_id_temp = match_id
while match_id_temp == match_id:
print('Tracking inner %d' % match_id)
try:
current_match_temp = self.game.get_current_match(summoner_id)
print('ANOTHER MATCH WAS FOUND')
match_id_temp = current_match_temp['gameId']
except NotFoundException:
break
time.sleep(60)
print('TRACKING INNER DONE')
print('CURRENT_MATCH %s' % str(current_match))
counter = 0
match_info = None
while True:
print('TRYING TO GET MOST RECENT ENDED GAME')
try:
match_info = self.game.get_match_info(match_id)
break
except NotFoundException:
counter += 1
time.sleep(10)
if counter > 20:
return
print('GOT MOST RECENT ENDED GAME')
pickle.dump((current_match, match_info), open('saved.p', 'wb'))
player_map = {(p['championId'], p['teamId']): p for p in current_match['participants']}
game_data = {'match_id': match_id, 'match_info': match_info}
self.table.put_item(Item=game_data)
# populate participant identities, ruck fito
match_info['participantIdentities'] = [player_mapper(p['participantId'], player_map[p['championId'], p['teamId']]) for p in match_info['participants']]
self.table.put_item(Item=game_data)
self.output.put(match_info)
# TODO: Replace this a wait/notify method instead
def get_game(self):
while self.output.empty():
time.sleep(10)
return self.output.get()
class DynamoData:
def __init__(self):
self.dynamodb = boto3.resource('dynamodb')
def get_table(self, table_name):
table = self.dynamodb.Table(table_name)
if table in self.dynamodb.tables.all():
return table
else:
raise ValueError('Table %s not yet created' % table_name)
def setup_table(self, table_name, attribute_name):
table = self.dynamodb.create_table(
TableName=table_name,
KeySchema=[dict(AttributeName=attribute_name, KeyType='HASH')],
AttributeDefinitions=[dict(AttributeName=attribute_name, AttributeType='N')],
ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
)
return table