Skip to content

Commit

Permalink
Merge pull request #9 from MyriaCore/shortcode-search
Browse files Browse the repository at this point in the history
Shortcode search
  • Loading branch information
gornostal authored Oct 3, 2020
2 parents 6b1ae8e + acc0244 commit 3445b30
Show file tree
Hide file tree
Showing 5,592 changed files with 126 additions and 36 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
81 changes: 64 additions & 17 deletions EmojiSpider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import scrapy
import requests
import lxml.html
import sqlite3
import shutil
import base64
Expand All @@ -27,14 +28,31 @@ def cleanup():
def setup_db():
conn = sqlite3.connect('emoji.sqlite', check_same_thread=False)
conn.executescript('''
CREATE TABLE emoji (name VARCHAR PRIMARY KEY, code VARCHAR,
icon_apple VARCHAR, icon_twemoji VARCHAR,
icon_noto VARCHAR, icon_blobmoji VARCHAR,
keywords VARCHAR, name_search VARCHAR);
CREATE TABLE skin_tone (name VARCHAR, code VARCHAR, tone VARCHAR,
icon_apple VARCHAR, icon_twemoji VARCHAR,
icon_noto VARCHAR, icon_blobmoji VARCHAR);
CREATE INDEX name_idx ON skin_tone (name);''')
CREATE TABLE emoji (
name VARCHAR PRIMARY KEY,
code VARCHAR,
icon_apple VARCHAR,
icon_twemoji VARCHAR,
icon_noto VARCHAR,
icon_blobmoji VARCHAR,
keywords VARCHAR,
name_search VARCHAR
);
CREATE TABLE skin_tone (
name VARCHAR,
code VARCHAR,
tone VARCHAR,
icon_apple VARCHAR,
icon_twemoji VARCHAR,
icon_noto VARCHAR,
icon_blobmoji VARCHAR
);
CREATE TABLE shortcode (
name VARCHAR,
code VARCHAR
);
CREATE INDEX name_idx ON skin_tone (name);
''')
conn.row_factory = sqlite3.Row

return conn
Expand Down Expand Up @@ -75,17 +93,36 @@ def codepoint_to_url(codepoint, style):
return 'https://github.com/C1710/blobmoji/raw/master/png/128/emoji_u%s.png' \
% base.replace(' ', '_')

def name_to_shortcodes(shortname):
"""
Given an emoji's CLDR Shortname (e.g. 'grinning face with smiling eyes'), returns a list
of common shortcodes used for that emoji.
NOTE: These shortcodes will NOT have colons at the beginning and end, even if they normally
would.
"""
url = 'https://emojipedia.org/%s/' % re.sub(r'[^a-z0-9 ]', '', shortname.lower()).replace(' ', '-')
response = requests.get(url, stream=True)
response.raw.decode_content = True
html = lxml.html.parse(response.raw) if response.ok else None
shortcode_nodes = html.xpath('//ul[@class="shortcodes"]/li/span[@class="shortcode"]') if html else []
return [s.text for s in shortcode_nodes]

cleanup()
conn = setup_db()

class EmojiSpider(scrapy.Spider):
name = 'emojispider'
start_urls = ['http://unicode.org/emoji/charts/emoji-list.html']
start_urls = ['http://unicode.org/emoji/charts/emoji-list.html',
'http://unicode.org/emoji/charts/full-emoji-modifiers.html']
# start_urls = ['http://172.17.0.1:8000/list2.html']

def parse(self, response):
icon = 0
for tr in response.xpath('//tr[.//td[@class="code"]]'):
emoji_nodes = response.xpath('//tr[.//td[@class="code"]]')
for i in range(0, len(emoji_nodes)):
# Scrape Data from unicode.org
tr = emoji_nodes[i]
code = tr.css('.code a::text').extract_first()
encoded_code = str_to_unicode_emoji(code)
name = ''.join(tr.xpath('(.//td[@class="name"])[1]//text()').extract())
Expand All @@ -98,22 +135,28 @@ def parse(self, response):
if found:
skin_tone = found.group('skin_tone')
name = name.replace(': %s skin tone' % skin_tone, '')
shortcodes = name_to_shortcodes(name)

# Prepare emoji data to be inserted into DB
print("Fetching %i/%i: %s %s" % (i+1, len(emoji_nodes), encoded_code, name))
record = {
'name': name,
'code': encoded_code,
'shortcodes': ' '.join(shortcodes),
'keywords': keywords,
'tone': skin_tone,
'name_search': ' '.join(set(
[kw.strip() for kw in ('%s %s' % (name, keywords)).split(' ')]
[s[1:-1] for s in shortcodes] + [kw.strip() for kw in ('%s %s' % (name, keywords)).split(' ')]
)),
# Icons Styles
# Merge icon styles into record
**{ 'icon_%s' % style: '%s/%s.png' \
% (ICONS_PATH(style), icon_name) \
for style in EMOJI_STYLES \
}
}


# Download Icons for each emoji
print("🖼 Downloading Icons...")
supported_styles = []
for style in EMOJI_STYLES:
if style == 'apple':
Expand All @@ -123,13 +166,14 @@ def parse(self, response):
link = codepoint_to_url(code, style)
resp = requests.get(link)
icon_data = resp.content if resp.ok else None
print('[%s] %s' % ('OK' if resp.ok else 'BAD', link))

print('- %s: %s' % ('' if resp.ok else '', style))
if icon_data:
with open(record['icon_%s' % style], 'wb') as f:
f.write(icon_data)
supported_styles += [style]

# Prepare emoji insertion query
supported_styles = ['icon_%s' % style for style in supported_styles]
if skin_tone:
query = '''INSERT INTO skin_tone (name, code, tone, ''' + ', '.join(supported_styles) + ''')
Expand All @@ -140,8 +184,11 @@ def parse(self, response):
VALUES (:name, :code, ''' + ', '.join([':%s' % s for s in supported_styles]) + ''',
:keywords, :name_search)'''

# Insert emoji & associated shortcodes into DB
conn.execute(query, record)

yield record
for sc in shortcodes:
squery = '''INSERT INTO shortcode (name, code)
VALUES (:name, "%s")''' % sc
conn.execute(squery, record)

conn.commit()
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# Emoji Extension

<img aligh="center" src="screenshots/search.png">
<table>
<tr>
<td><img src="screenshots/search.png"></td>
<td><img src="screenshots/shortcode-search.png"></td>
</tr>
</table>

<img aligh="center" src="screenshots/preferences.png">
## Features

### Credits
- Supports Twemoji, Blobmoji, and Noto emoji preview renders
- Search by emoji name, *or* by shortcode by beginning the search with `:`
- Support for multiple skin tones via settings

[twemoji](https://github.com/twitter/twemoji), [noto-emoji](https://github.com/googlefonts/noto-emoji), and [blobmoji](https://github.com/C1710/blobmoji) for emoji styles :heart:.
### Settings

![](screenshots/preferences.png)

## Credits

- [emojipedia.org](https://emojipedia.org/) for emoji shortcode data
- [twemoji](https://github.com/twitter/twemoji), [noto-emoji](https://github.com/googlefonts/noto-emoji), and [blobmoji](https://github.com/C1710/blobmoji) for emoji styles :heart:.
Binary file modified emoji.sqlite
Binary file not shown.
Binary file added images/apple/emoji/Mrs._Claus_dark_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/Mrs._Claus_light_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/OK_hand_dark_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/OK_hand_light_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/OK_hand_medium_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/Santa_Claus_dark_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/artist_dark_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/artist_light_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/artist_medium_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/astronaut_dark_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/astronaut_light_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/astronaut_medium_skin_tone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/apple/emoji/baby_angel_dark_skin_tone.png
Binary file added images/apple/emoji/baby_angel_light_skin_tone.png
Binary file added images/apple/emoji/baby_dark_skin_tone.png
Binary file added images/apple/emoji/baby_light_skin_tone.png
Binary file added images/apple/emoji/baby_medium-dark_skin_tone.png
Binary file added images/apple/emoji/baby_medium_skin_tone.png
Binary file added images/apple/emoji/boy_dark_skin_tone.png
Binary file added images/apple/emoji/boy_light_skin_tone.png
Binary file added images/apple/emoji/boy_medium-dark_skin_tone.png
Binary file added images/apple/emoji/boy_medium-light_skin_tone.png
Binary file added images/apple/emoji/boy_medium_skin_tone.png
Binary file added images/apple/emoji/call_me_hand_dark_skin_tone.png
Binary file added images/apple/emoji/child_dark_skin_tone.png
Binary file added images/apple/emoji/child_light_skin_tone.png
Binary file added images/apple/emoji/child_medium_skin_tone.png
Binary file added images/apple/emoji/cook_dark_skin_tone.png
Binary file added images/apple/emoji/cook_light_skin_tone.png
Binary file added images/apple/emoji/cook_medium-dark_skin_tone.png
Binary file added images/apple/emoji/cook_medium_skin_tone.png
Binary file added images/apple/emoji/dark_skin_tone.png
Binary file added images/apple/emoji/deaf_man_dark_skin_tone.png
Binary file added images/apple/emoji/deaf_man_light_skin_tone.png
Binary file added images/apple/emoji/deaf_man_medium_skin_tone.png
Binary file added images/apple/emoji/deaf_person_dark_skin_tone.png
Binary file added images/apple/emoji/deaf_woman_dark_skin_tone.png
Binary file added images/apple/emoji/deaf_woman_light_skin_tone.png
Binary file added images/apple/emoji/detective_dark_skin_tone.png
Binary file added images/apple/emoji/detective_light_skin_tone.png
Binary file added images/apple/emoji/detective_medium_skin_tone.png
Binary file added images/apple/emoji/ear_dark_skin_tone.png
Binary file added images/apple/emoji/ear_light_skin_tone.png
Binary file added images/apple/emoji/ear_medium-dark_skin_tone.png
Binary file added images/apple/emoji/ear_medium-light_skin_tone.png
Binary file added images/apple/emoji/ear_medium_skin_tone.png
Binary file added images/apple/emoji/elf_dark_skin_tone.png
Binary file added images/apple/emoji/elf_light_skin_tone.png
Binary file added images/apple/emoji/elf_medium-dark_skin_tone.png
Binary file added images/apple/emoji/elf_medium-light_skin_tone.png
Binary file added images/apple/emoji/elf_medium_skin_tone.png
Binary file added images/apple/emoji/fairy_dark_skin_tone.png
Binary file added images/apple/emoji/fairy_light_skin_tone.png
Binary file added images/apple/emoji/fairy_medium_skin_tone.png
Binary file added images/apple/emoji/farmer_dark_skin_tone.png
Binary file added images/apple/emoji/farmer_light_skin_tone.png
Binary file added images/apple/emoji/farmer_medium_skin_tone.png
Binary file added images/apple/emoji/firefighter_dark_skin_tone.png
Binary file added images/apple/emoji/flexed_biceps_dark_skin_tone.png
Binary file added images/apple/emoji/foot_dark_skin_tone.png
Binary file added images/apple/emoji/foot_light_skin_tone.png
Binary file added images/apple/emoji/foot_medium-dark_skin_tone.png
Binary file added images/apple/emoji/foot_medium_skin_tone.png
Binary file added images/apple/emoji/girl_dark_skin_tone.png
Binary file added images/apple/emoji/girl_light_skin_tone.png
Binary file added images/apple/emoji/girl_medium-dark_skin_tone.png
Binary file added images/apple/emoji/girl_medium_skin_tone.png
Binary file added images/apple/emoji/guard_dark_skin_tone.png
Binary file added images/apple/emoji/guard_light_skin_tone.png
Binary file added images/apple/emoji/guard_medium_skin_tone.png
Binary file added images/apple/emoji/judge_dark_skin_tone.png
Binary file added images/apple/emoji/judge_light_skin_tone.png
Binary file added images/apple/emoji/judge_medium_skin_tone.png
Binary file added images/apple/emoji/leg_dark_skin_tone.png
Binary file added images/apple/emoji/leg_light_skin_tone.png
Binary file added images/apple/emoji/leg_medium-dark_skin_tone.png
Binary file added images/apple/emoji/leg_medium-light_skin_tone.png
Binary file added images/apple/emoji/leg_medium_skin_tone.png
Binary file added images/apple/emoji/light_skin_tone.png
Binary file added images/apple/emoji/mage_dark_skin_tone.png
Binary file added images/apple/emoji/mage_light_skin_tone.png
Binary file added images/apple/emoji/mage_medium-dark_skin_tone.png
Binary file added images/apple/emoji/mage_medium_skin_tone.png
Binary file added images/apple/emoji/man_artist_dark_skin_tone.png
Binary file added images/apple/emoji/man_artist_light_skin_tone.png
Binary file added images/apple/emoji/man_biking_dark_skin_tone.png
Binary file added images/apple/emoji/man_biking_light_skin_tone.png
Binary file added images/apple/emoji/man_bowing_dark_skin_tone.png
Binary file added images/apple/emoji/man_bowing_light_skin_tone.png
Binary file added images/apple/emoji/man_cook_dark_skin_tone.png
Binary file added images/apple/emoji/man_cook_light_skin_tone.png
Binary file added images/apple/emoji/man_cook_medium_skin_tone.png
Binary file added images/apple/emoji/man_dancing_dark_skin_tone.png
Binary file added images/apple/emoji/man_dark_skin_tone,_bald.png
Binary file added images/apple/emoji/man_dark_skin_tone,_beard.png
Binary file added images/apple/emoji/man_dark_skin_tone.png
Binary file added images/apple/emoji/man_elf_dark_skin_tone.png
Binary file added images/apple/emoji/man_elf_light_skin_tone.png
Binary file added images/apple/emoji/man_elf_medium_skin_tone.png
Loading

0 comments on commit 3445b30

Please sign in to comment.