Skip to content

Commit

Permalink
Create documentation source RST (#55)
Browse files Browse the repository at this point in the history
* Initial commit: create documentation source RST

* Update 'default' descriptions of const.py vars

* Update parameter documentation
  • Loading branch information
bsolomon1124 authored and mattlisiv committed Sep 12, 2019
1 parent fa0654a commit bc8f0a4
Show file tree
Hide file tree
Showing 12 changed files with 476 additions and 124 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -3,7 +3,7 @@
build/
dist/
*.egg-info/
*.rst
venv/
*log.txt
*~
docs/build/
20 changes: 20 additions & 0 deletions docs/Makefile
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
35 changes: 35 additions & 0 deletions docs/make.bat
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
37 changes: 37 additions & 0 deletions docs/source/api.rst
@@ -0,0 +1,37 @@
.. _api:

API Reference
=============

.. module:: newsapi-python

This page is a technical reference to the public classes, exceptions, and data
defined in newsapi-python.

While newsapi-python makes every effort to keep up with the API,
please consider the official News API `docs <https://newsapi.org/docs>`_
as the canonical News API reference.

Classes
-------

.. autoclass:: newsapi.NewsApiClient
:members:

Exceptions
----------

.. autoexception:: newsapi.newsapi_exception.NewsAPIException

Constants
---------

The :mod:`newsapi.const` module holds constants and allowed parameter values specified in the official News API documentation.

.. autodata:: newsapi.const.languages

.. autodata:: newsapi.const.countries

.. autodata:: newsapi.const.categories

.. autodata:: newsapi.const.sort_method
65 changes: 65 additions & 0 deletions docs/source/conf.py
@@ -0,0 +1,65 @@
from __future__ import unicode_literals

import os
import sys

# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# 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("../../"))

from setup import VERSION

# -- Project information -----------------------------------------------------

project = "newsapi-python"
copyright = "2019, Matt Lisivick"
author = "Matt Lisivick & Brad Solomon"
version = release = VERSION


# -- General configuration ---------------------------------------------------

# 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"]

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = [".DS_Store"]


# -- 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 = "classic"

# 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"]

html_show_sourcelink = True
html_show_sphinx = False
html_last_updated_fmt = ""


# -- Autodoc -----------------------------------------------------------------

autodoc_warningiserror = True
117 changes: 117 additions & 0 deletions docs/source/examples.rst
@@ -0,0 +1,117 @@
.. _examples:

Example :class:`NewsApiClient` Usage
====================================

This page is a tutorial-by-example for using the :class:`NewsApiClient` class.

Basic Usage
-----------

The top-level :class:`NewsApiClient` class allows you to access News API endpoints. Initialize the client with your API key::

import os
from newsapi import NewsApiClient

# An API key; for example: "74f9e72a4bfd4dbaa0cbac8e9a17d34a"
key = os.environ["news_api_secret"]

api = NewsApiClient(api_key=key)

The only required parameter is an `API key <https://newsapi.org/register>`_. You can also pass a persistent ``session`` object; see `Using a Dedicated Session`_.

Accessing the `/top-headlines` Endpoint
---------------------------------------

Use :meth:`newsapi.NewsApiClient.get_top_headlines` to pull from the `/top-headlines` endpoint::

api.get_top_headlines()
api.get_top_headlines(q="hurricane")
api.get_top_headlines(category="sports")
api.get_top_headlines(sources="abc-news,ars-technica", page_size=50)

Accessing the `/everything` Endpoint
------------------------------------

Use :meth:`newsapi.NewsApiClient.get_everything` to pull from the `/everything` endpoint::

api.get_everything("hurricane OR tornado", sort_by="relevancy", language="en")
api.get_everything("(hurricane OR tornado) AND FEMA", sort_by="relevancy")


Accessing the `/sources` Endpoint
---------------------------------

Use :meth:`newsapi.NewsApiClient.get_sources` to pull from the `/sources` endpoint::

api.get_sources()
api.get_sources(category="technology")
api.get_sources(country="ru")
api.get_sources(category="health", country="us")
api.get_sources(language="en", country="in")

Using a Dedicated Session
-------------------------

By default, each method call from :class:`NewsApiClient` uses a new TCP session (and ``requests.Session`` instance).
This is not ideal if you'd like to call endpoints multiple times,
whereas using a single session can provide connection-pooling and cookie persistence.

To use a single session across multiple method calls, pass the session object to :class:`NewsApiClient`::

import requests

with requests.Session() as session:
# Use a single session for multiple requests. Using a 'with'
# context manager closes the session and TCP connection after use.
api = NewsApiClient(api_key=key, session=session)
data1 = api.get_top_headlines(category="technology")
data2 = api.get_everything(q="facebook", domains="mashable.com,wired.com")

Date Inputs
-----------

The optional parameters ``from_param`` and ``to`` used in :meth:`newsapi.NewsApiClient.get_everything`
allow you to constrain the result set to articles published within a given span.

You can pass a handful of different types:

- ``datetime.date``
- ``datetime.datetime`` (assumed to be in UTC time)
- ``str`` formated as either ``%Y-%m-%d`` (e.g. *2019-09-07*) or ``%Y-%m-%dT%H:%M:%S`` (e.g. *2019-09-07T13:04:15*)
- ``int`` or ``float`` (assumed represents a Unix timestamp)
- ``None`` (the default, in which there is no constraint)

Here are a few valid examples::

import datetime as dt

api.get_everything(
q="hurricane",
from_param=dt.date(2019, 9, 1),
to=dt.date(2019, 9, 3),
)

api.get_everything(
q="hurricane",
from_param=dt.datetime(2019, 9, 1, hour=5),
to=dt.datetime(2019, 9, 1, hour=15),
)

api.get_everything(
q="hurricane",
from_param="2019-08-01",
to="2019-09-15",
)

api.get_everything(
q="hurricane",
from_param="2019-08-01",
to="2019-09-15",
)

api.get_everything(
q="venezuela",
from_param="2019-08-01T10:30:00",
to="2019-09-15T14:00:00",
)
23 changes: 23 additions & 0 deletions docs/source/index.rst
@@ -0,0 +1,23 @@
Welcome: Documentation for `newsapi-python`
===========================================

.. image:: https://img.shields.io/github/license/mattlisiv/newsapi-python.svg
:target: https://github.com/mattlisiv/newsapi-python/blob/master/LICENSE.txt
.. image:: https://img.shields.io/pypi/v/newsapi-python.svg
:target: https://pypi.org/project/newsapi-python/
.. image:: https://img.shields.io/pypi/status/newsapi-python.svg
:target: https://pypi.org/project/newsapi-python/
.. image:: https://img.shields.io/pypi/pyversions/newsapi-python.svg
:target: https://pypi.org/project/newsapi-python

This is documentation for version \ |version| of `newsapi-python`,
a Python client for the `News API <https://newsapi.org/>`_.

The project source repository is `hosted on GitHub <https://github.com/mattlisiv/newsapi-python>`_.

Documentation Contents
----------------------

.. toctree::
examples
api
10 changes: 10 additions & 0 deletions newsapi/const.py
@@ -1,6 +1,11 @@
"""Constants and allowed parameter values specified in the News API."""

TOP_HEADLINES_URL = "https://newsapi.org/v2/top-headlines"
EVERYTHING_URL = "https://newsapi.org/v2/everything"
SOURCES_URL = "https://newsapi.org/v2/sources"

#: The 2-letter ISO 3166-1 code of the country you want to get headlines for. If not specified,
#: the results span all countries.
countries = {
"ae",
"ar",
Expand Down Expand Up @@ -62,8 +67,13 @@
"zh",
}

#: The 2-letter ISO-639-1 code of the language you want to get articles for. If not specified,
#: the results span all languages.
languages = {"ar", "en", "cn", "de", "es", "fr", "he", "it", "nl", "no", "pt", "ru", "sv", "se", "ud", "zh"}

#: The category you want to get articles for. If not specified,
#: the results span all categories.
categories = {"business", "entertainment", "general", "health", "science", "sports", "technology"}

#: The order to sort article results in. If not specified, the default is ``"publishedAt"``.
sort_method = {"relevancy", "popularity", "publishedAt"}

0 comments on commit bc8f0a4

Please sign in to comment.