|
|
@ -1,4 +1,9 @@
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#TODO: make sure things dont explode no matter what terminal size
|
|
|
|
|
|
|
|
# -> prevent linebreaks in a single story
|
|
|
|
|
|
|
|
# -> only load as many stories as fit
|
|
|
|
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup as Soup
|
|
|
|
from bs4 import BeautifulSoup as Soup
|
|
|
|
import curses
|
|
|
|
import curses
|
|
|
@ -20,15 +25,18 @@ class Story:
|
|
|
|
def main(stdscr):
|
|
|
|
def main(stdscr):
|
|
|
|
stdscr.clear()
|
|
|
|
stdscr.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
height, width = stdscr.getmaxyx()
|
|
|
|
|
|
|
|
num_stories = curses.LINES - 2 # headline and detail
|
|
|
|
# Query Hacker News API for top stories and their titles/links
|
|
|
|
# Query Hacker News API for top stories and their titles/links
|
|
|
|
url = 'https://hacker-news.firebaseio.com/v0/topstories.json'
|
|
|
|
url = 'https://hacker-news.firebaseio.com/v0/topstories.json'
|
|
|
|
r = requests.get(url)
|
|
|
|
r = requests.get(url)
|
|
|
|
if not r.ok:
|
|
|
|
if not r.ok:
|
|
|
|
raise Exception('Error fetching data from Hacker News API')
|
|
|
|
raise Exception('Error fetching data from Hacker News API')
|
|
|
|
ids = r.json()[:15] # Show only the first ten stories
|
|
|
|
ids = r.json()[:10]
|
|
|
|
stories = []
|
|
|
|
stories = []
|
|
|
|
for idx, i in enumerate(ids):
|
|
|
|
for idx, i in enumerate(ids):
|
|
|
|
stdscr.clear()
|
|
|
|
stdscr.clear()
|
|
|
|
|
|
|
|
stdscr.addstr(f'{num_stories}\n')
|
|
|
|
stdscr.addstr(f'[{spinner_states[idx%4]}] Getting stories...')
|
|
|
|
stdscr.addstr(f'[{spinner_states[idx%4]}] Getting stories...')
|
|
|
|
stdscr.refresh()
|
|
|
|
stdscr.refresh()
|
|
|
|
story_url = f'https://hacker-news.firebaseio.com/v0/item/{i}.json'
|
|
|
|
story_url = f'https://hacker-news.firebaseio.com/v0/item/{i}.json'
|
|
|
@ -46,7 +54,15 @@ def main(stdscr):
|
|
|
|
stdscr.addstr('Hacker News Top Stories:\n')
|
|
|
|
stdscr.addstr('Hacker News Top Stories:\n')
|
|
|
|
for i, story in enumerate(stories):
|
|
|
|
for i, story in enumerate(stories):
|
|
|
|
prefix = '>>> ' if i == current_pos else ' '
|
|
|
|
prefix = '>>> ' if i == current_pos else ' '
|
|
|
|
text = f'{prefix}{story.title} ({story.link})\n'
|
|
|
|
#text = f'{prefix}{story.title} ({story.link})\n'
|
|
|
|
|
|
|
|
# calculate length of line
|
|
|
|
|
|
|
|
text = f'{prefix} ()\n'
|
|
|
|
|
|
|
|
chars_available = width - len(text)
|
|
|
|
|
|
|
|
max_title_len = (chars_available//3)*2
|
|
|
|
|
|
|
|
max_url_len = chars_available//3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text = f'{prefix}{(story.title[:max_title_len-1] + "…") if len(story.title) > max_title_len else story.title} ({story.link[:max_url_len-1] + "…" if len(story.link) > max_url_len else story.link})\n'
|
|
|
|
|
|
|
|
|
|
|
|
stdscr.addstr(text)
|
|
|
|
stdscr.addstr(text)
|
|
|
|
if i == current_pos:
|
|
|
|
if i == current_pos:
|
|
|
|
detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n'
|
|
|
|
detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n'
|
|
|
|