Created
February 24, 2016 00:58
-
-
Save dangayle/4e6864300b58fee09ce1 to your computer and use it in GitHub Desktop.
Get all available submissions within a subreddit newer than x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.