-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_funcs.py
82 lines (67 loc) · 2.54 KB
/
helper_funcs.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
import os
from time import sleep
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut, GeocoderServiceError
import openpyxl
from openpyxl.styles import Border, Side, PatternFill
# Helper functions #
def clear_screen(sleep_time=1):
sleep(sleep_time)
os.system('cls' if os.name == 'nt' else 'clear')
def wait_for_keypress(sleep_time=0):
while True:
input('\nPress "Enter" to continue...')
sleep(sleep_time)
break
def clear_or_create_sheet(workbook, sheet_name):
is_new_sheet = False
if sheet_name in workbook.sheetnames:
sheet = workbook[sheet_name]
sheet.delete_rows(1, sheet.max_row)
else:
sheet = workbook.create_sheet(title=sheet_name)
is_new_sheet = True
return sheet, is_new_sheet
def create_excel_headers(sheet):
headers = ["Fetched", "Latitude", "Longitude", "Date", "Hour",
"Temperature", "Precipitation Category", "Precipitation in mm"]
thin_border = Border(
left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin')
)
grey_fill = PatternFill(start_color='00CCCCCC',
end_color='00CCCCCC', fill_type='solid')
for col, header in enumerate(headers, start=1):
cell = sheet.cell(row=1, column=col)
cell.value = header
cell.border = thin_border
cell.fill = grey_fill
sheet.column_dimensions[openpyxl.utils.get_column_letter(
col)].width = 20
def get_coordinates():
geolocator = Nominatim(user_agent='GeocodingApp')
while True:
user_input = input(
'Enter city name(type "exit" to exit): ')
if user_input.lower() == 'exit':
print('\nAction cancelled.')
clear_screen()
return None
try:
location = geolocator.geocode(user_input)
latitude = None
longitude = None
if location:
latitude, longitude = round(
location.latitude, 6), round(location.longitude, 6)
return latitude, longitude, location[0].split(',')[0]
else:
print('Geocoding failed. Check your input or try a different location.')
except GeocoderTimedOut:
print('Geocoder service timed out. Please try again.')
except GeocoderServiceError:
print('Geocoder service error. Please try again later.')
except Exception as e:
print(f'An unexpected error occurred: {e}')