import json
import os
import argparse
import re
try:
import urllib.request as urlrequest
except ImportError
import urllib as urlrequest
unicode_re = re.compile(r'.*?/unicode/(.*?).png\?.*')
def get_emojis()
response = urlrequest.urlopen('https://api.github.com/emojis')
raw_data = response.read()
return json.loads(raw_data)
def download_images(dir_name, silent)
if not os.path.exists(dir_name)
os.makedirs(dir_name)
json_data = get_emojis()
num_items = len(json_data)
cur_item=0
for image,url in sorted(json_data.items())
image_name = image+'.png'
cur_item=cur_item+1
if url.find('/unicode/')==-1 or not os.path.isfile(dir_name+'/'+image_name)
success = True
with open(dir_name+'/'+image_name,'wb') as file
if not silent
print('%s/%s: 正在获取 %s' % (cur_item,num_items,image_name))
try:
file.write(urlrequest.urlopen(url).read())
except:
print('无法获取 %s' % (image_name))
success = False
if not success
os.remove(dir_name+'/'+image_name)
else:
if not silent
print('%s/%s: 跳过 %s' % (cur_item,num_items,image_name))
def produce_table()
json_data = get_emojis()
lines = []
for image,url in sorted(json_data.items())
match = unicode_re.match(url)
if match
unicodes = match.group(1).split('-')
unicodes_html = ''.join(["&#x"+x+";" for x in unicodes])
image_str = "\":"+image+":\","
unicode_str = "\""+unicodes_html+"\""
lines.append(' { %-42s %-38s }' % (image_str,unicode_str))
out_str = ',\n'.join(lines)
print("{")
print(out_str)
print("};")
if __name__=="__main__"
parser = argparse.ArgumentParser()
parser.add_argument('-d','--dir',help='放置图像的目录')
parser.add_argument('-t','--table',help='生成代码片段',action='store_true')
parser.add_argument('-s','--silent',help='静默模式',action='store_true')
args = parser.parse_args()
if args.table
produce_table()
if args.dir
download_images(args.dir, args.silent)