Skip to content

Instantly share code, notes, and snippets.

@jcande
Created April 25, 2018 00:40
Show Gist options
  • Save jcande/62dfccc9b4017268892c133ae15bb5ed to your computer and use it in GitHub Desktop.
Save jcande/62dfccc9b4017268892c133ae15bb5ed to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import sys
import random
import argparse
import requests
import irc.client
import jaraco.logging
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
from pytz import timezone
the_blazing_hour = 16
the_blazing_minute = 20
# someone should really verify this list
places = { -11: [ "Midway Atoll"
, "Jarvis Island"
, "Niue, New Zealand"
]
, -10: [ "Papeete, Honolulu"
, "Aleutian Islands"
, "Johnston Atoll"
]
, -9: [ "Anchorage"
, "the Gambier Islands"
]
, -8: [ "Seattle"
, "Tijuana"
, "Yukon"
, "Las Vegas, Nevada"
]
# this one (vvv) bugged out
, -7: [ "Phoenix, Arizona"
, "Calgary"
, "Albuquerque"
]
, -6: [ "Cartago, Costa Rica"
, "Galapagos Islands"
, "Granada, Nicaragua"
, "Chicago, IL"
]
# this one (vvv) bugged out
, -5: [ "New York"
, "Montreal"
, "Easter Island"
, "the Cayman Islands"
]
, -4: [ "Bridgetown, Barbados"
, "Aruba"
, "Puerto Rico"
, "the Amazon Rainforest"
]
, -3: [ "Arsuk"
, "Qaqortoq"
, "the Falkland Islands"
]
, -2: [ "the South Sandwich Islands"
, "Fernando de Noronha"
]
, -1: [ "Ittoqqortoormiit"
, "Azores"
]
, 0: [ "London"
, "Lisbon"
, "Casablanca"
, "Reykjavik"
, "Burkina Faso"
]
, 1: [ "Belgium"
, "Chad"
, "Kosovo"
, "Stockholm"
, "Vatican City"
, "Utrecht"
, "Warsaw"
, "Oslo"
]
, 2: [ "Cairo"
, "Tripoli"
, "Bucharest"
, "Kaliningrad Oblast"
, "Lobamba, Swaziland"
, "Harare, Zimbabwe"
]
, 3: [ "Addis Ababa, Ethiopia"
, "Baghdad"
, "Nairobi"
, "Mahajanga"
, "Sana'a, Yemen"
]
, 4: [ "Baku, Azerbaijan"
, "Muscat, Oman"
, "Abu Dhabi, UAE"
, "Victoria, Seychelles"
]
, 5: [ "Heard Island and McDonald Islands"
, "Aktobe, Kazakhstan"
, "Tashkent, Uzbekistan"
, "Ashgabat, Turkmenistan"
]
, 6: [ "Dhaka, Bangladesh"
, "Bishkek, Kyrgyzstan"
, "Novosibirsk"
, "Almaty"
]
, 7: [ "Christmas Island"
, "Bangkok"
, "Hanoi"
]
, 8: [ "China"
, "Ulaanbaatar"
, "Singapore"
, "Perth"
, "Zabaykalsky Krai"
]
, 9: [ "Seoul"
, "Tokyo"
, "Yakutsk"
, "Ambon, Maluku"
]
, 10: [ "Guam"
, "Port Moresby, Papua New Guinea"
, "Canberra, Australia"
, "Vladivostok"
]
, 11: [ "Pohnpei"
, "the Soloman Islands"
, "Vanuatu"
]
# this one (vvv) bugged out
, 12: [ "Fiji"
, "Marshall Islands"
, "Wake Island"
]
}
class Map:
def __init__(self):
self.timezone_url = "https://maps.googleapis.com/maps/api/timezone/json"
self.geocode_url = "http://maps.googleapis.com/maps/api/geocode/json"
def timezone_req(self, location, timestamp):
params = dict(
location = "{0},{1}".format(*location),
timestamp = "{0}".format(timestamp)
#key = self.api_key
)
response = requests.get(url=self.timezone_url, params=params)
data = response.json()
return data
def get_timezone(self, response):
if "timeZoneId" in response:
return timezone(response["timeZoneId"])
return None
def location_req(self, city):
params = dict(
address = city
)
response = requests.get(url=self.geocode_url, params=params)
data = response.json()
return data
def get_coords(self, response):
if "results" in response:
results = response["results"]
if len(results) > 0:
option = random.choice(results)
if "geometry" in option:
geometry = option["geometry"]
if "location" in geometry:
location = geometry["location"]
if "lat" in location and "lng" in location:
return (location["lat"], location["lng"])
return None
class Bot:
def __init__(self, server, port, channel, nickname):
self.channel = channel
self.map = Map()
self.reactor = irc.client.Reactor()
try:
self.connection = self.reactor.server().connect(server, port, nickname)
except irc.client.ServerConnectionError:
print(sys.exc_info()[1])
raise SystemExit(1)
self.connection.add_global_handler("welcome", self.on_connect)
self.sched = BackgroundScheduler()
self.sched.add_job(self.blazeit, 'cron', minute=the_blazing_minute)
#self.sched.add_job(self.blazeit, 'interval', seconds=5)
def on_connect(self, connection, event):
connection.join(self.channel)
def go(self):
self.sched.start()
self.reactor.process_forever()
def blazeit(self):
def get_delta(utc_hour):
delta = (the_blazing_hour - utc_hour) % 24
if delta > 12:
delta -= 24
return delta
def get_city(delta):
city = random.choice(places[delta])
print "city: {0}".format(city)
city_data = self.map.location_req(city)
print "city data: {0}".format(city_data)
coord = self.map.get_coords(city_data)
print "coord: {0}".format(coord)
if not coord:
return None
tz_data = self.map.timezone_req(coord, utc_timestamp)
print "tz data: {0}".format(tz_data)
tz = self.map.get_timezone(tz_data)
print "tz: {0}".format(tz)
if not tz:
return None
localtime = tz.fromutc(utc_now)
print "localtime: {0}".format(localtime.strftime("%s"))
if localtime.strftime("%H") == str(the_blazing_hour):
return city
else:
return None
utc_now = datetime.utcnow()
utc_hour = utc_now.hour
utc_timestamp = utc_now.strftime("%s")
blaze_city = get_city(get_delta(utc_hour))
# maybe it's dst
if blaze_city is None:
blaze_city = get_city(get_delta(utc_hour - 1))
if blaze_city:
self.connection.privmsg(self.channel, "It's 4:20 in {0}. toketoketoke".format(blaze_city))
else:
self.connection.privmsg(self.channel, "It's 4:20 somewhere. toketoketoke")
def terminate(self):
self.sched.shutdown()
self.connection.quit("laters blizzers")
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('server')
parser.add_argument('nickname')
parser.add_argument('channel', help="a nickname or channel")
parser.add_argument('-p', '--port', default=6667, type=int)
jaraco.logging.add_arguments(parser)
return parser.parse_args()
def main():
args = get_args()
jaraco.logging.setup(args)
bot = Bot(args.server, args.port, args.channel, args.nickname)
try:
bot.go()
except:
bot.terminate()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment