2022-01-25 14:07:07 +01:00
|
|
|
import requests
|
|
|
|
import json
|
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'):
|
2022-01-25 14:14:54 +01:00
|
|
|
request_json = request.json
|
|
|
|
if not request_json['campaign_name'] or not request_json['user_input'] or not request_json['twitter_account_id']:
|
2022-01-24 23:20:49 +01:00
|
|
|
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-25 14:14:54 +01:00
|
|
|
twitter_account = TwitterAccount.query.filter_by(twitter_account_id=request_json['twitter_account_id']).first()
|
2022-01-24 23:20:49 +01:00
|
|
|
|
2022-01-25 14:07:07 +01:00
|
|
|
# generate campaign content
|
|
|
|
core_url = 'http://65.108.80.28:4999/generate'
|
|
|
|
payload = {
|
2022-01-25 14:14:54 +01:00
|
|
|
"data": request_json['user_input'],
|
2022-01-25 14:07:07 +01:00
|
|
|
"length": 400
|
|
|
|
}
|
|
|
|
|
|
|
|
response = requests.post(core_url, headers={"Content-Type":"application/json"}, data=json.dumps(payload))
|
|
|
|
|
|
|
|
generated_content = response.content.decode("utf-8").replace('<|endoftext|>', '')
|
|
|
|
#generated_content = "This is wonderful post content generated by our very smart core API!"
|
2022-01-24 23:20:49 +01:00
|
|
|
|
|
|
|
# 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",
|
2022-01-25 14:07:07 +01:00
|
|
|
json={"text": generated_content[0:260]},
|
2022-01-24 23:42:20 +01:00
|
|
|
).json()['data']
|
2022-01-24 23:20:49 +01:00
|
|
|
|
|
|
|
# save campaign to database
|
|
|
|
new_twitter_campaign = TwitterCampaign(
|
2022-01-25 14:14:54 +01:00
|
|
|
campaign_name = request_json['campaign_name'],
|
|
|
|
twitter_account_id = request_json['twitter_account_id'],
|
|
|
|
user_input = request_json['user_input'],
|
2022-01-25 14:30:40 +01:00
|
|
|
generated_content = generated_content[0:260],
|
2022-01-24 23:20:49 +01:00
|
|
|
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
|