Skip to content

Instantly share code, notes, and snippets.

@rishi93
Last active November 21, 2022 15:57
Show Gist options
  • Save rishi93/bd2ffa80298188e9cb0d8f1da1267f87 to your computer and use it in GitHub Desktop.
Save rishi93/bd2ffa80298188e9cb0d8f1da1267f87 to your computer and use it in GitHub Desktop.
A simple script to filter hacker news articles
import aiohttp
import itertools
from collections import namedtuple
import asyncio
async def get_top_stories(session):
url = "https://hacker-news.firebaseio.com/v0/topstories.json"
async with session.get(url) as response:
data = await response.json()
return data
async def get_story(session, story):
Article = namedtuple('Article', ['title', 'url'])
url = f"https://hacker-news.firebaseio.com/v0/item/{story}.json"
async with session.get(url) as response:
data = await response.json()
return Article(title=data.get('title'), url=data.get('url'))
async def get_top_stories_full():
async with aiohttp.ClientSession() as session:
stories = await get_top_stories(session)
args = zip(itertools.repeat(session), stories)
result = await asyncio.gather(*itertools.starmap(get_story, args))
return result
async def get_top_stories_filter(terms):
top_stories_data = await get_top_stories_full()
print("Filtered HN stories")
for article in top_stories_data:
if any([term in article.title.lower().split() for term in terms]):
print(article.title)
print(article.url)
print()
if __name__ == "__main__":
terms = ["python", "ruby", "rust", "go", "golang", "javascript",
"node", "deno", "cpython", "wifi", "hack", "hacking", "C",
"C++", "programming", "AI"]
asyncio.run(get_top_stories_filter(terms))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment