- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathget_top_hn_posts.py
26 lines (17 loc) · 896 Bytes
/
get_top_hn_posts.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
from __future__ importannotations
importrequests
defget_hackernews_story(story_id: str) ->dict:
url=f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty"
returnrequests.get(url, timeout=10).json()
defhackernews_top_stories(max_stories: int=10) ->list[dict]:
"""
Get the top max_stories posts from HackerNews - https://news.ycombinator.com/
"""
url="https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
story_ids=requests.get(url, timeout=10).json()[:max_stories]
return [get_hackernews_story(story_id) forstory_idinstory_ids]
defhackernews_top_stories_as_markdown(max_stories: int=10) ->str:
stories=hackernews_top_stories(max_stories)
return"\n".join("* [{title}]({url})".format(**story) forstoryinstories)
if__name__=="__main__":
print(hackernews_top_stories_as_markdown())