|
| 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 | + ) |
0 commit comments