-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
179 lines (144 loc) · 5.71 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from time import sleep
import speedtest
import datetime
import sys
# from DATA.storage import "objet"/"function"
import DATA.storage
import DATA.manage
# Constante défini pour plus de simplicité de gestion :
LOOP_MAX = 1
WAITING_TIME = 1
class Server():
def __init__(self) -> None:
self.speedtest_ = speedtest.Speedtest()
self.servers_dict = self.speedtest_.get_servers()
self.avaible_paris_server = False
# DATA SAVING = better execution
# {ID : DISTANCE}
self.distance_dict = {}
# {ID : DISTANCE}
self.orange_servers_dict = {}
# {ID : SPRONSOR}
self.paris_servers_dict = {}
# storage informations (ORANGE - distances - Paris)
def get_informations(self):
for value in self.servers_dict :
# go inside key 1 (access to values key 1)
for server in self.servers_dict[value] :
if not self.check_paris_servers(server):
# capture the distance with id
self.distance_dict[server['id']] = server['d']
if server["sponsor"] == "ORANGE" :
# save in a dict {ID : DISTANCE, ...}
self.orange_servers_dict[server["id"]] = server["d"]
def check_paris_servers(self, server):
if server['name'] == 'Paris':
self.paris_servers_dict[server["id"]] = server["d"]
return True
# There is actualy not Paris servers
else : return False
def check_orange_servers(self, in_Paris):
# in Paris we return any ORANGE servers
if in_Paris :
if len(self.orange_servers_dict) > 0 :
# return firts value
for i in self.orange_servers_dict.keys() :
return str(i)
else :
for i in self.paris_servers_dict.keys():
return str(i)
# else we return the closest to us
else : return self.compare_distances(self.orange_servers_dict)
def compare_distances(self, distances_dict):
# sorte servers according to distance (<)
if len(distances_dict) > 1 :
servers_distance_sorted = sorted(distances_dict.items(), key=lambda x: x[1])
# return list[(x, y), (x, y)]
return servers_distance_sorted[0][0]
# there is also one key
else :
return str(distances_dict.keys())
def choose_server(self):
self.get_informations()
# if there is a server in Paris, we choose it
if len(self.paris_servers_dict) > 0 :
return self.check_orange_servers(True)
# else we check if there is a ORANGE servers in France
elif len(self.orange_servers_dict) > 0:
return self.check_orange_servers(False)
# we take the server with the best distance
else :
return self.compare_distances(self.distance_dict)
# we take data : download and upload from the server that we have chosen
def ping_server(server, SPEEDTEST):
SPEEDTEST.get_servers(servers= [server])
download = SPEEDTEST.download()
upload = SPEEDTEST.upload()
return (download, upload)
def usual_checking():
"""
* return tuple(0, 0, (0, 0, 0, 0))
- upload (int)
- download (int)
- date (tuple) {years - month - day - hour}
"""
download_list = []
download = 0
upload_list = []
upload = 0
date_list = 0
time = 0
Best_Server = Server()
server = Best_Server.choose_server()
# repaet the ping as long as we want for a more precise data
while LOOP_MAX > time:
time += 1
download, upload = ping_server(server, Best_Server.speedtest_)
print(f" log -> {time} : recovery of the data...")
# we add the ping in a list to make the avarage
download_list.append(download)
upload_list.append(upload)
# last loop we take all informations ()
if LOOP_MAX == time:
date_list = get_info_date()
download = DATA.manage.set_avarage(download_list)
upload = DATA.manage.set_avarage(upload_list)
# interval of x second (default : 10s)
sleep(WAITING_TIME)
# we need to convert data
download = bits_to_megabits(int(download))
upload = bits_to_megabits(int(upload))
print(f"log -> usual_check() -> download : {download} -- upload : {upload} -- dates : {date_list}")
return download, upload, date_list
def get_info_date():
sys_time = datetime.datetime.now()
# récupère les données sur le temps
date = sys_time.strftime("%d")
years = sys_time.strftime("%Y")
month = sys_time.strftime("%m")
month = str_modifie_month(month)
hour = sys_time.strftime("%H")
return (years, month, date, hour)
def str_modifie_month(month):
if month == "01" : month = "january"
elif month == "02" : month = "february"
elif month == "03" : month = "march"
elif month == "04" : month = "april"
elif month == "05" : month = "may"
elif month == "06" : month = "june"
elif month == "07" : month = "july"
elif month == "08" : month = "august"
elif month == "09" : month = "september"
elif month == "10" : month = "october"
elif month == "11" : month = "november"
elif month == "12" : month = "december"
else : return 0
return month
def bits_to_megabits(bits):
megabits = bits // 1000000
return megabits
usual_data = usual_checking() # => (download, upload (years, month, day, hour))
# put in the data base
DATA.storage.get_save_json(usual_data)
# work with data to have result (best day, best part of the day)
DATA.manage.fetch_data(usual_data[2][2], usual_data[2][1])