Skip to content

Commit

Permalink
Merge pull request #7 from Smlep/develop
Browse files Browse the repository at this point in the history
Add new york times
  • Loading branch information
Smlep committed Dec 13, 2020
2 parents 8f7045a + 986d08c commit ee2d269
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions smlep_news/nytimes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import requests

from .article import Article

base = "https://api.nytimes.com/svc/mostpopular/v2/viewed/"


def get_most_viewed_articles(key, days_count):
if days_count not in [1, 7, 30]:
raise ValueError("The days period must be 1, 7 or 30")
url = "{}{}.json?api-key={}".format(base, days_count, key)

r = requests.get(url)
r.raise_for_status()

return [Article(a) for a in r.json()["results"]]
9 changes: 9 additions & 0 deletions smlep_news/nytimes/article.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Article:
def __init__(self, article_json):
self.title = article_json["title"]
self.pub_date = article_json["published_date"]
self.url = article_json["url"]
self.abstract = article_json["abstract"]

def __repr__(self):
return "NYT Article: {}".format(self.title)

0 comments on commit ee2d269

Please sign in to comment.