-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.py
More file actions
407 lines (289 loc) · 12.6 KB
/
Model.py
File metadata and controls
407 lines (289 loc) · 12.6 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
from requests import get
from bs4 import BeautifulSoup
from requests import post
import smtplib
import sqlite3
from sqlite3 import Error
import hashlib
import email.message
import pickle
def FetchData(): #scrape data and store in db
try:
url = 'https://www.worldometers.info/coronavirus/'
response = get(url)
if response.ok:
html_soup = BeautifulSoup(response.text, 'html.parser')
lastchecked=html_soup.find(id='page-top').findNext('div').text
table=html_soup.find(id='main_table_countries_today')
rows = table.find('tbody').find_all('tr')
conn = create_connection()
with conn:
for tr in rows:
col=tr.find_all('td')
countryname=col[1].text
if '\n' not in countryname:
deaths=col[4].text.replace(',','').strip()
confirmed=col[2].text.replace(',','').strip()
recovered=col[6].text.replace(',','').strip()
if deaths.isnumeric()==False:
deaths=None
if confirmed.isnumeric()==False:
confirmed=None
if recovered.isnumeric()==False:
recovered=None
#create_db(conn, (countryname,deaths,confirmed,recovered,lastchecked))
update_db(conn, (deaths, confirmed, recovered, lastchecked,countryname))
return(True)
else:
return False
except Exception as e:
return False
def create_connection():
db_file="pythonsqlite.db"
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return conn
def create_db(conn, project): #append data to db
sql = ''' INSERT INTO project(countryName,deaths,confirmed,recovered,lastchecked)
VALUES(?,?,?,?,?) '''
cur = conn.cursor()
cur.execute(sql, project)
def initdb(): #create table in db
sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS project (
id integer PRIMARY KEY,
countryName text NOT NULL,
deaths int,
confirmed int,
recovered int,
lastchecked text
); """
# create a database connection
conn = create_connection()
# create table
if conn is not None:
# create table
try:
c = conn.cursor()
c.execute(sql_create_projects_table)
except Error as e:
print(e)
else:
print("Error! cannot create the database connection.")
def update_db(conn, task): #update data in db
sql = ''' UPDATE project
SET deaths = ? ,
confirmed = ? ,
recovered = ? ,
lastchecked = ?
WHERE countryName = ?'''
cur = conn.cursor()
cur.execute(sql, task)
conn.commit()
def select_by_country(conn,order):
cur = conn.cursor()
cur.execute("SELECT * FROM project ORDER BY countryName {0}".format(order))
rows = cur.fetchall()
return rows
def select_by_deaths(conn,order):
cur = conn.cursor()
cur.execute("SELECT * FROM project ORDER BY deaths {0}".format(order))
rows = cur.fetchall()
return rows
def select_by_confirmed(conn,order):
cur = conn.cursor()
cur.execute("SELECT * FROM project ORDER BY confirmed {0}".format(order))
rows = cur.fetchall()
return rows
def select_by_recovered(conn,order):
cur = conn.cursor()
cur.execute("SELECT * FROM project ORDER BY recovered {0}".format(order))
rows = cur.fetchall()
return rows
def select_top5_deaths(conn):
cur = conn.cursor()
cur.execute("SELECT countryName,deaths FROM project ORDER BY deaths DESC LIMIT 5 OFFSET 1")
rows = cur.fetchall()
return rows
def select_top5_recovered(conn):
cur = conn.cursor()
cur.execute("SELECT countryName,recovered FROM project ORDER BY recovered DESC LIMIT 5 OFFSET 1")
rows = cur.fetchall()
return rows
def select_top5_confirmed(conn):
cur = conn.cursor()
cur.execute("SELECT countryName,confirmed FROM project ORDER BY confirmed DESC LIMIT 5 OFFSET 1")
rows = cur.fetchall()
return rows
#DESC ASC
################################################################################################
def posttoserver(name,email,msg,stars):
try:
url='https://gfarah.000webhostapp.com/esibpythonapp/script.php'
myobj = {'name':name,'emailaddress': email,'message':msg,'stars':stars}
x = post(url, data = myobj)
return x.text
except Exception as e:
return False
def getfromserver(username,password):
try:
url='https://gfarah.000webhostapp.com/esibpythonapp/script2.php'
myobj = {'username':username,'password': password}
x = post(url, data = myobj)
response=x.json()
return response
except Exception as e:
return False
def sendmail(receiver,sender,password):
body = """\
Hello,
Thank you for your message. We appreciate you sharing your thoughts.
This is an automated message sent from Python."""
msg = email.message.Message()
msg['Subject'] = 'ESIB 19/20 TP'
msg['From'] = sender
msg['To'] = receiver
msg.add_header('Content-Type', 'text/plain')
msg.set_payload(body)
smtpserver = smtplib.SMTP('smtp.gmail.com',587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(sender,password)
smtpserver.sendmail(sender,receiver,msg.as_string())
print('sent')
smtpserver.close()
################################################################################################
class Location:
country='Lebanon'
def __init__(self):
self.getLocation()
@classmethod
def getLocation(cls):
try:
url = 'https://whatismycountry.com/'
response = get(url)
if response.ok:
html_soup = BeautifulSoup(response.text, 'html.parser')
allH4=html_soup.find_all('h4')
#country=allH4[1].text
Location.country=allH4[1].text
if Location.country=='United States':
Location.country='United States of America'
return True
else:
return False
except Exception as e:
return False
################################################################################################
def FetchNumbers():
try:
url = 'https://en.wikipedia.org/wiki/List_of_emergency_telephone_numbers'
response = get(url)
if response.ok:
data={}
html_soup = BeautifulSoup(response.text, 'html.parser')
tables=html_soup.find_all('table')
for table in tables:
rows = table.find('tbody').find_all('tr')
for row in rows:
tds=row.find_all('td')
if len(tds)==5:
countryname=tds[0].text.strip().split('[')[0]
police=tds[1].text.strip().split('[')[0]
ambulance=tds[2].text.strip().split('[')[0]
fire=tds[3].text.strip().split('[')[0]
data[countryname]={'police':police,'ambulance':ambulance,'fire':fire}
if len(tds)==3:
countryname=tds[0].text.strip().split('[')[0]
police=tds[1].text.strip().split('[')[0]
ambulance=tds[1].text.strip().split('[')[0]
fire=tds[1].text.strip().split('[')[0]
data[countryname]={'police':police,'ambulance':ambulance,'fire':fire}
if len(tds)==4:
if tds[1].has_attr('colspan'):
countryname=tds[0].text.strip().split('[')[0]
police=tds[1].text.strip().split('[')[0]
ambulance=tds[1].text.strip().split('[')[0]
fire=tds[2].text.strip().split('[')[0]
data[countryname]={'police':police,'ambulance':ambulance,'fire':fire}
elif tds[2].has_attr('colspan'):
countryname=tds[0].text.strip().split('[')[0]
police=tds[1].text.strip().split('[')[0]
ambulance=tds[2].text.strip().split('[')[0]
fire=tds[2].text.strip().split('[')[0]
data[countryname]={'police':police,'ambulance':ambulance,'fire':fire}
else:#lithunia malaysia the bahamas
countryname=tds[0].text.strip().split('[')[0]
police=tds[1].text.strip().split('[')[0]
ambulance=tds[1].text.strip().split('[')[0]
fire=tds[1].text.strip().split('[')[0]
data[countryname]={'police':police,'ambulance':ambulance,'fire':fire}
open('phoneNumbers.txt', 'w').close()
with open('phoneNumbers.txt', 'wb') as f:
pickle.dump(data, f)
except Exception as e:
print(str(e))
def getPhoneNumbers():
with open('phoneNumbers.txt','rb') as f:
return pickle.load(f)
###################################################################################
def hashpasswordInit(password):
salt=b'Rz9\xbd\xda.\x99y\xf0\xb1E\x13\x15\x18Vf\xa5\xbf\xc4\xb0\x98!\x05B\x92"?{?\xbc\xa9\xa8'
key = hashlib.pbkdf2_hmac(
'sha256',
password.encode('utf-8'),
salt,
100000
)
return {'salt':str(salt),'key':str(key)}
###################################################################################
def FetchLatLong(): #scrape data and store in db
try:
url = 'http://www.csgnetwork.com/llinfotable.html'
response = get(url)
if response.ok:
html_soup = BeautifulSoup(response.text, 'html.parser')
table=html_soup.find_all('table')[5]
rows = table.find_all('tr')
countries={}
for tr in rows:
tds=tr.find_all('td')
countryname=tds[0].text
capital=tds[1].text
lat=tds[2].text
long=tds[3].text
if countryname!='Country':
if countryname=='United States\r\n of America':
countryname='USA'
if countryname=='United Kingdom of Great Britain and Northern Ireland':
countryname='UK'
if countryname=='Russian Federation':
countryname='Russia'
if lat[-1]=='S':
a=lat.split("'")[0].replace('°','.')
lat=-float(a)
else:
a=lat.split("'")[0].replace('°','.')
lat=float(a)
if long[-1]=='W':
a=long.split("'")[0].replace('°','.')
long=-float(a)
else:
a=long.split("'")[0].replace('°','.')
long=float(a)
countries[countryname]=[capital,lat,long]
open('CountriesCoords.txt','w').close()
f=open('CountriesCoords.txt','wb')
pickle.dump(countries,f)
f.close()
else:
return False
except Exception as e:
return str(e)
def getLatLong():
with open('CountriesCoords.txt','rb') as f:
return pickle.load(f)