Compare commits
55 Commits
Author | SHA1 | Date | |
---|---|---|---|
8adf41ed35 | |||
|
dbc9448601 | ||
|
3b78c15b85 | ||
|
2458cff797 | ||
|
edb98740a0 | ||
|
9a4b84552b | ||
|
611f7a02d9 | ||
|
5faadaa69b | ||
|
9d95c8a411 | ||
|
12a946c88a | ||
|
48fc3fc954 | ||
|
895df4998e | ||
|
7a2f9f832d | ||
|
2b1f3405a0 | ||
|
4d4992ed1d | ||
|
26620708f3 | ||
|
ca114e3c64 | ||
|
708e241ea1 | ||
|
77c4fc49d9 | ||
|
0756ec8ce3 | ||
|
f72f25bebd | ||
|
7aba9dfa39 | ||
|
163d477a55 | ||
|
14d411f98f | ||
|
43a8763c77 | ||
|
630e57d260 | ||
|
61096f0c4f | ||
|
aeb87ec272 | ||
|
315292f0e8 | ||
|
498d514708 | ||
|
abf43f17ec | ||
|
ac7e7a44e0 | ||
|
6679436a9e | ||
|
7eeff9bd4c | ||
|
9af91e3760 | ||
|
a295309398 | ||
|
a41a7dc052 | ||
|
f8aab50eae | ||
|
8ddfdf0639 | ||
|
03689c9b0a | ||
|
1c0179e088 | ||
|
9bc20aea07 | ||
|
5ba33a5f0f | ||
|
74b2cd9d91 | ||
|
426feab1fd | ||
|
7dd6a34338 | ||
|
5d730cdfe3 | ||
|
34a8af1e17 | ||
|
38094e7b26 | ||
|
7907028283 | ||
|
9dd62bb4f0 | ||
|
41d66c5486 | ||
|
522e3ad6ec | ||
|
1f6260c976 | ||
|
1dc93c5f0d |
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,2 +1,6 @@
|
||||
__pycache__/*
|
||||
venv/*
|
||||
*.egg-info
|
||||
__pycache__
|
||||
.pytest_cache
|
||||
something/*
|
@ -1,12 +1,12 @@
|
||||
FROM python:3
|
||||
ENV DOCKER_APP True
|
||||
USER root
|
||||
WORKDIR /app
|
||||
|
||||
RUN git clone https://git.wmi.amu.edu.pl/s415366/pbr-ayct-core
|
||||
COPY . .
|
||||
RUN chmod u+x setup_core.sh
|
||||
RUN bash setup_core.sh
|
||||
#RUN chmod u+x setup_core.sh
|
||||
#RUN bash setup_core.sh
|
||||
RUN pip3 install -r requirements.txt
|
||||
EXPOSE 5000/udp
|
||||
EXPOSE 5000/tcp
|
||||
CMD ["python3", "app.py"]
|
||||
CMD ["python3","-m", "ayct_backend.app"]
|
22
Jenkinsfile
vendored
Normal file
22
Jenkinsfile
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
pipeline {
|
||||
agent {
|
||||
docker {
|
||||
image 'python:latest'
|
||||
args '-u root --privileged'
|
||||
}
|
||||
}
|
||||
stages {
|
||||
stage('build') {
|
||||
steps {
|
||||
sh 'bash setup_core.sh'
|
||||
sh 'pip install -r requirements.txt'
|
||||
sh 'pip install --user -e .'
|
||||
}
|
||||
}
|
||||
stage('test') {
|
||||
steps {
|
||||
sh 'pytest'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
17
app.py
17
app.py
@ -1,17 +0,0 @@
|
||||
from flask import Flask
|
||||
from flask_restful import Api, Resource
|
||||
import os
|
||||
import pbrAyctCore.core as core
|
||||
|
||||
app = Flask(__name__)
|
||||
api = Api(app)
|
||||
|
||||
class Hello(Resource):
|
||||
def get(self):
|
||||
return core.getTestString()
|
||||
|
||||
api.add_resource(Hello, "/hello")
|
||||
|
||||
if __name__ == "__main__":
|
||||
port = int(os.environ.get('PORT', 5000))
|
||||
app.run(host = '0.0.0.0', port = port)
|
29
ayct_backend/__init__.py
Normal file
29
ayct_backend/__init__.py
Normal file
@ -0,0 +1,29 @@
|
||||
import os
|
||||
from flask import Flask
|
||||
from ayct_backend.twitter import twitter
|
||||
from ayct_backend.campaign import campaign
|
||||
from ayct_backend.models import db
|
||||
#import pbrAyctCore.core as core
|
||||
|
||||
def create_app():
|
||||
app = Flask('ayct-backend')
|
||||
|
||||
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
|
||||
database_uri = os.getenv('DATABASE_URL')
|
||||
if database_uri and database_uri.startswith("postgres://"):
|
||||
database_uri = database_uri.replace("postgres://", "postgresql://", 1)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = database_uri
|
||||
app.config['TWITTER_CONSUMER_KEY'] = os.getenv('TWITTER_CONSUMER_KEY')
|
||||
app.config['TWITTER_CONSUMER_SECERT'] = os.getenv('TWITTER_CONSUMER_SECERT')
|
||||
|
||||
db.init_app(app)
|
||||
db.create_all(app=app)
|
||||
|
||||
app.register_blueprint(twitter, url_prefix='/twitter')
|
||||
app.register_blueprint(campaign, url_prefix='/campaign')
|
||||
|
||||
@app.route('/hello')
|
||||
def hello():
|
||||
return "Hello world!"
|
||||
|
||||
return app
|
7
ayct_backend/app.py
Normal file
7
ayct_backend/app.py
Normal file
@ -0,0 +1,7 @@
|
||||
import os
|
||||
from . import create_app
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = create_app()
|
||||
port = int(os.environ.get('PORT', 5000))
|
||||
app.run(host = '0.0.0.0', port = port)
|
164
ayct_backend/campaign/__init__.py
Normal file
164
ayct_backend/campaign/__init__.py
Normal file
@ -0,0 +1,164 @@
|
||||
import requests
|
||||
import json
|
||||
from uuid import uuid4
|
||||
from requests_oauthlib import OAuth1Session
|
||||
from flask import Blueprint, current_app, request, jsonify
|
||||
from ayct_backend.models import *
|
||||
from ayct_backend.firebase import verify_token
|
||||
|
||||
campaign = Blueprint('campaign', __name__)
|
||||
|
||||
@campaign.route('/', methods=['GET'])
|
||||
def get_twitter_campaigns():
|
||||
decoded_token = verify_token(request.headers)
|
||||
if not decoded_token:
|
||||
return "Not authorised!", 401
|
||||
|
||||
user_id = decoded_token['sub']
|
||||
|
||||
twitter_campaigns = TwitterCampaign.query.filter_by(user_id=user_id).all()
|
||||
|
||||
campaigns = []
|
||||
|
||||
for campaign in twitter_campaigns:
|
||||
campaign_data = {}
|
||||
campaign_data['campaign_id'] = campaign.id
|
||||
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
|
||||
|
||||
@campaign.route('/', methods=['POST'])
|
||||
def add_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
|
||||
if 'campaign_name' not in request_json or 'user_input' 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(
|
||||
id = str(uuid4()),
|
||||
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
|
||||
|
||||
@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
|
||||
if 'campaign_id' not in request_json:
|
||||
return "Invalid request!", 400
|
||||
|
||||
twitter_campaign = TwitterCampaign.query.filter_by(user_id=user_id, id=request_json['campaign_id']).first()
|
||||
|
||||
if not twitter_campaign:
|
||||
return "Capmaign not found!", 404
|
||||
|
||||
db.session.delete(twitter_campaign)
|
||||
db.session.commit()
|
||||
|
||||
return "Twitter campaign succesfully deleted.", 200
|
||||
|
||||
@campaign.route('/<campaign_id>', methods=['GET'])
|
||||
def get_twitter_campaign_details(campaign_id):
|
||||
decoded_token = verify_token(request.headers)
|
||||
if not decoded_token:
|
||||
return "Not authorised!", 401
|
||||
|
||||
user_id = decoded_token['sub']
|
||||
|
||||
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()
|
||||
|
||||
posts = []
|
||||
for post in campaign_posts:
|
||||
post_data = {}
|
||||
post_data['post_id'] = post.id
|
||||
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
|
16
ayct_backend/firebase.py
Normal file
16
ayct_backend/firebase.py
Normal file
@ -0,0 +1,16 @@
|
||||
import os
|
||||
import google.oauth2.id_token
|
||||
import google.auth.transport.requests
|
||||
|
||||
HTTP_REQUEST = google.auth.transport.requests.Request()
|
||||
AUDIENCE = os.environ.get('GOOGLE_CLOUD_PROJECT')
|
||||
|
||||
def verify_token(headers):
|
||||
id_token = headers['Authorization'].split(' ').pop()
|
||||
|
||||
try:
|
||||
claims = google.oauth2.id_token.verify_firebase_token(id_token, HTTP_REQUEST, audience=AUDIENCE)
|
||||
except:
|
||||
return None
|
||||
|
||||
return claims
|
32
ayct_backend/models.py
Normal file
32
ayct_backend/models.py
Normal file
@ -0,0 +1,32 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
class TwitterAccount(db.Model):
|
||||
__tablename__ = 'twitter_account'
|
||||
|
||||
id = db.Column(db.String(36), primary_key=True)
|
||||
user_id = db.Column(db.String(64), nullable=False)
|
||||
twitter_account_id = db.Column(db.String(32), nullable=False)
|
||||
username = db.Column(db.String(16), nullable=False)
|
||||
access_token = db.Column(db.String(256), nullable=False)
|
||||
access_token_secret = db.Column(db.String(256), nullable=False)
|
||||
|
||||
class TwitterCampaign(db.Model):
|
||||
__tablename__ = 'twitter_campaign'
|
||||
|
||||
id = db.Column(db.String(36), primary_key=True)
|
||||
user_id = db.Column(db.String(64), nullable=False)
|
||||
campaign_name = db.Column(db.String(64), nullable=False)
|
||||
twitter_account_id = db.Column(db.String(32), nullable=False)
|
||||
user_input = db.Column(db.String(100), nullable=False)
|
||||
posts = db.relationship('TwitterPost', backref='campaign', lazy=True)
|
||||
|
||||
class TwitterPost(db.Model):
|
||||
__tablename__ = 'twitter_post'
|
||||
|
||||
id = db.Column(db.String(36), primary_key=True)
|
||||
campaign_id = db.Column(db.String(36), db.ForeignKey(TwitterCampaign.id), nullable=False)
|
||||
user_id = db.Column(db.String(64), nullable=False)
|
||||
post_content = db.Column(db.String(300), nullable=False)
|
||||
twitter_post_id = db.Column(db.String(32), unique=True, nullable=False)
|
103
ayct_backend/twitter/__init__.py
Normal file
103
ayct_backend/twitter/__init__.py
Normal file
@ -0,0 +1,103 @@
|
||||
from uuid import uuid4
|
||||
from requests_oauthlib import OAuth1Session
|
||||
from flask import Blueprint, Response, current_app, request, jsonify
|
||||
from ayct_backend.models import *
|
||||
from ayct_backend.firebase import verify_token
|
||||
|
||||
twitter = Blueprint('twitter', __name__)
|
||||
|
||||
@twitter.route('/account', methods=['GET'])
|
||||
def get_twitter_accounts():
|
||||
decoded_token = verify_token(request.headers)
|
||||
if not decoded_token:
|
||||
return "Not authorised!", 401
|
||||
|
||||
user_id = decoded_token['sub']
|
||||
|
||||
twitter_accounts = TwitterAccount.query.filter_by(user_id=user_id).all()
|
||||
|
||||
accounts = []
|
||||
|
||||
for account in twitter_accounts:
|
||||
account_data = {}
|
||||
account_data['twitter_account_id'] = account.twitter_account_id
|
||||
account_data['username'] = account.username
|
||||
accounts.append(account_data)
|
||||
|
||||
return jsonify({
|
||||
"twitter_accounts": accounts
|
||||
}), 200
|
||||
|
||||
@twitter.route('/account', methods=['POST'])
|
||||
def add_twitter_account():
|
||||
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 'veryfier' not in request_json or 'oauth_token' not in request_json or 'oauth_token_secret' not in request_json:
|
||||
return "Invalid request!", 400
|
||||
|
||||
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
|
||||
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
|
||||
|
||||
access_token_url = "https://api.twitter.com/oauth/access_token"
|
||||
oauth = OAuth1Session(
|
||||
consumer_key,
|
||||
client_secret = consumer_secret,
|
||||
resource_owner_key = request_json['oauth_token'],
|
||||
resource_owner_secret = request_json['oauth_token_secret'],
|
||||
verifier = request_json['veryfier'],
|
||||
)
|
||||
oauth_tokens = oauth.fetch_access_token(access_token_url)
|
||||
|
||||
twitter_account = TwitterAccount.query.filter_by(user_id=user_id, twitter_account_id=oauth_tokens['user_id']).first()
|
||||
|
||||
if twitter_account:
|
||||
return "Account already exists!", 409
|
||||
|
||||
twitter_account = TwitterAccount(
|
||||
id = str(uuid4()),
|
||||
user_id = user_id,
|
||||
twitter_account_id = oauth_tokens['user_id'],
|
||||
username = oauth_tokens['screen_name'],
|
||||
access_token = oauth_tokens['oauth_token'],
|
||||
access_token_secret = oauth_tokens['oauth_token_secret']
|
||||
)
|
||||
|
||||
db.session.add(twitter_account)
|
||||
db.session.commit()
|
||||
|
||||
return "Twitter account succesfully added.", 201
|
||||
|
||||
@twitter.route('/account', methods=['DELETE'])
|
||||
def delete_twitter_account():
|
||||
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 'twitter_account_id' not in request_json:
|
||||
return "Invalid request!", 400
|
||||
|
||||
twitter_account = TwitterAccount.query.filter_by(user_id=user_id, twitter_account_id=request_json['twitter_account_id']).first()
|
||||
|
||||
if not twitter_account:
|
||||
return "Account not found!", 404
|
||||
|
||||
db.session.delete(twitter_account)
|
||||
db.session.commit()
|
||||
|
||||
return "Twitter account succesfully deleted.", 200
|
@ -1,2 +1,7 @@
|
||||
Flask==2.0.2
|
||||
Flask-RESTful==0.3.9
|
||||
flask_restful==0.3.9
|
||||
flask_sqlalchemy==2.5.1
|
||||
requests_oauthlib==1.3.0
|
||||
psycopg2==2.9.3
|
||||
pytest==6.2.5
|
||||
google-api-python-client==2.36.0
|
||||
|
5
setup.cfg
Normal file
5
setup.cfg
Normal file
@ -0,0 +1,5 @@
|
||||
[bdist_wheel]
|
||||
universal = True
|
||||
|
||||
[tool:pytest]
|
||||
testpaths = tests
|
18
setup.py
Normal file
18
setup.py
Normal file
@ -0,0 +1,18 @@
|
||||
from setuptools import find_packages
|
||||
from setuptools import setup
|
||||
|
||||
|
||||
setup(
|
||||
name="ayct_backend",
|
||||
version="1.0.0",
|
||||
url="https://git.wmi.amu.edu.pl/s434708/pbr-ayct-backend",
|
||||
license="BSD",
|
||||
maintainer="RŁP",
|
||||
maintainer_email="random@mail.com",
|
||||
description="Backend for AYCT",
|
||||
packages=find_packages(),
|
||||
include_package_data=True,
|
||||
zip_safe=False,
|
||||
install_requires=["flask"],
|
||||
extras_require={"test": ["pytest"]},
|
||||
)
|
@ -1,3 +1,4 @@
|
||||
git clone https://git.wmi.amu.edu.pl/s415366/pbr-ayct-core
|
||||
cd pbr-ayct-core
|
||||
pip3 install setuptools
|
||||
python3 -m pip install --upgrade build
|
||||
|
19
tests/test_ayct_backend.py
Normal file
19
tests/test_ayct_backend.py
Normal file
@ -0,0 +1,19 @@
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
import pytest
|
||||
from ayct_backend import create_app
|
||||
|
||||
@pytest.fixture
|
||||
def app():
|
||||
app = create_app()
|
||||
|
||||
yield app
|
||||
|
||||
@pytest.fixture
|
||||
def client(app):
|
||||
yield app.test_client()
|
||||
|
||||
def test_first_endpoint(client):
|
||||
rv = client.get("/hello")
|
||||
assert b'All You Can Tweet' in rv.data
|
Loading…
Reference in New Issue
Block a user