forked from gustavoalmeida/fullstack-coding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackernews_api.py
More file actions
32 lines (26 loc) · 881 Bytes
/
hackernews_api.py
File metadata and controls
32 lines (26 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from requests import get
class HackerNewsAPI(object):
"""
Class to interact with Hacker News API
"""
base_url = 'https://hacker-news.firebaseio.com/v0/'
def __init__(self, top_num):
self.top_num = top_num
def get_top_stories(self):
try:
url = '{}/topstories.json'.format(self.base_url)
ids = get(url)
if ids and ids.status_code == 200:
return [id_ for id_ in ids.json()[:self.top_num]]
return []
except:
return []
def get_item(self, id_):
try:
url = '{}item/{}.json'.format(self.base_url, id_)
item = get(url)
if item and item.status_code == 200:
return item.json()
return None
except:
return None