-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.py
133 lines (100 loc) · 3.47 KB
/
response.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import wikipedia
from wikipedia import WikipediaException
import time
import json, requests
from datetime import datetime
from constans import *
class Response:
""" User response class"""
def __init__(self):
pass
def time_now(self)-> str:
"""
# Current time now
:return: time_n
"""
time_n = time.strftime("%H:%M:%S")
return time_n
def day_today(self)->str:
"""
Current day today
:return:
"""
day_n = time.strftime("%D")
return day_n
def wikipedia(self, text:str)->list:
"""
func get wikipedia tittles
:param text: str user input
:return: list wikipedia titles
"""
try:
return wikipedia.search(text)
except WikipediaException:
print("You need something to write")
except Exception as Err:
print(f"Something wrong!\nCheck internet connection\n{Err}")
def wiki_text_answer(self, text:str)->str:
"""
func to get response wikipedia
:param text: str request wikipedia
:return: str response wikidedia
"""
print(text)
try:
response = wikipedia.page(text).content
except:
response = f"Not found Page:{text}, try another request"
return response
def currency_convector(self,f_rates):
def receive_data():
"""
Receive_data func check
:return: dict(collected_rates) or False
"""
try:
# receive data
response_data = requests.get(REQUEST_URL, headers={"UserAgent": "XY", "apikey": API_KEY})
collected_rates = json.loads(response_data.text)
if collected_rates.get("success") == True: return collected_rates
except:
return False
def reload_rates() -> dict:
"""
Func Reload_data checks date from file
if date!= date.now try upload
:return: dict (rates), str(date)
"""
time_now = datetime.now().strftime("%Y-%m-%d")
# open rates from file
with open(os.path.join(CURRENT_PATCH_JASON, "data_rates.json")) as f:
rates_from_file = json.load(f)
rates = rates_from_file
# check from file current date
if rates_from_file.get("date") != time_now:
print("Uploading Data")
rates = receive_data()
# cannot receive data
if rates == False:
print("Cannot update")
rates = rates_from_file
else:
print("Uploading successful")
# create file
with open(os.path.join(CURRENT_PATCH_JASON, "data_rates.json"), "w") as f:
json.dump(rates, f, indent=4)
pass
return rates
all_rates = reload_rates()
# get data rates
date=all_rates.get("date")
# create dict favorite rates
dict_favorite_rates = dict.fromkeys(f_rates, 0)
for next_key in f_rates:
dict_favorite_rates[next_key] = all_rates.get("rates").get(next_key)
# get all rates
full_rates = all_rates.get("rates")
return dict_favorite_rates, date, full_rates
# for tests functions !
#responses = Response()
#responses.currency_convector()