get_posts: Get posts from a specified subreddit

Description Usage Arguments Details Value Examples

View source: R/reddit_functions.R

Description

A maximum of 100 posts can be requested from a specified subreddit. The timeframe and the sorting of the posts can be specified.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
get_posts(
  subreddit,
  accesstoken,
  sort = "new",
  limit = 100,
  time = c("all", "hour", "day", "week", "month", "year"),
  after = NULL,
  before = NULL,
  output = c("df", "json", "all"),
  verbose = FALSE,
  retry = FALSE
)

Arguments

subreddit

The name of the subreddit from which posts are requested.

accesstoken

The accesstoken required to access the endpoint. Scope must be "read".

sort

The order in which the posts in the subreddit should be ordered when accessing them. Possible values are:

  • new: Sorts posts by the time when they were created

  • hot: Sorts posts by posts currently on the frontpage of the subreddit

  • controversial: Posts with a higher number of up- and downvotes simultaneously.

  • random:

  • rising:

  • top: Most upvoted posts in a certain timeframe. Timeframe can be specified with time.

limit

The maximum number of posts to return. Must be a number between 1 and 100.

time

The timeframe for the sort order controversial and top. Possible values are:

  • hour

  • day

  • week

  • month

  • year

  • all for posts from all time. The default value if time is not specified.

after

The fullname of a post serving as anchor in the request. Posts after this post in the listing are returned.

before

The fullname of a post serving as anchor in the request. Posts before this post in the listing are returned.

output

What the function should return. Choose json for an unparsed json object, df for a parsed object in form of a dataframe, and all for a list containg the json object, a dataframe, and the before and after anchors (if those exist for the endpoint).

verbose

A logical flag whether information about the data extraction should be printed to the console.

retry

A logical flag whether a failed api request should be retried. Requests will be tried up to three times with varying time intervals between requests.

Details

More info at https://www.reddit.com/dev/api/

Value

The requested posts as a dataframe, json object or a list containing both.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
## Not run: 
read_token <- get_token(scope = "read",
                        useragent = my_useragent,
                        username = my_username,
                        password = my_password,
                        client_id = my_client_id,
                        client_secret = my_client_secret)
                        
# 10 "hot" posts from the subreddit askreddit
posts <- get_posts(subreddit = "askreddit",
                   accesstoken = read_token,
                   sort = "hot", limit = 10)

# The next set of posts can be retrieved with the argument "after"
posts_after <- get_posts(subreddit = "soccer",
                         accesstoken = read_token,
                         sort = "hot",
                         limit = 10,
                         after = after)

## End(Not run)

jfeldhege/redditoR documentation built on April 2, 2020, 9:42 p.m.