Skip to content

Commit bc8f0a4

Browse files
bsolomon1124mattlisiv
authored andcommitted
Create documentation source RST (#55)
* Initial commit: create documentation source RST * Update 'default' descriptions of const.py vars * Update parameter documentation
1 parent fa0654a commit bc8f0a4

12 files changed

+476
-124
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
build/
44
dist/
55
*.egg-info/
6-
*.rst
76
venv/
87
*log.txt
98
*~
9+
docs/build/

Diff for: docs/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = source
9+
BUILDDIR = build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

Diff for: docs/make.bat

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ECHO OFF
2+
3+
pushd %~dp0
4+
5+
REM Command file for Sphinx documentation
6+
7+
if "%SPHINXBUILD%" == "" (
8+
set SPHINXBUILD=sphinx-build
9+
)
10+
set SOURCEDIR=source
11+
set BUILDDIR=build
12+
13+
if "%1" == "" goto help
14+
15+
%SPHINXBUILD% >NUL 2>NUL
16+
if errorlevel 9009 (
17+
echo.
18+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
19+
echo.installed, then set the SPHINXBUILD environment variable to point
20+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
21+
echo.may add the Sphinx directory to PATH.
22+
echo.
23+
echo.If you don't have Sphinx installed, grab it from
24+
echo.http://sphinx-doc.org/
25+
exit /b 1
26+
)
27+
28+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29+
goto end
30+
31+
:help
32+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33+
34+
:end
35+
popd

Diff for: docs/source/api.rst

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
.. _api:
2+
3+
API Reference
4+
=============
5+
6+
.. module:: newsapi-python
7+
8+
This page is a technical reference to the public classes, exceptions, and data
9+
defined in newsapi-python.
10+
11+
While newsapi-python makes every effort to keep up with the API,
12+
please consider the official News API `docs <https://newsapi.org/docs>`_
13+
as the canonical News API reference.
14+
15+
Classes
16+
-------
17+
18+
.. autoclass:: newsapi.NewsApiClient
19+
:members:
20+
21+
Exceptions
22+
----------
23+
24+
.. autoexception:: newsapi.newsapi_exception.NewsAPIException
25+
26+
Constants
27+
---------
28+
29+
The :mod:`newsapi.const` module holds constants and allowed parameter values specified in the official News API documentation.
30+
31+
.. autodata:: newsapi.const.languages
32+
33+
.. autodata:: newsapi.const.countries
34+
35+
.. autodata:: newsapi.const.categories
36+
37+
.. autodata:: newsapi.const.sort_method

Diff for: docs/source/conf.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from __future__ import unicode_literals
2+
3+
import os
4+
import sys
5+
6+
# Configuration file for the Sphinx documentation builder.
7+
#
8+
# This file only contains a selection of the most common options. For a full
9+
# list see the documentation:
10+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
11+
12+
# -- Path setup --------------------------------------------------------------
13+
14+
# If extensions (or modules to document with autodoc) are in another directory,
15+
# add these directories to sys.path here. If the directory is relative to the
16+
# documentation root, use os.path.abspath to make it absolute, like shown here.
17+
18+
sys.path.insert(0, os.path.abspath("../../"))
19+
20+
from setup import VERSION
21+
22+
# -- Project information -----------------------------------------------------
23+
24+
project = "newsapi-python"
25+
copyright = "2019, Matt Lisivick"
26+
author = "Matt Lisivick & Brad Solomon"
27+
version = release = VERSION
28+
29+
30+
# -- General configuration ---------------------------------------------------
31+
32+
# Add any Sphinx extension module names here, as strings. They can be
33+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
34+
# ones.
35+
extensions = ["sphinx.ext.autodoc"]
36+
37+
# Add any paths that contain templates here, relative to this directory.
38+
templates_path = ["_templates"]
39+
40+
# List of patterns, relative to source directory, that match files and
41+
# directories to ignore when looking for source files.
42+
# This pattern also affects html_static_path and html_extra_path.
43+
exclude_patterns = [".DS_Store"]
44+
45+
46+
# -- Options for HTML output -------------------------------------------------
47+
48+
# The theme to use for HTML and HTML Help pages. See the documentation for
49+
# a list of builtin themes.
50+
#
51+
html_theme = "classic"
52+
53+
# Add any paths that contain custom static files (such as style sheets) here,
54+
# relative to this directory. They are copied after the builtin static files,
55+
# so a file named "default.css" will overwrite the builtin "default.css".
56+
html_static_path = ["_static"]
57+
58+
html_show_sourcelink = True
59+
html_show_sphinx = False
60+
html_last_updated_fmt = ""
61+
62+
63+
# -- Autodoc -----------------------------------------------------------------
64+
65+
autodoc_warningiserror = True

Diff for: docs/source/examples.rst

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
.. _examples:
2+
3+
Example :class:`NewsApiClient` Usage
4+
====================================
5+
6+
This page is a tutorial-by-example for using the :class:`NewsApiClient` class.
7+
8+
Basic Usage
9+
-----------
10+
11+
The top-level :class:`NewsApiClient` class allows you to access News API endpoints. Initialize the client with your API key::
12+
13+
import os
14+
from newsapi import NewsApiClient
15+
16+
# An API key; for example: "74f9e72a4bfd4dbaa0cbac8e9a17d34a"
17+
key = os.environ["news_api_secret"]
18+
19+
api = NewsApiClient(api_key=key)
20+
21+
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`_.
22+
23+
Accessing the `/top-headlines` Endpoint
24+
---------------------------------------
25+
26+
Use :meth:`newsapi.NewsApiClient.get_top_headlines` to pull from the `/top-headlines` endpoint::
27+
28+
api.get_top_headlines()
29+
api.get_top_headlines(q="hurricane")
30+
api.get_top_headlines(category="sports")
31+
api.get_top_headlines(sources="abc-news,ars-technica", page_size=50)
32+
33+
Accessing the `/everything` Endpoint
34+
------------------------------------
35+
36+
Use :meth:`newsapi.NewsApiClient.get_everything` to pull from the `/everything` endpoint::
37+
38+
api.get_everything("hurricane OR tornado", sort_by="relevancy", language="en")
39+
api.get_everything("(hurricane OR tornado) AND FEMA", sort_by="relevancy")
40+
41+
42+
Accessing the `/sources` Endpoint
43+
---------------------------------
44+
45+
Use :meth:`newsapi.NewsApiClient.get_sources` to pull from the `/sources` endpoint::
46+
47+
api.get_sources()
48+
api.get_sources(category="technology")
49+
api.get_sources(country="ru")
50+
api.get_sources(category="health", country="us")
51+
api.get_sources(language="en", country="in")
52+
53+
Using a Dedicated Session
54+
-------------------------
55+
56+
By default, each method call from :class:`NewsApiClient` uses a new TCP session (and ``requests.Session`` instance).
57+
This is not ideal if you'd like to call endpoints multiple times,
58+
whereas using a single session can provide connection-pooling and cookie persistence.
59+
60+
To use a single session across multiple method calls, pass the session object to :class:`NewsApiClient`::
61+
62+
import requests
63+
64+
with requests.Session() as session:
65+
# Use a single session for multiple requests. Using a 'with'
66+
# context manager closes the session and TCP connection after use.
67+
api = NewsApiClient(api_key=key, session=session)
68+
data1 = api.get_top_headlines(category="technology")
69+
data2 = api.get_everything(q="facebook", domains="mashable.com,wired.com")
70+
71+
Date Inputs
72+
-----------
73+
74+
The optional parameters ``from_param`` and ``to`` used in :meth:`newsapi.NewsApiClient.get_everything`
75+
allow you to constrain the result set to articles published within a given span.
76+
77+
You can pass a handful of different types:
78+
79+
- ``datetime.date``
80+
- ``datetime.datetime`` (assumed to be in UTC time)
81+
- ``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*)
82+
- ``int`` or ``float`` (assumed represents a Unix timestamp)
83+
- ``None`` (the default, in which there is no constraint)
84+
85+
Here are a few valid examples::
86+
87+
import datetime as dt
88+
89+
api.get_everything(
90+
q="hurricane",
91+
from_param=dt.date(2019, 9, 1),
92+
to=dt.date(2019, 9, 3),
93+
)
94+
95+
api.get_everything(
96+
q="hurricane",
97+
from_param=dt.datetime(2019, 9, 1, hour=5),
98+
to=dt.datetime(2019, 9, 1, hour=15),
99+
)
100+
101+
api.get_everything(
102+
q="hurricane",
103+
from_param="2019-08-01",
104+
to="2019-09-15",
105+
)
106+
107+
api.get_everything(
108+
q="hurricane",
109+
from_param="2019-08-01",
110+
to="2019-09-15",
111+
)
112+
113+
api.get_everything(
114+
q="venezuela",
115+
from_param="2019-08-01T10:30:00",
116+
to="2019-09-15T14:00:00",
117+
)

Diff for: docs/source/index.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Welcome: Documentation for `newsapi-python`
2+
===========================================
3+
4+
.. image:: https://img.shields.io/github/license/mattlisiv/newsapi-python.svg
5+
:target: https://github.com/mattlisiv/newsapi-python/blob/master/LICENSE.txt
6+
.. image:: https://img.shields.io/pypi/v/newsapi-python.svg
7+
:target: https://pypi.org/project/newsapi-python/
8+
.. image:: https://img.shields.io/pypi/status/newsapi-python.svg
9+
:target: https://pypi.org/project/newsapi-python/
10+
.. image:: https://img.shields.io/pypi/pyversions/newsapi-python.svg
11+
:target: https://pypi.org/project/newsapi-python
12+
13+
This is documentation for version \ |version| of `newsapi-python`,
14+
a Python client for the `News API <https://newsapi.org/>`_.
15+
16+
The project source repository is `hosted on GitHub <https://github.com/mattlisiv/newsapi-python>`_.
17+
18+
Documentation Contents
19+
----------------------
20+
21+
.. toctree::
22+
examples
23+
api

Diff for: newsapi/const.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
"""Constants and allowed parameter values specified in the News API."""
2+
13
TOP_HEADLINES_URL = "https://newsapi.org/v2/top-headlines"
24
EVERYTHING_URL = "https://newsapi.org/v2/everything"
35
SOURCES_URL = "https://newsapi.org/v2/sources"
6+
7+
#: The 2-letter ISO 3166-1 code of the country you want to get headlines for. If not specified,
8+
#: the results span all countries.
49
countries = {
510
"ae",
611
"ar",
@@ -62,8 +67,13 @@
6267
"zh",
6368
}
6469

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

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

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

0 commit comments

Comments
 (0)