mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
827b8f04a4
* updating DIRECTORY.md * updating DIRECTORY.md * Create get_top_hn_posts.py * updating DIRECTORY.md * Add return type and desc * Add texttable * Update web_programming/get_top_hn_posts.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update web_programming/get_top_hn_posts.py Co-authored-by: Christian Clauss <cclauss@me.com> * Get top 10 posts * Update get_top_hn_posts.py * Don't use texttable * Setup doctest * Fix pre-commit issues * Remove print statement * Add hackernews_top_stories_as_markdown() Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
27 lines
872 B
Python
27 lines
872 B
Python
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"
|
|
return requests.get(url).json()
|
|
|
|
|
|
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"
|
|
story_ids = requests.get(url).json()[:max_stories]
|
|
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())
|