pbr-ayct-backend/ayct_backend/twitter/__init__.py

104 lines
3.3 KiB
Python
Raw Normal View History

2022-05-10 11:37:54 +02:00
from uuid import uuid4
2022-01-24 19:37:27 +01:00
from requests_oauthlib import OAuth1Session
from flask import Blueprint, Response, current_app, request, jsonify
2022-05-10 11:37:54 +02:00
from ayct_backend.models import *
2022-01-31 18:29:20 +01:00
from ayct_backend.firebase import verify_token
2022-01-24 19:37:27 +01:00
twitter = Blueprint('twitter', __name__)
@twitter.route('/account', methods=['GET'])
def get_twitter_accounts():
2022-01-31 18:29:20 +01:00
decoded_token = verify_token(request.headers)
if not decoded_token:
return "Not authorised!", 401
2022-01-31 19:05:14 +01:00
user_id = decoded_token['sub']
2022-01-31 18:29:20 +01:00
2022-05-10 11:37:54 +02:00
twitter_accounts = TwitterAccount.query.filter_by(user_id=user_id).all()
2022-01-24 19:37:27 +01:00
accounts = []
for account in twitter_accounts:
account_data = {}
2022-01-24 23:26:55 +01:00
account_data['twitter_account_id'] = account.twitter_account_id
2022-01-24 19:37:27 +01:00
account_data['username'] = account.username
accounts.append(account_data)
2022-01-24 22:20:54 +01:00
return jsonify({
"twitter_accounts": accounts
}), 200
2022-01-24 19:37:27 +01:00
@twitter.route('/account', methods=['POST'])
def add_twitter_account():
2022-01-31 18:29:20 +01:00
decoded_token = verify_token(request.headers)
if not decoded_token:
return "Not authorised!", 401
2022-01-31 19:05:14 +01:00
user_id = decoded_token['sub']
2022-01-31 18:29:20 +01:00
2022-01-24 19:37:27 +01:00
content_type = request.headers.get('Content-Type')
2022-04-12 17:55:52 +02:00
if content_type != 'application/json':
2022-01-24 22:20:54 +01:00
return "Content-type not supported!", 400
2022-04-12 17:55:52 +02:00
2022-05-10 11:37:54 +02:00
request_json = request.json
if 'veryfier' not in request_json or 'oauth_token' not in request_json or 'oauth_token_secret' not in request_json:
2022-04-12 17:55:52 +02:00
return "Invalid request!", 400
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
access_token_url = "https://api.twitter.com/oauth/access_token"
oauth = OAuth1Session(
consumer_key,
2022-05-10 11:37:54 +02:00
client_secret = consumer_secret,
resource_owner_key = request_json['oauth_token'],
resource_owner_secret = request_json['oauth_token_secret'],
verifier = request_json['veryfier'],
2022-04-12 17:55:52 +02:00
)
oauth_tokens = oauth.fetch_access_token(access_token_url)
2022-05-10 11:37:54 +02:00
twitter_account = TwitterAccount.query.filter_by(user_id=user_id, twitter_account_id=oauth_tokens['user_id']).first()
2022-04-12 17:55:52 +02:00
if twitter_account:
2022-05-10 11:37:54 +02:00
return "Account already exists!", 409
2022-04-12 17:55:52 +02:00
twitter_account = TwitterAccount(
2022-05-10 11:37:54 +02:00
id = str(uuid4()),
2022-04-12 17:55:52 +02:00
user_id = user_id,
twitter_account_id = oauth_tokens['user_id'],
username = oauth_tokens['screen_name'],
access_token = oauth_tokens['oauth_token'],
access_token_secret = oauth_tokens['oauth_token_secret']
)
2022-05-10 11:37:54 +02:00
db.session.add(twitter_account)
db.session.commit()
2022-04-12 17:55:52 +02:00
return "Twitter account succesfully added.", 201
@twitter.route('/account', methods=['DELETE'])
def delete_twitter_account():
decoded_token = verify_token(request.headers)
if not decoded_token:
return "Not authorised!", 401
user_id = decoded_token['sub']
content_type = request.headers.get('Content-Type')
if content_type != 'application/json':
return "Content-type not supported!", 400
request_json = request.json
2022-05-10 11:37:54 +02:00
if 'twitter_account_id' not in request_json:
2022-04-12 17:55:52 +02:00
return "Invalid request!", 400
2022-05-10 11:37:54 +02:00
twitter_account = TwitterAccount.query.filter_by(user_id=user_id, twitter_account_id=request_json['twitter_account_id']).first()
2022-04-12 17:55:52 +02:00
if not twitter_account:
return "Account not found!", 404
2022-05-10 11:37:54 +02:00
db.session.delete(twitter_account)
db.session.commit()
2022-04-12 17:55:52 +02:00
return "Twitter account succesfully deleted.", 200