Python wrapper for NYTimes Top Stories API
Project description
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:kaarora123/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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file nytimes-top-stories-1.0.0.tar.gz
.
File metadata
- Download URL: nytimes-top-stories-1.0.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
fe1a11414817f02ff8f378624bc0c72d10cb3431118f137b661299ca112cff94
|
|
MD5 |
5b16f24da0f1071b7969674b2c04c343
|
|
BLAKE2b-256 |
97929a60a65ffda337c4dd8b1e1acb0aca2f3a38d51259f8df276e9e534e74ab
|
File details
Details for the file nytimes_top_stories-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: nytimes_top_stories-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
cc90f11ab32ad193cece4997b34c9259db4175abcc4104408befb6b065df2b90
|
|
MD5 |
1b7c781648aaad7ef73c4286afb4a802
|
|
BLAKE2b-256 |
6f681da3aa6d8cd7cdfb9b3e694633edd2ca9c5b8d4530dfeabb29e4a6f4a5bb
|