2015-10-08 12:57:36 +01:00
|
|
|
import json, requests, os, base64, printr
|
2015-10-05 16:17:51 +01:00
|
|
|
from glob import glob
|
|
|
|
from lxml import html
|
|
|
|
from collections import namedtuple
|
|
|
|
|
2015-10-08 12:49:24 +01:00
|
|
|
|
2015-10-05 16:17:51 +01:00
|
|
|
ASSET_DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
LOCAL_FILES = glob('assets/*.png') + glob('assets/*.gif')
|
|
|
|
Emoticon = namedtuple('Emoticon', ['ident', 'url'])
|
2015-10-08 12:49:24 +01:00
|
|
|
|
|
|
|
|
2015-10-05 16:17:51 +01:00
|
|
|
def get_filename(path):
|
|
|
|
return path.replace('assets/', '')
|
|
|
|
|
2015-10-08 12:49:24 +01:00
|
|
|
|
2015-10-05 16:17:51 +01:00
|
|
|
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 = []
|
2015-10-08 12:49:24 +01:00
|
|
|
for i in range(len(emoticon_names) - 1):
|
2015-10-05 16:17:51 +01:00
|
|
|
emoticons.append(Emoticon(emoticon_names[i][1:-1], emoticon_paths[i]))
|
|
|
|
|
|
|
|
print("Downloading offline emoticons...")
|
|
|
|
DOWNLOAD_FILES = []
|
2015-10-08 12:57:36 +01:00
|
|
|
printer = printr.ItterPrintr('{c}/{m} Files Downloaded', len(emoticons), 1)
|
2015-10-05 16:17:51 +01:00
|
|
|
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)
|
2015-10-08 12:57:36 +01:00
|
|
|
printer.update()
|
|
|
|
# print("{}/{} Files Downloaded\r".format(len(DOWNLOAD_FILES), len(emoticons)), end='' if len(DOWNLOAD_FILES) != len(emoticons) else '\n')
|
|
|
|
print()
|
2015-10-05 16:17:51 +01:00
|
|
|
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...")
|
2015-10-08 12:49:24 +01:00
|
|
|
image_decoder = {"images": images}
|
2015-10-05 16:17:51 +01:00
|
|
|
|
|
|
|
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)
|