Reddit.jl

Reddit API wrapper for Julia.
Author RossBerry
Popularity
1 Star
Updated Last
1 Year Ago
Started In
August 2019

Reddit.jl

Build Status

Reddit API wrapper for Julia. This package is still a work in progress, and most of the planned functionality is yet to be implemented.

Prerequisites

Reddit account - A Reddit account is required to access Reddit's API. Create one at reddit.com.

Client ID & Client Secret - These two values are needed to access Reddit's API as a script application, which is currently the only aplication type supported by this package. If you don’t already have a client ID and client secret, follow Reddit’s First Steps Guide to create them.

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.example.myredditapp:v1.2.3 (by /u/kemitche).
Read more about user-agents at Reddit’s API wiki page.

Installation

The package can be installed with Julia's package manager, either by using the Pkg REPL mode (press ] to enter):

pkg> add Reddit

or by using Pkg functions

julia> using Pkg; Pkg.add("Reddit")

Usage

The Reddit script application and user account credentials mentioned in the prerequisites section can be used to create a Credentials object:

creds = Credentials("CLIENT_ID", "CLIENT_SECRET", "USER_AGENT", "USER_NAME", "PASSWORD")

The information required to create Credentials can be stored in an ini file. The example config file, config.ini, contains two example clients named client1 and client2, with placeholders for the information.

[client1]
client_id=CLIENT_ID_1
client_secret=CLIENT_SECRET_1
user_agent=USER_AGENT_1
password=PASSWORD_1
username=USER_NAME_1

[client2]
client_id=CLIENT_ID_2
client_secret=CLIENT_SECRET_2
user_agent=USER_AGENT_2
password=PASSWORD_2
username=USER_NAME_2

The readconfig() function can be used to read credentials information from an ini file.

# read credentials from default config.ini
creds = readconfig("CLIENT_NAME")

# read credentials from an alternate ini
creds = readconfig("CLIENT_NAME", "PATH/TO/ALTERNATE.ini")

In order to access Reddit's API, the Credentials need to be authorized. The authorize() function can be used with Credentials to get an Authorized type, which contains the same fields as Credentials with the addition of a token representing the Oauth access token needed to interact with most of Reddit's API.

auth = authorize(creds)

The token() function can also be called with Credentials to get the access token without creating an Authorized type.

accesstoken = token(creds)

The Authorized credentials can then be used in the various API call functions included in this project.

Examples

Get information about user account used to create the script application.

myinfo = me(auth)

Get karma breakdown for user account used to create the script application.

mykarma = karma(auth)

Get the number of subsribers for /r/Julia

subcount = subscribers(Subreddit("Julia"), auth)

Get an array of friends for the user account associated with the script application.

f = friends(auth)

An Authorized object can also be set as the default using the default!() function. When the default credentials are set, the same API call functions can be used without specifying the Authorized to use.

default!(auth)

myinfo = me()

mykarma = karma()

subcount = subscribers(Subreddit("Julia"))

f = friends()

Get comments made by users.

# get comments made by default user
mycomments = comments()

# get comments made by /user/USERNAME
theircomments = comments(User("USERNAME"))

The Reddit API will only return up to 1000 items in a listing, so the comments() function will return a max of 1000 comments. The number of comments to fetch and the sorting can be specified with the count and sort parameters.

# get top 100 comments by /user/USERNAME
topcomments = comments(User("USERNAME"), count=100, sort="top")

Default sorting is by new. Other options are hot, top, and controversial.

Get the text of a user's latest comment.

text = comments(User("USERNAME"), count=1)[1]["body"]

Reply to a user's latest comment

reply(comments(User("USERNAME"), count=1)[1]["name"], "REPLY TEXT")

Submit a new text post to a subreddit.

submit(Subreddit("SUBREDDIT"), "TITLE", "BODY TEXT")

Submit a new link post to a subreddit

submit(Subreddit("SUBREDDIT"), "TITLE", "URL", kind="link")

Default kind is "self" for text posts.

Used By Packages

No packages found.