Submission view up and running.
This commit is contained in:
@@ -1,28 +1,70 @@
|
||||
import textwrap
|
||||
import praw
|
||||
|
||||
from utils import clean, strip_subreddit_url, humanize_timestamp
|
||||
|
||||
class SubmissionContent(object):
|
||||
"""
|
||||
Facilitates navigating through the comments in a PRAW submission.
|
||||
"""
|
||||
|
||||
def __init__(self, submission):
|
||||
|
||||
self.submission = submission
|
||||
self._comments = []
|
||||
|
||||
class BaseContent(object):
|
||||
|
||||
@staticmethod
|
||||
def flatten_comments(submission):
|
||||
def strip_praw_comment(comment):
|
||||
"""
|
||||
Parse through a submission comment and return a dict with data ready to
|
||||
be displayed through the terminal.
|
||||
"""
|
||||
|
||||
data = {}
|
||||
data['object'] = comment
|
||||
data['level'] = comment.nested_level
|
||||
|
||||
|
||||
if isinstance(comment, praw.objects.MoreComments):
|
||||
data['type'] = 'MoreComments'
|
||||
data['body'] = 'More comments [{}]'.format(comment.count)
|
||||
else:
|
||||
data['type'] = 'Comment'
|
||||
data['body'] = clean(comment.body)
|
||||
data['created'] = humanize_timestamp(comment.created_utc)
|
||||
data['score'] = '{} pts'.format(comment.score)
|
||||
data['author'] = (clean(comment.author.name) if
|
||||
getattr(comment, 'author') else '[deleted]')
|
||||
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def strip_praw_submission(sub):
|
||||
"""
|
||||
Parse through a submission and return a dict with data ready to be
|
||||
displayed through the terminal.
|
||||
"""
|
||||
|
||||
is_selfpost = lambda s: s.startswith('http://www.reddit.com/r/')
|
||||
|
||||
data = {}
|
||||
data['object'] = sub
|
||||
data['type'] = 'Submission'
|
||||
data['title'] = clean(sub.title)
|
||||
data['text'] = clean(sub.selftext)
|
||||
data['created'] = humanize_timestamp(sub.created_utc)
|
||||
data['comments'] = '{} comments'.format(sub.num_comments)
|
||||
data['score'] = '{} pts'.format(sub.score)
|
||||
data['author'] = (clean(sub.author.name) if getattr(sub, 'author')
|
||||
else '[deleted]')
|
||||
data['permalink'] = clean(sub.permalink)
|
||||
data['subreddit'] = strip_subreddit_url(sub.permalink)
|
||||
data['url'] = ('(selfpost)' if is_selfpost(sub.url) else clean(sub.url))
|
||||
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def flatten_comments(comments, initial_level=0):
|
||||
"""
|
||||
Flatten a PRAW comment tree while preserving the nested level of each
|
||||
comment via the `nested_level` attribute.
|
||||
"""
|
||||
|
||||
stack = submission[:]
|
||||
stack = comments[:]
|
||||
for item in stack:
|
||||
item.nested_level = 0
|
||||
item.nested_level = initial_level
|
||||
|
||||
retval = []
|
||||
while stack:
|
||||
@@ -35,24 +77,8 @@ class SubmissionContent(object):
|
||||
retval.append(item)
|
||||
return retval
|
||||
|
||||
@staticmethod
|
||||
def strip_praw_comment(comment):
|
||||
"""
|
||||
Parse through a submission comment and return a dict with data ready to
|
||||
be displayed through the terminal.
|
||||
"""
|
||||
|
||||
data = {}
|
||||
data['body'] = clean(comment.body)
|
||||
data['created'] = humanize_timestamp(comment.created_utc)
|
||||
data['score'] = '{} pts'.format(comment.score)
|
||||
data['author'] = (clean(comment.author.name) if
|
||||
getattr(comment, 'author') else '[deleted]')
|
||||
|
||||
return data
|
||||
|
||||
|
||||
class SubredditContent(object):
|
||||
class SubredditContent(BaseContent):
|
||||
"""
|
||||
Grabs a subreddit from PRAW and lazily stores submissions to an internal
|
||||
list for repeat access.
|
||||
@@ -74,28 +100,6 @@ class SubredditContent(object):
|
||||
|
||||
self.reset(subreddit=subreddit)
|
||||
|
||||
@staticmethod
|
||||
def strip_praw_submission(sub):
|
||||
"""
|
||||
Parse through a submission and return a dict with data ready to be
|
||||
displayed through the terminal.
|
||||
"""
|
||||
|
||||
is_selfpost = lambda s: s.startswith('http://www.reddit.com/r/')
|
||||
|
||||
data = {}
|
||||
data['title'] = clean(sub.title)
|
||||
data['text'] = clean(sub.selftext)
|
||||
data['created'] = humanize_timestamp(sub.created_utc)
|
||||
data['comments'] = '{} comments'.format(sub.num_comments)
|
||||
data['score'] = '{} pts'.format(sub.score)
|
||||
data['author'] = (clean(sub.author.name) if getattr(sub, 'author')
|
||||
else '[deleted]')
|
||||
data['subreddit'] = strip_subreddit_url(sub.permalink)
|
||||
data['url'] = ('(selfpost)' if is_selfpost(sub.url) else clean(sub.url))
|
||||
|
||||
return data
|
||||
|
||||
def get(self, index, n_cols=70):
|
||||
"""
|
||||
Grab the `i`th submission, with the title field formatted to fit inside
|
||||
@@ -119,6 +123,7 @@ class SubredditContent(object):
|
||||
data = self._submission_data[index]
|
||||
data['split_title'] = textwrap.wrap(data['title'], width=n_cols)
|
||||
data['n_rows'] = len(data['split_title']) + 3
|
||||
data['offset'] = 0
|
||||
|
||||
return data
|
||||
|
||||
@@ -143,4 +148,71 @@ class SubredditContent(object):
|
||||
self.display_name = 'Front Page'
|
||||
else:
|
||||
self._submissions = self.r.get_subreddit(self.subreddit, limit=None)
|
||||
self.display_name = self._submissions.display_name
|
||||
self.display_name = self._submissions.display_name
|
||||
|
||||
|
||||
class SubmissionContent(BaseContent):
|
||||
"""
|
||||
Grabs a submission from PRAW and lazily store comments to an internal
|
||||
list for repeat access and to allow expanding and hiding comments.
|
||||
"""
|
||||
|
||||
def __init__(self, submission, indent_size=2, max_indent_level=4):
|
||||
|
||||
self.submission = submission
|
||||
self.indent_size = indent_size
|
||||
self.max_indent_level = max_indent_level
|
||||
|
||||
self.display_name = None
|
||||
self._submission_data = None
|
||||
self._comments = None
|
||||
self._comment_data = None
|
||||
|
||||
self.reset()
|
||||
|
||||
def get(self, index, n_cols=70):
|
||||
"""
|
||||
Grab the `i`th submission, with the title field formatted to fit inside
|
||||
of a window of width `n`
|
||||
"""
|
||||
|
||||
if index < -1:
|
||||
raise IndexError
|
||||
|
||||
elif index == -1:
|
||||
data = self._submission_data
|
||||
data['split_title'] = textwrap.wrap(data['title'], width=n_cols)
|
||||
data['n_rows'] = len(data['split_title']) + 3
|
||||
data['offset'] = 0
|
||||
|
||||
else:
|
||||
data = self._comment_data[index]
|
||||
indent_level = min(data['level'], self.max_indent_level)
|
||||
data['offset'] = indent_level * self.indent_size
|
||||
|
||||
if data['type'] == 'Comment':
|
||||
data['split_body'] = textwrap.wrap(
|
||||
data['body'], width=n_cols-data['offset'])
|
||||
data['n_rows'] = len(data['split_body']) + 1
|
||||
else:
|
||||
data['n_rows'] = 1
|
||||
|
||||
return data
|
||||
|
||||
def iterate(self, index, step, n_cols):
|
||||
|
||||
while True:
|
||||
yield self.get(index, n_cols)
|
||||
index += step
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Fetch changes to the submission from PRAW and clear the internal list.
|
||||
"""
|
||||
|
||||
self.submission.refresh()
|
||||
self._submission_data = self.strip_praw_submission(self.submission)
|
||||
|
||||
self.display_name = self._submission_data['permalink']
|
||||
self._comments = self.flatten_comments(self.submission.comments)
|
||||
self._comment_data = [self.strip_praw_comment(c) for c in self._comments]
|
||||
Reference in New Issue
Block a user