Skip to main content

nytimes-top-stories is a simple Python wrapper for New York Times’ Top Stories API.

Compatible with Python 2.7 and 3+.

Dependencies

nytimes-top-stories requires the requests package.

Installation

Pip

pip install nytimes-top-stories

Clone repository

  • git clone git@github.com:kashiish/nytimes-top-stories.git

  • cd into your nytimes-top-stories directory

  • python setup.py install

Usage

Register for a Top Stories API key at https://developer.nytimes.com/signup.

from topstories import TopStoriesAPI
api = TopStoriesAPI(<SECRET_API_KEY>)

The get_stories method takes one required argument and two optional arguments. Checkout the API documentation to see the data that is returned from the API.

def get_stories(self, section, format_type="json", return_json_string=False):
  """
  Gets a list of current top articles and associated images in the
  specified section and in the specified format.

  params:
      section: (string) the section the articles appears in
      format: (string) json or jsonp, default is json
      return_json_string: (boolean) if True, return value will be JSON string instead of a python list,
                          default is False
  return:
      format_type=json: A list of articles (articles are python dicts) is returned.
                        If return_json_string, string is returned.
      format_type=jsonp: API returns a callback function (string) in the format
                        "{section}TopStoriesCallback({data})".
                        {data} is an object, not an array, and it is not parsed/decoded.
  """
stories = api.get_stories("politics") # list of story dicts
stories_string = api.get_stories("home", return_json_string=True) # json string
stories_jsonp = api.get_stories("work", format_type="jsonp") # (string) callback function with data input

In addition, there are two more public methods:

#returns a list of valid sections
>>> api.get_sections_list()
>>> ["home","opinion","world","national","politics","upshot","nyregion","business",
"technology","science","health","sports","arts","books","theater","sundayreview",
"fashion","tmagazine","food","travel","magazine","realestate","automobiles",
"obituaries","insider"]
# writes a list of stories to a json file
# input a path to file and list of stories
api.write_to_json_file("example.json", stories)

Testing

pytest and mock are used for testing this package.

python setup.py pytest

Metadata

Release files for nytimes-top-stories 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nytimes-top-stories 1.0.1
File Size Uploaded Provenance Actions
nytimes-top-stories-1.0.1.tar.gz 3.8 kB Without provenance Details for nytimes-top-stories-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nytimes-top-stories 1.0.1
File Interpreter ABI Platform Actions
nytimes_top_stories-1.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details for nytimes_top_stories-1.0.1-py2.py3-none-any.whl

Total release size: 10.4 kB

Release files / nytimes-top-stories-1.0.1.tar.gz

Download URL nytimes-top-stories-1.0.1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
01ed95ae373af24e69f565e2d2eabcb898f6759ae4d0ae81171f31ecacfa6de7
BLAKE2b-256 checksum
How to use checksums
8545516fedb536ddf6b75489539ebbf99fbad44bf30a0a5dcc8d08c4765e0f36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / nytimes_top_stories-1.0.1-py2.py3-none-any.whl

Download URL nytimes_top_stories-1.0.1-py2.py3-none-any.whl
Size 6.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
83a52af4139515aeb1a4c584d119a6bcbdc907025bc440598a8a0cd6950e0ea1
BLAKE2b-256 checksum
How to use checksums
57f3309510b647c12220001ac49ece55a8b61c47b8310a2e05cf31345efa0b86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for nytimes-top-stories 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page