You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.5 KiB
Python

#!/usr/bin/env python3
#TODO: make sure things dont explode no matter what terminal size
1 year ago
# 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
1 year ago
import api
spinner_states = ['-', '\\', '|', '/']
1 year ago
class Client:
def __init__(self):
# set up curses
self.screen = curses.initscr()
curses.noecho()
curses.cbreak()
self.screen.keypad(True)
self.topstories = api.get_topstories()
self.loadedstories = []
self.story_pos = 0
self.cursor_pos = 0
self.lines = curses.LINES
self.cols = curses.COLS
self.stories_in_a_site = self.lines - 3
def load_stories(self, from_pos, to_pos):
for idx, i in enumerate(self.topstories[from_pos:to_pos]):
#stdscr.clear()
self.set_footer(f'[{spinner_states[idx%4]}] Loading stories...')
self.screen.refresh()
self.loadedstories.append(api.get_story(i))
def set_footer(self, footer):
self.screen.addstr(curses.LINES - 1, 0, footer, curses.A_REVERSE)
def draw(self):
self.screen.clear()
self.lines = curses.LINES
self.cols = curses.COLS
# header, detail, footer:
self.stories_in_a_site = self.lines - 3
self.screen.addstr('Hacker News Top Stories:\n')
for i, story in enumerate(self.loadedstories[self.story_pos:self.story_pos + self.stories_in_a_site]):
prefix = '>>> ' if i == self.cursor_pos else ' '
# calculate length of line
text = f'{prefix} ()\n'
chars_available = self.cols - len(text)
1 year ago
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://', ''))
self.screen.addstr(text)
if i == self.cursor_pos:
detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n'
self.screen.addstr(detail)
self.set_footer(f'Loaded {self.stories_in_a_site} stories.')
def handle_input(self):
c = self.screen.getch()
if c == ord('q'): # Quit
self.exit()
elif c == curses.KEY_UP:
self.cursor_pos -= 1
if self.cursor_pos < 0:
self.cursor_pos = self.stories_in_a_site-1
elif c == curses.KEY_DOWN:
self.cursor_pos += 1
if self.cursor_pos >= self.stories_in_a_site:
self.cursor_pos = 0
elif c == ord('c'):
webbrowser.open(f'https://news.ycombinator.com/item?id={self.loadedstories[self.story_pos + self.cursor_pos].id}')
elif c == curses.KEY_ENTER or c == 10:
webbrowser.open(self.loadedstories[self.story_pos + self.cursor_pos].link)
def run(self):
self.load_stories(0, self.stories_in_a_site)
while True:
self.draw()
self.handle_input()
def exit(self):
curses.endwin()
import sys
sys.exit(0)
def main():
client = Client()
client.run()
if __name__ == '__main__':
main()