-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhn_api.py
46 lines (34 loc) · 1.27 KB
/
hn_api.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from datetime import datetime
import humanize
import requests
API_ENDPOINT = 'https://hacker-news.firebaseio.com/v0/'
def get_items_data(item_ids: list, get_comments: bool = True) -> list:
items = []
for item_id in item_ids:
r = requests.get(f'{API_ENDPOINT}item/{item_id}.json')
data = r.json()
timestamp = datetime.fromtimestamp(data['time'])
now = datetime.now()
data['display_time'] = humanize.naturaldelta(now - timestamp)
items.append(data)
return items
def get_items_list(story_type: str, start_id: int, limit: int) -> dict:
result = {'items': [], 'next_page_start_id': -1}
url = f'{API_ENDPOINT}{story_type}stories.json'
r = requests.get(url)
story_ids = r.json()
start_index = 0
if start_id != -1:
try:
start_index = story_ids.index(start_id)
except Exception:
pass
end_index = start_index + limit
if start_index >= len(story_ids):
start_index, end_index = 0, 0
elif end_index >= len(story_ids):
end_index = len(story_ids)
result['items'] = get_items_data(story_ids[start_index:end_index])
if end_index > 0 and end_index < len(story_ids):
result['next_page_start_id'] = story_ids[end_index]
return result