File update_json.py changed (mode: 100644) (index 8b7fc9a..7fc81c4) |
3 |
3 |
import jinja2 |
import jinja2 |
4 |
4 |
import json |
import json |
5 |
5 |
import os |
import os |
|
6 |
|
import os.path |
6 |
7 |
import requests |
import requests |
|
8 |
|
import traceback |
7 |
9 |
|
|
8 |
10 |
def login(username, password): |
def login(username, password): |
9 |
11 |
headers = { |
headers = { |
|
... |
... |
def get_avatar_id(token): |
51 |
53 |
r.raise_for_status() |
r.raise_for_status() |
52 |
54 |
return r.json()['id'] |
return r.json()['id'] |
53 |
55 |
|
|
|
56 |
|
def precache_images(images): |
|
57 |
|
images_dir = os.path.join(os.getcwd(), "e") |
|
58 |
|
if not os.path.exists(images_dir): |
|
59 |
|
os.makedirs(images_dir) |
|
60 |
|
|
|
61 |
|
for image in images: |
|
62 |
|
try: |
|
63 |
|
image_path = os.path.join(images_dir, "%s.png" % image['template_id']) |
|
64 |
|
if not os.path.exists(image_path): |
|
65 |
|
r = requests.get(image['src']) |
|
66 |
|
with open(image_path, "wb") as f: |
|
67 |
|
for chunk in r.iter_content(chunk_size=4096): |
|
68 |
|
if chunk: |
|
69 |
|
f.write(chunk) |
|
70 |
|
|
|
71 |
|
except: |
|
72 |
|
traceback.print_exc() |
|
73 |
|
|
|
74 |
|
if os.path.exists(image_path): |
|
75 |
|
image['original_src'] = image['src'] |
|
76 |
|
image['src'] = 'e/%s.png' % image['template_id'] |
|
77 |
|
|
|
78 |
|
|
54 |
79 |
def generate_html(images, avatar_id): |
def generate_html(images, avatar_id): |
55 |
80 |
for image in images: |
for image in images: |
56 |
81 |
image['src'] = image['src'] % avatar_id |
image['src'] = image['src'] % avatar_id |
57 |
82 |
|
|
|
83 |
|
precache_images(images) |
|
84 |
|
|
58 |
85 |
images.sort(key=lambda x: x['category_rank'] * x['download_order']) |
images.sort(key=lambda x: x['category_rank'] * x['download_order']) |
59 |
86 |
env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.getcwd())) |
env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.getcwd())) |
60 |
87 |
template = env.get_template("bitmoji.html") |
template = env.get_template("bitmoji.html") |
|
... |
... |
def generate_html(images, avatar_id): |
65 |
92 |
def generate_page(username, password): |
def generate_page(username, password): |
66 |
93 |
token = login(username, password) |
token = login(username, password) |
67 |
94 |
avatar_id = get_avatar_id(token) |
avatar_id = get_avatar_id(token) |
68 |
|
# avatar_id = "206259971_1-s1" |
|
69 |
95 |
templates = get_templates(token) |
templates = get_templates(token) |
70 |
96 |
images = templates['imoji'] |
images = templates['imoji'] |
71 |
97 |
for pack in templates['packs']: |
for pack in templates['packs']: |