2022-01-25 14:07:07 +01:00
|
|
|
import requests
|
|
|
|
import json
|
2022-05-10 11:37:54 +02:00
|
|
|
from uuid import uuid4
|
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-05-10 11:37:54 +02:00
|
|
|
from ayct_backend.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-04-12 17:55:52 +02:00
|
|
|
@campaign.route('/', methods=['GET'])
|
2022-01-24 23:20:49 +01:00
|
|
|
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
|
|
|
|
2022-05-10 11:37:54 +02:00
|
|
|
twitter_campaigns = TwitterCampaign.query.filter_by(user_id=user_id).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 = {}
|
2022-04-12 17:55:52 +02:00
|
|
|
campaign_data['campaign_id'] = campaign.id
|
2022-01-24 23:20:49 +01:00
|
|
|
campaign_data['campaign_name'] = campaign.campaign_name
|
|
|
|
campaign_data['twitter_account_id'] = campaign.twitter_account_id
|
|
|
|
campaigns.append(campaign_data)
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
"twitter_campaigns": campaigns
|
|
|
|
}), 200
|
|
|
|
|
2022-04-12 17:55:52 +02:00
|
|
|
@campaign.route('/', methods=['POST'])
|
|
|
|
def add_twitter_campaign():
|
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')
|
2022-04-12 17:55:52 +02:00
|
|
|
if content_type != 'application/json':
|
|
|
|
return "Content-type not supported!", 400
|
|
|
|
|
|
|
|
request_json = request.json
|
2022-05-10 11:37:54 +02:00
|
|
|
if 'campaign_name' not in request_json or 'user_input' not in request_json or 'twitter_account_id' not in request_json:
|
2022-04-12 17:55:52 +02:00
|
|
|
return "Invalid request!", 400
|
|
|
|
|
|
|
|
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
|
|
|
|
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
|
|
|
|
|
|
|
|
twitter_account = TwitterAccount.query.filter_by(twitter_account_id=request_json['twitter_account_id']).first()
|
|
|
|
|
|
|
|
# generate campaign content
|
|
|
|
core_url = 'http://65.108.80.28:4999/generate'
|
|
|
|
payload = {
|
|
|
|
"data": request_json['user_input'],
|
|
|
|
"length": 420
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:37:54 +02:00
|
|
|
#response = requests.post(core_url, headers={"Content-Type":"application/json"}, data=json.dumps(payload))
|
|
|
|
#generated_content = response.content.decode("utf-8").replace('<|endoftext|>', '').replace(' ', ' ')
|
2022-04-12 17:55:52 +02:00
|
|
|
|
2022-05-10 11:37:54 +02:00
|
|
|
# trimmed_text = generated_content[:270]
|
|
|
|
# dot_index = trimmed_text.rfind(".")
|
|
|
|
# space_index = trimmed_text.rfind(" ")
|
|
|
|
# if dot_index > 0:
|
|
|
|
# tweet_text = trimmed_text[:dot_index + 1].capitalize()
|
|
|
|
# else:
|
|
|
|
# tweet_text = trimmed_text[:space_index + 1].capitalize()
|
2022-04-12 17:55:52 +02:00
|
|
|
|
2022-05-17 19:23:15 +02:00
|
|
|
random_part = str(uuid4().hex)
|
|
|
|
tweet_text = "This is placeholder content generated by our application. Normal generation doesn't work." + random_part
|
2022-04-12 17:55:52 +02: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",
|
|
|
|
json={"text": tweet_text},
|
|
|
|
).json()['data']
|
|
|
|
|
|
|
|
# save campaign to database
|
|
|
|
twitter_campaign = TwitterCampaign(
|
2022-05-10 11:37:54 +02:00
|
|
|
id = str(uuid4()),
|
2022-04-12 17:55:52 +02:00
|
|
|
user_id = user_id,
|
|
|
|
campaign_name = request_json['campaign_name'],
|
|
|
|
twitter_account_id = request_json['twitter_account_id'],
|
|
|
|
user_input = request_json['user_input'],
|
|
|
|
)
|
|
|
|
|
|
|
|
twitter_campaign_post = TwitterPost(
|
2022-05-10 11:37:54 +02:00
|
|
|
id = str(uuid4()),
|
2022-04-12 17:55:52 +02:00
|
|
|
campaign_id = twitter_campaign.id,
|
|
|
|
user_id = user_id,
|
|
|
|
post_content = tweet_text,
|
|
|
|
twitter_post_id = response['id']
|
|
|
|
)
|
|
|
|
|
2022-05-17 19:19:47 +02:00
|
|
|
db.session.add(twitter_campaign)
|
2022-05-10 11:37:54 +02:00
|
|
|
db.session.add(twitter_campaign_post)
|
|
|
|
db.session.commit()
|
2022-04-12 17:55:52 +02:00
|
|
|
|
|
|
|
return "Campaign succesfully created.", 201
|
|
|
|
|
|
|
|
@campaign.route('/', methods=['DELETE'])
|
|
|
|
def delete_twitter_campaign():
|
|
|
|
decoded_token = verify_token(request.headers)
|
|
|
|
if not decoded_token:
|
|
|
|
return "Not authorised!", 401
|
|
|
|
|
|
|
|
user_id = decoded_token['sub']
|
|
|
|
|
|
|
|
content_type = request.headers.get('Content-Type')
|
|
|
|
if content_type != 'application/json':
|
|
|
|
return "Content-type not supported!", 400
|
|
|
|
|
|
|
|
request_json = request.json
|
2022-05-10 11:37:54 +02:00
|
|
|
if 'campaign_id' not in request_json:
|
2022-04-12 17:55:52 +02:00
|
|
|
return "Invalid request!", 400
|
|
|
|
|
2022-05-17 21:20:37 +02:00
|
|
|
twitter_campaign = TwitterCampaign.query.filter_by(user_id=user_id, id=request_json['campaign_id']).first()
|
2022-04-12 17:55:52 +02:00
|
|
|
|
|
|
|
if not twitter_campaign:
|
2022-05-10 11:37:54 +02:00
|
|
|
return "Capmaign not found!", 404
|
2022-04-12 17:55:52 +02:00
|
|
|
|
2022-05-10 11:37:54 +02:00
|
|
|
db.session.delete(twitter_campaign)
|
|
|
|
db.session.commit()
|
2022-04-12 17:55:52 +02:00
|
|
|
|
|
|
|
return "Twitter campaign succesfully deleted.", 200
|
|
|
|
|
2022-05-17 22:11:55 +02:00
|
|
|
@campaign.route('/<campaign_id>', methods=['GET'])
|
|
|
|
def get_twitter_campaign_details(campaign_id):
|
2022-04-12 17:55:52 +02:00
|
|
|
decoded_token = verify_token(request.headers)
|
|
|
|
if not decoded_token:
|
|
|
|
return "Not authorised!", 401
|
|
|
|
|
|
|
|
user_id = decoded_token['sub']
|
|
|
|
|
2022-05-17 22:11:55 +02:00
|
|
|
twitter_campaign = TwitterCampaign.query.filter_by(user_id=user_id, id=campaign_id).first()
|
|
|
|
campaign_posts = TwitterPost.query.filter_by(user_id=user_id, campaign_id=campaign_id).all()
|
2022-04-12 17:55:52 +02:00
|
|
|
|
|
|
|
posts = []
|
|
|
|
for post in campaign_posts:
|
|
|
|
post_data = {}
|
2022-05-17 22:25:20 +02:00
|
|
|
post_data['post_id'] = post.id
|
2022-04-12 17:55:52 +02:00
|
|
|
post_data['campaign_id'] = post.campaign_id
|
|
|
|
post_data['post_content'] = post.post_content
|
|
|
|
post_data['twitter_post_id'] = post.twitter_post_id
|
|
|
|
posts.append(post_data)
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
"campaign_id": twitter_campaign.id,
|
|
|
|
"campaign_name": twitter_campaign.campaign_name,
|
|
|
|
"user_input": twitter_campaign.user_input,
|
|
|
|
"twitter_account_id": twitter_campaign.twitter_account_id,
|
|
|
|
"posts": posts
|
|
|
|
}), 200
|