Created
July 6, 2017 05:24
-
-
Save sarchak/74018e97b3bdfdcb4f1fb762743de6e4 to your computer and use it in GitHub Desktop.
Fetch Data from reddit
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 praw | |
import os | |
import pymongo | |
from pymongo import MongoClient | |
client = MongoClient() | |
db = client.redditdb | |
submissions = db.submissions | |
comments = db.comments | |
reddit = praw.Reddit(client_id=os.environ.get('REDDIT_CLIENT_ID'), | |
client_secret=os.environ.get('REDDIT_CLIENT_SECRET'), | |
user_agent='RedditAI') | |
def save_post(submission): | |
for i, comment in enumerate(submission.comments.list()): | |
try: | |
comm = { | |
'text' : comment.body, | |
'author': comment.author.name, | |
'score': comment.score, | |
'comment_id': comment.id, | |
'subreddit' : submission.subreddit.display_name, | |
'submission_id': submission.id | |
} | |
comments.update({'comment_id': comment.id}, comm, upsert=True) | |
except: | |
pass | |
try: | |
sub = { | |
'text': submission.title, | |
'author': submission.author.name, | |
'submission_id': submission.id, | |
'subreddit': submission.subreddit.display_name, | |
'score': submission.score, | |
'created_at' : submission.created_utc, | |
'url': submission.url, | |
} | |
submissions.update({'submission_id': submission.id}, sub, upsert=True) | |
except: | |
pass | |
reddit_list = ['travel', 'hiking', 'camping', 'solotravel', 'outdoors', 'RoadTrip'] | |
for sr in reddit_list: | |
for submission in reddit.subreddit(sr).hot(limit=2000): | |
print("{}".format(submission.title)) | |
save_post(submission) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment