forked from mrtrkmn/deprem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprem.py
414 lines (359 loc) · 13.9 KB
/
deprem.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
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
408
409
410
411
412
413
414
# Kandilli Rasathanesi ve Deprem Araştırma Enstitüsü'nün
# http://www.koeri.boun.edu.tr/scripts/lasteq.asp adresinden
# son 24 saatte meydana gelen depremlerin içerisinden
# aranan şehire göre bildirim gönderen ve
# Aynı zamanda detaylı aramak yaparak veri seti indirebileceğiniz bir script
import csv
import datetime as dt
import time
from io import StringIO
from os import environ
import pandas as pd
import requests
import unidecode
from bs4 import BeautifulSoup
KANDILLI_URL = "http://www.koeri.boun.edu.tr/scripts/lasteq.asp"
SEARCH_RECENT_FOR_EVENTS = "http://sc3.koeri.boun.edu.tr/eqevents/eq_events"
TELEGRAM_CHAT_ID = environ.get("TELEGRAM_CHAT_ID")
TELEGRAM_TOKEN = environ.get("TELEGRAM_TOKEN")
CITY_TO_BE_CHECKED = environ.get("CITY_TO_BE_CHECKED")
TIME_INTERVAL = environ.get("TIME_INTERVAL")
SAVE_DATA_TO_ARTIFACT = environ.get("SAVE_DATA_TO_ARTIFACT")
# create array of cities in Turkey
CITIES = [
"ADANA",
"ADIYAMAN",
"AFYON",
"AGRI",
"AKSARAY",
"AMASYA",
"ANKARA",
"ANTALYA",
"ARDAHAN",
"ARTVIN",
"AYDIN",
"BALIKESIR",
"BARTIN",
"BATMAN",
"BAYBURT",
"BILECIK",
"BINGOL",
"BITLIS",
"BOLU",
"BURDUR",
"BURSA",
"CANAKKALE",
"CANKIRI",
"CORUM",
"DENIZLI",
"DIYARBAKIR",
"DUZCE",
"EDIRNE",
"ELAZIG",
"ERZINCAN",
"ERZURUM",
"ESKISEHIR",
"GAZIANTEP",
"GIRESUN",
"GUMUSHANE",
"HAKKARI",
"HATAY",
"IGDIR",
"ISPARTA",
"ISTANBUL",
"IZMIR",
"KAHRAMANMARAS",
"KARABUK",
"KARAMAN",
"KARS",
"KASTAMONU",
"KAYSERI",
"KILIS",
"KIRIKKALE",
"KIRKLARELI",
"KIRSEHIR",
"KOCAELI",
"KONYA",
"KUTAHYA",
"MALATYA",
"MANISA",
"MARDIN",
"MERSIN",
"MUGLA",
"MUS",
"NEVSEHIR",
"NIGDE",
"ORDU",
"OSMANIYE",
"RIZE",
"SAKARYA",
"SAMSUN",
"SANLIURFA",
"SIIRT",
"SINOP",
"SIRNAK",
"SIVAS",
"TEKIRDAG",
"TOKAT",
"TRABZON",
"TUNCELI",
"USAK",
"VAN",
"YALOVA",
"YOZGAT",
"ZONGULDAK",
]
class Deprem:
def __init__(self):
# convert string input to int
if not environ.get("SAVE_DATA_TO_ARTIFACT"):
if environ.get("TIME_INTERVAL"):
self.time_interval = int(environ.get("TIME_INTERVAL"))
else:
self.time_interval = 5
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
return self
def send_message(self, message):
try:
response = requests.get(
f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage?chat_id={TELEGRAM_CHAT_ID}&text={message}"
)
if response.status_code != 200:
print("Error while sending message: ", response.status_code)
except Exception as e:
print(e)
def check_date_time(self, date_text):
try:
dt.date.fromisoformat(date_text)
return True
except ValueError:
print(f"Hatalı tarih aralığı, tarih aralığı değiştirilmeden devam ediliyor {date_text}")
return False
def check_city_input(self, city):
if unidecode.unidecode(city.upper()) not in CITIES:
print("Türkiye'de bulunmayan bir şehir girdiniz veya şehir adını yanlış girdiniz.")
exit(1)
def get_data_from_kandilli(self):
# get data from kandilli url and parse it
try:
response = requests.get(KANDILLI_URL)
html_content = response.content
soup = BeautifulSoup(html_content, "html.parser")
table = soup.find("pre").find(string=True)
return table
except Exception as e:
print(e)
exit(1)
def extract_data(self, table, city_to_be_checked, time_interval):
# find the earthquake pre table
try:
time_interval = int(time_interval)
except ValueError:
print("Lütfen zaman aralığını sayı olarak giriniz.")
exit(1)
buff = StringIO(table)
reader = csv.reader(buff)
for line in reader:
try:
attributes = line[0].split()
city = attributes[8] + " " + attributes[9]
date_time = attributes[0] + " " + attributes[1]
# parse the date and time
timestamp = (
dt.datetime.strptime(date_time, "%Y.%m.%d %H:%M:%S")
.replace(tzinfo=dt.timezone.utc)
.replace(tzinfo=None)
)
current_timestamp = dt.datetime.utcnow().replace(tzinfo=None)
time_difference = (current_timestamp - timestamp).total_seconds() / 60
if time_difference <= time_interval and city_to_be_checked.upper() in city.upper():
print("-" * 100)
message = f"""Tarih/Zaman: {attributes[0]} {attributes[1]}\nŞehir: {city}\nDerinlik(km): {attributes[4]}\t MD: {attributes[5]}\t ML: {attributes[6]}\tMw: {attributes[7]}\nKaynak: Kandilli Rasathanesi ve Deprem Araştırma Enstitüsü """
if not TELEGRAM_TOKEN or not TELEGRAM_CHAT_ID:
print(message)
else:
self.send_message(message)
except Exception as e:
# print(e)
continue
def export_data_to_excel_file(self, source, city="", filter_city=False):
timestamp = dt.datetime.now().strftime("%d_%m_%Y")
if filter_city:
data_file_name = "deprem_data_" + city + "_" + timestamp + ".xlsx"
else:
data_file_name = "deprem_data_" + timestamp + ".xlsx"
try:
all_tables = pd.read_html(source, attrs={"class": "index"})
df = all_tables[0]
if filter_city:
df = df[df["Yer-Region Name"].str.contains(unidecode.unidecode(city), case=False)]
df.to_excel(data_file_name)
except Exception as e:
print(e)
exit(1)
print(f"Deprem verileri {data_file_name} excel dosyasina aktarildi\n ")
def search_and_filter_on_kandilli(self, from_date, to_date, city):
resp = self.search_on_kandilli_with_requests(from_date, to_date)
self.export_data_to_excel_file(source=resp, city=city, filter_city=True)
def search_on_kandilli_with_requests(
self,
start_date,
end_date,
min_depth=0,
max_depth=50,
min_magnitude=1,
max_magnitude=7,
sorting="Origin Time UTC",
asc_desc="descending",
):
from_year, from_month, from_day = start_date.split("-")
to_year, to_month, to_day = end_date.split("-")
# create POST request to get data
try:
data = {
"MIME Type": "application/x-www-form-urlencoded",
"fromTY": from_year, # 1900-2020
"fromTM": from_month, # 1-12
"fromTD": from_day, # 1-31
"toY": to_year, # 1900-2020
"toM": to_month, # 1-12
"toD": to_day, # 1-31
"min_lat": "", # 35-42
"max_lat": "",
"min_long": "",
"max_long": "",
"min_depth": min_depth, # 0-700
"max_depth": max_depth, # 0-700
"min_mag": min_magnitude, # 0-10
"max_mag": max_magnitude, # 0-10
"sort": sorting, # Origin Time UTC, Longitude, Latitude, Magnitude, Depth
"desc": asc_desc.lower(), # ascending or descending
"get_events": "true",
}
user_request = requests.post(
SEARCH_RECENT_FOR_EVENTS,
data=data,
headers={
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
},
)
if user_request.status_code == 200:
return user_request.text
else:
print("Kandilli'den veri çekilemedi")
print(f"Sunucu cevabı: {user_request.status_code}")
except Exception as e:
print(e)
exit(1)
def retrive_data_from_kandilli(city, time_interval):
with Deprem() as deprem_bot:
deprem_bot.check_city_input(city)
depremler = deprem_bot.get_data_from_kandilli()
# extract data is either sending message to telegram or printing to console
deprem_bot.extract_data(depremler, city, time_interval)
# option #3
def search_based_on_city(date, city):
if date[-1] not in ["A", "Y", "D"]:
print("Kabul edilen zaman dilimleri: A (Ay), Y (Yil), D (Gun) \n")
print("Yanlış zaman dilimi belirttiniz, program kapatılıyor...")
exit(1)
# take digit part of the string
time_interval = date[:-1]
if not time_interval.isdigit():
print("Zaman dilimi rakam olmalıdır. Program kapatiliyor...")
exit(1)
if date[-1] == "A":
from_date = dt.datetime.now() - dt.timedelta(days=int(time_interval) * 30)
if date[-1] == "Y":
from_date = dt.datetime.now() - dt.timedelta(days=int(time_interval) * 365)
if date[-1] == "D":
from_date = dt.datetime.now() - dt.timedelta(days=int(time_interval))
from_date = from_date.strftime("%Y-%m-%d")
now_date = dt.datetime.now().strftime("%Y-%m-%d")
with Deprem() as deprem_bot:
deprem_bot.check_city_input(city)
deprem_bot.search_and_filter_on_kandilli(from_date, now_date, city)
def check_telegram_token_and_chat_id():
try:
return TELEGRAM_CHAT_ID is not None and TELEGRAM_TOKEN is not None
except TypeError:
return False
def check_time_interval_value():
try:
return TIME_INTERVAL[-1] in ["A", "Y", "D"] and TIME_INTERVAL[:-1].isdigit()
except TypeError:
return False
if __name__ == "__main__":
if check_telegram_token_and_chat_id() and not check_time_interval_value():
retrive_data_from_kandilli(CITY_TO_BE_CHECKED, TIME_INTERVAL)
exit(0)
if check_time_interval_value():
if CITY_TO_BE_CHECKED is None:
print("CITY_TO_BE_CHECKED degiskeni bos olamaz")
exit(1)
search_based_on_city(city=CITY_TO_BE_CHECKED, date=TIME_INTERVAL)
exit(0)
print("Deprem veri çekme programı başlatılıyor...")
print("Ne yapmak istersiniz?")
print("1. Şehir ve zamana bağlı arama ya ve yazdır")
print("2. Detaylı arama yaparak, istenilen veriyi excel dosyasına aktar")
print("3. Şehir bazında geçmişe dair verileri excel dosyasına aktar\n")
choose = input("Seçiminiz: ")
if choose == "1":
city = input("Sehir giriniz: ")
time_interval = input("Zaman araligi giriniz: (* 90: son 90 dk icindeki depremler)\n")
retrive_data_from_kandilli(city, time_interval)
if choose == "2":
print(
"Detaylı arama yapabilmek için, aşağıdaki bilgileri giriniz, boş bırakmak istediğiniz alanları boş bırakınız"
)
user_input = input("Tarih aralığı belirlemek istiyor musunuz ? (e/h)\n")
start_date = ""
end_date = ""
min_depth = ""
max_depth = ""
min_magnitude = ""
max_magnitude = ""
ordered_by = ""
sorting_type = ""
if user_input == "e":
start_date = input("Baslangic tarihi: (YYYY-MM-DD)\n")
end_date = input("Bitis tarihi: (YYYY-MM-DD)\n")
user_input = input("Derinlik aralığı belirlemek istiyor musunuz ? (e/h)\n")
if user_input == "e":
min_depth = float(input("Minimum derinlik: (km))\n"))
max_depth = float(input("Maksimum derinlik: (km)\n"))
user_input = input("Büyüklük aralığı belirlemek istiyor musunuz ? (e/h)\n")
if user_input == "e":
min_magnitude = float(input("Minimum büyüklük: (Richter)\n"))
max_magnitude = float(input("Maksimum büyüklük: (Richter)\n"))
user_input = input("Siralamak istiyor musunuz ? (e/h)\n")
if user_input == "e":
order_options = ["Origin Time UTC", "Longitude", "Latitude", "Magnitude", "Depth"]
ordered_by = input("Siralanma kriteri: *(Origin Time UTC, Longitude, Latitude, Magnitude, Depth)*\n")
while ordered_by not in order_options:
print("Hatali giris yaptiniz, lutfen tekrar deneyiniz")
ordered_by = input("Siralanma kriteri: *(Origin Time UTC, Longitude, Latitude, Magnitude, Depth)*\n")
sorting_options = ["ascending", "descending"]
sorting_type = input("Siralama tipi: *(Ascending, Descending)*\n")
while sorting_type.lower() not in sorting_options:
print("Hatali giris yaptiniz, lutfen tekrar deneyiniz")
sorting_type = input("Siralama tipi: *(Ascending, Descending)*\n")
with Deprem() as deprem_bot:
source = deprem_bot.search_on_kandilli_with_requests(
start_date, end_date, min_depth, max_depth, min_magnitude, max_magnitude, ordered_by, sorting_type
)
deprem_bot.export_data_to_excel_file(source)
if choose == "3":
city = input("Sehir giriniz: ")
date = input(
"Geçmişe dair verileri almak için aşağıda belirtildiği şekilde zaman belirtiniz:\n * 3A - Son 3 Aydaki veriler\n * 3Y - Son 3 Yildaki veriler\n * 3D - Son 3 Gunluk veriler\n"
)
search_based_on_city(date, city)
if choose not in ["1", "2", "3"]:
print("Yanlis secim yaptiniz, program kapatiliyor...")
exit(1)