-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsources.py
84 lines (67 loc) · 2.74 KB
/
sources.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
import re
from datetime import datetime
class CurrencyAzSource:
def is_alive(self):
return bool(requests.get('http://currency.az'))
def get_supported_currencies(self):
return 'AZN', 'USD', 'EUR', 'RUB'
def get_rate(self, currency):
if currency.lower() == 'azn':
return 1
request = requests.get('http://currency.az')
content = request.content.decode()
pattern = f'[a-z <">=]+1 {currency}[a-z /"<>=-]+(?P<rate>[0-9.]+) AZN'
match = re.search(pattern, content, re.IGNORECASE)
result = match.group('rate')
return float(result)
def validate_currency(self, curr):
if curr in self.get_supported_currencies():
return True, 'valid'
else:
return False, f'There isn\'t any currency type as {curr.upper()}. Try again'
class CentralBankSource:
def build_url(self):
now = datetime.now()
new_day = now.strftime('%d.%m.%Y')
return f'https://www.cbar.az/currencies/{new_day}.xml'
def get_rate(self, currency):
if currency.upper() == 'AZN':
return 1
pattern = r'<Valute Code="(?P<valute>{})">\n.+\n.+\n +<Value>(?P<value>[0-9]+\.[0-9]+)'.format(currency)
response = requests.get(self.build_url())
content = response.content.decode()
match = re.search(pattern, content, re.IGNORECASE)
return float(match.group('value'))
def get_all_rates(self):
pattern = r'<Valute Code="(?P<valute_code>[a-z]+)">\n +.+\n +<Name>(?P<name>[^<]+)</Name>\n +<Value>(?P<value>[0-9.]+)'
response = requests.get(self.build_url())
content = response.content.decode()
matches = re.findall(pattern, content, re.IGNORECASE)
result = []
for match in matches:
code, name, rate = match
rate = {
'code': code,
'name': name,
'rate': float(rate)
}
result.append(rate)
return result
def get_supported_currencies(self):
pattern = r'<Valute Code="(?P<valute>[a-z]+)">\n.+\n.+\n +<Value>(?P<value>[0-9]+\.[0-9]+)'
response = requests.get(self.build_url())
content = response.content.decode()
matches = re.findall(pattern, content, re.IGNORECASE)
valutes = ['AZN']
for match in matches:
valutes.append(match[0])
return valutes
def is_alive(self):
response = requests.get(self.build_url())
return bool(response)
def validate_currency(self, curr):
if curr.upper() in self.get_supported_currencies():
return True, 'valid'
else:
return False, f'There isn\'t any currency type as {curr.upper()}. Try again'