diff options
| author | Tianhao Wang <shrik3@riseup.net> | 2023-03-28 18:48:37 +0200 |
|---|---|---|
| committer | Tianhao Wang <shrik3@riseup.net> | 2023-03-28 18:48:37 +0200 |
| commit | 5331214b8654d9b8e2441f9ca568a19b6afa7347 (patch) | |
| tree | d0f11a5e8d1e899278d4c585ca87efec6456bc3c /hnnews.py | |
| parent | b99178fd7b03a7ebd1f65b8b60814a32f13f2333 (diff) | |
post HN news ..
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 |
