From 9dd62bb4f0e729b9d74bd0090b24b1655fc33a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20J=C4=99dyk?= Date: Mon, 24 Jan 2022 19:37:27 +0100 Subject: [PATCH] twitter backend part1 --- ayct_backend/__init__.py | 19 +++++++- ayct_backend/campaign/__init__.py | 31 ++++++++++++ ayct_backend/campaign/models.py | 7 +++ ayct_backend/twitter/__init__.py | 80 +++++++++++++++++++++++++++++++ ayct_backend/twitter/models.py | 11 +++++ 5 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 ayct_backend/campaign/__init__.py create mode 100644 ayct_backend/campaign/models.py create mode 100644 ayct_backend/twitter/__init__.py create mode 100644 ayct_backend/twitter/models.py diff --git a/ayct_backend/__init__.py b/ayct_backend/__init__.py index 6c67c1c..5613982 100644 --- a/ayct_backend/__init__.py +++ b/ayct_backend/__init__.py @@ -1,9 +1,26 @@ +import os from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from twitter import twitter +from twitter.models import twitter_db +from campaign import campaign +from campaign.models import campaign_db import pbrAyctCore.core as core def create_app(): app = Flask('ayct-backend') - + + app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') + app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('SQLALCHEMY_DATABASE_URI') + app.config['TWITTER_CONSUMER_KEY'] = os.getenv('TWITTER_CONSUMER_KEY') + app.config['TWITTER_CONSUMER_SECERT'] = os.getenv('TWITTER_CONSUMER_SECERT') + + twitter_db.init_app(app) + campaign_db.init_app(app) + + app.register_blueprint(twitter, url_prefix='/twitter') + app.register_blueprint(campaign, url_prefix='/campaign') + @app.route('/hello') def hello(): return core.getTestString() diff --git a/ayct_backend/campaign/__init__.py b/ayct_backend/campaign/__init__.py new file mode 100644 index 0000000..bb7017c --- /dev/null +++ b/ayct_backend/campaign/__init__.py @@ -0,0 +1,31 @@ +import os +from flask import Blueprint, current_app, request, jsonify + +campaign = Blueprint('campaign', __name__) + +@campaign.route('/account', methods=['GET']) +def get_twitter_accounts(): + content_type = request.headers.get('Content-Type') + if (content_type == 'application/json'): + json = request.json + return json + else: + return 'Content-Type not supported!' + +@campaign.route('/account', methods=['POST']) +def add_twitter_account(): + content_type = request.headers.get('Content-Type') + if (content_type == 'application/json'): + json = request.json + return json + else: + return 'Content-Type not supported!' + +@campaign.route('/account', methods=['DELETE']) +def delete_twitter_account(): + content_type = request.headers.get('Content-Type') + if (content_type == 'application/json'): + json = request.json + return json + else: + return 'Content-Type not supported!' diff --git a/ayct_backend/campaign/models.py b/ayct_backend/campaign/models.py new file mode 100644 index 0000000..8664272 --- /dev/null +++ b/ayct_backend/campaign/models.py @@ -0,0 +1,7 @@ +from flask_sqlalchemy import SQLAlchemy + +campaign_db = SQLAlchemy() + +class TwitterAccount(campaign_db.Model): + # fields here + pass \ No newline at end of file diff --git a/ayct_backend/twitter/__init__.py b/ayct_backend/twitter/__init__.py new file mode 100644 index 0000000..59e9591 --- /dev/null +++ b/ayct_backend/twitter/__init__.py @@ -0,0 +1,80 @@ +import os +from uuid import uuid4 +from requests_oauthlib import OAuth1Session +from flask import Blueprint, Response, current_app, request, jsonify +from models import * + +twitter = Blueprint('twitter', __name__) + +consumer_key = current_app.config["TWITTER_CONSUMER_KEY"] +consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"] + +@twitter.route('/account', methods=['GET']) +def get_twitter_accounts(): + twitter_accounts = TwitterAccount.query.all() + + accounts = [] + + for account in twitter_accounts: + account_data = {} + account_data['twitter_user_id'] = account.twitter_user_id + account_data['username'] = account.username + accounts.append(account_data) + + return Response( + jsonify({ + "twitter_accounts": accounts + }), + status=200, + ) + +@twitter.route('/account', methods=['POST']) +def add_twitter_account(): + content_type = request.headers.get('Content-Type') + if (content_type == 'application/json'): + json = request.json + if not json['veryfier']: + return Response( + "Invalid request!", + status=400, + ) + else: + oauth = OAuth1Session( + consumer_key, + client_secret=consumer_secret + ) + request_token_url = "https://api.twitter.com/oauth/request_token?oauth_callback=oob&x_auth_access_type=write" + fetch_response = oauth.fetch_request_token(request_token_url) + + access_token_url = "https://api.twitter.com/oauth/access_token" + oauth = OAuth1Session( + consumer_key, + client_secret=consumer_secret, + resource_owner_key=fetch_response.get("oauth_token"), + resource_owner_secret=fetch_response.get("oauth_token_secret"), + verifier=json['veryfier'], + ) + oauth_tokens = oauth.fetch_access_token(access_token_url) + + new_twitter_account = TwitterAccount( + twitter_user_id = oauth_tokens["user_id"], + username = oauth_tokens["screen_name"], + access_token = oauth_tokens["oauth_token"], + access_token_secret = oauth_tokens["oauth_token_secret"] + ) + + twitter_db.session.add(new_twitter_account) + twitter_db.session.commit() + + return Response( + jsonify({ + "message": "Twitter account succesfully added." + }), + status=201, + ) + else: + return Response( + "Content-type not supported!", + status=400, + ) + diff --git a/ayct_backend/twitter/models.py b/ayct_backend/twitter/models.py new file mode 100644 index 0000000..4eeacd2 --- /dev/null +++ b/ayct_backend/twitter/models.py @@ -0,0 +1,11 @@ +from enum import unique +from flask_sqlalchemy import SQLAlchemy + +twitter_db = SQLAlchemy() + +class TwitterAccount(twitter_db.Model): + id = twitter_db.Column(twitter_db.Integer, primary_key=True) + twitter_user_id = twitter_db.Column(twitter_db.String(16), unique=True) + username = twitter_db.Column(twitter_db.String(16), unique=True) + access_token = twitter_db.Column(twitter_db.String(256)) + access_token_secret = twitter_db.Column(twitter_db.String(256))