-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.py
executable file
·138 lines (114 loc) · 5.14 KB
/
crawler.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
import urllib.request as r
import urllib.error
import json
import os.path
import csv
from datetime import datetime
class StatsCrawler:
def __init__(self, config):
self.config = config
self.api = 'https://api.nexusmods.com'
self.dl_stats_csv = 'https://staticstats.nexusmods.com/live_download_counts/files/{:d}.csv?cachesteamp={:d}'
self.mod_stats_json = 'https://staticstats.nexusmods.com/mod_monthly_stats/{:d}/{:d}.json'
self.endpoint = { 'files': '/v1/games/{:s}/mods/{:d}/files.json?category={:s}',
'details': '/v1/games/{:s}/mods/{:d}/files/{:d}.json',
'games': '/v1/games/{:s}.json',
'mod': '/v1/games/{:s}/mods/{:d}.json' }
self.spoof = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36' }
self.games = self.config.games
self.mods = self.config.mods
self.headers = { 'accept': 'application/json', 'apikey': self.config.apikey }
self.game_id_file = self.config.game_id_file
self.gameid = self.getGameIDs()
def getGameIDs(self):
if os.path.isfile(self.game_id_file):
with open(self.game_id_file, 'r') as f:
gameid = json.load(f)
else:
try:
gameid = dict()
for g in self.games:
# get game id for later
url = self.api + self.endpoint['games'].format(g)
req = r.Request(url, headers=self.headers)
response = r.urlopen(req)
gameid[g] = json.loads(response.read()).get('id')
with open(self.game_id_file, 'w') as f:
json.dump(gameid, f)
except urllib.error.HTTPError as e:
print("Couldn't fetch game IDs! Details:")
print(e)
exit(1)
return gameid
def getStats(self, time=datetime.now()):
endorsements = { g: dict() for g in self.games }
files = { g: { m: dict() for m in self.mods[g] } for g in self.games }
stats = { g: dict() for g in self.games }
mod_stats = { g: dict() for g in self.games }
for g in self.games:
for m in self.mods[g].keys():
try:
# get mod stats
url = self.api + self.endpoint['mod'].format(g, m)
req = r.Request(url, headers=self.headers)
response = r.urlopen(req)
endorsements[g][m] = json.loads(response.read())
except urllib.error.HTTPError as e:
print(e)
for g in self.games:
for m,categories in self.mods[g].items():
try:
# get list of files
url = self.api + self.endpoint['files'].format(g, m, ','.join(categories))
req = r.Request(url, headers=self.headers)
response = r.urlopen(req)
l = json.loads(response.read()).get('files', [])
for f in l:
files[g][m][f.get('file_id', 0)] = f
except urllib.error.HTTPError as e:
print(e)
# match nexus' cache busting timestamp format
timestamp = int(time.timestamp() * 1000000 / 300000000)
for g in self.games:
# get download stats
try:
with r.urlopen(r.Request(self.dl_stats_csv.format(self.gameid[g],timestamp), headers=self.spoof)) as dl_csv:
# decode binary format
dl_csv = dl_csv.read().decode('utf-8').split('\n')
for row in csv.reader(dl_csv):
# remove lines that don't match the expected id,dls,unique format
if len(row) < 3:
continue
stats[g][int(row[0])] = { 'downloads': int(row[1]), 'unique_downloads': int(row[2])}
except urllib.error.HTTPError as e:
print(e)
# fill info in with file details
for g,mod in files.items():
for m,f in mod.items():
for i,v in f.items():
if g in stats:
if i in stats[g]:
v.update(stats[g][i])
for g in self.games:
for m in self.mods[g]:
# get mod stats
url = self.mod_stats_json.format(self.gameid[g],m)
req = r.Request(url, headers=self.spoof)
dl_json = r.urlopen(req)
mod_stats[g][m] = json.loads(dl_json.read())
return endorsements, mod_stats, files
if __name__ == '__main__':
from config import StatsConfig
from pprint import pprint
config = StatsConfig()
crawler = StatsCrawler(config)
endorsements, mod_stats, files_stats = crawler.getStats()
pprint(endorsements)
print()
pprint(mod_stats)
print()
for n,g in files_stats.items():
print('\n', n)
for f,v in g.items():
pprint(v)