Fixed speed issue, no longer load each subreddit just to get the url.

This commit is contained in:
Michael Lazar
2015-01-22 23:24:24 -08:00
parent 8da813833f
commit b468a11fa8
9 changed files with 1719 additions and 7 deletions

View File

@@ -1,6 +1,15 @@
from datetime import datetime, timedelta
import textwrap
def strip_subreddit_url(permalink):
"""
Grab the subreddit from the permalink because submission.subreddit.url
makes a seperate call to the API.
"""
subreddit = clean(permalink).split('/')[4]
return '/r/{}'.format(subreddit)
def clean(unicode_string):
"""
Convert unicode string into ascii-safe characters.
@@ -106,7 +115,7 @@ class SubredditGenerator(object):
data['comments'] = '{} comments'.format(sub.num_comments)
data['score'] = '{} pts'.format(sub.score)
data['author'] = clean(sub.author.name)
data['subreddit'] = clean(sub.subreddit.url)
data['subreddit'] = strip_subreddit_url(sub.permalink)
data['url'] = ('(selfpost)' if is_selfpost(sub.url) else clean(sub.url))
return data

View File

View File

@@ -3,6 +3,7 @@ import textwrap
import curses
from content_generators import SubredditGenerator
from utils import curses_session
class SubredditViewer(object):
@@ -66,6 +67,13 @@ class SubredditViewer(object):
def move_cursor(self, delta):
new_index = self._cursor_index + delta
if new_index < 0:
curses.flash()
return
self.remove_cursor()
self._cursor_index += delta
self.draw_cursor()
@@ -144,13 +152,14 @@ class SubredditViewer(object):
win.refresh()
def main(stdscr):
def main():
r = praw.Reddit(user_agent='reddit terminal viewer (rtv) v0.0')
generator = SubredditGenerator(r)
viewer = SubredditViewer(stdscr, generator)
viewer.loop()
with curses_session() as stdscr:
r = praw.Reddit(user_agent='reddit terminal viewer (rtv) v0.0')
generator = SubredditGenerator(r)
viewer = SubredditViewer(stdscr, generator)
viewer.loop()
if __name__ == '__main__':
curses.wrapper(main)
main()