Skip to content

Instantly share code, notes, and snippets.

@peterc
Created June 15, 2020 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterc/d22823e240e208196687d4215deba04f to your computer and use it in GitHub Desktop.
Save peterc/d22823e240e208196687d4215deba04f to your computer and use it in GitHub Desktop.
Get a heads up when a new item hits the Hacker News front page
# Get a heads up when a new item hits the Hacker News front page
import requests
import sys
from time import sleep
from random import randint
def fetchItems():
res = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json")
assert(res.status_code == 200)
items = res.json()
return set(items[:30])
items = fetchItems()
while True:
old_items = items
items = fetchItems()
new_items = items - old_items
if len(new_items) > 0:
# There's a new item!
for item_id in new_items:
print(f"New item: https://news.ycombinator.com/item?id={item_id}")
else:
print("No new items", file=sys.stderr)
sleep(randint(10, 60))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment