From 995297dc94980bab4cea97a2dd9d867933f3ab20 Mon Sep 17 00:00:00 2001 From: "beth.null" Date: Thu, 30 Mar 2017 05:55:37 +0200 Subject: [PATCH 1/2] Using package attrdict instead of the initial custom implementation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The AttrDict class implemented at base_news was not behaving as expected as keys can’t be accessed as instance attributes. --- newsapi/base_news.py | 7 +------ requirements.txt | 1 + 2 files changed, 2 insertions(+), 6 deletions(-) create mode 100644 requirements.txt diff --git a/newsapi/base_news.py b/newsapi/base_news.py index 76a03b3..1dbdb9f 100644 --- a/newsapi/base_news.py +++ b/newsapi/base_news.py @@ -1,11 +1,6 @@ import requests - -class AttrDict(dict): - def __init__(self, *args, **kwargs): - super(AttrDict, self).__init__(*args, **kwargs) - self.__dict__ = self - +from attrdict import AttrDict class BaseNews(object): def __init__(self, API_KEY): diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3c6f771 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +attrdict==2.0.0 From 6608966bfbd18ac3ee3582847145699f4b3c710e Mon Sep 17 00:00:00 2001 From: "beth.null" Date: Sat, 7 Oct 2017 13:17:55 +0200 Subject: [PATCH 2/2] Backup commit. --- newsapi/articles.py | 18 ++++++------------ newsapi/base_news.py | 22 +++++++++++++++++++--- newsapi/sources.py | 18 ++++++------------ 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/newsapi/articles.py b/newsapi/articles.py index 8e8518f..ede2b1f 100644 --- a/newsapi/articles.py +++ b/newsapi/articles.py @@ -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") diff --git a/newsapi/base_news.py b/newsapi/base_news.py index 1dbdb9f..ac46af2 100644 --- a/newsapi/base_news.py +++ b/newsapi/base_news.py @@ -1,10 +1,26 @@ import requests - +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 diff --git a/newsapi/sources.py b/newsapi/sources.py index d845e0d..c026eaf 100644 --- a/newsapi/sources.py +++ b/newsapi/sources.py @@ -1,10 +1,8 @@ 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 = {} @@ -12,20 +10,16 @@ def __init__(self, API_KEY): 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()