Created
December 9, 2021 20:58
-
-
Save CrimsonScythe/efc62d70f5e32c6b438f41e534eadb96 to your computer and use it in GitHub Desktop.
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
from flask import Flask, request, jsonify | |
from calc import getTwitterUser, genReport | |
from celery import chord | |
import requests | |
from default_settings import * | |
app = Flask(__name__) | |
# CREDITS: reddit oauth from https://gist.github.com/jamescalam/d7e6a7236e99369123237f0ba371da18#file-reddit-oauth-py | |
@app.route('/api/reddittoken', methods=['GET']) | |
def getToken(): | |
# note that CLIENT_ID refers to 'personal use script' and SECRET_TOKEN to 'token' | |
auth = requests.auth.HTTPBasicAuth(CLIENT_ID, SECRET_TOKEN) | |
# here we pass our login method (password), username, and password | |
data = {'grant_type': 'password', | |
'username': USERNAME, | |
'password': PASSWORD} | |
# setup our header info, which gives reddit a brief description of our app | |
headers = {'User-Agent': 'MyBot/0.0.1'} | |
# send our request for an OAuth token | |
res = requests.post('https://www.reddit.com/api/v1/access_token', | |
auth=auth, data=data, headers=headers) | |
# convert response to JSON and pull access_token value | |
TOKEN = res.json()['access_token'] | |
return jsonify({"token": TOKEN}) | |
@app.route('/api/send', methods=['GET', 'POST']) | |
def send(): | |
token = request.get_json()["token"] | |
topics = request.get_json()["topics"] | |
# add authorization to our headers dictionary | |
headers = {'User-Agent': 'MyBot/0.0.1'} | |
headers = {**headers, **{'Authorization': f"bearer {token}"}} | |
tasks = [getTwitterUser.s(topic, headers) for topic in topics] | |
res = chord(tasks)(genReport.s()) | |
df = res.get() | |
return df | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0', port=5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment