2022-01-24 19:37:27 +01:00
|
|
|
import os
|
2022-01-24 23:20:49 +01:00
|
|
|
from requests_oauthlib import OAuth1Session
|
2022-01-24 19:37:27 +01:00
|
|
|
from flask import Blueprint, current_app, request, jsonify
|
2022-01-24 23:20:49 +01:00
|
|
|
from ayct_backend.twitter.models import *
|
|
|
|
from ayct_backend.campaign.models import *
|
2022-01-24 19:37:27 +01:00
|
|
|
|
|
|
|
campaign = Blueprint('campaign', __name__)
|
|
|
|
|
2022-01-24 23:20:49 +01:00
|
|
|
@campaign.route('/campaign', methods=['GET'])
|
|
|
|
def get_twitter_campaigns():
|
|
|
|
twitter_campaigns = TwitterCampaign.query.all()
|
2022-01-24 19:37:27 +01:00
|
|
|
|
2022-01-24 23:20:49 +01:00
|
|
|
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'])
|
2022-01-24 19:37:27 +01:00
|
|
|
def add_twitter_account():
|
|
|
|
content_type = request.headers.get('Content-Type')
|
|
|
|
if (content_type == 'application/json'):
|
|
|
|
json = request.json
|
2022-01-24 23:20:49 +01:00
|
|
|
if not json['campaign_name'] or not json['user_input'] or not json['twitter_account_id']:
|
|
|
|
return "Invalid request!", 400
|
|
|
|
else:
|
|
|
|
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
|
|
|
|
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
|
2022-01-24 19:37:27 +01:00
|
|
|
|
2022-01-24 23:20:49 +01:00
|
|
|
twitter_account = TwitterAccount.query.filter_by(twitter_account_id=json['twitter_account_id']).first()
|
|
|
|
|
|
|
|
# generate campaign content, change after core integration
|
|
|
|
generated_content = "This is wonderful post content generated by our very smart core API!"
|
|
|
|
|
|
|
|
# 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},
|
2022-01-24 23:32:06 +01:00
|
|
|
).json()
|
2022-01-24 23:20:49 +01:00
|
|
|
|
|
|
|
# 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
|
2022-01-24 19:37:27 +01:00
|
|
|
else:
|
2022-01-24 23:20:49 +01:00
|
|
|
return "Content-type not supported!", 400
|