-
-
Save hsm207/b5e85961962a721db86a0252f67bf5d4 to your computer and use it in GitHub Desktop.
Get all available submissions within a subreddit newer than x
This file contains 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