twitter #2

Merged
s434708 merged 50 commits from twitter into master 2022-06-09 19:42:33 +02:00
5 changed files with 71 additions and 26 deletions
Showing only changes of commit a295309398 - Show all commits

View File

@ -23,7 +23,8 @@ def create_app():
twitter_db.drop_all(app=app) twitter_db.drop_all(app=app)
twitter_db.create_all(app=app) twitter_db.create_all(app=app)
#campaign_db.create_all(app=app) campaign_db.drop_all(app=app)
campaign_db.create_all(app=app)
app.register_blueprint(twitter, url_prefix='/twitter') app.register_blueprint(twitter, url_prefix='/twitter')
app.register_blueprint(campaign, url_prefix='/campaign') app.register_blueprint(campaign, url_prefix='/campaign')

View File

@ -1,31 +1,71 @@
import os import os
from requests_oauthlib import OAuth1Session
from flask import Blueprint, current_app, request, jsonify from flask import Blueprint, current_app, request, jsonify
from ayct_backend.twitter.models import *
from ayct_backend.campaign.models import *
campaign = Blueprint('campaign', __name__) campaign = Blueprint('campaign', __name__)
@campaign.route('/account', methods=['GET']) @campaign.route('/campaign', methods=['GET'])
def get_twitter_accounts(): def get_twitter_campaigns():
content_type = request.headers.get('Content-Type') twitter_campaigns = TwitterCampaign.query.all()
if (content_type == 'application/json'):
json = request.json
return json
else:
return 'Content-Type not supported!'
@campaign.route('/account', methods=['POST']) campaigns = []
for campaign in twitter_campaigns:
campaign_data = {}
campaign_data['campaign_name'] = campaign.campaign_name
campaign_data['twitter_account_id'] = campaign.twitter_account_id
campaign_data['user_input'] = campaign.user_input
campaign_data['generated_content'] = campaign.generated_content
campaign_data['twitter_post_id'] = campaign.twitter_post_id
campaigns.append(campaign_data)
return jsonify({
"twitter_campaigns": campaigns
}), 200
@campaign.route('/campaign', methods=['POST'])
def add_twitter_account(): def add_twitter_account():
content_type = request.headers.get('Content-Type') content_type = request.headers.get('Content-Type')
if (content_type == 'application/json'): if (content_type == 'application/json'):
json = request.json json = request.json
return json if not json['campaign_name'] or not json['user_input'] or not json['twitter_account_id']:
return "Invalid request!", 400
else: else:
return 'Content-Type not supported!' consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
@campaign.route('/account', methods=['DELETE']) twitter_account = TwitterAccount.query.filter_by(twitter_account_id=json['twitter_account_id']).first()
def delete_twitter_account():
content_type = request.headers.get('Content-Type') # generate campaign content, change after core integration
if (content_type == 'application/json'): generated_content = "This is wonderful post content generated by our very smart core API!"
json = request.json
return json # create post on twitter
oauth = OAuth1Session(
consumer_key,
client_secret=consumer_secret,
resource_owner_key=twitter_account.access_token,
resource_owner_secret=twitter_account.access_token_secret,
)
response = oauth.post(
"https://api.twitter.com/2/tweets",
json={"text": generated_content},
)
# save campaign to database
new_twitter_campaign = TwitterCampaign(
campaign_name = json['campaign_name'],
twitter_account_id = json['twitter_account_id'],
user_input = json['user_input'],
generated_content = generated_content,
twitter_post_id = response['id']
)
campaign_db.session.add(new_twitter_campaign)
campaign_db.session.commit()
return "Campaign succesfully created.", 201
else: else:
return 'Content-Type not supported!' return "Content-type not supported!", 400

View File

@ -6,3 +6,8 @@ class TwitterCampaign(campaign_db.Model):
__tablename__ = 'twitter_campaign' __tablename__ = 'twitter_campaign'
campaign_id = campaign_db.Column(campaign_db.Integer, primary_key=True) campaign_id = campaign_db.Column(campaign_db.Integer, primary_key=True)
campaign_name = campaign_db.Column(campaign_db.String(64), nullable=False)
twitter_account_id = campaign_db.Column(campaign_db.String(32), unique=True, nullable=False)
user_input = campaign_db.Column(campaign_db.String(100), nullable=False)
generated_content = campaign_db.Column(campaign_db.String(300), nullable=False)
twitter_post_id = campaign_db.Column(campaign_db.String(32), unique=True, nullable=False)

View File

@ -1,5 +1,4 @@
import os import os
from uuid import uuid4
from requests_oauthlib import OAuth1Session from requests_oauthlib import OAuth1Session
from flask import Blueprint, Response, current_app, request, jsonify from flask import Blueprint, Response, current_app, request, jsonify
from ayct_backend.twitter.models import * from ayct_backend.twitter.models import *
@ -44,10 +43,10 @@ def add_twitter_account():
oauth_tokens = oauth.fetch_access_token(access_token_url) oauth_tokens = oauth.fetch_access_token(access_token_url)
new_twitter_account = TwitterAccount( new_twitter_account = TwitterAccount(
twitter_user_id = oauth_tokens["user_id"], twitter_account_id = oauth_tokens['user_id'],
username = oauth_tokens["screen_name"], username = oauth_tokens['screen_name'],
access_token = oauth_tokens["oauth_token"], access_token = oauth_tokens['oauth_token'],
access_token_secret = oauth_tokens["oauth_token_secret"] access_token_secret = oauth_tokens['oauth_token_secret']
) )
twitter_db.session.add(new_twitter_account) twitter_db.session.add(new_twitter_account)

View File

@ -7,7 +7,7 @@ class TwitterAccount(twitter_db.Model):
__tablename__ = 'twitter_account' __tablename__ = 'twitter_account'
account_id = twitter_db.Column(twitter_db.Integer, primary_key=True) account_id = twitter_db.Column(twitter_db.Integer, primary_key=True)
twitter_user_id = twitter_db.Column(twitter_db.String(32), unique=True, nullable=False) twitter_account_id = twitter_db.Column(twitter_db.String(32), unique=True, nullable=False)
username = twitter_db.Column(twitter_db.String(16), unique=True, nullable=False) username = twitter_db.Column(twitter_db.String(16), unique=True, nullable=False)
access_token = twitter_db.Column(twitter_db.String(256), nullable=False) access_token = twitter_db.Column(twitter_db.String(256), nullable=False)
access_token_secret = twitter_db.Column(twitter_db.String(256), nullable=False) access_token_secret = twitter_db.Column(twitter_db.String(256), nullable=False)