display number of comments

This commit is contained in:
Felix Pankratz 2023-08-29 09:27:41 +02:00
parent b9aeb0e8de
commit a7c3b845d9

12
hn.py Normal file → Executable file
View File

@ -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()