diff options
Diffstat (limited to 'hnnews.py')
| -rw-r--r-- | hnnews.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/hnnews.py b/hnnews.py new file mode 100644 index 0000000..d186b84 --- /dev/null +++ b/hnnews.py @@ -0,0 +1,45 @@ +import requests +baseurl = "https://hacker-news.firebaseio.com" + +def _get_topnews_list(): + try: + resp = requests.get(f'{baseurl}/v0/topstories.json') + return resp.json() + except: + print("error fetching top news list, skipping") + return [] + +def get_topnews(limit): + result = [] + list = _get_topnews_list() + if len(list) < limit: + limit = len(list) + for story_id in list[:limit]: + try: + url = f'{baseurl}/v0/item/{story_id}.json' + resp = requests.get(url) + story = resp.json() + if story["type"] != "story": + continue + if "url" not in story.keys(): + print("using alt url") + story["url"] = f'https://news.ycombinator.com/item?id={story_id}' + result.append(story) + except Exception as e: + print("error fetching story skipping ", e) + return result + +def get_one_item(id): + try: + url = f'{baseurl}/v0/item/{story_id}.json' + resp = requests.get(url) + story = resp.json() + if story["type"] != "story": + return None + if "url" not in story.keys(): + print("using alt url") + story["url"] = f'https://news.ycombinator.com/item?id={story_id}' + return story + except Exception as e: + print("error fetching story skipping ", e) + return None |
