Write news stories to disk

This commit is contained in:
2019-08-24 05:07:16 +00:00
parent dde6ac4566
commit c1a81a4d8c
3 changed files with 82 additions and 41 deletions

View File

@@ -1,3 +1,8 @@
import logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
import requests
API_TOPSTORIES = lambda x: 'https://hacker-news.firebaseio.com/v0/topstories.json'
@@ -7,11 +12,17 @@ SITE_LINK = lambda x : 'https://news.ycombinator.com/item?id={}'.format(x)
SITE_AUTHOR_LINK = lambda x : 'https://news.ycombinator.com/user?id={}'.format(x)
def api(route, ref=None):
r = requests.get(route(ref), timeout=5)
return r.json()
try:
r = requests.get(route(ref), timeout=5)
if r.status_code != 200:
raise
return r.json()
except BaseException as e:
logging.error('Problem hitting hackernews API: {}'.format(str(e)))
return False
def feed():
return api(API_TOPSTORIES)[:30]
return api(API_TOPSTORIES)[:30] or []
def comment(i):
c = {}
@@ -29,6 +40,7 @@ def comment_count(i):
def story(ref):
r = api(API_ITEM, ref)
if not r: return False
if 'deleted' in r:
return False