Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 83 lines (64 sloc) 2.38 KB
#!/usr/bin/python3
"""
pip install lxml cssselect
"""
import os
import base64
import lxml.html
import subprocess
from urllib import request
N_LINES = 10
URL = 'https://www.unicode.org/emoji/charts/full-emoji-list.html'
CACHE = os.path.expanduser('~/.cache/emoji')
DATA = os.path.join(CACHE, 'emoji.dat')
def download():
"""download emoji image data, codes, and names"""
resp = request.urlopen(URL)
html = resp.read()
root = lxml.html.fromstring(html)
rows = root.cssselect('.main table:first-of-type tr')
# filter to rows with emoji
rows = [r for r in rows if r.cssselect('.code')]
emos = []
for row in rows:
code = row.cssselect('.code a')[0].attrib['name']
name = row.cssselect('.name')[0].text
# this should get us the facebook versions
try:
img = row.cssselect('.andr img')[4]
except IndexError:
# some emoji aren't widely supported yet,
# so won't have an image to get. skip these.
continue
# decode from base64
_, img_data = img.attrib['src'].split(',')
img_data = base64.decodebytes(img_data.encode('utf8'))
# save as png
path = os.path.join(CACHE, '{}.png'.format(code))
with open(path, 'wb') as f:
f.write(img_data)
emos.append((code, name, path))
with open(DATA, 'w') as f:
f.write('\n'.join(['\t'.join(e) for e in emos]))
if __name__ == '__main__':
if not os.path.exists(CACHE):
subprocess.run(['notify-send', 'No emoji data, downloading...'])
os.mkdir(CACHE)
download()
with open(DATA, 'r') as f:
emo = [l.strip().split('\t') for l in f.readlines()]
lookup = {}
choices = []
for code, name, path in emo:
choices.append('IMG:{}\t{}'.format(path, name))
# convert from code to actual unicode value
lookup[name] = ''.join(chr(int(c, 16)) for c in code.split('_'))
# render choices with dmenu
cmd = ['dmenu', '-ia', 'center', '-is', '100x100', '-l', str(N_LINES), '-i', '-p', '>']
inp = '\n'.join(choices).encode('utf8')
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
out, err = proc.communicate(inp)
choice = out.strip().decode('utf8')
if choice:
p = subprocess.Popen(['xsel', '-b'], stdin=subprocess.PIPE)
p.communicate(lookup[choice].encode('utf8'))