hackernewsbs.py 863 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env python
  2. import requests
  3. import sys
  4. def api(id):
  5. request = requests.get(f"https://hacker-news.firebaseio.com/v0/item/{id}.json?print=pretty")
  6. if request.status_code != 200:
  7. sys.exit(f"Could not fetch {id}")
  8. return request.json()
  9. maxitem = requests.get("https://hacker-news.firebaseio.com/v0/maxitem.json?print=pretty")
  10. if maxitem.status_code != 200:
  11. sys.exit(f"Could not fetch max id")
  12. maxitem = int(maxitem.text)
  13. hnrange = list(range(1, maxitem))
  14. for post in hnrange:
  15. print(post)
  16. data = api(post)
  17. if data["type"] == "story" and data["dead"] == False:
  18. print(f"{data['by']} - {data['title']} - {data['url']}")
  19. elif data["type"] in ["comment", "poll"]:
  20. print(f"{data['by']} - {data['text']}")
  21. elif data["type"] == "job":
  22. print(f"{data['by']} - {data['title']} - {data['text']}")