change campaign endpoint

This commit is contained in:
Łukasz Jędyk 2022-06-09 22:03:32 +02:00
parent c484359495
commit fbe41cfec9

View File

@ -45,47 +45,8 @@ def add_twitter_campaign():
return "Content-type not supported!", 400
request_json = request.json
if 'campaign_name' not in request_json or 'user_input' not in request_json or 'twitter_account_id' not in request_json:
if 'campaign_name' not in request_json or 'twitter_account_id' not in request_json:
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
}
#response = requests.post(core_url, headers={"Content-Type":"application/json"}, data=json.dumps(payload))
#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:
# tweet_text = trimmed_text[:dot_index + 1].capitalize()
# else:
# tweet_text = trimmed_text[:space_index + 1].capitalize()
random_part = str(uuid4().hex)
tweet_text = "This is placeholder content generated by our application. Normal generation doesn't work." + random_part
# 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(
@ -93,19 +54,9 @@ def add_twitter_campaign():
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(
id = str(uuid4()),
campaign_id = twitter_campaign.id,
user_id = user_id,
post_content = tweet_text,
twitter_post_id = response['id']
)
db.session.add(twitter_campaign)
db.session.add(twitter_campaign_post)
db.session.commit()
return "Campaign succesfully created.", 201
@ -206,3 +157,52 @@ def generate_content():
return jsonify({
"content": trimmed_content
}), 200
@campaign.route('/post', methods=['POST'])
def create_post():
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
if 'user_input' not in request_json or 'content' not in request_json or 'campaign_id' not in request_json:
return "Invalid request!", 400
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
twitter_campaign = TwitterCampaign.query.filter_by(id=request_json['campaign_id']).first()
twitter_account = TwitterAccount.query.filter_by(id=twitter_campaign.twitter_account_id).first()
# 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": request_json['content']},
).json()['data']
twitter_campaign_post = TwitterPost(
id = str(uuid4()),
campaign_id = twitter_campaign.id,
user_id = user_id,
user_input = request_json['user_input'],
post_content = request_json['content'],
twitter_post_id = response['id']
)
db.session.add(twitter_campaign_post)
db.session.commit()
return "Campaign post succesfully created.", 201