-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrapeViewCounts.py
58 lines (50 loc) · 1.56 KB
/
ScrapeViewCounts.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
#!/usr/bin/env python
import requests
import requests.auth
import json
from bson.json_util import dumps as darkdump # ばかな!
import threading
import sys
import pymongo
import csv
'''This function flattens the multi-tiered JSON
objects returned from Twitch's API into a
single-layered object that's fit for a CSV
file.'''
def flatten4csv(input):
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(input)
return out
def connectAndScrape(gameName):
try:
client = pymongo.MongoClient (host='da1.eecs.utk.edu', port=9109)
db = client['D2Discovery']
twitchstreams = db.twitchstreams
except pymongo.errors.ServerSelectionTimeoutError:
print("Error: Unable to connect to da1")
twitchstreams = None
if twitchstreams is not None:
objflat = twitchstreams.find_one()
#objflat = flatten4csv(db.twitchstreams.find({"streams": {"game": gameName}}))
return(objflat)
def queryGames():
## Open the config file and build game queries from its contents. ##
viewsOfGames = []
gamefile = open("gamelist.txt", 'r')
graphfile = open("graphfile.txt", 'wb')
#for name in gamefile:
# print(connectAndScrape(name))
connectAndScrape(viewsOfGames)
if __name__ == '__main__':
queryGames()