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-31 18:29:20 +01:00
|
|
|
from rsa import verify
|
2022-01-24 23:20:49 +01:00
|
|
|
from ayct_backend.twitter.models import *
|
|
|
|
from ayct_backend.campaign.models import *
|
2022-01-31 18:29:20 +01:00
|
|
|
from ayct_backend.firebase import verify_token
|
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():
|
2022-01-31 18:29:20 +01:00
|
|
|
decoded_token = verify_token(request.headers)
|
|
|
|
if not decoded_token:
|
|
|
|
return "Not authorised!", 401
|
2022-01-31 17:28:52 +01:00
|
|
|
|
2022-01-31 19:05:14 +01:00
|
|
|
user_id = decoded_token['sub']
|
2022-01-31 18:29:20 +01:00
|
|
|
|
|
|
|
twitter_campaigns = TwitterCampaign.query.filter_by(user_id=user_id)
|
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():
|
2022-01-31 18:29:20 +01:00
|
|
|
decoded_token = verify_token(request.headers)
|
|
|
|
if not decoded_token:
|
|
|
|
return "Not authorised!", 401
|
|
|
|
|
2022-01-31 19:05:14 +01:00
|
|
|
user_id = decoded_token['sub']
|
2022-01-31 18:29:20 +01:00
|
|
|
|
2022-01-24 19:37:27 +01:00
|
|
|
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-31 23:25:02 +01:00
|
|
|
"length": 420
|
2022-01-25 14:07:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response = requests.post(core_url, headers={"Content-Type":"application/json"}, data=json.dumps(payload))
|
|
|
|
|
2022-01-31 23:25:02 +01:00
|
|
|
generated_content = response.content.decode("utf-8").replace('<|endoftext|>', '').replace(' ', ' ')
|
|
|
|
|
|
|
|
trimmed_text = generated_content[:270]
|
|
|
|
dot_index = trimmed_text.rfind(".")
|
|
|
|
space_index = trimmed_text.rfind(" ")
|
|
|
|
if dot_index > 0:
|
2022-01-31 23:31:11 +01:00
|
|
|
tweet_text = trimmed_text[:dot_index].capitalize()
|
2022-01-31 23:25:02 +01:00
|
|
|
else:
|
2022-01-31 23:31:11 +01:00
|
|
|
tweet_text = trimmed_text[:space_index].capitalize()
|
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-02-01 11:16:57 +01:00
|
|
|
json={"text": tweet_text + str(dot_index) + str(space_index)},
|
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'],
|
2022-01-31 18:29:20 +01:00
|
|
|
user_id = user_id,
|
2022-01-25 14:14:54 +01:00
|
|
|
twitter_account_id = request_json['twitter_account_id'],
|
|
|
|
user_input = request_json['user_input'],
|
2022-01-31 23:25:02 +01:00
|
|
|
generated_content = tweet_text,
|
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
|