-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase_save.py
110 lines (96 loc) · 4.83 KB
/
Database_save.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
from Database_login import LoginInformation
from Crawaling_information import WeatherInformationCralwer
from apscheduler.schedulers.background import BackgroundScheduler
import pymysql
import time
import sys
class DatabaseSave:
def __init__(self):
self._db = None
def _set_db(self, database):
if database is None:
print("Connection method of pymysql object returns none. Please check.")
sys.exit(0)
self._db = database
def main(self):
self._db_connect()
self._save_data()
def _db_connect(self):
_login = LoginInformation()
_login.main()
_host = _login.host
_user = _login.user
_port = int(_login.port)
_passwd = _login.password
_db = _login.db_name
_charset = _login.charset
_db = pymysql.connect(host=_host, port=_port, user=_user,
passwd=_passwd, db=_db, charset=_charset)
self._set_db(_db)
def _save_data(self):
def _get_table_name():
try:
with open('./db_table_name_information.txt', 'rt', encoding='utf8') as file:
_table_name_dict = {}
for row in file.readlines():
key = row.split("=")[0].strip()
value = row.split("=")[1].strip()
if len(key) == 0 or len(value) == 0:
raise AttributeError
_table_name_dict[key] = value
return _table_name_dict
except FileNotFoundError as e:
print("The file containing table name information doesn't exist in the same path. Please check.")
sys.exit(0)
except AttributeError as e:
print("Some information in the table_name information file are missing. Please check.")
sys.exit(0)
def _convert_into_valid_form(value_dict):
for area in value_dict.keys():
for i, (key, value) in enumerate(value_dict[area].items()):
if value is None:
if i in [0, 1, 2, 3, 5, 6, 7, 8, 9, 12, 14, 16]:
value_dict[area][key] = 'NULL'
else:
value_dict[area][key] = ''
continue
try:
value_dict[area][key] = float(value)
except ValueError as e:
continue
return value_dict
_weather_info = WeatherInformationCralwer()
_weather_info.main()
_table_dict = _get_table_name()
_data_dict = _convert_into_valid_form(_weather_info.data_dict)
for area, table in _table_dict.items():
for region, data in _data_dict.items():
if region.split()[2] == area:
_sql = f"""
INSERT INTO {table}
(m_date, area, current_temp, min_temp, max_temp, sensible_temp,
weather, uv_num, uv_non_num_content, rainfall_amount, find_dust_num,
find_dust, ultra_find_dust_num, ultra_find_dust,
ozone_num, ozone, p_rainfall, wind_speed, humidity)
VALUES(now(), "{region}", {_data_dict[region]['current_temp']},
{_data_dict[region]['min_temp']}, {_data_dict[region]['max_temp']},
{_data_dict[region]['sensible_temp']}, "{_data_dict[region]['weather']}",
{_data_dict[region]['uv_num']}, "{_data_dict[region]['uv_non_num_content']}",
{_data_dict[region]['rainfall_amount']}, {_data_dict[region]['fine_dust_num']},
"{_data_dict[region]['fine_dust']}", {_data_dict[region]['ultra_fine_dust_num']},
"{_data_dict[region]['ultra_fine_dust']}", {_data_dict[region]['ozone_num']},
"{_data_dict[region]['ozone']}", {_data_dict[region]['p_rainfall']},
{_data_dict[region]['wind_speed']}, {_data_dict[region]['humidity']});
"""
_cursor = self._db.cursor()
_cursor.execute(_sql)
self._db.commit()
self._db.close()
print("done")
if __name__ == "__main__":
save = DatabaseSave()
scheduler = BackgroundScheduler()
scheduler.add_job(save.main, 'interval', minutes=15, start_date="2020-06-13 12:00:00")
scheduler.start()
while True:
time.sleep(0.3)