Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python-mongo-reddit
from flask import Flask, jsonify
from flask import request
from bson.json_util import dumps
from flask_cors import CORS
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client['reddit']
app = Flask(__name__)
CORS(app)
@app.route("/search/")
def find():
query = request.args.get('q')
collection = request.args.get('c')
if collection == "comments":
res = db.comments.find({"body": {"$regex": query}})
return dumps(res)
if collection == "posts":
res = db.submissions.find({"title": {"$regex": query}})
return dumps(res)
return jsonify({})
if __name__ == "__main__":
app.run()
import praw
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client['reddit']
reddit = praw.Reddit(client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET",
user_agent="my user agent", username="YOUR_USERNAME",
password="YOUR_PASSWORD")
reddit.read_only = True
fh = reddit.subreddit("SUBREDDIT_NAME")
subreddit = {
"display_name": fh.display_name,
"title": fh.title,
"description": fh.description
}
subreddits = db.subreddits
sub_id = subreddits.insert_one(subreddit).inserted_id
len_sub = 0
for submission in fh.submissions():
len_sub += 1
_submission = {
"title": submission.title,
"subreddit": sub_id
}
submissions = db.submissions
subm_id = submissions.insert_one(_submission).inserted_id
submission.comments.replace_more(limit=0)
for comment in submission.comments.list():
_comment = {
"submission": subm_id,
"body": comment.body
}
comments = db.comments
comments.insert_one(_comment)
print("There is " + str(len_sub) + " submissions in the subreddit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.