From a7c3b845d94833bf5678ba447e5bce86423c0dcd Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Tue, 29 Aug 2023 09:27:41 +0200 Subject: [PATCH] display number of comments --- hn.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) mode change 100644 => 100755 hn.py diff --git a/hn.py b/hn.py old mode 100644 new mode 100755 index fd3cfda..4106381 --- a/hn.py +++ b/hn.py @@ -13,6 +13,7 @@ class Story: link: str author: str votes: int + comments: int def main(stdscr): stdscr.clear() @@ -27,10 +28,11 @@ def main(stdscr): for i in ids: story_url = f'https://hacker-news.firebaseio.com/v0/item/{i}.json' s = requests.get(story_url).json() - try: - stories.append(Story(s['id'], s['title'], s['url'], s['by'], s['score'])) - except KeyError: - stories.append(Story(s['id'], s['title'], 'No URL', s['by'], s['score'])) + stories.append(Story(s['id'], + s['title'], + s['url'] if 'url' in s else 'No URL', + s['by'], s['score'], + len(s['kids']) if 'kids' in s else 0)) # Display list of stories in terminal window with arrow key navigation current_pos = 0 @@ -42,7 +44,7 @@ def main(stdscr): text = f'{prefix}{story.title} ({story.link})\n' stdscr.addstr(text) if i == current_pos: - detail = f' by {story.author} | {story.votes} points\n' + detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n' stdscr.addstr(detail) stdscr.refresh()