forked from Tustin/PlayStationDiscord-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
174 lines (130 loc) · 5.8 KB
/
script.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sys, os, requests, re, json, urllib.request, urllib.error, hashlib, hmac, traceback, logging, shutil
from pytablewriter import MarkdownTableWriter
# key for tdmb link generation (from ps3, ps4)
tmdb_key = bytearray.fromhex('F5DE66D2680E255B2DF79E74F890EBF349262F618BCAE2A9ACCDEE5156CE8DF2CDF2D48C71173CDC2594465B87405D197CF1AED3B7E9671EEB56CA6753C2E6B0')
title_ids = []
print('checking games.txt for custom titles...')
with open('games.txt', 'r') as game_reader:
for line in game_reader.readlines():
line = line.strip()
if line.startswith('#'):
continue
line = line.split('#', 1)[0].strip()
title_ids.append(line)
print(f'added {len(title_ids)} games from games.txt')
urls = [
# Top 50 Games
"https://store.playstation.com/valkyrie-api/en/US/19/container/STORE-MSF77008-TOPGAMES?size=200&bucket=games&start=0&gameContentType=games&platform=ps4",
# PS+ Games
"https://store.playstation.com/valkyrie-api/en/US/19/container/STORE-MSF77008-PSPLUSFREEGAMES?size=30&bucket=games&start=0&platform=ps4",
# Top 50 digital only games
"https://store.playstation.com/valkyrie-api/en/US/19/container/STORE-MSF77008-TOPPSNGAMES?size=50&bucket=games&start=0&platform=ps4",
# 10 newest free games
"https://store.playstation.com/valkyrie-api/en/US/19/container/STORE-MSF77008-GAMESFREETOPLAY?sort=release_date&direction=desc&size=10&bucket=games&start=0&platform=ps4",
# Newest games this month
"https://store.playstation.com/valkyrie-api/en/US/19/container/STORE-MSF77008-NEWTHISMONTH?game_content_type=games&size=100&bucket=games&start=0&platform=ps4",
# Coming soon
"https://store.playstation.com/valkyrie-api/en/US/19/container/STORE-MSF77008-PS3PSNPREORDERS?gameContentType=games&gameType=ps4_full_games%2Cpsn_games&releaseDate=coming_soon%2Clast_30_days&platform=ps4"
]
image_dir = 'ps4'
def create_url(title_id):
hash = hmac.new(tmdb_key, bytes(title_id, 'utf-8'), hashlib.sha1)
return f'https://tmdb.np.dl.playstation.net/tmdb2/{title_id}_{hash.hexdigest().upper()}/{title_id}.json'
if __name__ == '__main__':
log = logging.getLogger(__name__)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('[%(asctime)s] %(message)s'))
handler.setLevel(logging.INFO)
log.addHandler(handler)
log.setLevel(logging.INFO)
discord_title_ids = []
done = {"ps4": []}
table_writer = None
if os.path.isfile('README.template'):
table_writer = MarkdownTableWriter()
table_writer.headers = ["Icon", "Title"]
table_writer.value_matrix = []
else:
print('missing README.template. wont update README.md file.')
if os.path.exists(image_dir):
shutil.rmtree(image_dir)
for url in urls:
print(f'--- {url} ---')
content = requests.get(url).json()
for item in content['included']:
info = item['attributes']
if 'thumbnail-url-base' not in info:
continue
if 'game' not in str(info['game-content-type']).lower():
continue
print(info['name'])
rating = info['star-rating']
if not rating['total']:
print('\tno ratings')
continue
if rating['total'] < 10 or rating['score'] < 4:
print('\tfailed rating check')
continue
match = re.search(r'([A-Z]{4}[0-9]{5}_00)', info['default-sku-id'])
if not match:
print('\tfailed regex check')
continue
title_id = match.group(1)
if title_id not in title_ids:
title_ids.append(title_id)
print('\tadded to list')
else:
print('\talready added')
# added all the titleIds... now get their images
for title_id in title_ids:
url = create_url(title_id)
print(url)
content = requests.get(url)
if content.status_code != 200:
print('skipping', title_id)
continue
try:
content = content.json()
except ValueError:
# Sometimes the json for a game can be empty for some reason. Just remove it from the list.
title_ids.remove(title_id)
print('removed')
continue
game_name = content['names'][0]['name']
print(game_name)
if not content['icons'] or len(content['icons']) == 0:
print('\tno icons')
continue
game_icon = None
for icon in content['icons']:
if icon['type'] == '512x512':
game_icon = icon['icon']
break
if game_icon == None:
print('\tno 512x512 icon')
continue
done["ps4"].append({
"name": game_name,
"titleId": title_id
})
discord_title_ids.append(title_id.lower())
if not os.path.exists(image_dir):
os.mkdir(image_dir)
icon_file = f'{image_dir}/{title_id}.png'
if table_writer != None:
table_writer.value_matrix.append([
f'<img src="{icon_file}?raw=true" width="100" height="100">',
game_name
])
if os.path.exists(icon_file):
print('\ticon file exists')
continue
urllib.request.urlretrieve(game_icon, icon_file)
print('\tsaved')
if table_writer != None:
with open("README.template", "rt") as template:
with open('README.md', 'wt', encoding='utf-8') as readme:
for line in template:
readme.write(line.replace('!!games!!', table_writer.dumps()))
with open('games.json', 'w') as games_file:
json.dump(done, games_file)