-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit-hunter.py
308 lines (258 loc) · 10.1 KB
/
bit-hunter.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
# Bit-Hunter by Alex Camilleri.
from io import BytesIO
import json
import os
import sys
from PIL import Image, ImageDraw
from bs4 import BeautifulSoup
from pathlib import Path
import requests
import csv
import re
from errors import GameNotFoundError, InputError
def check_folders():
"""Creates image folders if any is missing"""
folders = ('/consume', '/originals', '/processed')
mkdir = False
for i, path in enumerate(folders):
if (os.path.exists('.'+path)):
pass
else:
mkdir = True
os.makedirs('.'+path)
print("Generating "+path+" folder...")
if (i == len(folders)-1) and mkdir:
print("-------------------------------")
def load_config():
"""Loads config file or creates a new one
if none can be found"""
data = {}
try:
with open('config.json') as json_data_file:
data = json.load(json_data_file)
except FileNotFoundError:
print("Could not find config.json. Generating one with default values...")
data = {
'exportTrophyInfo': True,
'storeOriginals': False,
'processOriginals': True,
'acceptedTypes': ['.PNG', '.JPG', '.JPEG'],
'frameThickness': 15,
'exportSizes': [240],
'exportTypes': ['.PNG'],
'imageNameRoot': '',
'imageNameEnd': ''
}
with open('config.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
return data
check_folders()
config = load_config()
exportTrophyInfo = config.get('exportTrophyInfo')
storeOriginals = config.get('storeOriginals')
processOriginals = config.get('processOriginals')
acceptedTypes = config.get('acceptedTypes')
frameThickness = config.get('frameThickness')
exportSizes = config.get('exportSizes')
exportTypes = config.get('exportTypes')
imageNameRoot = config.get('imageNameRoot')
imageNameEnd = config.get('imageNameEnd')
class Game:
def __init__(self, _id):
self.id, self.name, self.platform = _id, None, None
self.trophies = []
def get_soup(self):
"""Pulls HTML data from game page"""
URL = "https://psnprofiles.com/trophies/"+str(self.id)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"}
page = requests.get(URL, headers=headers)
return BeautifulSoup(page.content, 'html.parser')
def get_name(self, _soup):
titles = _soup.findAll("div", class_="title flex v-align center")
if titles == []:
raise GameNotFoundError(self.id)
for title in titles:
info = str(title)
self.name = info[info.find(
'<h3>')+4:info.find(" Trophies")].replace('&', '&')
break
def get_all_trophies(self, _soup):
"""Stores information of every trophy"""
tables = _soup.find_all(
'table', class_='zebra')
for table in tables:
rows = table.find_all('tr', class_='')
for row in rows:
columns = row.find_all('td')
if len(columns) == 6:
name = columns[1].find('a').get_text().strip()
desc = columns[1].get_text()[len(name)+1:].strip()
URL = "https://psnprofiles.com" + \
columns[1].find('a').get('href')
type_ = columns[5].find('img').get('title')
trophy = Trophy(name, desc, type_, URL)
self.trophies.append(trophy)
def export_data_to_csv(self):
"""Exports trophy list to csv file"""
filename = str(self.id)+'-'+slugify(self.name)+'.csv'
with (open(filename, 'w', newline='')) as f:
writer = csv.writer(f)
writer.writerow(['Name', 'Description', 'Type'])
for trophy in self.trophies:
writer.writerow([trophy.name, trophy.desc, trophy.type])
print("Game trophies info exported to " + filename)
def process_all_trophies(self):
"""Scrapes image and apply frame to every image"""
for trophy in self.trophies:
if trophy.scrape():
if storeOriginals:
store_remote_image(trophy.imageURL)
if processOriginals:
process_image(trophy.imageURL, False,
self.name, trophy.name)
class Trophy:
def __init__(self, _name='', _desc='', _type='', _url=''):
self.name, self.desc, self.type, self.URL = _name, _desc, _type, _url
self.imageURL = None
def scrape(self):
"""Scrapes URL of HD trophy image"""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"}
try:
page = requests.get(self.URL, headers=headers)
except requests.exceptions.RequestException as e:
print("Invalid get request for "+self.URL+"\n")
return False
soup = BeautifulSoup(page.content, 'html.parser')
blocks = soup.find_all('td')
block = str(blocks[0])
snippet = block[block.find('href="')+6:]
self.imageURL = snippet[:snippet.find('"')]
print("\nTrophy:\n"+self.name+" ("+self.type+")\n"+self.desc)
return True
def consume_images():
"""
Chews images from the /consume folder.
Only chews accepted filetypes defined in config file
"""
for r, d, f in os.walk('./consume/'):
for file in f:
filename, file_extension = os.path.splitext(file)
canChew = any(s in file_extension.upper()
for s in acceptedTypes)
if canChew:
path = os.path.join(r, file)
process_image(path, True)
def slugify(value):
"""Removes invalid characters from string"""
value = re.sub(r'[^\w\s-]', '', value.lower())
return re.sub(r'[-\s]+', '-', value).strip('-_').replace('.', '')
def store_remote_image(_imageURL):
"""Store image from URL"""
URL = _imageURL
filename = URL.split('/')[-1]
response = requests.get(URL)
with open(os.path.join('./originals/', filename), 'wb') as f:
f.write(response.content)
def process_image(_imageURL='', _local=False, _game='', _trophy=''):
"""Add frame to images"""
print("Processing "+_imageURL+"...")
try:
imgFrame = Image.open('./frame.png')
except IOError:
print("Could not find frame image. Using default one.")
imgFrame = Image.new('RGB', (240, 240), color='#fff')
URL = _imageURL
if (_local):
img = URL
else:
response = requests.get(URL, headers={'Cache-Control': 'no-cache'})
img = BytesIO(response.content)
imgTrophy = Image.open(img)
imgTrophy_w = int(min(imgTrophy.width, imgFrame.width))
imgTrophy_h = int(min(imgTrophy.height, imgFrame.height))
imgTrophyResized_size = (
imgTrophy_w-(frameThickness*2), imgTrophy_h-(frameThickness*2))
imgTrophyResized = imgTrophy.resize(imgTrophyResized_size)
imgFinal = Image.new('RGB', imgFrame.size, color='#fff')
imgFinal.paste(imgFrame)
imgFinal.paste(imgTrophyResized, (frameThickness, frameThickness))
for i, size in enumerate(exportSizes):
imgResized = imgFinal.resize((size, size))
for exportType in exportTypes:
filename = URL.split('/')[-1]
name, extension = os.path.splitext(filename)
name = slugify(name)
trophy = slugify(_trophy)
game = slugify(_game)
root = imageNameRoot.replace(
'@g', game).replace('@t', trophy).replace('@s', str(size))
if root != '':
root = root+'-'
name = root+name
end = imageNameEnd.replace(
'@g', game).replace('@t', trophy).replace('@s', str(size))
if end != '':
end = '-'+end
filename = name+end+exportType.lower()
try:
final = imgResized.save(
'./processed/'+filename, exportType[1:])
except KeyError:
if exportType == ".JPG":
if ".JPEG" in exportTypes:
print("Cannot export "+filename+" to JPG.")
else:
print("Cannot export "+filename +
"to JPG. Exporting to JPEG.")
final = imgResized.save(
'./processed/'+filename, 'JPEG')
else:
print("File format "+exportType+" not supported.")
intro = """
-----------------------
__________________ ______ __ _____
___ __ )__(_)_ /_ ___ / / /___ __________ /_____________
__ __ |_ /_ __/________ /_/ /_ / / /_ __ \ __/ _ \_ ___/
_ /_/ /_ / / /_ _/_____/ __ / / /_/ /_ / / / /_ / __/ /
/_____/ /_/ \__/ /_/ /_/ \__,_/ /_/ /_/\__/ \___//_/
-----------------------
"""
prompt = """
Input Game ID or type 0 to consume local images:
"""
def check_input(input):
if input.isdigit() == False:
if (user_input == 'exit' or user_input == 'q'):
sys.exit()
else:
raise InputError()
print(intro)
while True:
user_input = input(prompt)
try:
check_input(user_input)
except InputError:
pass
else:
gameID = user_input
if (gameID == 0):
consume_images()
print("\nAll the trophy images have been processed!\n\n")
else:
game = Game(gameID)
soup = game.get_soup()
try:
game.get_name(soup)
except GameNotFoundError:
pass
else:
print("\nGame Title: "+game.name+"\n")
game.get_all_trophies(soup)
if (exportTrophyInfo):
game.export_data_to_csv()
game.process_all_trophies()
print("\n\nAll the trophy images for " +
game.name+" have been processed!\n\n")