/
run.py
71 lines (66 loc) · 2.82 KB
/
run.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
from PIL import Image, ImageChops
from bs4 import BeautifulSoup
from io import BytesIO
import urllib.request
import requests
import json
import re
def flairAdded(newImage):
"""Compare fetch of flairs with an existing copy to see if new flairs were added.
Args:
newImage (Image): picture containing result of request for flairs.
Returns:
Boolean: result of comparing images.
"""
# Look for differences between images.
diff = ImageChops.difference(newImage, Image.open('original.png'))
# New flair was added.
if diff.getbbox():
# Overwrite backup with new image.
newImage.save('original.png', 'PNG')
return True
# No difference between the images.
else:
return False
def getAllFlairs():
"""Determine location of all flairs.
Returns:
Dictionary: key-value pairs where keys are flair names and values are lists containing x and y offsets.
"""
# Open up a profile to access the full list of flairs.
html_page = urllib.request.urlopen("https://tagpro.koalabeast.com/profile/548a41cee27aa91f22d731a8")
soup = BeautifulSoup(html_page, "html.parser")
result = dict()
# Find the tab containing all flairs.
all_flairs = soup.find('div', {'id':'all-flair'})
# Flair names and offsets can be found in the span elements.
for div in all_flairs.findAll('li'):
span = div.find('span')
# Replace hyphens with periods to stick to flair names elsewhere.
name = span['class'][1].replace('-','.')
# Regular expression to extract only digits and '-'s from offsets.
digit_filter = re.compile(r'[^\d\-]+')
# Offsets are found in the background-position attribute of the style tag.
offset = span['style'].split(' ')[1:]
# Perform regex matching and store their results as integers.
offset[0] = int(digit_filter.sub('', offset[0]))
offset[1] = int(digit_filter.sub('', offset[1]))
result[name] = dict()
result[name]['offset'] = offset
# Extract flair description.
description = div.find('div', {'class':'flair-description'}).getText().strip()
result[name]['description'] = description
return result
def main():
# Load the flairs spritesheet from Tagpro's website.
spritesheet = Image.open(BytesIO(requests.get('https://tagpro.koalabeast.com/images/flair.png').content))
# Proceed only if a new flair has been added.
if flairAdded(spritesheet):
# Save an upscaled version of the spritesheet.
width, height = spritesheet.size
spritesheet.resize((width * 3, height * 3), resample=Image.NEAREST).save('upscaled.png', 'PNG')
# Save the flair offsets to a json file.
with open('flairLocations.json', 'w') as fp:
json.dump(getAllFlairs(), fp)
if __name__ == "__main__":
main()