-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameStatsParser.py
62 lines (54 loc) · 1.95 KB
/
GameStatsParser.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
import argparse
import json
import requests
import settings
import mongoTools
def main():
parser = argparse.ArgumentParser()
def convert(argument):
return list(map(str, argument.split(', ')))
parser.add_argument('games', nargs='+', type=convert)
args = parser.parse_args()
gameparser = GamesParser(args.games)
gameparser.gamelist()
class GamesParser:
def __init__(self, games):
self.games = games[0]
self.appid = settings.PlaysTvAppId
self.appkey = settings.PlaysTvKey
def gamelist(self):
call = 'https://api.plays.tv/data/v1/games?appid={0}&appkey={1}'.format(self.appid, self.appkey)
print(self.games)
r = requests.get(call)
if r.status_code != 404:
if r.status_code == 200:
#print(r.text)
self.parse_gamelist(r.text)
elif r.status_code == 403:
return 'forbidden'
else:
return 'error {}'.format(r.status_code)
else:
return 'down'
def parse_gamelist(self, gamelist):
game_data = json.loads(gamelist)
for i in game_data['content']['games'].values():
if i['title'] in self.games:
name = i['title']
id = i['id']
videos = i['stats']['videos']
js = {'id': id, 'videos': videos, 'name': name}
db = mongoTools.MongoDbTools('GameStats')
#print(db)
current = db.read_videodata_from_db({"name": name})
#print(current)
if len(current) == 0:
db.write_videodata_to_db(js)
#else:
elif current['videos'][0] < videos:
#print(current)
#print(current['id'][0], db.read_videodata_from_db({"id": current['id'][0]}))
db.replace_videodata_from_db(current['id'][0], js)
return
if __name__ == "__main__":
main()