move to dict instead of list for loadedstories

master
Felix Pankratz 1 year ago
parent f6b7a4cd8c
commit 6ec69dc892

39
hn.py

@ -22,7 +22,7 @@ class Client:
self.screen.keypad(True) self.screen.keypad(True)
self.topstories = api.get_topstories() self.topstories = api.get_topstories()
self.loadedstories = [] self.loadedstories = {}
self.story_pos = 0 self.story_pos = 0
self.cursor_pos = 0 self.cursor_pos = 0
@ -34,7 +34,15 @@ class Client:
for idx, i in enumerate(self.topstories[from_pos:to_pos]): for idx, i in enumerate(self.topstories[from_pos:to_pos]):
self.set_footer(f'[{spinner_states[idx%4]}] Loading { to_pos - from_pos } stories...') self.set_footer(f'[{spinner_states[idx%4]}] Loading { to_pos - from_pos } stories...')
self.screen.refresh() self.screen.refresh()
self.loadedstories.append(api.get_story(i)) if i in self.loadedstories.keys():
# upon reloading, refresh counts + title
updated_story = api.get_story(i)
self.loadedstories[i].votes = updated_story.votes
self.loadedstories[i].comments = updated_story.comments
self.loadedstories[i].title = updated_story.title
pass
else:
self.loadedstories[i] = api.get_story(i)
def set_footer(self, footer): def set_footer(self, footer):
self.screen.addstr(curses.LINES - 1, 0, footer, curses.A_REVERSE) self.screen.addstr(curses.LINES - 1, 0, footer, curses.A_REVERSE)
@ -52,7 +60,8 @@ class Client:
self.screen.addstr(title_string, curses.color_pair(1)) self.screen.addstr(title_string, curses.color_pair(1))
for i, story in enumerate(self.loadedstories[self.story_pos:self.story_pos + self.stories_in_a_site]): for i, story_id in enumerate(self.topstories[self.story_pos:self.story_pos + self.stories_in_a_site]):
story = self.loadedstories[story_id]
prefix = '>' if i == self.cursor_pos else '' prefix = '>' if i == self.cursor_pos else ''
# calculate length of line # calculate length of line
text = f'{prefix} ()\n' text = f'{prefix} ()\n'
@ -81,11 +90,12 @@ class Client:
self.screen.addstr(f' ({link})\n', curses.A_DIM ) self.screen.addstr(f' ({link})\n', curses.A_DIM )
page = int(self.story_pos / self.stories_in_a_site + 1) page = int(self.story_pos / self.stories_in_a_site + 1)
total_pages = math.ceil(len(self.loadedstories)/self.stories_in_a_site) total_pages = math.ceil(len(self.loadedstories)/500)
self.set_footer(f'Page {page}/{total_pages}, loaded {len(self.loadedstories)} stories.') self.set_footer(f'Page {page}/{total_pages}, loaded {len(self.loadedstories)} stories.')
def handle_input(self): def handle_input(self):
c = self.screen.getch() c = self.screen.getch()
story = self.loadedstories[self.topstories[self.story_pos + self.cursor_pos]]
if c == ord('q'): # Quit if c == ord('q'): # Quit
self.exit() self.exit()
elif c == curses.KEY_UP: elif c == curses.KEY_UP:
@ -105,13 +115,16 @@ class Client:
self.load_more_if_needed() self.load_more_if_needed()
elif c == ord('c'): elif c == ord('c'):
webbrowser.open(f'https://news.ycombinator.com/item?id={self.loadedstories[self.story_pos + self.cursor_pos].id}') # open comments
webbrowser.open(f'https://news.ycombinator.com/item?id={story.id}')
elif c == curses.KEY_ENTER or c == 10: elif c == curses.KEY_ENTER or c == 10:
if self.loadedstories[self.story_pos + self.cursor_pos].link == 'No URL': # open link
webbrowser.open(f'https://news.ycombinator.com/item?id={self.loadedstories[self.story_pos + self.cursor_pos].id}') if story.link == 'No URL':
webbrowser.open(f'https://news.ycombinator.com/item?id={story.id}')
else: else:
webbrowser.open(self.loadedstories[self.story_pos + self.cursor_pos].link) webbrowser.open(story.link)
self.loadedstories[self.story_pos + self.cursor_pos].read = True story.read = True
elif c == ord('r'): elif c == ord('r'):
self.reload() self.reload()
elif c == curses.KEY_RESIZE: elif c == curses.KEY_RESIZE:
@ -121,9 +134,9 @@ class Client:
self.load_more_if_needed() self.load_more_if_needed()
def load_more_if_needed(self): def load_more_if_needed(self):
if len(self.loadedstories) < self.story_pos + self.stories_in_a_site: # if len(self.loadedstories) < self.story_pos + self.stories_in_a_site:
# load more # load more
self.load_stories(len(self.loadedstories), self.story_pos + self.stories_in_a_site) self.load_stories(self.story_pos, self.story_pos + self.stories_in_a_site)
def run(self): def run(self):
self.load_stories(0, self.stories_in_a_site) self.load_stories(0, self.stories_in_a_site)
@ -137,8 +150,10 @@ class Client:
sys.exit(0) sys.exit(0)
def reload(self): def reload(self):
self.set_footer("Reloading...")
self.screen.refresh()
self.topstories = api.get_topstories() self.topstories = api.get_topstories()
self.loadedstories = [] #self.loadedstories = []
self.story_pos = 0 self.story_pos = 0
self.cursor_pos = 0 self.cursor_pos = 0
self.load_more_if_needed() self.load_more_if_needed()

Loading…
Cancel
Save