Reddit::Client - A Perl wrapper for the Reddit API.
The official documentation can be found at http://redditclient.readthedocs.org/en/latest/.
use Reddit::Client; my $client_id = "DFhtrhBgfhhRTd"; my $secret = "KrDNsbeffdbILOdgbgSvSBsbfFs"; my $username = "reddit_username"; my $password = "reddit_password"; # Create a Reddit::Client object and authorize in one step my $reddit = new Reddit::Client( user_agent => 'MyScriptName 0.5 by /u/earth-tone', client_id => $client_id, secret => $secret, username => $username, password => $password, ); # Or create object then authorize. # Useful if you need to switch between accounts, for example if you were to check the inboxes of several accounts. my $reddit = Reddit::Client->new( user_agent => 'MyApp/1.0 by /u/earth-tone', ); $reddit->get_token( client_id => $client_id, secret => $secret, username => $username, password => $password, ); # Check your inbox my $me = $reddit->me(); print "You've got mail!" if $me->{has_mail}; # Submit a link $reddit->submit_link( subreddit => 'test', title => 'Perl is still alive!', url => 'http://www.perl.org' ); # Submit a text post $reddit->submit_text( subreddit => 'test', title => 'my test', text => 'a test' ); # Get posts from a subreddit or multi my $posts = $reddit->fetch_links(subreddit=>'test', limit=>5); foreach my $post (@$posts) { print $post->{title} . "\n"; if (!$post->{is_self}) { # Is it a self post or a link? print $post->{url}; } else { print $post->{selftext}; } } # Get comments from a subreddit or multi my $cmts = $reddit->get_subreddit_comments('test'); foreach my $cmt (@$cmts) { print "$cmt->{author} says: $cmt->{body}\n"; }
Reddit::Client provides methods and object wrappers for objects exposed by the Reddit API. This module handles HTTP communication, oauth session management, and communication with Reddit's external API. For more information about the Reddit API, see https://github.com/reddit/reddit/wiki/API.
Beginning August 3rd, 2015, the Reddit API requires Oauth authentication. This amounts to two extra arguments at the beginning of your script; in exchange you you twice as many requests per minute as before (60 vs 30) and some added convenience on the back end.
To get Oauth keys, visit your apps page: https://www.reddit.com/prefs/apps. Choose a "script" type app. None of the other fields are needed for a script type app; you can put in 127.0.0.1 for the URLs or whatever you like.
127.0.0.1
Every account that uses this script must have permission to use it. So for example if you were to check the inboxes of your various accounts, each one would need permission. To add accounts click "add developer" on the right, after you've created the app.
VIEW_HOT "Hot" links feed VIEW_NEW "New" links feed VIEW_RISING "Rising" links feed VIEW_CONTROVERSIAL "Controversial" links feed VIEW_TOP "Top" links feed VIEW_DEFAULT Default feed if not specified (VIEW_HOT) DEFAULT_LIMIT The default number of links to be retried (25) VOTE_UP Up vote VOTE_DOWN Down vote VOTE_NONE Remove any vote SUBREDDITS_HOME List reddits on the homepage SUBREDDITS_POPULAR List popular reddits SUBREDDITS_NEW List new reddits SUBREDDITS_MINE List reddits for which the logged in user is subscribed SUBREDDITS_CONTRIB List reddits for which the logged in user is a contributor SUBREDDITS_MOD List reddits for which the logged in user is a moderator
When set to true, outputs a small amount of debugging information.
Creates a new Reddit::Client object. Optionally authenticates at the same time. If any of the four optional arguments are used, all are required.
Get an Oauth token from Reddit. The client ID and secret come from your apps page, where you will first have to register the script: https://www.reddit.com/prefs/apps. Only the account that created the app and accounts that are registered as developers for the app will be able to use it.
For a Perl script, select a "script" type app. The "about url" and "redirect uri" can be any address; they are not used for scripts.
Return true if a valid Oauth token exists.
Returns a Reddit::Client::Account object.
Mark your inbox read. May not happen instantly.
Returns a hash of extended information about $fullname. Contains some information not available elsewhere.
$fullname
Returns a list of Reddit::Client::SubReddit objects for $type, where $type is a SUBREDDITS_* constant. A number of alias functions are available:
$type
SUBREDDITS_*
Alias for list_subreddits(SUBREDDITS_MINE).
list_subreddits(SUBREDDITS_MINE)
Alias for list_subreddits(SUBREDDITS_HOME).
list_subreddits(SUBREDDITS_HOME)
Alias for list_subreddits(SUBREDDITS_MOD).
list_subreddits(SUBREDDITS_MOD)
Alias for list_subreddits(SUBREDDITS_CONTRIB).
list_subreddits(SUBREDDITS_CONTRIB)
Alias for list_subreddits(SUBREDDITS_POPULAR).
list_subreddits(SUBREDDITS_POPULAR)
Alias for list_subreddits(SUBREDDITS_NEW).
list_subreddits(SUBREDDITS_NEW)
Returns a list of SubReddit objects matching $query.
$query
Returns a list of Link objects.
All arguments are optional. If subreddit is specified, the list of links is returned from the desired subreddit or multi (i.e. "pics+new"). Otherwise, the links will be from the front page of the authenticated user (i.e. what you would see as the front page if you logged in as that user) . view specifieds the feed (e.g. VIEW_NEW or VIEW_HOT). limit may be used to limit the number of objects returned; Reddit returns a maximum of 100. before and after denote the placeholders for slicing the feed up, just as the reddit urls themselves do. Data is returned as a hash with three keys, before, after, and items.
subreddit
view
VIEW_NEW
VIEW_HOT
limit
before
after
Deletes a post or comment. The object's fullname is required.
Submits a link to a reddit. Returns the fullname of the new link. If inbox_replies is set and is false, disable inbox replies for that post.
Submits a self-post to a reddit. Returns the fullname of the new post. If inbox_replies is set and is false, disable inbox replies for that post.
Return a list of Reddit::Client::Comment objects from a subreddit or multi. All arguments are optional. If subreddit is ommitted, a multi of the subscribed subreddits from the authenticating account will be returned (i.e. what you see when you visit reddit.com's from page and are logged in). If limit is ommitted, Reddit's default limit of 25 is used. If limit is present but false, this is interpreted as no limit and the maximum is returned (100).
Submits a new comment underneath parent_id. parent_id must be a fullname.
parent_id
Votes for item_id. direction is one of VOTE_UP, VOTE_DOWN, or VOTE_NONE. item_id must be a fullname.
item_id
direction
VOTE_UP
VOTE_DOWN
VOTE_NONE
Saves $item_id under the user's account. item_id must be a fullname.
$item_id
Unsaves $item_id under the user's account. item_id must be a fullname.
Hides $<item_id>. Throws an error if the user does not have permission to hide the item in question. item_id must be a fullname.
Unhides $<item_id>. Throws an error if the user does not have permission to unhide the item in question. item_id must be a fullname.
When $Reddit::Client::DEBUG is true, acts as syntactic sugar for warn(sprintf(@_)). Used to provided logging.
$Reddit::Client::DEBUG
Strips slashes and leading /r from a subreddit to ensure that only the "display name" of the subreddit is returned.
Performs a request to reddit's servers using LWP. If the user is logged in, adds the "uh" and "modhash" parameters to POST queries as well as adding the reddit-specified cookie value for reddit_session.
Wraps request, configuring the parameters to perform the request with an api_type of "json". After the request is complete, parses the JSON result and throws and error if one is specified in the result contents. Otherwise, returns the json data portion of the result.
request
Wraps json_request, getting method and path from an API_CONSTANT.
json_request
mailto:earth-tone@ubwg.net
BSD license
To install Reddit::Client, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Reddit::Client
CPAN shell
perl -MCPAN -e shell install Reddit::Client
For more information on module installation, please visit the detailed CPAN module installation guide.