Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using package attrdict instead of the initial custom implementation. #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
18 changes: 6 additions & 12 deletions newsapi/articles.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
from newsapi.base_news import BaseNews


class Articles(BaseNews):
def __init__(self, API_KEY):
super(Articles, self).__init__(API_KEY)
self.endpoint = "https://newsapi.org/v1/articles"

@property
def endpoint(self):
return "https://newsapi.org/v1/articles"

def get(self, source, sort_by="top", attributes_format=True):
self.payload['source'] = source
self.payload['sortBy'] = sort_by
r = self.requests.get(self.endpoint, params=self.payload)
if r.status_code != 200:
raise BaseException("Either server didn't respond or has resulted in zero results.")
try:
content = r.json()
except ValueError:
raise ValueError("No json data could be retrieved.")
if attributes_format:
return self.AttrDict(content)
return content

return super(Articles, self).get(attributes_format)

def get_by_top(self, source):
return self.get(source, sort_by="top")
Expand Down
29 changes: 20 additions & 9 deletions newsapi/base_news.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import requests


class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self

from abc import ABCMeta, abstractproperty
from attrdict import AttrDict

class BaseNews(object):
__metaclass__ = ABCMeta

def __init__(self, API_KEY):
self.API_KEY = API_KEY
self.payload = {"apiKey": self.API_KEY}
self.AttrDict = AttrDict
self.requests = requests

@abstractproperty
def endpoint(self):
raise NotImplemented

def get(self, attributes_format=True):
r = requests.get(self.endpoint, params=self.payload)
if r.status_code != 200:
raise BaseException("Either server didn't respond or has resulted in zero results.")
try:
content = r.json()
except ValueError:
raise ValueError("No json data could be retrieved.")
if attributes_format:
return AttrDict(content)
return content
18 changes: 6 additions & 12 deletions newsapi/sources.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
from newsapi.base_news import BaseNews


class Sources(BaseNews):
def __init__(self, API_KEY):
super(Sources, self).__init__(API_KEY)
self.endpoint = "https://newsapi.org/v1/sources"
self.sources = []
self.sources_base_info = {}
self.sources_id_info = {}
self.categories = {}
self.languages = {}
self.countries = {}

@property
def endpoint(self):
return "https://newsapi.org/v1/sources"

def get(self, category="", language="", country="", attributes_format=True):
self.payload['category'] = category
self.payload['language'] = language
self.payload['country'] = country
r = self.requests.get(self.endpoint, params=self.payload)
if r.status_code != 200:
raise BaseException("Either server didn't respond or has resulted in zero results.")
try:
content = r.json()
except ValueError:
raise ValueError("No json data could be retrieved.")
if attributes_format:
return self.AttrDict(content)
return content

return super(Sources, self).get(attributes_format)

def all(self):
return self.get()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
attrdict==2.0.0