-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.py
More file actions
58 lines (41 loc) · 1.82 KB
/
Copy pathhistory.py
File metadata and controls
58 lines (41 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pandas as pd
import matplotlib.pyplot as plt
import requests
import json
from datetime import datetime, timedelta
"""
This module contains a make_graph function that creates and saves a graph based on data received from a user request.
"""
def __draw_image(data: pd.DataFrame, message: str, cur1_name: str, cur2_name: str, user: int):
plt.title(message.replace('/history ', ''))
plt.xlabel('Date')
plt.ylabel(f'Ratio {cur1_name}/{cur2_name}')
plt.plot(data['date'], data['price'], color='Green')
plt.savefig(f'resources/graphs/{user}.jpg')
plt.close()
def make_graph(message: str, user: int):
data = message.split()
try:
__check_data(data)
count_days, cur1_name, cur2_name = __parse_command(data)
data = __load_data(count_days, cur1_name, cur2_name)
__draw_image(data, message, cur1_name, cur2_name, user)
return True
except Exception as e:
print(e)
def __parse_command(data: list):
count_days = int(data[-2])
curs = data[1].split('/')
return count_days, curs[0], curs[1]
def __check_data(data: list):
if not (len(data) == 5 and data[-1] == 'days' and data[-3] == 'for'):
raise Exception('Incorrect data')
def __load_data(count_days: int, cur1: str, cur2: str) -> pd.DataFrame:
final_date = datetime.today().strftime('%Y-%m-%d')
start_date = (datetime.today() - timedelta(days=count_days)).strftime('%Y-%m-%d')
link = \
f'https://api.exchangeratesapi.io/history?start_at={start_date}&end_at={final_date}&base={cur1}&symbols={cur2}'
content = requests.get(link).text
rates = json.loads(content)['rates']
df = pd.DataFrame({'date': [date for date in rates], 'price': [rates[date][cur2] for date in rates]})
return df.sort_values(by='date')