2021-10-26 13:13:23 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
def get_hackernews_story(story_id: str) -> dict:
|
|
|
|
url = f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty"
|
2024-04-21 17:34:18 +00:00
|
|
|
return requests.get(url, timeout=10).json()
|
2021-10-26 13:13:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def hackernews_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"
|
2024-04-21 17:34:18 +00:00
|
|
|
story_ids = requests.get(url, timeout=10).json()[:max_stories]
|
2021-10-26 13:13:23 +00:00
|
|
|
return [get_hackernews_story(story_id) for story_id in story_ids]
|
|
|
|
|
|
|
|
|
|
|
|
def hackernews_top_stories_as_markdown(max_stories: int = 10) -> str:
|
|
|
|
stories = hackernews_top_stories(max_stories)
|
|
|
|
return "\n".join("* [{title}]({url})".format(**story) for story in stories)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print(hackernews_top_stories_as_markdown())
|