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 76a03b3..ac46af2 100644 --- a/newsapi/base_news.py +++ b/newsapi/base_news.py @@ -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 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() 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