Commit b8c6bf10 authored by Dilip Wijethunga's avatar Dilip Wijethunga

Merge branch 'IT19240466' into 'master'

api configuration update

See merge request !2
parents ed1b3f11 63f31090
venv/
.env
*.pyc
__pycache__/
instance/
.idea
.vscode
\ No newline at end of file
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
flask = "*"
[requires]
python_version = "3.9"
### How to set up and run
##### Create virtual environment
###### Windows
py -3 -m venv <name of environment>
###### Linux/MaxOS
python3 -m venv <name of environment>
##### Activate virtual environment
###### Windows
<name of environment>\Scripts\activate
###### Linux/MaxOS
. <name of environment>/bin/activate
##### Install required libraries
## How to set up and run
### Create virtual environment
##### Windows
py -3 -m venv venv
##### Linux/MaxOS
python3 -m venv venv
### Activate virtual environment
##### Windows
venv\Scripts\activate
##### Linux/MaxOS
. venv/bin/activate
### Install required libraries
pip3 install -r requirements.txt
##### Run app locally
### Run app locally
flask run
## Endpoints
### Ping Check
http://127.0.0.1:5000/
### Crypto Currency
##### Get Prediction
http://127.0.0.1:5000/crypto-currency/predict
##### Get Sentiment
http://127.0.0.1:5000/crypto-currency/sentiment
##### Get Prediction By Currency
http://127.0.0.1:5000/crypto-currency/predict/<name>
Ex: http://127.0.0.1:5000/crypto-currency/predict/BTC_USD
##### Get Prediction By Currency and Action
http://127.0.0.1:5000/crypto-currency/predict/<name>/<value>
Ex: http://127.0.0.1:5000/crypto-currency/predict/BTC_USD/price
### News Endpoints
##### News Create
http://127.0.0.1:5000/news/add
{
"title": "",
"description": "",
"author": ""
}
##### News Get All
http://127.0.0.1:5000/news/all
##### News Get By id
http://127.0.0.1:5000/news/<id>
Ex: http://127.0.0.1:5000/news/633b563a98178d25528e4ca5
##### News Update
http://127.0.0.1:5000/news/update
{
"_id":"",
"title": "",
"description": "",
"author": ""
}
##### News Delete
http://127.0.0.1:5000/news/delete/<id>
Ex: http://127.0.0.1:5000/news/delete/633b563a98178d25528e4ca5
### Currency Endpoints
##### Currency Create
http://127.0.0.1:5000/currency/add
{
"name": "",
"code": "",
"description": "",
"image": "https://example.com/example.jpg"
}
##### Currency Get All
http://127.0.0.1:5000/currency/all
##### Currency Get By id
http://127.0.0.1:5000/currency/<id>
Ex: http://127.0.0.1:5000/currency/633b563a98178d25528e4ca5
##### Currency Update
http://127.0.0.1:5000/currency/update
{
"_id":"",
"name": "",
"code": "",
"description": "",
"image": "https://example.com/example.jpg"
}
##### Currency Delete
http://127.0.0.1:5000/currency/delete/<id>
Ex: http://127.0.0.1:5000/currency/delete/633b563a98178d25528e4ca5
from flask import Flask, jsonify
from flask_cors import CORS
from bson.json_util import dumps
from bson.objectid import ObjectId
from datetime import datetime
from flask import Flask, jsonify, request
from flask_apscheduler import APScheduler
from flask_cors import CORS
from flask_pymongo import PyMongo
from model import schedule_model_training, is_training, CURRENCIES
from web_scrapping import get_sentiment
from web_scrapping import get_sentiment, get_sentiment_score
app = Flask(__name__)
cors = CORS(app, resources={r"/crypto-currency/*": {"origins": "*"}})
cors = CORS(app, resources={r"/*": {"origins": "*"}})
app.config['CORS_HEADERS'] = 'Content-Type'
scheduler = APScheduler()
# Mongo Config
app.config["MONGO_URI"] = 'mongodb+srv://CryptoForeteller:cryptoforeteller@cluster0.ssp1lwg.mongodb.net/?retryWrites=true&w=majority'
mongo = PyMongo(app)
schedule_model_training()
'''
schedule model re-training
......@@ -18,9 +26,9 @@ scheduler.add_job(id='Scheduled Task', func=schedule_model_training, trigger="in
scheduler.start()
@app.route('/crypto-currency', methods=['GET'])
@app.route('/', methods=['GET'])
def index():
return f"<div align='center'><h2>Crypto Currency Forecasting Sever is Active</h2></div>"
return jsonify({'server': 'active', 'message': 'Crypto Currency Forecasting Sever is Active'})
@app.route("/crypto-currency/predict", methods=['GET'])
......@@ -54,11 +62,216 @@ def sentiment():
response = jsonify({
"code": 200,
"message": "Success",
"sentiment": get_sentiment()
"sentiment": get_sentiment(),
"score": get_sentiment_score()
})
response.headers.add('Access-Control-Allow-Origin', '*')
return response, 200
@app.route('/crypto-currency/predict/<name>')
def predict_currency(name=None):
if is_training():
response = jsonify({
"message": "all forecasting models are training now!",
"code": 100
})
else:
data = dict()
if CURRENCIES[name]["enable"] and CURRENCIES[name]["available_data"]:
data = {
"price": CURRENCIES[name]["price"],
"volume": CURRENCIES[name]["volume"],
"market_cap": CURRENCIES[name]["market_cap"]
}
response = jsonify({
"code": 200,
"message": "Success",
"data": data
})
response.headers.add('Access-Control-Allow-Origin', '*')
return response, 200
@app.route('/crypto-currency/predict/<name>/<value>')
def predict_currency_action(name=None, value=None):
if is_training():
response = jsonify({
"message": "all forecasting models are training now!",
"code": 100
})
else:
data = dict()
if CURRENCIES[name]["enable"] and CURRENCIES[name]["available_data"]:
data = {
"exceeded": round(CURRENCIES[name][value]["exceeded"], 2),
"score": round(CURRENCIES[name][value]["score"], 5),
"today": round(CURRENCIES[name][value]["today"], 2),
"tomorrow": round(CURRENCIES[name][value]["tomorrow"], 2)
}
response = jsonify({
"code": 200,
"message": "Success",
"data": data
})
response.headers.add('Access-Control-Allow-Origin', '*')
return response, 200
# News Endpoints
@app.route('/news/add', methods=['POST'])
def add_news():
_json = request.json
_title = _json['title']
_description = _json['description']
_author = _json['author']
_image = _json['image']
_date = datetime.now()
# validate the received values
if _title and _description and _author and _image and _date and request.method == 'POST':
# save details
response = mongo.db.news.insert_one(
{'title': _title, 'description': _description, 'author': _author, 'image': _image, 'date': _date})
resp = jsonify('News Create Successfully!')
resp.status_code = 200
return resp
else:
return not_found()
@app.route('/news/all')
def all_news():
news = mongo.db.news.find()
resp = dumps(news, indent=2)
return resp
@app.route('/news/<id>')
def get_news_by_id(id):
news = mongo.db.news.find_one({'_id': ObjectId(id)})
resp = dumps(news, indent=2)
return resp
@app.route('/news/update', methods=['PUT'])
def update_news():
_json = request.json
_id = _json['_id']
_title = _json['title']
_description = _json['description']
_author = _json['author']
_image = _json['image']
_date = datetime.now()
# validate the received values
if _title and _description and _author and _image and _date and _id and request.method == 'PUT':
# save edits
news = mongo.db.news.update_one(
{'_id': ObjectId(_id['$oid']) if '$oid' in _id else ObjectId(_id)},
{'$set': {'title': _title, 'description': _description, 'author': _author, 'image': _image, 'date': _date}}
)
resp = jsonify('News Update Successfully!')
resp.status_code = 200
return resp
else:
return not_found()
@app.route('/news/delete/<id>', methods=['DELETE'])
def delete_news(id):
mongo.db.news.delete_one({'_id': ObjectId(id)})
resp = jsonify('News Deleted successfully!')
resp.status_code = 200
return resp
# Currency Data Endpoints
@app.route('/currency/add', methods=['POST'])
def add_currency():
_json = request.json
_name = _json['name']
_code = _json['code']
_description = _json['description']
_image = _json['image']
_date = datetime.now()
# validate the received values
if _name and _code and _description and _image and _date and request.method == 'POST':
# save details
response = mongo.db.currency.insert_one(
{'name': _name, 'code': _code, 'description': _description, 'image': _image, 'date': _date})
resp = jsonify('Currency Create Successfully!')
resp.status_code = 200
return resp
else:
return not_found()
@app.route('/currency/all')
def all_currency():
currency = mongo.db.currency.find()
resp = dumps(currency, indent=2)
return resp
@app.route('/currency/<id>')
def get_currency_by_id(id):
currency = mongo.db.currency.find_one({'_id': ObjectId(id)})
resp = dumps(currency, indent=2)
return resp
@app.route('/currency/update', methods=['PUT'])
def update_currency():
_json = request.json
_id = _json['_id']
_name = _json['name']
_code = _json['code']
_description = _json['description']
_image = _json['image']
_date = datetime.now()
# validate the received values
if _name and _code and _description and _image and _date and request.method == 'PUT':
# save edits
currency = mongo.db.currency.update_one(
{'_id': ObjectId(_id['$oid']) if '$oid' in _id else ObjectId(_id)},
{'$set': {'name': _name, 'code': _code, 'description': _description, 'image': _image, 'date': _date}}
)
resp = jsonify('Currency Update Successfully!')
resp.status_code = 200
return resp
else:
return not_found()
@app.route('/currency/delete/<id>', methods=['DELETE'])
def delete_currency(id):
mongo.db.currency.delete_one({'_id': ObjectId(id)})
resp = jsonify('Currency Deleted successfully!')
resp.status_code = 200
return resp
@app.errorhandler(404)
def not_found(error=None):
message = {
'status': 404,
'message': 'Not Found: ' + request.url,
}
resp = jsonify(message)
resp.status_code = 404
return resp
if __name__ == "__main__":
app.run()
Flask~=2.1.1
numpy~=1.23.3
pandas~=1.4.2
gunicorn~=20.1.0
scikit-learn==1.0
flask_cors
pip~=20.3.2
wheel~=0.36.2
cryptography~=3.3.1
lxml~=4.6.2
pytz~=2020.4
MarkupSafe~=2.0.1
Werkzeug~=2.1.1
Jinja2~=3.0.1
click~=8.1.2
itsdangerous~=2.1.2
setuptools~=49.2.1
nose~=1.3.7
cffi~=1.14.4
numba~=0.56.2
scipy~=1.5.4
joblib~=0.17.0
python-dateutil~=2.8.1
threadpoolctl~=2.1.0
Pillow~=9.2.0
six~=1.15.0
tornado~=6.1
decorator~=5.0.9
zipp~=3.7.0
certifi~=2020.12.5
flask_apscheduler
defusedxml~=0.7.1
ipython~=7.25.0
pyzmq~=22.1.0
pexpect~=4.8.0
parso~=0.8.2
jedi~=0.18.0
attrs~=20.3.0
llvmlite~=0.39.1
Pygments~=2.9.0
ipykernel~=5.5.5
nbformat~=5.1.3
traitlets~=5.0.5
testpath~=0.5.0
packaging~=21.3
backcall~=0.2.0
pickleshare~=0.7.5
wcwidth~=0.2.5
ptyprocess~=0.7.0
tzlocal~=4.2
jsonschema~=3.2.0
pycparser~=2.20
pyrsistent~=0.17.3
idna~=2.10
APScheduler~=3.9.1
entrypoints~=0.3
statsmodels~=0.13.2
APScheduler==3.9.1
certifi==2020.12.5
click==8.1.3
cloudpickle==2.2.0
colorama==0.4.5
dask==2022.9.2
Flask==2.1.3
Flask-APScheduler==1.12.4
Flask-Cors==3.0.10
fsspec==2022.8.2
importlib-metadata==4.12.0
itsdangerous==2.1.2
Jinja2==3.1.2
joblib==1.2.0
locket==1.0.0
MarkupSafe==2.1.1
nltk~=3.7
tqdm~=4.64.1
requests~=2.25.1
regex~=2022.9.13
pyparsing~=2.4.7
keras~=2.10.0
ipywidgets~=7.6.3
notebook~=6.4.0
patsy~=0.5.2
uritemplate~=3.0.1
google-api-python-client~=2.8.0
bs4==0.0.1
requests~=2.25.1
numpy==1.23.3
packaging==21.3
pandas==1.5.0
partd==1.3.0
patsy==0.5.2
pyparsing==3.0.9
python-dateutil==2.8.2
pytz==2022.2.1
pytz-deprecation-shim==0.1.0.post0
PyYAML==6.0
scikit-elm==0.21a0
scikit-learn==1.1.2
scipy==1.9.1
six==1.16.0
StandardScaler==0.5
statsmodels==0.13.2
threadpoolctl==3.1.0
toolz==0.12.0
tzdata==2022.4
tzlocal==4.2
Werkzeug==2.2.2
zipp==3.8.1
gunicorn
Flask-PyMongo
pymongo[srv]
\ No newline at end of file
python-3.9.13
\ No newline at end of file
{
"version": 2,
"builds": [
{
"src": "./api/index.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}
\ No newline at end of file
......@@ -8,6 +8,16 @@ from sentiment_analysis import predict
TAGS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'td', 'li', 'a']
SOURCES = ['https://cointelegraph.com/', 'https://news.bitcoin.com/']
sentiment = 'Not Available'
sentiment_score = 0
def get_sentiment_score():
return sentiment_score
def set_sentiment_score(_sentiment_score):
global sentiment_score
sentiment_score = _sentiment_score
def get_sentiment():
......@@ -62,8 +72,10 @@ def start_web_scrapping():
m[pred] += 1
if m[1] > m[0]:
set_sentiment('Positive')
set_sentiment_score(m[1])
else:
set_sentiment('Negative')
set_sentiment_score(m[1])
# only for testing
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment