diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/dictionaries/da2001.xml b/.idea/dictionaries/da2001.xml new file mode 100644 index 0000000..5198b51 --- /dev/null +++ b/.idea/dictionaries/da2001.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/epam_diplom.iml b/.idea/epam_diplom.iml new file mode 100644 index 0000000..0ba5c28 --- /dev/null +++ b/.idea/epam_diplom.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..52b9fb1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d3c9a2f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/__pycache__/flask_app.cpython-38.pyc b/__pycache__/flask_app.cpython-38.pyc new file mode 100644 index 0000000..61db144 Binary files /dev/null and b/__pycache__/flask_app.cpython-38.pyc differ diff --git a/check_date.py b/check_date.py new file mode 100644 index 0000000..1963912 --- /dev/null +++ b/check_date.py @@ -0,0 +1,27 @@ +from datetime import datetime, date + + +def check_date(input_date): + try: + datetime.strptime(input_date, '%Y-%m-%d').date() + return True + # valid_date = datetime.strptime(input_date, '%Y-%m-%d').date() + # if valid_date < date.today(): + # return True + # else: + # print('Введенная дата еще не наступила') + # return False + except ValueError: + return False + + +while True: + input_day = input('Введите дату в формате ГГГГ-ММ-ДД): ') + if check_date(input_day): + print('Введенная дата корректна') + break + else: + print('Введенная дата некорректна') + continue + + diff --git a/flask_app.py b/flask_app.py new file mode 100644 index 0000000..5e5b1de --- /dev/null +++ b/flask_app.py @@ -0,0 +1,27 @@ +from flask import Flask, render_template + +app = Flask(__name__) + + +@app.route('/') +@app.route('/index') +def index(): + return '

Hello, World!

' + + +@app.route('/sum//') +def summa(a, b): + return str(a + b) + + +""" +Типы динамических переменных +string принимает любые строки (значение по умолчанию) +int принимает целые числа +float принимает числа с плавающей точкой +path принимает полный путь включая слэши и завершающий слэш +uuid принимает строки uuid (символьные id) +""" + +if __name__ == "__main__": + app.run(host='127.0.0.1', port=8181) diff --git a/flask_sample/app.py b/flask_sample/app.py new file mode 100644 index 0000000..5b06491 --- /dev/null +++ b/flask_sample/app.py @@ -0,0 +1,80 @@ +import json +from flask import Flask, render_template, request +from datetime import datetime, date + +import requests + +url = 'https://covidtrackerapi.bsg.ox.ac.uk/api/v2/stringency/actions/' +''' +01. Belarus - BLR +02. China - CHN +03. France - FRA +04. Germany - DEU +05. India - IND +06. Israel - ISR +07. Russia - RUS +08. Serbia - SRB +09. Spain - ESP +10. Turkey - TUR +''' +countries = ['blr', 'chn', 'fra', 'deu', 'ind', 'isr', 'rus', 'srb', 'esp', 'tur'] +tmp_output = {} + + +def check_date(input_date): + """ Check date format from input string. + + Args: input_date - (str). + Return: Bool - True if date correct. """ + try: + # valid_date = datetime.strptime(input_date, '%Y-%m-%d').date() + datetime.strptime(input_date, '%Y-%m-%d').date() + return True + except ValueError: + return False + + +def request_one_country(api, cntr, day): + """ Request from api-url. + + Args: api - (str) - api url, + cntr - (str) country alpha-3 code, + day - (int) YYYY-MM-DD. + Return: Response - (dict). """ + r_out = requests.get(f'{api}{cntr}/{day}') + return r_out.json() + +###################################################### + + +app = Flask(__name__) + + +@app.route('/', methods=['post', 'get']) +def index(): + if request.method == 'POST': + input_date = request.form.get('input_date') # запрос к данным формы + if check_date(input_date): + correct_date = datetime.strptime(input_date, '%Y-%m-%d').date() + if correct_date >= date.today(): + return render_template('index.html', message='Input date has not arrived yet') + + for country in countries: + out = request_one_country(url, country, correct_date) + tmp_output.setdefault(country, out.get('stringencyData')) + + output = {input_date: tmp_output} + with open('../temp_out.json', 'w') as f: + json.dump(output, f, sort_keys=True, indent=2) + + return render_template('index.html') + elif not input_date: + return render_template('index.html', message='Please input date') + else: + return render_template('index.html', message='Incorrect input') + else: + return render_template('index.html') + + +if __name__ == "__main__": + app.run(host='127.0.0.1', port=8181, debug=True) diff --git a/flask_sample/static/img/covid19.png b/flask_sample/static/img/covid19.png new file mode 100644 index 0000000..98b23e3 Binary files /dev/null and b/flask_sample/static/img/covid19.png differ diff --git a/flask_sample/static/img/logo.png b/flask_sample/static/img/logo.png new file mode 100644 index 0000000..a4592d8 Binary files /dev/null and b/flask_sample/static/img/logo.png differ diff --git a/flask_sample/static/style.css b/flask_sample/static/style.css new file mode 100644 index 0000000..ca2c520 --- /dev/null +++ b/flask_sample/static/style.css @@ -0,0 +1,3 @@ +body { + color: blue; +} \ No newline at end of file diff --git a/flask_sample/templates/index.html b/flask_sample/templates/index.html new file mode 100644 index 0000000..8215377 --- /dev/null +++ b/flask_sample/templates/index.html @@ -0,0 +1,30 @@ + + + + + COVID-19 Some statistics + + + +

Some statistics

+ Img + + +
+

+ + + +

+ + + +
+ {{ message }} + + + + + + diff --git a/flask_sql.py b/flask_sql.py new file mode 100644 index 0000000..a8e87a9 --- /dev/null +++ b/flask_sql.py @@ -0,0 +1,25 @@ +from datetime import datetime + +from flask import Flask +from flask_sqlalchemy import SQLAlchemy + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' +''' +postgresql://user:password@localhost/mydatabase +mysql://user:password@localhost/mydatabase +oracle://user:password@127.0.0.1:1521/mydatabase +''' + +db = SQLAlchemy(app) + + +class Users(db.Model): + id = db.Column(db.Integer, primary_key=True) + email = db.Column(db.String(50), unique=True) + psw = db.Column(db.String(500), nullable=True) + date = db.Column(db.DateTime, default=datetime.utcnow) + + +if __name__ == "__main__": + app.run(debug=True) diff --git a/get_data_sample.py b/get_data_sample.py new file mode 100644 index 0000000..7171964 --- /dev/null +++ b/get_data_sample.py @@ -0,0 +1,135 @@ +import sys, getopt +import argparse +import datetime +import re +import json +import os +import requests +import MySQLdb #need sudo apt-get install python-mysqldb (for debian based distros), yum install MySQL-python (for rpm-based), or dnf install python-mysql (for modern fedora distro) in command line to download + +start_date = '2021-01-1' +end_date = '2021-06-01' +url = 'https://covidtrackerapi.bsg.ox.ac.uk/api/v2/stringency/date-range/' + +try: + os.environ["DB_HOST"] +except KeyError: + print ("Please set the environment variable DB_HOST") + sys.exit(1) + +try: + os.environ["DB_USER"] +except KeyError: + print ("Please set the environment variable DB_USER") + sys.exit(1) + +try: + os.environ["DB_PASS"] +except KeyError: + print ("Please set the environment variable DB_PASS") + sys.exit(1) + +try: + os.environ["DB_NAME"] +except KeyError: + print ("Please set the environment variable DB_NAME") + sys.exit(1) + +db_host = os.environ.get('DB_HOST') + +print("DBHOST IS ", os.environ.get('DB_HOST')) + +db_user = os.environ.get('DB_USER') +db_pass = os.environ.get('DB_PASS') +db_name = os.environ.get('DB_NAME') + +query_insert = """INSERT INTO covid (date_value,country_code,confirmed,deaths,stringency_actual,stringency) VALUES (%s,%s,%s,%s,%s,%s)""" +query_select = """SELECT date_value,country_code,confirmed,deaths,stringency_actual,stringency FROM covid""" +query_create = """CREATE TABLE IF NOT EXISTS covid ( + id INT AUTO_INCREMENT PRIMARY KEY, + date_value VARCHAR(255) NOT NULL, + country_code VARCHAR(255) NOT NULL, + confirmed INT, + deaths INT, + stringency_actual FLOAT, + stringency FLOAT)""" +qurey_unique_index = """CREATE UNIQUE INDEX ix_uq ON covid (date_value, country_code)""" +query_update = """INSERT IGNORE INTO covid (date_value,country_code,confirmed,deaths,stringency_actual,stringency) VALUES (%s,%s,%s,%s,%s,%s)""" + +# Initialising database connection +try: + db = MySQLdb.connect(host = db_host, # host, usually localhost + user = db_user, # username + passwd = db_pass, # password + db = db_name) # of the data base +except ValueError: + raise ValueError("No datatbase connect") + +def create_table(url, start, end): + print ("Creating table \"covid\"") + cur = db.cursor() + try: + cur.execute(query_create) + except ValueError: + raise ValueError("Something go wrong while creating table") + print("Getting data from ", url + str(start) + '/' + str(end)) + r = requests.get(url + str(start) + '/' + str(end)) + data = r.json() + print("Opening database") + cur = db.cursor() + print("Parsing data and inserting it to database") + for date in data["data"]: + for country in data["data"][date]: + try: + cur.execute(query_insert, (data["data"][date][country]["date_value"], data["data"][date][country]["country_code"], data["data"][date][country]["confirmed"], data["data"][date][country]["deaths"], data["data"][date][country]["stringency_actual"],data["data"][date][country]['stringency'])) + except ValueError: + raise ValueError("Something go wrong while inserting new data") + print("Creating unique indexes for date_value and country_code fields") + try: + cur.execute(qurey_unique_index) + except ValueError: + raise ValueError("Something go wrong while creating indexes") + print("Commititng data") + db.commit() + print("Closing database") + cur.close() + return("DB created and data has been inserted from: " + start_date + " to: " + end_date ) + +def update_data(url, start, end): +# Getting data from API and inserting it to database + print("Getting data from ", url + str(start) + '/' + str(end)) + r = requests.get(url + str(start) + '/' + str(end)) + data = r.json() + print("Opening database") + cur = db.cursor() + print("Parsing data and inserting it to database") + for date in data["data"]: + for country in data["data"][date]: + try: + cur.execute(query_update, (data["data"][date][country]["date_value"], data["data"][date][country]["country_code"], data["data"][date][country]["confirmed"], data["data"][date][country]["deaths"], data["data"][date][country]["stringency_actual"],data["data"][date][country]['stringency'])) + except ValueError: + raise ValueError("Something go wrong while inserting new data") + print("Commititng data") + db.commit() + print("Closing database") + cur.close() + return("DB has been update to: " + end_date ) + +def select_data(): +# Selecting data drom database + print("Opening database") + cur = db.cursor() + print("Selecting data from database") + try: + cur.execute(query_select) + except ValueError: + print("Something going wrong. Select didn't working. I'll check if table exist and if no I'll fix it") + create_table(url, start_date, end_date) + raise ValueError("Cannot get data from DB") + row_headers=[h[0] for h in cur.description] + results = cur.fetchall() + j_data=[] + for result in results: + j_data.append(dict(zip(row_headers, result))) + cur.close() + return j_data \ No newline at end of file diff --git a/probe.py b/probe.py new file mode 100644 index 0000000..e69de29 diff --git a/temp_out.json b/temp_out.json new file mode 100644 index 0000000..78cc3d1 --- /dev/null +++ b/temp_out.json @@ -0,0 +1,64 @@ +{ + "2022-03-08": { + "blr": { + "confirmed": 932949, + "country_code": "BLR", + "date_value": "2022-03-08", + "deaths": 6594, + "stringency": 23.15, + "stringency_actual": 23.15 + }, + "chn": { + "msg": "Data unavailable" + }, + "deu": { + "confirmed": 16326087, + "country_code": "DEU", + "date_value": "2022-03-08", + "deaths": 124769, + "stringency": 80.56, + "stringency_actual": 80.56 + }, + "esp": { + "confirmed": 11159574, + "country_code": "ESP", + "date_value": "2022-03-08", + "deaths": 100859, + "stringency": 33.8, + "stringency_actual": 33.8 + }, + "fra": { + "confirmed": 22530160, + "country_code": "FRA", + "date_value": "2022-03-08", + "deaths": 136539, + "stringency": 69.44, + "stringency_actual": 69.44 + }, + "ind": { + "confirmed": 42975883, + "country_code": "IND", + "date_value": "2022-03-08", + "deaths": 515355, + "stringency": 71.76, + "stringency_actual": 71.76 + }, + "isr": { + "confirmed": 3688096, + "country_code": "ISR", + "date_value": "2022-03-08", + "deaths": 10322, + "stringency": 21.3, + "stringency_actual": 21.3 + }, + "rus": { + "msg": "Data unavailable" + }, + "srb": { + "msg": "Data unavailable" + }, + "tur": { + "msg": "Data unavailable" + } + } +} \ No newline at end of file