Skip to content

Commit 045b3f7

Browse files
authored
Merge pull request #1 from bcongdon/all-emoji
Support for Emojipedia.all(), Test Fixes
2 parents fadff5d + ce5da5e commit 045b3f7

File tree

8 files changed

+121
-14
lines changed

8 files changed

+121
-14
lines changed

.codeclimate.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ engines:
1212
enabled: true
1313
radon:
1414
enabled: true
15+
exclude_paths:
16+
- "test_emojipedia.py"
1517
ratings:
1618
paths:
1719
- "**.inc"

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ python:
66
- "3.6"
77
install:
88
- "pip install -r requirements.txt"
9-
- "pip install pep8"
9+
- "pip install -r test_requirements.txt"
1010
script:
1111
- pep8 emojipedia/ test_emojipedia.py
12-
- nosetests
12+
- nosetests --with-coverage --cover-package=emojipedia
13+
after_sucess:
14+
- CODECLIMATE_REPO_TOKEN=$COVERAGE_TOKEN codeclimate-test-reporter

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[![Build Status](https://travis-ci.org/bcongdon/python-emojipedia.svg?branch=master)](https://travis-ci.org/bcongdon/python-emojipedia)
33
[![Code Climate](https://codeclimate.com/github/bcongdon/python-emojipedia/badges/gpa.svg)](https://codeclimate.com/github/bcongdon/python-emojipedia)
44
[![PyPI version](https://badge.fury.io/py/Emojipedia.svg)](https://badge.fury.io/py/Emojipedia)
5+
[![Test Coverage](https://codeclimate.com/github/bcongdon/python-emojipedia/badges/coverage.svg)](https://codeclimate.com/github/bcongdon/python-emojipedia/coverage)
56
>Emoji data from Emojipedia :sunglasses:
67
78
## Installation:
@@ -22,22 +23,45 @@ from emojipedia import Emojipedia
2223
taco = Emojipedia.search('taco')
2324
2425
# Emojipedia description
25-
print taco.description # "A taco; a Mexican food item displayed with a variety of fillings. ..."
26+
print(taco.description) # "A taco; a Mexican food item displayed with a variety of fillings. ..."
2627
2728
# Emojipedia codepoints
28-
print taco.codepoints # "U+1F32E"
29+
print(taco.codepoints) # "U+1F32E"
2930
3031
# Emojipedia listed platforms
3132
# Contains title, Emojipedia platform url, and platform specific emoji img url
3233
platforms = taco.platforms
33-
print platforms[0] # {'title': 'Apple', 'platform_url': '...', 'platform_img': '...'}
34+
print(platforms[0]) # {'title': 'Apple', 'platform_url': '...', 'platform_img': '...'}
3435
3536
joy = Emojipedia.search('face-with-tears-of-joy')
3637
# Emoji shortcodes
37-
joy.shortcodes # ":joy:"
38+
joy.shortcodes # ":joy:"
3839
3940
# Search for emoji by emoji
4041
smirk = Emojipedia.search('😏')
4142
# Custom Emoji string preview
42-
print str(smirk) # <Emoji - 'Smirking Face' - character: 😏, description: A sly smile, often u...>
43+
print(str(smirk)) # <Emoji - 'Smirking Face' - character: 😏, description: A sly smile, often u...>
44+
45+
# Get a category of emoji
46+
people = Emojipedia.category('people')
47+
people[0].title # <Emoji - 'Grinning Face' - character: 😀, description: A face with a big op...>
48+
print(len(people)) # 306
49+
50+
# Get all the emoji
51+
emojis = Emojipedia.all()
52+
print(len(emojis)) # 2621
53+
for emoji in emojis:
54+
print(emoji.title)
4355
```
56+
57+
## Contributing
58+
59+
Contributions to `python-emojipedia` are welcomed! 😁
60+
61+
1. Fork the repo.
62+
2. Create a new feature branch.
63+
3. Add your feature / make your changes.
64+
4. Install [pep8](https://pypi.python.org/pypi/pep8) and run `pep8 *.py` in the root project directory to lint your changes. Fix any linting errors.
65+
5. Create a PR.
66+
6. ???
67+
7. 🎉 Profit. 🎉

emojipedia/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .emojipedia import Emojipedia
1+
from .emojipedia import Emojipedia, Emoji

emojipedia/emoji.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from bs4 import BeautifulSoup
22
import requests
3+
import re
4+
import six
35

46

57
class Emoji:
@@ -18,9 +20,32 @@ def __init__(self, soup=None, url=None):
1820
self._shortcodes = None
1921
self._title = None
2022

23+
def _get_emoji_article_url(self):
24+
response = requests.get('http://emojipedia.org' + self._url)
25+
if response.status_code != 200:
26+
raise RuntimeError('Could not get emojipedia page for '
27+
"'{}'".format(self._url))
28+
29+
soup = BeautifulSoup(response.text, 'html.parser')
30+
desc = soup.find('td', text=re.compile('Description'))
31+
32+
if not desc:
33+
raise ValueError('Could not parse emoji description')
34+
35+
article_url = desc.parent.find_next('a')
36+
if not article_url:
37+
raise ValueError('Could not find emoji article')
38+
39+
return article_url['href']
40+
2141
@property
2242
def soup(self):
2343
if not self._soup:
44+
# Check to see if we've given a general emoji page
45+
if 'emoji/' in self._url:
46+
# Resolve to emoji article
47+
self._url = self._get_emoji_article_url()
48+
2449
response = requests.get('http://emojipedia.org' + self._url)
2550
if response.status_code != 200:
2651
raise RuntimeError('Could not get emojipedia page for \'{0}\''
@@ -98,12 +123,15 @@ def character(self):
98123
self._character = self.soup.find('h1').text.split()[0]
99124
return self._character
100125

101-
def __str__(self):
126+
def __unicode__(self):
102127
string = u"<Emoji - '{0}' - character: {2}, description: {1}>"
103128
string = string.format(self.title,
104-
self.description[:20] + "...",
129+
self.description[:20] + u"...",
105130
self.character)
106131
return string
107132

133+
def __str__(self):
134+
return six.text_type(self.__unicode__()).encode('utf-8')
135+
108136
def __repr__(self):
109137
return self.__str__()

emojipedia/emojipedia.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ def category(query):
3636
emojis.append(e)
3737
return emojis
3838

39+
@staticmethod
40+
def all():
41+
soup = Emojipedia._get_page('emoji')
42+
emoji_list = soup.find('table', {'class': 'emoji-list'})
43+
if not emoji_list:
44+
raise ValueError('Could not extract emoji list')
45+
emojis = []
46+
for emoji_entry in emoji_list.find_all('tr'):
47+
emoji_link = emoji_entry.find('a')
48+
emoji_text = emoji_link.text.split(' ')
49+
emoji_row, codepoints = emoji_entry.find_all('td')
50+
51+
e = Emoji(url=emoji_link['href'])
52+
e._codepoints = codepoints.text.split(', ')
53+
e._character, e._title = emoji_text[0], ' '.join(emoji_text[1:])
54+
emojis.append(e)
55+
return emojis
56+
3957
@staticmethod
4058
def _valid_emoji_page(soup):
4159
"""

test_emojipedia.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# -*- coding: utf-8 -*-
22

3-
from emojipedia import Emojipedia
3+
from emojipedia import Emojipedia, Emoji
44
import nose.tools
5+
from nose.tools import timed
6+
from nose import run
57

68

79
@nose.tools.raises(RuntimeError)
@@ -79,13 +81,41 @@ def test_emoji_repr():
7981
pizza = Emojipedia.search('slice-of-pizza')
8082
correct = (u"<Emoji - 'Pizza' - character: 🍕, "
8183
u"description: A slice  of pizza, w...>")
82-
print(type(correct))
83-
print(type(pizza.__str__()))
84-
assert pizza.__str__() == correct
84+
assert pizza.__unicode__() == correct
85+
assert pizza.__repr__() == pizza.__str__()
8586

8687

8788
def test_emoji_category():
8889
people = Emojipedia.category('people')
8990
for e in people:
9091
assert e.title
9192
assert e.character
93+
94+
95+
@timed(5)
96+
def test_all_emoji():
97+
all_emoji = Emojipedia.all()
98+
assert len(all_emoji) >= 2621
99+
for e in all_emoji:
100+
# Test private properties so we don't scrape Emojipedia
101+
# if this fails
102+
assert e._title
103+
assert e._character
104+
assert e._codepoints
105+
106+
107+
def test_lazy_parsing_article():
108+
article_emoji = Emoji(url='/heavy-plus-sign')
109+
assert article_emoji.title
110+
assert article_emoji.character
111+
assert article_emoji.description
112+
assert article_emoji.codepoints
113+
114+
generic_emoji = Emoji(url=u'/emoji/🌮')
115+
assert generic_emoji.title
116+
assert generic_emoji.character
117+
assert generic_emoji.description
118+
assert generic_emoji.codepoints
119+
120+
if __name__ == '__main__':
121+
run()

test_requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
codeclimate-test-reporter==0.1.0
2+
coverage==4.0.2
3+
pep8==1.7.0

0 commit comments

Comments
 (0)