twitter #2
@ -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()
|
||||
|
31
ayct_backend/campaign/__init__.py
Normal file
31
ayct_backend/campaign/__init__.py
Normal file
@ -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!'
|
7
ayct_backend/campaign/models.py
Normal file
7
ayct_backend/campaign/models.py
Normal file
@ -0,0 +1,7 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
campaign_db = SQLAlchemy()
|
||||
|
||||
class TwitterAccount(campaign_db.Model):
|
||||
# fields here
|
||||
pass
|
80
ayct_backend/twitter/__init__.py
Normal file
80
ayct_backend/twitter/__init__.py
Normal file
@ -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,
|
||||
)
|
||||
|
11
ayct_backend/twitter/models.py
Normal file
11
ayct_backend/twitter/models.py
Normal file
@ -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))
|
Loading…
Reference in New Issue
Block a user