-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhacker_news.py
30 lines (22 loc) · 863 Bytes
/
hacker_news.py
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
import json
import requests
def get_top_stories(top_number=25):
url = 'https://hacker-news.firebaseio.com/v0/topstories.json'
result = requests.get(url=url, verify=False)
if result.status_code == 200:
stories_list = str2list(result.text)[0:top_number]
return stories_list
raise Exception('Failed to get top stories with error code: ' + result.status_code)
def get_topic_item(id):
url = 'https://hacker-news.firebaseio.com/v0/item/{}.json?print=pretty'.format(id)
result = requests.get(url=url, verify=False)
if result.status_code == 200:
j_str = result.text
topic_obj = json.loads(j_str)
return topic_obj
raise Exception('Failed to get topic item with error code: ' + result.status_code)
def str2list(s):
if not s:
return []
temp = s[1:-1]
return temp.split(',')