Fixed speed issue, no longer load each subreddit just to get the url.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user