-
Notifications
You must be signed in to change notification settings - Fork 0
/
wt.py
executable file
·207 lines (178 loc) · 7.13 KB
/
wt.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
import datetime
import json
import os
import sqlite3
import sys
import importlib.util
import pytz
import requests
import time
from yaspin import yaspin
spinner = yaspin(text="Searching...", color="cyan")
packages = ['pytz', 'requests', 'yaspin']
missing_packages = [package for package in packages if importlib.util.find_spec(package) is None]
if missing_packages:
print("The following required packages are missing:")
for package in missing_packages:
print(f"- {package}")
try:
install_prompt = input("Do you want to install these packages? (yes/no): ")
except KeyboardInterrupt:
print("\nInstallation process interrupted.")
sys.exit(0)
if install_prompt.lower() == 'yes':
try:
import subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install', *missing_packages])
except Exception as e:
print(f"Error installing packages: {e}")
sys.exit(1)
except KeyboardInterrupt:
print("\nScript terminated by user.")
sys.exit(0)
else:
print("Please install the required packages manually and rerun the script.")
sys.exit(1)
GREEN = "\033[92m"
BLUE = "\033[94m"
RED = "\033[91m"
RESET = "\033[0m"
ERROR_EMOJI = "❌"
def load_country_timezones_from_db(db_file):
try:
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute("SELECT * FROM country_timezones")
country_timezones = dict(cursor.fetchall())
conn.close()
return {country.lower(): json.loads(timezones) for country, timezones in country_timezones.items()}
except sqlite3.Error as e:
# print(f"{ERROR_EMOJI} Error loading data from SQLite database: {e}")
print("✅ load data from fallback sources...")
fallback_data = load_country_timezones_from_url("https://time.mskian.com/database/country.json")
if fallback_data:
return fallback_data
return {}
def load_country_timezones_from_url(url):
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return {country.lower(): timezones for country, timezones in data.items()}
else:
print(f"{ERROR_EMOJI} Error loading data from URL: {response.status_code}")
return None
except Exception as e:
print(f"{ERROR_EMOJI} Error loading data from URL: {e}")
return None
def get_timezones_for_country(country, country_timezones):
country = country.lower()
matching_countries = [(c, tzs) for c, tzs in country_timezones.items() if country in c]
if len(matching_countries) == 1:
return matching_countries[0][1]
elif len(matching_countries) > 1:
print("✅ Multiple matches found:")
print("\n")
for matched_country, timezones in matching_countries:
print(f"{BLUE}{matched_country.capitalize()}:")
for timezone in timezones:
time_now = get_world_time(timezone)
if time_now is not None:
print(f"- {timezone}: {time_now}")
print(RESET)
return None
else:
print(f"{ERROR_EMOJI} No matches found.")
print("\n")
return None
def get_world_time(timezone):
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz)
time_format = "%A, %Y-%m-%d %I:%M:%S %p"
return local_time.strftime(time_format)
except pytz.UnknownTimeZoneError as e:
print(f"{ERROR_EMOJI} Error: Unknown timezone '{timezone}': {e}")
print("\n")
return None
def print_help():
print("\nUsage:")
print("\npython wt.py or python wt.py [DB_FILE_LOCATION]")
print("\nArguments:\n")
print("> python wt.py # Using External JSON data as fallback database.")
print("> python wt.py ./database/country.db # DB_FILE Path to the SQLite database file containing country timezones.")
print('> python wt.py ./database/country.db "country_Name" # Search Timezone with your custom DB_FILE_PATH.')
print("> Use 'quit' to exit.\n")
def main(db_file="./database/country.db", search_input=None):
try:
country_timezones = load_country_timezones_from_db(db_file)
print("\n")
if search_input:
while search_input.lower() != 'quit':
print("\n")
if not search_input:
print(f"{ERROR_EMOJI} Error: Please enter a country or timezone.")
print("\n")
break
spinner.start()
time.sleep(3)
spinner.stop()
if '/' in search_input:
timezones = [search_input]
else:
timezones = get_timezones_for_country(search_input, country_timezones)
if timezones is not None:
for timezone in timezones:
time_now = get_world_time(timezone)
if time_now is not None:
print(f"The current time in {GREEN}{timezone}{RESET} is: {RED}{time_now}{RESET}")
print("\n")
search_input = input("Enter a country or timezone to get the current time (or 'quit' to exit): ").strip()
else:
while True:
search_input = input("Enter a country or timezone to get the current time (or 'quit' to exit): ").strip()
print("\n")
if search_input.lower() == 'quit':
break
if not search_input:
print(f"{ERROR_EMOJI} Error: Please enter a country or timezone.")
print("\n")
continue
spinner.start()
time.sleep(3)
spinner.stop()
if '/' in search_input:
timezones = [search_input]
else:
timezones = get_timezones_for_country(search_input, country_timezones)
if timezones is not None:
for timezone in timezones:
time_now = get_world_time(timezone)
if time_now is not None:
print(f"The current time in {GREEN}{timezone}{RESET} is: {RED}{time_now}{RESET}")
print("\n")
except KeyboardInterrupt:
print("\n")
print("\nProgram terminated by user.")
print("\n")
except Exception as e:
print("\n")
print("\nAn error occurred")
print("\n")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] in ("-h", "--help"):
print_help()
else:
if len(sys.argv) > 2:
db_file = sys.argv[1]
search_input = sys.argv[2]
main(db_file, search_input)
elif len(sys.argv) > 1:
db_file = sys.argv[1]
if not os.path.isfile(db_file):
print("Error: The specified database file is not a valid file path.")
sys.exit(1)
else:
main(db_file)
else:
main()