Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PRAW: Comment Submitter's Username

Tags:

python

api

reddit

I'm developing a reddit bot that needs to know which user submitted a comment.

According to the PRAW API wrapper docs, there's no specific way to get the username of a Comment object's author. Ideally I could directly get the username back. If that's not possible, is there a way to get the fullname of the author and then convert it to a username?

like image 982
Humus Avatar asked Dec 29 '13 06:12

Humus


People also ask

What is user agent PRAW?

User Agent. A user agent is a unique identifier that helps Reddit determine the source of network requests. To use Reddit's API, you need a unique and descriptive user agent. The recommended format is <platform>:<app ID>:<version string> (by u/<Reddit username>) . For example, android:com.

How do you reply to a comment on PRAW?

If you're looking for how to reply to a comment, you want to use the Comment#reply method. If you have the comment bound to variable name c , then you can do the following: c. reply("Here is a reply to your comment.")

What is PRAW used for?

PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's API. PRAW aims to be easy to use and internally follows all of Reddit's API rules. With PRAW there's no need to introduce sleep calls in your code. Give your client an appropriate user agent and you're set.

What is a Reddit object PRAW?

As its name suggests, PRAW is a Python wrapper for the Reddit API, which enables you to scrape data from subreddits, create a bot, and much more. In this article, we will learn how to use PRAW to scrape posts from different subreddits and get comments from a specific post.


2 Answers

I'm a maintainer for PRAW. Where does it say that you cannot get the username of a Comment objects author? Because that is incorrect and needs to be fixed.

Anyway, a Comment has a author attribute, which is a Redditor instance of the author.

import praw

r = praw.Reddit(UNIQUE_AND_DESCRIPTIVE_USERAGENT)
submission = r.get_submission("http://www.reddit.com/r/redditdev/comments/16m0uu/praw_20_is_coming_release_in_2_days/")
comment = submission.comments[0]
author = comment.author  # This returns a ``Redditor`` object.
print(author.name)  # The username
like image 196
Damgaard Avatar answered Sep 22 '22 11:09

Damgaard


Can't comment as don't have enough reputation. @Humus mentioned in his comment that it was no where mentioned in the PRAW readthedocs.org documentation. There is a simple workaround.We can use dir(object_name) to get a list of the attributes for that object. Then it is just a guessing game thereafter.

Edit: You can also use pprint(vars(object_name))

like image 40
chamcham Avatar answered Sep 22 '22 11:09

chamcham