Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ ENV/

# mypy
.mypy_cache/

.idea/
*.iml
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,29 @@ Coming soon... this is where our officially supported SDK for Python is going to

## Developers... we need you!
We need some help fleshing out this repo. If you're a Python dev with experience building PyPI-compatible libraries and web API wrappers, we're offering a reward of $250 to help us get started. For more info please email [email protected], or dive right in and send us a pull request.


# Usage
```
import newsapi
newsapi.API_KEY = '<API Key>'

# list of all sources
newsapi.Sources().list()

# list of American sources
newsapi.Sources().list(country='us')

# list of American Gaming sources
newsapi.Sources().list(**{'country': 'us', 'category': 'gaming'})

# Same pattern for TopHeadlines and Everything

newsapi.TopHeadlines().list()
newsapi.TopHeadlines().list(arg1=..., arg2=...)
newsapi.TopHeadlines().list(**{'arg1':..., 'arg2':...})

newsapi.Everything().list()
newsapi.Everything().list(arg1=..., arg2=...)
newsapi.Everything().list(**{'arg1':..., 'arg2':...})
```
4 changes: 4 additions & 0 deletions newsapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
API_KEY = None
API_URL_BASE = 'https://newsapi.org/v2/'

from . resources import TopHeadlines, Everything, Sources
24 changes: 24 additions & 0 deletions newsapi/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import requests

from .errors import NewsAPIError


class Client:

def __init__(self):
self._session = requests.Session()

from newsapi import API_KEY
self._api_key = API_KEY
self._session.headers.update({'Authorization': self._api_key})

from newsapi import API_URL_BASE
self._api_url_base = API_URL_BASE

def get(self, endpoint, params):
resp = self._session.get(self._api_url_base + endpoint, params=params)
try:
resp.raise_for_status()
return resp.json()
except requests.exceptions.HTTPError:
raise NewsAPIError(resp.json())
5 changes: 5 additions & 0 deletions newsapi/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NewsAPIError(Exception):

def __init__(self, response_json, *args, **kwargs):
message = '{code} : {message}'.format(**response_json)
super().__init__(message, *args, **kwargs)
31 changes: 31 additions & 0 deletions newsapi/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from .client import Client


class BaseResource:

endpoint = None
item = None

def __init__(self):
self.client = Client()

def list(self, **params):
return self.client.get(self.endpoint, params)[self.item]


class TopHeadlines(BaseResource):

endpoint = 'top-headlines'
item = 'articles'


class Everything(BaseResource):

endpoint = 'everything'
item = 'articles'


class Sources(BaseResource):

endpoint = 'sources'
item = 'sources'
29 changes: 29 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
from setuptools import setup, find_packages

install_reqs = [
'requests',
]

setup(
name='newsapi',
version='0.0.0',
description='Python client for the News-API REST API',
author='James Christopher',
author_email='[email protected]',
packages=find_packages(),
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python',
'Topic :: Internet :: WWW/HTTP',
],
url='https://github.com/News-API-gh/News-API-python',
install_requires=install_reqs,
)