master
Долгий Артём 2022-06-15 10:55:48 +03:00
parent 4411bc1601
commit aa04b09e7f
19 changed files with 434 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -0,0 +1,3 @@
<component name="ProjectDictionaryState">
<dictionary name="da2001" />
</component>

10
.idea/epam_diplom.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.9 (epam_diplom)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (epam_diplom)" project-jdk-type="Python SDK" />
<component name="PyCharmProfessionalAdvertiser">
<option name="shown" value="true" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/epam_diplom.iml" filepath="$PROJECT_DIR$/.idea/epam_diplom.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

Binary file not shown.

27
check_date.py Normal file
View File

@ -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

27
flask_app.py Normal file
View File

@ -0,0 +1,27 @@
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/index')
def index():
return '<h1>Hello, World!</h1>'
@app.route('/sum/<int:a>/<int:b>')
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)

80
flask_sample/app.py Normal file
View File

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -0,0 +1,3 @@
body {
color: blue;
}

View File

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>COVID-19 Some statistics</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<p>Some statistics</p>
<img src="{{ url_for('static', filename='img/covid19.png') }}"
alt="Img" width="300">
<!-- alt="Img" width="300" height="300">-->
<form action="" method="post">
<p>
<label for="input_date">Input date (YYYY-MM-DD)</label>
<input type="text" name="input_date">
<input type="submit" value="Show">
</p>
<!-- <p>-->
<!-- <input type="submit" value="Show">-->
<!-- </p>-->
</form>
{{ message }}
</body>
</html>
<!--<script>-->
<!-- alert('Эй!')-->
<!--</script>-->

25
flask_sql.py Normal file
View File

@ -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)

135
get_data_sample.py Normal file
View File

@ -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

0
probe.py Normal file
View File

64
temp_out.json Normal file
View File

@ -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"
}
}
}