From 28faa212041a10b360555442efb91bce3b04e437 Mon Sep 17 00:00:00 2001 From: Benjamin Congdon Date: Sat, 15 Jul 2017 20:24:30 -0700 Subject: [PATCH] Added Sphinx/ReadTheDocs; Minor refactoring; Versioned to 0.4.0 --- README.md | 16 +- docs/Makefile | 216 +++++++++++++++++++++++++++ docs/source/conf.py | 291 +++++++++++++++++++++++++++++++++++++ docs/source/emoji.rst | 19 +++ docs/source/emojipedia.rst | 7 + docs/source/index.rst | 22 +++ docs/source/modules.rst | 8 + emojipedia/emoji.py | 79 +++++++--- emojipedia/emojipedia.py | 42 +++++- setup.py | 4 +- test_emojipedia.py | 7 +- 11 files changed, 674 insertions(+), 37 deletions(-) create mode 100644 docs/Makefile create mode 100644 docs/source/conf.py create mode 100644 docs/source/emoji.rst create mode 100644 docs/source/emojipedia.rst create mode 100644 docs/source/index.rst create mode 100644 docs/source/modules.rst diff --git a/README.md b/README.md index e4f1821..d6b388d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,14 @@ [![Test Coverage](https://codeclimate.com/github/bcongdon/python-emojipedia/badges/coverage.svg)](https://codeclimate.com/github/bcongdon/python-emojipedia/coverage) >Emoji data from Emojipedia :sunglasses: +## Basic Usage +```python +>>>from emojipedia import Emojipedia +>>>taco = Emojipedia.search('taco') +>>>taco +# +``` + ## Installation: To install, simply run `pip install emojipedia`. @@ -15,7 +23,7 @@ python setup.py build python setup.py install ``` -## Example: +## Usage: ```python from emojipedia import Emojipedia @@ -31,7 +39,7 @@ print(taco.codepoints) # "U+1F32E" # Emojipedia listed platforms # Contains title, Emojipedia platform url, and platform specific emoji img url platforms = taco.platforms -print(platforms[0]) # {'title': 'Apple', 'platform_url': '...', 'platform_img': '...'} +print(platforms[0]) # Platform(name=u'Apple', image_url=u'.../taco_1f32e.png') joy = Emojipedia.search('face-with-tears-of-joy') # Emoji shortcodes @@ -54,6 +62,10 @@ for emoji in emojis: print(emoji.title) ``` +## Documentation + +Read more about python-emojipedia on the [ReadTheDocs](https://python-emojipedia.readthedocs.io/en/latest/) page. + ## Contributing Contributions to `python-emojipedia` are welcomed! 😁 diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..ad465a0 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,216 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = build + +# User-friendly check for sphinx-build +ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) +endif + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source + +.PHONY: help +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " applehelp to make an Apple Help Book" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + @echo " coverage to run coverage check of the documentation (if enabled)" + +.PHONY: clean +clean: + rm -rf $(BUILDDIR)/* + +.PHONY: html +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +.PHONY: dirhtml +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +.PHONY: singlehtml +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +.PHONY: pickle +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +.PHONY: json +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +.PHONY: htmlhelp +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +.PHONY: qthelp +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/emojipedia.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/emojipedia.qhc" + +.PHONY: applehelp +applehelp: + $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp + @echo + @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." + @echo "N.B. You won't be able to view it unless you put it in" \ + "~/Library/Documentation/Help or install it in your application" \ + "bundle." + +.PHONY: devhelp +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/emojipedia" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/emojipedia" + @echo "# devhelp" + +.PHONY: epub +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +.PHONY: latex +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +.PHONY: latexpdf +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +.PHONY: latexpdfja +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +.PHONY: text +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +.PHONY: man +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +.PHONY: texinfo +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +.PHONY: info +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +.PHONY: gettext +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +.PHONY: changes +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +.PHONY: linkcheck +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +.PHONY: doctest +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +.PHONY: coverage +coverage: + $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage + @echo "Testing of coverage in the sources finished, look at the " \ + "results in $(BUILDDIR)/coverage/python.txt." + +.PHONY: xml +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +.PHONY: pseudoxml +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/docs/source/conf.py b/docs/source/conf.py new file mode 100644 index 0000000..5fcd94c --- /dev/null +++ b/docs/source/conf.py @@ -0,0 +1,291 @@ +# -*- coding: utf-8 -*- +# +# emojipedia documentation build configuration file, created by +# sphinx-quickstart on Sat Jul 15 19:37:37 2017. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys +import os + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +#sys.path.insert(0, os.path.abspath('.')) + +# -- General configuration ------------------------------------------------ + +# If your documentation needs a minimal Sphinx version, state it here. +#needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.viewcode', +] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +source_suffix = ['.rst', '.md'] +# source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'emojipedia' +copyright = u'2017, Benjamin Congdon' +author = u'Benjamin Congdon' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = u'0.4.0' +# The full version, including alpha/beta/rc tags. +release = u'0.4.0' + + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = [] + +# The reST default role (used for this markup: `text`) to use for all +# documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +#keep_warnings = False + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = False + + +# -- Options for HTML output ---------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'sphinx_rtd_theme' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (relative to this directory) to use as a favicon of +# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# Add any extra paths that contain custom files (such as robots.txt or +# .htaccess) here, relative to this directory. These files are copied +# directly to the root of the documentation. +#html_extra_path = [] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Language to be used for generating the HTML full-text search index. +# Sphinx supports the following languages: +# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' +# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' +#html_search_language = 'en' + +# A dictionary with options for the search language support, empty by default. +# Now only 'ja' uses this config value +#html_search_options = {'type': 'default'} + +# The name of a javascript file (relative to the configuration directory) that +# implements a search results scorer. If empty, the default will be used. +#html_search_scorer = 'scorer.js' + +# Output file base name for HTML help builder. +htmlhelp_basename = 'emojipediadoc' + +# -- Options for LaTeX output --------------------------------------------- + +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +#'papersize': 'letterpaper', + +# The font size ('10pt', '11pt' or '12pt'). +#'pointsize': '10pt', + +# Additional stuff for the LaTeX preamble. +#'preamble': '', + +# Latex figure (float) alignment +#'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + (master_doc, 'emojipedia.tex', u'emojipedia Documentation', + u'Benjamin Congdon', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output --------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + (master_doc, 'emojipedia', u'emojipedia Documentation', + [author], 1) +] + +source_parsers = { + '.md': 'recommonmark.parser.CommonMarkParser', +} + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + (master_doc, 'emojipedia', u'emojipedia Documentation', + author, 'emojipedia', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +#texinfo_no_detailmenu = False diff --git a/docs/source/emoji.rst b/docs/source/emoji.rst new file mode 100644 index 0000000..6b52373 --- /dev/null +++ b/docs/source/emoji.rst @@ -0,0 +1,19 @@ +emojipedia\.emoji module +------------------------ + +.. autoclass:: emojipedia.emoji.Emoji + :members: + :undoc-members: + :show-inheritance: + +.. py:class:: emojipedia.emoji.Platform + + Data container object for contribution data of a given day. + + .. py:attribute:: name + + The name of the platform + + .. py:attribute:: image_url + + URL pointing to an image of the emoji as it appears on this platform diff --git a/docs/source/emojipedia.rst b/docs/source/emojipedia.rst new file mode 100644 index 0000000..b1ae44b --- /dev/null +++ b/docs/source/emojipedia.rst @@ -0,0 +1,7 @@ +emojipedia\.emojipedia module +----------------------------- + +.. automodule:: emojipedia.emojipedia + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/index.rst b/docs/source/index.rst new file mode 100644 index 0000000..ef9cbfd --- /dev/null +++ b/docs/source/index.rst @@ -0,0 +1,22 @@ +.. emojipedia documentation master file, created by + sphinx-quickstart on Sat Jul 15 19:37:37 2017. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Welcome to emojipedia's documentation! +====================================== + +Contents: + +.. toctree:: + :maxdepth: 2 + + modules + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`search` + diff --git a/docs/source/modules.rst b/docs/source/modules.rst new file mode 100644 index 0000000..d90539c --- /dev/null +++ b/docs/source/modules.rst @@ -0,0 +1,8 @@ +emojipedia +========== + +.. toctree:: + :maxdepth: 4 + + emojipedia + emoji diff --git a/emojipedia/emoji.py b/emojipedia/emoji.py index fe54c1a..1cf949f 100644 --- a/emojipedia/emoji.py +++ b/emojipedia/emoji.py @@ -1,12 +1,18 @@ +# -*- coding: utf-8 -*- + from bs4 import BeautifulSoup import requests import re import six +from collections import namedtuple + +Platform = namedtuple('Platform', ['image_url', 'name']) + class Emoji: def __init__(self, soup=None, url=None): - self._soup = soup + self._soup_data = soup self._url = url if not (soup or url): raise ValueError('Emoji needs one of soup or url to be ' @@ -39,8 +45,8 @@ def _get_emoji_article_url(self): return article_url['href'] @property - def soup(self): - if not self._soup: + def _soup(self): + if not self._soup_data: # Check to see if we've given a general emoji page if 'emoji/' in self._url: # Resolve to emoji article @@ -50,21 +56,40 @@ def soup(self): if response.status_code != 200: raise RuntimeError('Could not get emojipedia page for \'{0}\'' .format(self._url)) - self._soup = BeautifulSoup(response.text, 'html.parser') - return self._soup + self._soup_data = BeautifulSoup(response.text, 'html.parser') + return self._soup_data + + @property + def title(self): + '''The title of the emoji + + Example: 'Person Shrugging' + ''' + if not self._title: + # Remove initial emoji + whitespace + self._title = " ".join(self._soup.find('h1').text.split()[1:]) + return self._title @property def description(self): + '''The Emojipedia description of the emoji + + Example: ``u'A person shrugging their shoulders to indicate ...'`` + ''' if not self._description: - text = self.soup.find('section', {'class': 'description'}).text + text = self._soup.find('section', {'class': 'description'}).text if text: self._description = text.strip() return self._description @property def codepoints(self): + '''A list of unicode codepoints for the emoji + + Example: ``['U+1F937']`` + ''' if not self._codepoints: - code_list = self.soup.find(text='Codepoints').findNext('ul') + code_list = self._soup.find(text='Codepoints').findNext('ul') if code_list: nonunique = [child.text.split()[1] for child in code_list.findChildren()] @@ -73,35 +98,48 @@ def codepoints(self): @property def platforms(self): + '''A list of platforms that the emoji is present on + + :rtype: [Platform] + ''' if not self._platforms: self._platforms = list() - platform_section = self.soup.find('section', + platform_section = self._soup.find('section', {'class': 'vendor-list'}) for vendor in platform_section.findAll( 'div', {'class': 'vendor-rollout-target'}): vendor_title = vendor.findNext('a') vendor_img = vendor.find('div', {'class': 'vendor-image'}) - platform = { - 'title': vendor_title.text - } + title = vendor_title.text + platform_image = None if vendor_img and vendor_img.find('img'): - platform['platform_image'] = vendor_img.find('img')['src'] + platform_image = vendor_img.find('img')['src'] + + platform = Platform(name=title, image_url=platform_image) self._platforms.append(platform) return self._platforms @property def shortcodes(self): + '''A list of shortcodes that represent the emoji + + Example: ``[:cop:]`` + ''' if not self._shortcodes: - codelist = self.soup.find(text='Shortcodes') + codelist = self._soup.find(text='Shortcodes') if codelist: self._shortcodes = codelist.findNext('ul').text.strip() return self._shortcodes @property def aliases(self): + '''A list of aliases associated with the emoji + + Example: ``[u'¯\_(ツ)_/¯', u'shrugging']`` + ''' if not self._aliases: - alias_section = self.soup.find('section', {'class': 'aliases'}) + alias_section = self._soup.find('section', {'class': 'aliases'}) if alias_section: self._aliases = list() for alias in alias_section.findAll('li'): @@ -109,17 +147,14 @@ def aliases(self): self._aliases.append(" ".join(alias.text.split()[1:])) return self._aliases - @property - def title(self): - if not self._title: - # Remove initial emoji + whitespace - self._title = " ".join(self.soup.find('h1').text.split()[1:]) - return self._title - @property def character(self): + '''The unicode character of the emoji + + Example: ``u'🤷'`` + ''' if not self._character: - self._character = self.soup.find('h1').text.split()[0] + self._character = self._soup.find('h1').text.split()[0] return self._character def __unicode__(self): diff --git a/emojipedia/emojipedia.py b/emojipedia/emojipedia.py index 6e08d85..5ac9f61 100644 --- a/emojipedia/emojipedia.py +++ b/emojipedia/emojipedia.py @@ -1,24 +1,44 @@ +# -*- coding: utf-8 -*- + from __future__ import unicode_literals from bs4 import BeautifulSoup import requests from .emoji import Emoji +EMOJI_CATEGORIES = ['people', 'nature', 'food-drink', 'activity', + 'travel-places', 'objects', 'symbols', 'flags'] +'''List of all valid emoji categories +''' -class Emojipedia: - all_categories = ['people', 'nature', 'food-drink', 'activity', - 'travel-places', 'objects', 'symbols', 'flags'] +class Emojipedia: @staticmethod def search(query): - return Emoji(Emojipedia.get_emoji_page(query)) + '''Searches for emojis on Emojipedia. Query must be a valid emoji name. + + :param str query: the search query + :returns: Emoji with the given name + :rtype: Emoji + ''' + return Emoji(Emojipedia._get_emoji_page(query)) @staticmethod def random(): - return Emoji(Emojipedia.get_emoji_page('random')) + '''Returns a random emoji. + + :returns: A random emoji + :rtype: Emoji + ''' + return Emoji(Emojipedia._get_emoji_page('random')) @staticmethod def category(query): - if query not in Emojipedia.all_categories: + '''Returns list of all emojis in the given category. + + :returns: List of emojies in the category + :rtype: [Emoji] + ''' + if query not in EMOJI_CATEGORIES: raise ValueError('{} is not a valid emoji category.'.format(query)) soup = Emojipedia._get_page(query) emoji_list = soup.find('ul', {'class': 'emoji-list'}) @@ -38,6 +58,14 @@ def category(query): @staticmethod def all(): + '''Returns list of emojis in Emojipedia. + + An extremely powerful method. + Returns all emojis known to human-kind. 😎 + + :returns: List of all emojies + :rtype: [Emoji] + ''' soup = Emojipedia._get_page('emoji') emoji_list = soup.find('table', {'class': 'emoji-list'}) if not emoji_list: @@ -71,7 +99,7 @@ def _get_page(query): return BeautifulSoup(response.text, 'html.parser') @staticmethod - def get_emoji_page(query): + def _get_emoji_page(query): soup = Emojipedia._get_page(query) if not Emojipedia._valid_emoji_page(soup): raise ValueError('Query did not yield a emoji entry') diff --git a/setup.py b/setup.py index 8615355..4ce2bf0 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,10 @@ #!/usr/bin/env python -from distutils.core import setup +from setuptools import setup setup(name='Emojipedia', packages=['emojipedia'], - version='0.3.3', + version='0.4.0', description='Emoji data from Emojipedia', author='Benjamin Congdon', author_email='bcongdo2@illinois.edu', diff --git a/test_emojipedia.py b/test_emojipedia.py index 725f610..874e31f 100644 --- a/test_emojipedia.py +++ b/test_emojipedia.py @@ -36,11 +36,10 @@ def test_platforms(): 'Microsoft'] # Order not important - assert set(correct) <= set([x['title'] for x in wink.platforms]) + assert set(correct) <= set([x.name for x in wink.platforms]) for platform in wink.platforms: - assert 'title' in platform - assert ('platform_image' in platform and - platform['platform_image'].startswith('http')) + assert platform.name + assert platform.image_url.startswith('http') def test_emoji_shortcodes():