#!/usr/bin/env python3 #TODO: make sure things dont explode no matter what terminal size # x prevent linebreaks in a single story # x only load as many stories as fit # -> refresh on resize import requests from bs4 import BeautifulSoup as Soup import curses import webbrowser import api spinner_states = ['-', '\\', '|', '/'] def footer(stdscr, content): stdscr.addstr(curses.LINES-1, 0, content, curses.A_REVERSE) def main(stdscr): stdscr.clear() _, width = stdscr.getmaxyx(); num_stories = curses.LINES - 3 # headline, detail, footer topstories = api.get_topstories() stories = [] for idx, i in enumerate(topstories[:num_stories]): stdscr.clear() footer(stdscr, f'[{spinner_states[idx%4]}] Loading stories...') stdscr.refresh() stories.append(api.get_story(i)) # Display list of stories in terminal window with arrow key navigation current_pos = 0 while True: stdscr.clear() stdscr.addstr('Hacker News Top Stories:\n') for i, story in enumerate(stories): prefix = '>>> ' if i == current_pos else ' ' # calculate length of line text = f'{prefix} ()\n' chars_available = width - len(text) max_title_len = min((chars_available//3)*2, len(story.title)) max_url_len = chars_available - max_title_len title = story.title[:max_title_len-1] + "…" if len(story.title) > max_title_len else story.title link = story.link.replace('https://', '').replace('http://', '') link = link[:max_url_len-1] + "…" if len(link) > max_url_len else link text = '{}{} ({})\n'.format(prefix, title, link.replace('https://', '').replace('http://', '')) stdscr.addstr(text) if i == current_pos: detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n' stdscr.addstr(detail) footer(stdscr, f'Loaded {num_stories} stories.') stdscr.refresh() c = stdscr.getch() if c == ord('q'): # Quit break elif c == curses.KEY_UP: current_pos -= 1 if current_pos < 0: current_pos = len(stories)-1 elif c == curses.KEY_DOWN: current_pos += 1 if current_pos >= len(stories): current_pos = 0 elif c == ord('c'): webbrowser.open(f'https://news.ycombinator.com/item?id={stories[current_pos].id}') elif c == curses.KEY_ENTER or c == 10: webbrowser.open(stories[current_pos].link) curses.wrapper(main)