forked from SiongSng/mc-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_icons.py
63 lines (47 loc) · 1.8 KB
/
get_icons.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
from bs4 import BeautifulSoup
from requests import get
from PIL import Image
import tinycss
import re
# To generate images you need to install these:
# pip install bs4
# pip install requests
# pip install pil
# pip install tinycss
# Where to save pics?
path = "pics/[file].png"
# -----------------------------------------------------
base_url = "https://minecraft-ids.grahamedgecombe.com/"
base_url_html = get(base_url).text
css_parser = tinycss.make_parser('page3')
soup = BeautifulSoup(base_url_html, "html.parser")
items = soup.find_all("tr", {"class": "row"})
stylesheets = [base_url + style["href"]
for style in soup.select('link[rel="stylesheet"]')]
sprite_sheet_url = None
positions = {}
li = {}
for sheet in stylesheets:
data = get(sheet).text
style = css_parser.parse_stylesheet(data)
for rule in style.rules:
selector = rule.selector.as_css() # selector name (i.e. .items-*-*-*)
if re.search(r'.items-', selector[:7]):
[url, x, y, _] = rule.declarations[2].value.as_css().split(" ")
positions[selector[7:]] = (
abs(int(x.replace("px", ""))),
abs(int(y.replace("px", "")))
)
if (sprite_sheet_url == None):
sprite_sheet_url = base_url + url[4:-1]
if (sprite_sheet_url):
img = Image.open(get(sprite_sheet_url, stream=True).raw)
for a in items:
pos = a.find("td", {"class": "row-icon"}).div["class"][1][6:]
item_id = a.find("td", {"class": "row-desc"}
).find("span", {"class": "text-id"}).text.replace("(minecraft:", " ").replace(")", "").strip()
[x, y] = positions[pos]
final = path.replace("[file]", item_id)
print(item_id)
img.crop((x, y, x + 32, y + 32)).save(final)
li[a.td.text] = 0