Skip to content

Commit

Permalink
Merge 929b833 into afa438a
Browse files Browse the repository at this point in the history
  • Loading branch information
tcmetzger committed Jul 6, 2020
2 parents afa438a + 929b833 commit 0e7da90
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ repos:
- id: flake8
log_file: '.artifacts/flake8.log'
- id: requirements-txt-fixer
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.782
hooks:
- id: mypy
additional_dependencies: [tokenize-rt==3.2.0]
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Getting started
Ready to contribute? Here's how to set up PyZillow for
local development.

Fork_ the `pyzillow` repo on GitHub.
Fork the `pyzillow` repo on GitHub.

Clone your fork locally::

Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ coverage:
open htmlcov/index.html

docs:
rm -f docs/pyzillow.rst
rm -f docs/modules.rst
sphinx-apidoc -o docs/ pyzillow
pip install sphinx
pip install sphinx_rtd_theme
$(MAKE) -C docs clean
$(MAKE) -C docs html
open docs/_build/html/index.html
Expand Down
106 changes: 106 additions & 0 deletions docs/_ext/ogtag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""Automatically generates Open Graph tags for Sphinx html docs.
Based on https://sphinx-users.jp/cookbook/ogp/index.html
"""

from docutils import nodes
from sphinx import addnodes
from urllib.parse import urljoin


class Visitor:
def __init__(self, document):
self.document = document
self.text_list = []
self.images = []
self.n_sections = 0

def dispatch_visit(self, node):
# Skip toctree
if isinstance(node, addnodes.compact_paragraph) and node.get("toctree"):
raise nodes.SkipChildren

# Collect images
if isinstance(node, nodes.image):
self.images.append(node)

# Collect text (first three sections)
if self.n_sections < 3:

# Collect paragraphs
if isinstance(node, nodes.paragraph):
self.text_list.append(node.astext().replace("\n", " "))

# Include only paragraphs
if isinstance(node, nodes.section):
self.n_sections += 1

def dispatch_departure(self, node):
pass

def get_og_description(self):
# TODO: Find optimal length for description text
text = " ".join(self.text_list)
if len(text) > 200:
text = text[:197] + "..."
return text

def get_og_image_url(self, page_url):
# TODO: Check if picking first image makes sense
# TODO: Return fallback image if no image found on page
if self.images:
return urljoin(page_url, self.images[0]["uri"])
else:
return None


def get_og_tags(context, doctree, config):
# page_url
site_url = config.og_site_url
page_url = urljoin(site_url, context["pagename"] + context["file_suffix"])

# collection
visitor = Visitor(doctree)
doctree.walkabout(visitor)

# og:description
og_desc = visitor.get_og_description()

# og:image
og_image = visitor.get_og_image_url(page_url)

# OGP
tags = """
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="{cfg[og_twitter_site]}" />
<meta property="og:site_name" content="{ctx[shorttitle]}">
<meta property="og:title" content="{ctx[title]}">
<meta property="og:description" content="{desc}">
<meta property='og:url' content="{page_url}">
""".format(
ctx=context, desc=og_desc, page_url=page_url, cfg=config
)
# Add image if present, use default image if no image is found
if og_image:
tags += f'<meta property="og:image" content="{og_image}">'
elif config.og_fallback_image:
tags += f'<meta property="og:image" content="{config.og_fallback_image}">'
return tags


def html_page_context(app, pagename, templatename, context, doctree):
if not doctree:
return

context["metatags"] += get_og_tags(context, doctree, app.config)


def setup(app):
app.add_config_value("og_site_url", None, "html")
app.add_config_value("og_twitter_site", None, "html")
app.add_config_value("og_fallback_image", None, "html")
app.connect("html-page-context", html_page_context)
return {
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
55 changes: 35 additions & 20 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# -*- coding: utf-8 -*-
#
# complexity documentation build configuration file, created by
# sphinx-quickstart on Tue Jul 9 22:26:36 2013.
#
# 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.

"""Complexity documentation build configuration file, created by
sphinx-quickstart on Tue Jul 9 22:26:36 2013.
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 os
import sys
Expand All @@ -23,6 +24,9 @@
parent = os.path.dirname(cwd)
sys.path.insert(0, parent)

# Add path for _ext/ogtag.py extension
sys.path.append(os.path.abspath("_ext"))

import pyzillow # noqa

# -- General configuration -----------------------------------------------------
Expand All @@ -32,7 +36,13 @@

# 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"]
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.viewcode",
"ogtag",
# "sphinx.ext.autosummary"
]
# autosummary_generate = True

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
Expand Down Expand Up @@ -99,6 +109,11 @@

# -- Options for HTML output ---------------------------------------------------

# Constants for _ext/ogtag.py extension
og_site_url = "https://pyzillow.readthedocs.io/en/latest/"
og_twitter_site = "@tcmetzger"
og_fallback_image = "https://www.tcmetzger.de/STATIC/pyzillow_default.png"

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = "sphinx_rtd_theme"
Expand Down Expand Up @@ -179,14 +194,14 @@

# -- 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_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': '',
# }

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
Expand Down
11 changes: 7 additions & 4 deletions pyzillow/pyzillow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


class ZillowWrapper(object):
"""This class provides an interface into the Zillow API. An API key is required to create an instance of this class:
"""This class provides an interface into the Zillow API.
An API key is required to create an instance of this class:
>>> from pyzillow.pyzillow import ZillowWrapper
>>> zillow_data = ZillowWrapper(YOUR_ZILLOW_API_KEY)
Expand Down Expand Up @@ -90,7 +91,8 @@ def get_updated_property_details(self, zpid: str):
return self.get_data(url, params)

def get_data(self, url: str, params: dict):
"""This method requests data from the API endpoint specified in the url argument. It uses parameters from the params argument.
"""This method requests data from the API endpoint specified in the url argument.
It uses parameters from the params argument.
:param url: URL of API endpoint
:type url: str
Expand Down Expand Up @@ -130,7 +132,7 @@ def get_data(self, url: str, params: dict):
raise ZillowFail

if response.findall("message/code")[0].text != "0":
raise ZillowError(int(response.findall("message/code")[0].text))
raise ZillowError(int(str(response.findall("message/code")[0].text)))
else:
if not response.findall("response"):
print("Zillow returned no results for ({})".format(params["address"]))
Expand All @@ -143,7 +145,8 @@ class ZillowResults(object):
and :class:`pyzillow.pyzillow.GetUpdatedPropertyDetails`.
"""

attribute_mapping = {}
def __init__(self):
self.attribute_mapping = {}

def get_attr(self, attr):
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements/test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cov-core==1.15.0
coverage==5.1
coverage==4.5.4
pytest==5.4.3
pytest-cov==2.9.0
pytest-flakes==4.0.0
Expand Down

0 comments on commit 0e7da90

Please sign in to comment.