Created
March 10, 2017 16:55
-
-
Save antoine-briand/ac9b65bd3a7e1e9527f5224eca9cc8bc to your computer and use it in GitHub Desktop.
python-mongo-reddit
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
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() |
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 | |
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