From 1c1bd9876f181f1a92cdd961a71b9f3023b13865 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 5 Oct 2015 16:17:51 +0100 Subject: [PATCH] Renamed assets build --- assets/build-emoticons.py | 73 +++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 assets/build-emoticons.py diff --git a/assets/build-emoticons.py b/assets/build-emoticons.py new file mode 100644 index 0000000..72e3b91 --- /dev/null +++ b/assets/build-emoticons.py @@ -0,0 +1,73 @@ +import json, requests, os, base64 +from glob import glob +from lxml import html +from collections import namedtuple + +ASSET_DIR = os.path.dirname(os.path.realpath(__file__)) +LOCAL_FILES = glob('assets/*.png') + glob('assets/*.gif') + +Emoticon = namedtuple('Emoticon', ['ident', 'url']) +def get_filename(path): + return path.replace('assets/', '') + +def get_icon_name(path): + return get_filename(path).replace('.png', '').replace('.gif', '') + +print("Collecting Page Elements...") +page = requests.get("https://www.hipchat.com/emoticons") +html_tree = html.fromstring(page.text) +emoticon_paths = html_tree.xpath("//div[@class='emoticon-block']/img/@src") +emoticon_names = html_tree.xpath("//div[@class='emoticon-block']/div/text()") + +emoticons = [] +for i in range(len(emoticon_names)-1): + emoticons.append(Emoticon(emoticon_names[i][1:-1], emoticon_paths[i])) + +print("Downloading offline emoticons...") +DOWNLOAD_FILES = [] +for emoticon in emoticons: + ext = 'gif' if emoticon.url.endswith('gif') else 'png' + path = ASSET_DIR + "/{0}.{1}".format(emoticon.ident, ext) + DOWNLOAD_FILES.append(path) + if os.path.isfile(path): + continue + with open(path, 'wb') as handle: + response = requests.get(emoticon.url, stream=True) + if not response.ok: + print("Something went wrong getting " + emoticon.ident) + continue + + for block in response.iter_content(1024): + handle.write(block) + + print ("{}/{} Files Downloaded\r".format(len(DOWNLOAD_FILES), len(emoticons)), end='' if len(DOWNLOAD_FILES) != len(emoticons) else '\n') + +for filename in LOCAL_FILES: + emoticons.append(Emoticon(get_icon_name(filename), filename)) + +images = [] +print("Converting Files...") +for emoticon in emoticons: + ext = ext = 'gif' if emoticon.url.endswith('gif') else 'png' + path = ASSET_DIR + "/{0}.{1}".format(emoticon.ident, ext) + file = base64.b64encode(open(path, 'rb').read()).decode('utf-8').replace('\n', '') + data = "data:image/{0};base64,{1}".format(ext, file) + images.append({emoticon.ident: data}) + +print("Exporting JSON...") +image_decoder = {"images":images} + +image_decoder_json = json.dumps(image_decoder, indent=2, sort_keys=True) + +js_file = None +with open('src/global.js', "r") as file: + js_file = file.read() + +js_file = js_file.replace("%image_decoder%", image_decoder_json) + +with open('build/global.js', 'w') as file: + file.write(js_file) + +print("Deleting Temporary Files...") +for file in DOWNLOAD_FILES: + os.remove(file) diff --git a/package.json b/package.json index 9cef4b1..39d732f 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "release": "bash scripts/release.sh", "build-js": "bash scripts/build-js.sh", "develop": "npm run build-js && bash scripts/distribute.sh", - "build-assets": "env/bin/python3 assets/build-assets.py", + "build-assets": "env/bin/python3 assets/build-emoticons.py", "build-data-files": "bash scripts/data-files.sh", "build": "npm run create-dirs && npm run build-assets && npm run develop && npm run build-data-files", "test":"npm run lint",