|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""Emoji Picker for Albert |
| 4 | +Usage: :<emoji name> |
| 5 | +Example: :see no evil""" |
| 6 | + |
| 7 | +from albertv0 import * |
| 8 | + |
| 9 | +import os |
| 10 | +import shutil |
| 11 | +import urllib.request |
| 12 | +import datetime |
| 13 | + |
| 14 | +__iid__ = "PythonInterface/v0.2" |
| 15 | +__prettyname__ = "Albert Emoji Picker" |
| 16 | +__version__ = "0.2.0" |
| 17 | +__trigger__ = ":" |
| 18 | +__author__ = "Tim 'S.D.Eagle' Zeitz" |
| 19 | +__dependencies__ = [] |
| 20 | + |
| 21 | +emoji_data_src_url = "https://unicode.org/Public/emoji/latest/emoji-test.txt" |
| 22 | +emoji_data_path = os.path.join(dataLocation(), "emoji.txt") |
| 23 | + |
| 24 | +emojis = [] |
| 25 | + |
| 26 | +def initialize(): |
| 27 | + src_directory = os.path.dirname(os.path.realpath(__file__)) |
| 28 | + |
| 29 | + # if no emoji data exists copy offline src as fallback |
| 30 | + if not os.path.isfile(emoji_data_path): |
| 31 | + shutil.copyfile(os.path.join(src_directory, "emoji.txt"), emoji_data_path) |
| 32 | + |
| 33 | + current_version = get_emoji_data_version(emoji_data_path) |
| 34 | + |
| 35 | + try: |
| 36 | + new_path = os.path.join(dataLocation(), "emoji-new.txt") |
| 37 | + |
| 38 | + # try to fetch the latest emoji data |
| 39 | + with urllib.request.urlopen(emoji_data_src_url) as response, open(new_path, 'wb') as out_file: |
| 40 | + # save it |
| 41 | + shutil.copyfileobj(response, out_file) |
| 42 | + |
| 43 | + # update emoji data if the fetched data is newer |
| 44 | + if get_emoji_data_version(new_path) > current_version: |
| 45 | + shutil.copyfile(new_path, emoji_data_path) |
| 46 | + |
| 47 | + os.remove(new_path) |
| 48 | + |
| 49 | + except Exception as e: |
| 50 | + warn(e) |
| 51 | + |
| 52 | + build_index() |
| 53 | + |
| 54 | +def get_emoji_data_version(path): |
| 55 | + with open(emoji_data_path) as f: |
| 56 | + for line in f: |
| 57 | + if "# Date: " in line: |
| 58 | + return datetime.datetime.strptime(line.strip(), "# Date: %Y-%m-%d, %H:%M:%S GMT") |
| 59 | + |
| 60 | +def build_index(): |
| 61 | + emojis.clear() |
| 62 | + |
| 63 | + with open(emoji_data_path) as f: |
| 64 | + for line in f: |
| 65 | + if "; fully-qualified" in line: |
| 66 | + [emoji, name] = line.split('#', 1)[-1].split(None, 1) |
| 67 | + emojis.append((emoji, name.strip(), name.lower().split())) |
| 68 | + |
| 69 | +def handleQuery(query): |
| 70 | + if query.isValid and query.isTriggered: |
| 71 | + return [Item(id=key, completion=key, icon=emoji, text=emoji, subtext=key, actions=[ClipAction("Copy to clipboard", emoji)]) for (emoji, key, matchcount) in matches(query.string.lower().split())] |
| 72 | + return [] |
| 73 | + |
| 74 | +def matches(needles): |
| 75 | + matched = [(emoji, name, count_matches(tokens, needles)) for (emoji, name, tokens) in emojis] |
| 76 | + matched = [(emoji, name, matchcount) for (emoji, name, matchcount) in matched if matchcount > 0] |
| 77 | + matched = sorted(matched, key=lambda data: data[2], reverse=True) |
| 78 | + return matched |
| 79 | + |
| 80 | +def count_matches(tokens, needles): |
| 81 | + count = 0 |
| 82 | + for token in tokens: |
| 83 | + for needle in needles: |
| 84 | + if needle in token: |
| 85 | + count += 1 |
| 86 | + return count |
0 commit comments