Skip to content

Commit

Permalink
added Reddit.get_submission_by_id, fixed bugs
Browse files Browse the repository at this point in the history
- voting wasn't working because _request was trying to encode
  an int parameter as a string (the direction of the vote)
- comments weren't being printed to console correctly because the
  MoreComments class didn't implement __str__, and was falling back
  to the non-implemented base class implementation (come on, folks :P)
  • Loading branch information
tmelz committed Aug 8, 2011
1 parent a718cd5 commit 8039e71
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
4 changes: 4 additions & 0 deletions reddit/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def __init__(self, reddit_session, json_dict):

def _update_submission(self, _): pass

@limit_chars()
def __str__(self):
return "[[ need to fetch more comments... ]]".encode("utf8")

@property
def comments(self):
url = urljoin(self.parent.submission.permalink, self.parent.id)
Expand Down
39 changes: 20 additions & 19 deletions reddit/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,30 @@ def error_checked_func(*args, **kwargs):
return_value = func(*args, **kwargs)
if not return_value:
return
else:
# todo, clean up this code. for right now, i just surrounded it
# with a try, except. basically the issue is when the _json_request
# wants to return a list, for example when you request the page for
# a story; the reddit api returns json for the story and the
# comments.
try:
for k in return_value.keys():
if k not in (u"jquery", "iden", "captcha", "kind", "data"):
warnings.warn("Return value keys contained "
"{0}!".format(return_value.keys()))
jquery = return_value.get("jquery")
if jquery:
values = [x[-1] for x in jquery]
if [".error.USER_REQUIRED"] in values:
raise NotLoggedInException()
else:
# todo, clean up this code. for right now, i just surrounded it
# with a try, except. basically the issue is when the _json_request
# wants to return a list, for example when you request the page for
# a story; the reddit api returns json for the story and the
# comments.
try:
jquery = None
for k in return_value.keys():
if k not in (u"jquery", "iden", "captcha", "kind", "data"):
warnings.warn("Return value keys contained "
"{0}!".format(return_value.keys()))
jquery = return_value.get("jquery")
if jquery:
values = [x[-1] for x in jquery]
if [".error.USER_REQUIRED"] in values:
raise NotLoggedInException()
elif [".error.WRONG_PASSWORD.field-passwd"] in values:
raise InvalidUserPass()
elif [".error.RATELIMIT.field-vdelay"] in values:
raise Exception('Rate limit exceeded')
elif [".error.BAD_CAPTCHA.field-captcha"] in values:
raise BadCaptcha()
except AttributeError:
pass
return return_value
except AttributeError:
pass
return return_value
return error_checked_func
2 changes: 1 addition & 1 deletion reddit/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def vote(self, direction=0):
"""
url = urls["vote"]
params = {'id' : self.name,
'dir' : direction,
'dir' : str(direction),
'r' : self.subreddit.display_name,
'uh' : self.reddit_session.modhash}
return self.reddit_session._request_json(url, params)
Expand Down
15 changes: 15 additions & 0 deletions reddit/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ def get_subreddit(self, subreddit_name, *args, **kwargs):
"""Returns a Subreddit class for the user_name specified."""
return Subreddit(self, subreddit_name, *args, **kwargs)

def get_submission_by_id(self, story_id):
""" Given a story id, possibly prefixed by t3, return a
Submission object, also fetching its comments. """
if story_id.startswith("t3_"):
story_id = story_id.split("_")[1]
submission_info, comment_info = self._request_json(
urls["comments"] + story_id)
submission = submission_info["data"]["children"][0]
comments = comment_info["data"]["children"]
for comment in comments:
comment._update_submission(submission)

return submission


@require_login
def get_inbox(self, *args, **kwargs):
"""Return an Inbox object."""
Expand Down

0 comments on commit 8039e71

Please sign in to comment.