Compare commits
2 Commits
5b81106015
...
44e60fd535
| Author | SHA1 | Date | |
|---|---|---|---|
| 44e60fd535 | |||
| 36a7b8bcf1 |
96
main.py
96
main.py
@@ -1,7 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
|
import pytz
|
||||||
|
from datetime import datetime
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
import random
|
|
||||||
import requests
|
import requests
|
||||||
import schedule
|
import schedule
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@@ -17,13 +18,12 @@ load_dotenv()
|
|||||||
|
|
||||||
WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK_URL')
|
WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK_URL')
|
||||||
|
|
||||||
MSG_FOOTER = "THC - Toke Hash Coordinated | GIF via Tenor"
|
MSG_FOOTER = "THC - Toke Hash Coordinated"
|
||||||
|
|
||||||
MSG_TEST = "Discord 420 timer activated. This is a test notification."
|
|
||||||
MSG_REMINDER = "This is your 5 minute reminder to 420!"
|
MSG_REMINDER = "This is your 5 minute reminder to 420!"
|
||||||
MSG_HALFTIME_REMINDER = "Half-time in 5 minutes!"
|
MSG_HALFTIME_REMINDER = "Half-time in 5 minutes!"
|
||||||
MSG_HALFTIME = "Half-time!"
|
MSG_HALFTIME = "Half-time!"
|
||||||
MSG_NOTIFICATION = "420! Blaze it!"
|
MSG_NOTIFICATION = "Blaze it!"
|
||||||
|
|
||||||
COL_BLUE = 0x3498db
|
COL_BLUE = 0x3498db
|
||||||
COL_ORANGE = 0xe67e22
|
COL_ORANGE = 0xe67e22
|
||||||
@@ -31,15 +31,56 @@ COL_GREEN = 0x2ecc71
|
|||||||
COL_UNKNOWN = 0x95a5a6
|
COL_UNKNOWN = 0x95a5a6
|
||||||
|
|
||||||
messages = {
|
messages = {
|
||||||
"test": {"text": MSG_TEST, "color": COL_BLUE},
|
|
||||||
"reminder_halftime": {"text": MSG_HALFTIME_REMINDER, "color": COL_ORANGE},
|
"reminder_halftime": {"text": MSG_HALFTIME_REMINDER, "color": COL_ORANGE},
|
||||||
"halftime": {"text": MSG_HALFTIME, "color": COL_GREEN},
|
"halftime": {"text": MSG_HALFTIME, "color": COL_GREEN},
|
||||||
"reminder": {"text": MSG_REMINDER, "color": COL_ORANGE},
|
"reminder": {"text": MSG_REMINDER, "color": COL_ORANGE},
|
||||||
"notification": {"text": MSG_NOTIFICATION, "color": COL_GREEN},
|
"420": {"text": MSG_NOTIFICATION, "color": COL_GREEN},
|
||||||
"unknown": {"text": "Unknown notification type", "color": COL_UNKNOWN}
|
"unknown": {"text": "Unknown notification type", "color": COL_UNKNOWN}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def load_timezones() -> list[dict]:
|
||||||
|
"""Load timezones from csv file."""
|
||||||
|
# Read the CSV file and return a list of timezones
|
||||||
|
with open("tzdb/TimeZoneDB.csv/time_zone.csv", "r", encoding="utf-8") as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
# Fields: zone_name,country_code,abbreviation,time_start,gmt_offset,dst
|
||||||
|
timezones = []
|
||||||
|
for line in lines:
|
||||||
|
fields = line.strip().split(",")
|
||||||
|
if len(fields) >= 5:
|
||||||
|
timezones.append({
|
||||||
|
"zone_name": fields[0],
|
||||||
|
"country_code": fields[1],
|
||||||
|
"abbreviation": fields[2],
|
||||||
|
"time_start": fields[3],
|
||||||
|
"gmt_offset": int(fields[4]),
|
||||||
|
"dst": fields[5] == '1'
|
||||||
|
})
|
||||||
|
return timezones
|
||||||
|
|
||||||
|
|
||||||
|
def load_countries() -> list[dict]:
|
||||||
|
"""Load countries from csv file."""
|
||||||
|
# Read the CSV file and return a list of countries
|
||||||
|
with open("tzdb/TimeZoneDB.csv/country.csv", "r", encoding="utf-8") as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
# Fields: country_code,country_name
|
||||||
|
countries = []
|
||||||
|
for line in lines:
|
||||||
|
fields = line.strip().split(",")
|
||||||
|
if len(fields) >= 2:
|
||||||
|
countries.append({
|
||||||
|
"country_code": fields[0],
|
||||||
|
"country_name": fields[1]
|
||||||
|
})
|
||||||
|
return countries
|
||||||
|
|
||||||
|
|
||||||
|
timezones = load_timezones()
|
||||||
|
countries = load_countries()
|
||||||
|
|
||||||
|
|
||||||
def get_message(type: str) -> dict[str, int]:
|
def get_message(type: str) -> dict[str, int]:
|
||||||
"""
|
"""
|
||||||
Get the notification message based on the type.
|
Get the notification message based on the type.
|
||||||
@@ -47,9 +88,16 @@ def get_message(type: str) -> dict[str, int]:
|
|||||||
msg = messages["unknown"]
|
msg = messages["unknown"]
|
||||||
if type in messages:
|
if type in messages:
|
||||||
msg = messages[type]
|
msg = messages[type]
|
||||||
if type in ["halftime", "notification"]:
|
if type in ["halftime", "420"]:
|
||||||
msg["image"] = {
|
msg["image"] = {
|
||||||
"url": "https://www.freepnglogos.com/uploads/weed-leaf-png/cannabis-weed-leaf-png-clipart-images-24.png"}
|
"url": "https://www.freepnglogos.com/uploads/weed-leaf-png/cannabis-weed-leaf-png-clipart-images-24.png"}
|
||||||
|
if type == "420":
|
||||||
|
tz_list = where_is_it_420(timezones, countries)
|
||||||
|
if tz_list:
|
||||||
|
tz_str = "\n".join(tz_list)
|
||||||
|
msg["text"] += f"\nIt's 4:20 in:\n{tz_str}"
|
||||||
|
else:
|
||||||
|
msg["text"] += " It's not 4:20 anywhere right now."
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
|
||||||
@@ -91,19 +139,49 @@ def send_notification(message: str) -> None:
|
|||||||
logging.error(f"Error sending notification: {e}")
|
logging.error(f"Error sending notification: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def get_tz_info(tz_name: str, timezones: list[dict]) -> dict | None:
|
||||||
|
"""Get timezone info by name."""
|
||||||
|
return next((tz for tz in timezones if tz["zone_name"] == tz_name), None)
|
||||||
|
|
||||||
|
|
||||||
|
def get_country_info(country_code: str, countries: list[dict]) -> dict | None:
|
||||||
|
"""Get country info by country code."""
|
||||||
|
return next((c for c in countries if c["country_code"] == country_code), None)
|
||||||
|
|
||||||
|
|
||||||
|
def where_is_it_420(timezones: list[dict], countries: list[dict]) -> list[str]:
|
||||||
|
"""Get timezones where the current hour is 4 or 16, indicating it's 4:20 there.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list[str]: A list of timezones where it's currently 4:20 PM or AM.
|
||||||
|
"""
|
||||||
|
tz_list = []
|
||||||
|
for tz in pytz.all_timezones:
|
||||||
|
now = datetime.now(pytz.timezone(tz))
|
||||||
|
if now.hour == 4 or now.hour == 16:
|
||||||
|
# Find the timezone in the loaded timezones
|
||||||
|
tz_info = get_tz_info(tz, timezones)
|
||||||
|
if tz_info:
|
||||||
|
country = get_country_info(tz_info["country_code"], countries)
|
||||||
|
if country and country["country_name"] not in tz_list:
|
||||||
|
country_name = country["country_name"].strip().strip('"')
|
||||||
|
tz_list.append(country_name)
|
||||||
|
return tz_list
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
"""
|
"""
|
||||||
Main function to run the scheduler.
|
Main function to run the scheduler.
|
||||||
"""
|
"""
|
||||||
# Schedule notifications
|
# Schedule notifications
|
||||||
schedule.every().hour.at(":15").do(send_notification, "reminder")
|
schedule.every().hour.at(":15").do(send_notification, "reminder")
|
||||||
schedule.every().hour.at(":20").do(send_notification, "notification")
|
schedule.every().hour.at(":20").do(send_notification, "420")
|
||||||
schedule.every().hour.at(":45").do(send_notification, "reminder_halftime")
|
schedule.every().hour.at(":45").do(send_notification, "reminder_halftime")
|
||||||
schedule.every().hour.at(":50").do(send_notification, "halftime")
|
schedule.every().hour.at(":50").do(send_notification, "halftime")
|
||||||
logging.info("Scheduler started.")
|
logging.info("Scheduler started.")
|
||||||
|
|
||||||
# Test the notification on startup
|
# Test the notification on startup
|
||||||
send_notification("test")
|
send_notification("420")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
requests
|
|
||||||
python-dotenv
|
python-dotenv
|
||||||
|
pytz
|
||||||
|
requests
|
||||||
schedule
|
schedule
|
||||||
|
|||||||
247
tzdb/TimeZoneDB.csv/country.csv
Normal file
247
tzdb/TimeZoneDB.csv/country.csv
Normal file
@@ -0,0 +1,247 @@
|
|||||||
|
AF,Afghanistan
|
||||||
|
AL,Albania
|
||||||
|
DZ,Algeria
|
||||||
|
AS,American Samoa
|
||||||
|
AD,Andorra
|
||||||
|
AO,Angola
|
||||||
|
AI,Anguilla
|
||||||
|
AQ,Antarctica
|
||||||
|
AG,Antigua and Barbuda
|
||||||
|
AR,Argentina
|
||||||
|
AM,Armenia
|
||||||
|
AW,Aruba
|
||||||
|
AU,Australia
|
||||||
|
AT,Austria
|
||||||
|
AZ,Azerbaijan
|
||||||
|
BS,Bahamas
|
||||||
|
BH,Bahrain
|
||||||
|
BD,Bangladesh
|
||||||
|
BB,Barbados
|
||||||
|
BY,Belarus
|
||||||
|
BE,Belgium
|
||||||
|
BZ,Belize
|
||||||
|
BJ,Benin
|
||||||
|
BM,Bermuda
|
||||||
|
BT,Bhutan
|
||||||
|
BO,"Bolivia, Plurinational State of"
|
||||||
|
BQ,"Bonaire, Sint Eustatius and Saba"
|
||||||
|
BA,Bosnia and Herzegovina
|
||||||
|
BW,Botswana
|
||||||
|
BR,Brazil
|
||||||
|
IO,British Indian Ocean Territory
|
||||||
|
BN,Brunei Darussalam
|
||||||
|
BG,Bulgaria
|
||||||
|
BF,Burkina Faso
|
||||||
|
BI,Burundi
|
||||||
|
KH,Cambodia
|
||||||
|
CM,Cameroon
|
||||||
|
CA,Canada
|
||||||
|
CV,Cape Verde
|
||||||
|
KY,Cayman Islands
|
||||||
|
CF,Central African Republic
|
||||||
|
TD,Chad
|
||||||
|
CL,Chile
|
||||||
|
CN,China
|
||||||
|
CX,Christmas Island
|
||||||
|
CC,Cocos (Keeling) Islands
|
||||||
|
CO,Colombia
|
||||||
|
KM,Comoros
|
||||||
|
CG,Congo
|
||||||
|
CD,"Congo, the Democratic Republic of the"
|
||||||
|
CK,Cook Islands
|
||||||
|
CR,Costa Rica
|
||||||
|
HR,Croatia
|
||||||
|
CU,Cuba
|
||||||
|
CW,Curaçao
|
||||||
|
CY,Cyprus
|
||||||
|
CZ,Czech Republic
|
||||||
|
CI,Côte d'Ivoire
|
||||||
|
DK,Denmark
|
||||||
|
DJ,Djibouti
|
||||||
|
DM,Dominica
|
||||||
|
DO,Dominican Republic
|
||||||
|
EC,Ecuador
|
||||||
|
EG,Egypt
|
||||||
|
SV,El Salvador
|
||||||
|
GQ,Equatorial Guinea
|
||||||
|
ER,Eritrea
|
||||||
|
EE,Estonia
|
||||||
|
ET,Ethiopia
|
||||||
|
FK,Falkland Islands (Malvinas)
|
||||||
|
FO,Faroe Islands
|
||||||
|
FJ,Fiji
|
||||||
|
FI,Finland
|
||||||
|
FR,France
|
||||||
|
GF,French Guiana
|
||||||
|
PF,French Polynesia
|
||||||
|
TF,French Southern Territories
|
||||||
|
GA,Gabon
|
||||||
|
GM,Gambia
|
||||||
|
GE,Georgia
|
||||||
|
DE,Germany
|
||||||
|
GH,Ghana
|
||||||
|
GI,Gibraltar
|
||||||
|
GR,Greece
|
||||||
|
GL,Greenland
|
||||||
|
GD,Grenada
|
||||||
|
GP,Guadeloupe
|
||||||
|
GU,Guam
|
||||||
|
GT,Guatemala
|
||||||
|
GG,Guernsey
|
||||||
|
GN,Guinea
|
||||||
|
GW,Guinea-Bissau
|
||||||
|
GY,Guyana
|
||||||
|
HT,Haiti
|
||||||
|
VA,Holy See (Vatican City State)
|
||||||
|
HN,Honduras
|
||||||
|
HK,Hong Kong
|
||||||
|
HU,Hungary
|
||||||
|
IS,Iceland
|
||||||
|
IN,India
|
||||||
|
ID,Indonesia
|
||||||
|
IR,"Iran, Islamic Republic of"
|
||||||
|
IQ,Iraq
|
||||||
|
IE,Ireland
|
||||||
|
IM,Isle of Man
|
||||||
|
IL,Israel
|
||||||
|
IT,Italy
|
||||||
|
JM,Jamaica
|
||||||
|
JP,Japan
|
||||||
|
JE,Jersey
|
||||||
|
JO,Jordan
|
||||||
|
KZ,Kazakhstan
|
||||||
|
KE,Kenya
|
||||||
|
KI,Kiribati
|
||||||
|
KP,"Korea, Democratic People's Republic of"
|
||||||
|
KR,"Korea, Republic of"
|
||||||
|
KW,Kuwait
|
||||||
|
KG,Kyrgyzstan
|
||||||
|
LA,Lao People's Democratic Republic
|
||||||
|
LV,Latvia
|
||||||
|
LB,Lebanon
|
||||||
|
LS,Lesotho
|
||||||
|
LR,Liberia
|
||||||
|
LY,Libya
|
||||||
|
LI,Liechtenstein
|
||||||
|
LT,Lithuania
|
||||||
|
LU,Luxembourg
|
||||||
|
MO,Macao
|
||||||
|
MK,"Macedonia, the Former Yugoslav Republic of"
|
||||||
|
MG,Madagascar
|
||||||
|
MW,Malawi
|
||||||
|
MY,Malaysia
|
||||||
|
MV,Maldives
|
||||||
|
ML,Mali
|
||||||
|
MT,Malta
|
||||||
|
MH,Marshall Islands
|
||||||
|
MQ,Martinique
|
||||||
|
MR,Mauritania
|
||||||
|
MU,Mauritius
|
||||||
|
YT,Mayotte
|
||||||
|
MX,Mexico
|
||||||
|
FM,"Micronesia, Federated States of"
|
||||||
|
MD,"Moldova, Republic of"
|
||||||
|
MC,Monaco
|
||||||
|
MN,Mongolia
|
||||||
|
ME,Montenegro
|
||||||
|
MS,Montserrat
|
||||||
|
MA,Morocco
|
||||||
|
MZ,Mozambique
|
||||||
|
MM,Myanmar
|
||||||
|
NA,Namibia
|
||||||
|
NR,Nauru
|
||||||
|
NP,Nepal
|
||||||
|
NL,Netherlands
|
||||||
|
NC,New Caledonia
|
||||||
|
NZ,New Zealand
|
||||||
|
NI,Nicaragua
|
||||||
|
NE,Niger
|
||||||
|
NG,Nigeria
|
||||||
|
NU,Niue
|
||||||
|
NF,Norfolk Island
|
||||||
|
MP,Northern Mariana Islands
|
||||||
|
NO,Norway
|
||||||
|
OM,Oman
|
||||||
|
PK,Pakistan
|
||||||
|
PW,Palau
|
||||||
|
PS,"Palestine, State of"
|
||||||
|
PA,Panama
|
||||||
|
PG,Papua New Guinea
|
||||||
|
PY,Paraguay
|
||||||
|
PE,Peru
|
||||||
|
PH,Philippines
|
||||||
|
PN,Pitcairn
|
||||||
|
PL,Poland
|
||||||
|
PT,Portugal
|
||||||
|
PR,Puerto Rico
|
||||||
|
QA,Qatar
|
||||||
|
RO,Romania
|
||||||
|
RU,Russian Federation
|
||||||
|
RW,Rwanda
|
||||||
|
RE,Réunion
|
||||||
|
BL,Saint Barthélemy
|
||||||
|
SH,"Saint Helena, Ascension and Tristan da Cunha"
|
||||||
|
KN,Saint Kitts and Nevis
|
||||||
|
LC,Saint Lucia
|
||||||
|
MF,Saint Martin (French part)
|
||||||
|
PM,Saint Pierre and Miquelon
|
||||||
|
VC,Saint Vincent and the Grenadines
|
||||||
|
WS,Samoa
|
||||||
|
SM,San Marino
|
||||||
|
ST,Sao Tome and Principe
|
||||||
|
SA,Saudi Arabia
|
||||||
|
SN,Senegal
|
||||||
|
RS,Serbia
|
||||||
|
SC,Seychelles
|
||||||
|
SL,Sierra Leone
|
||||||
|
SG,Singapore
|
||||||
|
SX,Sint Maarten (Dutch part)
|
||||||
|
SK,Slovakia
|
||||||
|
SI,Slovenia
|
||||||
|
SB,Solomon Islands
|
||||||
|
SO,Somalia
|
||||||
|
ZA,South Africa
|
||||||
|
GS,South Georgia and the South Sandwich Islands
|
||||||
|
SS,South Sudan
|
||||||
|
ES,Spain
|
||||||
|
LK,Sri Lanka
|
||||||
|
SD,Sudan
|
||||||
|
SR,Suriname
|
||||||
|
SJ,Svalbard and Jan Mayen
|
||||||
|
SZ,Swaziland
|
||||||
|
SE,Sweden
|
||||||
|
CH,Switzerland
|
||||||
|
SY,Syrian Arab Republic
|
||||||
|
TW,"Taiwan, Province of China"
|
||||||
|
TJ,Tajikistan
|
||||||
|
TZ,"Tanzania, United Republic of"
|
||||||
|
TH,Thailand
|
||||||
|
TL,Timor-Leste
|
||||||
|
TG,Togo
|
||||||
|
TK,Tokelau
|
||||||
|
TO,Tonga
|
||||||
|
TT,Trinidad and Tobago
|
||||||
|
TN,Tunisia
|
||||||
|
TR,Turkey
|
||||||
|
TM,Turkmenistan
|
||||||
|
TC,Turks and Caicos Islands
|
||||||
|
TV,Tuvalu
|
||||||
|
UG,Uganda
|
||||||
|
UA,Ukraine
|
||||||
|
AE,United Arab Emirates
|
||||||
|
GB,United Kingdom
|
||||||
|
US,United States
|
||||||
|
UM,United States Minor Outlying Islands
|
||||||
|
UY,Uruguay
|
||||||
|
UZ,Uzbekistan
|
||||||
|
VU,Vanuatu
|
||||||
|
VE,"Venezuela, Bolivarian Republic of"
|
||||||
|
VN,Viet Nam
|
||||||
|
VG,"Virgin Islands, British"
|
||||||
|
VI,"Virgin Islands, U.S."
|
||||||
|
WF,Wallis and Futuna
|
||||||
|
EH,Western Sahara
|
||||||
|
YE,Yemen
|
||||||
|
ZM,Zambia
|
||||||
|
ZW,Zimbabwe
|
||||||
|
AX,Åland Islands
|
||||||
|
145637
tzdb/TimeZoneDB.csv/time_zone.csv
Normal file
145637
tzdb/TimeZoneDB.csv/time_zone.csv
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user