Last active
February 14, 2021 22:03
-
-
Save gavin19/a216befe49366c39798accb3a5e69936 to your computer and use it in GitHub Desktop.
Get posts going back given duration (d/h/m)
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 praw | |
import time | |
from datetime import datetime | |
from operator import attrgetter | |
def get_posts(subreddit): | |
r = praw.Reddit(...) | |
sub = r.subreddit(subreddit) | |
posts = set() | |
posts.update(sub.hot(limit=None)) # hot | |
posts.update(sub.new(limit=None)) # new | |
posts.update(sub.top(limit=None)) # top (all time) | |
posts.update(sub.top('hour', limit=None)) # top (last hour) | |
posts.update(sub.top('day'limit=None)) # top (last day) | |
posts.update(sub.top('week', limit=None)) # top (last week) | |
posts.update(sub.top('month', limit=None)) # top (last month) | |
posts.update(sub.top('year', limit=None)) # top (last year) | |
posts.update(sub.rising(limit=None)) # top rising | |
posts.update(sub.controversial(limit=None)) # controversial | |
sorted(posts, key=attrgetter("created_utc")) # sort into newest first | |
return posts | |
def userDate(ts): | |
return datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') | |
def main(): | |
subreddit = input('Enter a subreddit >> ') | |
print('Now enter a time range.') | |
print('E.g, if you enter 2 days, 6 hours, 0 minutes, you will get every hot post currently') | |
print('in r/' + subreddit + ' going back 2 days, 6 hours, 0 minutes.') | |
print('Enter 0\'s if you don\'t have to be that specific.') | |
days = int(input('Enter the number of days in your range >> ')) | |
hours = int(input('Enter the number of hours in your range >> ')) | |
minutes = int(input('Enter the number of minutes in your range >> ')) | |
duration = (days * 86400) + (hours * 3600) + (minutes * 60) | |
then = time.time() - duration | |
print("\nFetching posts...") | |
posts = get_posts(subreddit) | |
matches = [p for p in posts if p.created_utc > then] | |
print("\nFound " + str(len(matches)) + " matches\n") | |
for m in matches: | |
print(m.title + " : " + userDate(m.created_utc)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment