-
Notifications
You must be signed in to change notification settings - Fork 1
/
getLogosAndMeta.py
112 lines (86 loc) · 2.78 KB
/
getLogosAndMeta.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
import json
import math
import os
import requests
from bs4 import BeautifulSoup
from colorthief import ColorThief
from PIL import Image
import numpy
COMPANIES_JSON_PATH = "companies.json"
PALETTES_JSON_PATH = "src/palettes.json"
def clamp(x):
return max(0, min(x, 255))
def rgb_to_hex(rgb):
return "#{0:02x}{1:02x}{2:02x}".format(clamp(rgb[0]), clamp(rgb[1]), clamp(rgb[2]))
def file_save_log(filepath):
size = os.path.getsize(filepath)
kB = round(size / 1000, 1)
print(f"Saved: {filepath} [{kB}kB]")
def downloadLogo(soup, logoPath):
if os.path.isfile(logoPath):
print(f"\tFound: {logoPath}")
return
imgs = soup.find_all("img")
imgSrcs = [x['src'] for x in imgs]
externalImgSrcs = [x for x in imgSrcs if x.startswith("https://")]
logoURL = externalImgSrcs[0]
screenshotURL = None if len(externalImgSrcs) == 1 else externalImgSrcs[1]
print(f"\tDownloading {logoURL} ...")
res = requests.get(logoURL)
if res.status_code == 200:
with open(logoPath, "wb") as f:
f.write(res.content)
print(f"\t\tSaved: {logoPath}")
def downloadMeta(soup, logoPath, company):
metaJsonPath = f"meta/{company['metaJsonName']}"
if os.path.isfile(metaJsonPath):
print(f"\tFound: {metaJsonPath}")
# return
ct = ColorThief(logoPath)
palette = ct.get_palette(color_count=5)
palette_png_path = f"palettes/{company['codeName']}.palette.png"
jo = dict(company)
a = soup.find("a", class_="btn btn-primary btn-block mt-4")
jo["website"] = a["href"]
jo["palette"] = palette
jo["hexPalette"] = [rgb_to_hex(rgb) for rgb in palette]
jo["metaJsonPath"] = metaJsonPath
jo["logoPngPath"] = logoPath
jo["palettePngPath"] = palette_png_path
with open(metaJsonPath, "w") as f:
json.dump(jo, f, indent="\t")
print(f"\tSaved: {metaJsonPath}")
if os.path.isfile(palette_png_path):
print(f"\tFound: {palette_png_path}")
else:
SIZE = 200
SIZE_Y = SIZE * len(palette)
data = numpy.zeros((SIZE, SIZE_Y, 3), dtype=numpy.uint8)
for x in range(SIZE):
for y in range(SIZE_Y):
cid = math.floor(y / SIZE)
data[x][y] = palette[cid]
im = Image.fromarray(data)
im.save(palette_png_path)
file_save_log(palette_png_path)
return jo
def main():
companiesJson = json.load(open(COMPANIES_JSON_PATH))
companies = companiesJson["companies"]
palettes = []
for idx, company in enumerate(companies):
print(f"[{idx+1}/{len(companies)}] {company['name']}")
ycdbHtmlPath = f"cache/{company['codeName']}.ycdb.html"
soup = BeautifulSoup(open(ycdbHtmlPath), "lxml")
logoPath = f"logos/{company['logoPngName']}"
downloadLogo(soup, logoPath)
palette = downloadMeta(soup, logoPath, company)
palettes.append(palette)
# break
jo = {}
jo["palettes"] = palettes
with open(PALETTES_JSON_PATH, "w") as f:
json.dump(jo, f, indent="\t")
file_save_log(PALETTES_JSON_PATH)
if __name__ == '__main__':
main()