2019-08-24 05:07:16 +00:00
|
|
|
import logging
|
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
level=logging.INFO)
|
|
|
|
|
2019-08-23 08:23:48 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
API_TOPSTORIES = lambda x: 'https://hacker-news.firebaseio.com/v0/topstories.json'
|
|
|
|
API_ITEM = lambda x : 'https://hn.algolia.com/api/v1/items/{}'.format(x)
|
|
|
|
|
|
|
|
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):
|
2019-08-24 05:07:16 +00:00
|
|
|
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
|
2019-08-23 08:23:48 +00:00
|
|
|
|
|
|
|
def feed():
|
2019-08-24 05:07:16 +00:00
|
|
|
return api(API_TOPSTORIES)[:30] or []
|
2019-08-23 08:23:48 +00:00
|
|
|
|
|
|
|
def comment(i):
|
|
|
|
c = {}
|
|
|
|
c['author'] = i.get('author', '')
|
|
|
|
c['score'] = i.get('points', 0)
|
|
|
|
c['date'] = i.get('created_at_i', 0)
|
|
|
|
c['text'] = i.get('text', '')
|
|
|
|
c['link'] = SITE_LINK(i['id'])
|
|
|
|
c['comments'] = [comment(j) for j in i['children']]
|
|
|
|
return c
|
|
|
|
|
|
|
|
def comment_count(i):
|
|
|
|
alive = 1 if i['author'] else 0
|
|
|
|
return sum([comment_count(c) for c in i['comments']]) + alive
|
|
|
|
|
|
|
|
def story(ref):
|
|
|
|
r = api(API_ITEM, ref)
|
2019-08-24 05:07:16 +00:00
|
|
|
if not r: return False
|
2019-08-23 08:23:48 +00:00
|
|
|
|
|
|
|
if 'deleted' in r:
|
|
|
|
return False
|
|
|
|
elif r.get('type', '') != 'story':
|
|
|
|
return False
|
|
|
|
|
|
|
|
s = {}
|
|
|
|
s['author'] = r.get('author', '')
|
|
|
|
s['author_link'] = SITE_AUTHOR_LINK(r.get('author', ''))
|
|
|
|
s['score'] = r.get('points', 0)
|
|
|
|
s['date'] = r.get('created_at_i', 0)
|
|
|
|
s['title'] = r.get('title', '')
|
|
|
|
s['link'] = SITE_LINK(ref)
|
|
|
|
s['source'] = 'hackernews'
|
|
|
|
s['url'] = r.get('url', '')
|
|
|
|
s['comments'] = [comment(i) for i in r['children']]
|
|
|
|
s['num_comments'] = comment_count(s) - 1
|
|
|
|
|
|
|
|
if 'text' in r and r['text']:
|
|
|
|
s['text'] = r['text']
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print(story(20763961))
|