Skip to content

Instantly share code, notes, and snippets.

@dangayle
Created February 24, 2016 00:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dangayle/4e6864300b58fee09ce1 to your computer and use it in GitHub Desktop.
Save dangayle/4e6864300b58fee09ce1 to your computer and use it in GitHub Desktop.
Get all available submissions within a subreddit newer than x
import sys
from datetime import datetime, timedelta
import praw
user_agent = "hot test 1.0 by /u/dangayle"
r = praw.Reddit(user_agent=user_agent)
class SubredditLatest(object):
"""Get all available submissions within a subreddit newer than x."""
def __init__(self, subreddit, dt):
# master list of all available submissions
self.total_list = []
# subreddit must be a string of the subreddit name (e.g., "soccer")
self.subreddit = subreddit
# dt must be a utc datetime object
self.dt = dt
def __call__(self):
self.get_submissions(self)
return self.total_list
def get_submissions(self, paginate=False):
"""Get limit of subreddit submissions."""
limit = 100 # Reddit maximum limit
if paginate is True:
try:
# get limit of items past the last item in the total list
submissions = r.get_subreddit(self.subreddit).get_new(limit=limit, params={"after": self.total_list[-1].fullname})
except IndexError:
logger.exception("param error")
return
else:
submissions = r.get_subreddit(self.subreddit).get_new(limit=limit)
submissions_list = [
# iterate through the submissions generator object
x for x in submissions
# add item if item.created_utc is newer than an hour ago
if datetime.utcfromtimestamp(x.created_utc) >= self.dt
]
self.total_list += submissions_list
# if you've hit the limit, recursively run this function again to get
# all of the available items
if len(submissions_list) == limit:
self.get_submissions(paginate=True)
else:
return
if __name__ == '__main__':
an_hour_ago = datetime.utcnow() - timedelta(hours=1)
print SubredditLatest("soccer", an_hour_ago)()
@SubhasreeSengupta
Copy link

Hi a quick question, can we query more than 1000 posts or will the recursive method stop with 1000 posts (i.e is that the max limit we can get from PRAW)?
Thanks for this really helpful example!

@salvatelli
Copy link

Hi Dan,

Is pagination in PRAW still working? I tried your code on github and got:

<main.SubredditLatest object at 0x000001F503D03FA0>

I read on stackoverflow that reddit removed this function from the API.
https://stackoverflow.com/questions/53988619/praw-6-get-all-submission-of-a-subreddit

People in almost all forums advise to move to pushshift API, but fetching awards of submissions is more complicated with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment