-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplugin.py
More file actions
214 lines (182 loc) · 7.55 KB
/
plugin.py
File metadata and controls
214 lines (182 loc) · 7.55 KB
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
###
# Copyright (c) 2014, spline
# All rights reserved.
#
#
###
# my libs
import sys
import json
import time
import pytz
import datetime
if sys.version_info[0] <= 2:
from urllib import quote_plus
else:
from urllib.parse import quote_plus
import pickle
# supybot libs
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.world as world
import supybot.conf as conf
try:
from supybot.i18n import PluginInternationalization
_ = PluginInternationalization('WorldTime')
except ImportError:
# Placeholder that allows to run the plugin on a bot
# without the i18n module
_ = lambda x:x
filename = conf.supybot.directories.data.dirize('WorldTime.db')
class WorldTime(callbacks.Plugin):
"""Add the help for "@plugin help WorldTime" here
This should describe *how* to use this plugin."""
threaded = True
###############################
# DATABASE HANDLING FUNCTIONS #
###############################
def __init__(self, irc):
self.__parent = super(WorldTime, self)
self.__parent.__init__(irc)
self.db = {}
self._loadDb()
world.flushers.append(self._flushDb)
def _loadDb(self):
"""Loads the (flatfile) database mapping ident@hosts to timezones."""
try:
with open(filename, 'rb') as f:
self.db = pickle.load(f)
except Exception as e:
self.log.debug('WorldTime: Unable to load pickled database: %s', e)
def _flushDb(self):
"""Flushes the (flatfile) database mapping ident@hosts to timezones."""
try:
with open(filename, 'wb') as f:
pickle.dump(self.db, f, 2)
except Exception as e:
self.log.warning('WorldTime: Unable to write pickled database: %s', e)
def die(self):
self._flushDb()
world.flushers.remove(self._flushDb)
self.__parent.die()
##################
# TIME FUNCTIONS #
##################
def _converttz(self, msg, s, outputTZ):
"""Convert epoch seconds to a HH:MM readable string."""
# now do some timezone math.
try:
dtobj = datetime.datetime.fromtimestamp(s, tz=pytz.timezone(outputTZ)) # convert epoch into aware dtobj.
outstrf = self.registryValue("format", msg.args[0])
local_dt = dtobj.astimezone(pytz.timezone(outputTZ))
return local_dt.strftime(outstrf)
except Exception as e:
self.log.info("WorldTime: ERROR: _converttz: {0}".format(e))
##############
# GAPI STUFF #
##############
def _getlatlng(self, location):
location = quote_plus(location)
url = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false' % location
# try and fetch url
try:
response = utils.web.getUrl(url)
except utils.web.Error:
irc.error(str(e), Raise=True)
# wrap in a big try/except
try:
result = json.loads(response.decode('utf-8'))
if result['status'] == 'OK':
lat = str(result['results'][0]['geometry']['location']['lat'])
lng = str(result['results'][0]['geometry']['location']['lng'])
place = (result['results'][0]['formatted_address'])
ll = '%s,%s' % (lat, lng) # lat+long into a single string.
return {'place':place, 'll':ll}
else:
self.log.info("ERROR: _getlatlng: status result NOT ok. Result: {0}".format(result))
except Exception as e:
self.log.info("ERROR: _getlatlng: {0}".format(e))
def _gettime(self, latlng):
latlng = quote_plus(latlng)
url = 'https://maps.googleapis.com/maps/api/timezone/json?location=%s&sensor=false×tamp=%s' % (latlng, time.time())
# try and fetch url
try:
response = utils.web.getUrl(url)
except utils.web.Error:
irc.error(str(e), Raise=True)
# wrap in a big try/except
try:
result = json.loads(response.decode('utf-8'))
if result['status'] == 'OK':
return result
else:
self.log.info("WorldTime: _gettime: status result NOT ok. Result: {0}".format(result))
except Exception as e:
self.log.info("WorldTime: _gettime: {0}".format(e))
###################
# PUBLIC FUNCTION #
###################
def worldtime(self, irc, msg, args, opts, location):
"""[--nick <nick] [<location>]
Query GAPIs for <location> and attempt to figure out local time. [<location>]
is only required if you have not yet set a location for yourself using the 'set'
command. If --nick is given, try looking up the location for <nick>.
"""
opts = dict(opts)
if not location:
try:
if 'nick' in opts:
host = irc.state.nickToHostmask(opts['nick'])
else:
host = msg.prefix
ih = host.split('!')[1]
location = self.db[ih]
except KeyError:
irc.error("No location for %s is set. Use the 'set' command "
"to set a location for your current hostmask, or call 'worldtime' "
"with <location> as an argument." % ircutils.bold('*!'+ih), Raise=True)
# first, grab lat and long for user location
gc = self._getlatlng(location)
if not gc:
irc.error("I could not find the location for: {0}. Bad location? Spelled wrong?".format(location), Raise=True)
# next, lets grab the localtime for that location w/lat+long.
ll = self._gettime(gc['ll'])
if not ll:
irc.error("I could not find the local timezone for: {0}. Bad location? Spelled wrong?".format(location), Raise=True)
# if we're here, we have localtime zone.
utcnow = int(time.time()) # grab UTC now.
# localtm = utcnow+ll['rawOffset'] # grab raw offset from
# now lets use pytz to convert into the localtime in the place.
lt = self._converttz(msg, utcnow, ll['timeZoneId'])
if lt: # make sure we get it back.
if sys.version_info[0] <= 2:
s = "{0} :: Current local time is: {1} ({2})".format(ircutils.bold(gc['place'].encode('utf-8')), lt, ll['timeZoneName'].encode('utf-8'))
else:
s ="{0} :: Current local time is: {1} ({2})".format(ircutils.bold(gc['place']), lt, ll['timeZoneName'])
if self.registryValue('disableANSI', msg.args[0]):
s = ircutils.stripFormatting(s)
irc.reply(s)
else:
irc.error("Something went wrong during conversion to timezone. Check the logs.", Raise=True)
worldtime = wrap(worldtime, [getopts({'nick': 'nick'}), additional('text')])
def set(self, irc, msg, args, timezone):
"""<location>
Sets the location for your current ident@host to <location>."""
ih = msg.prefix.split('!')[1]
self.db[ih] = timezone
irc.replySuccess()
set = wrap(set, ['text'])
def unset(self, irc, msg, args):
"""takes no arguments.
Unsets the location for your current ident@host."""
ih = msg.prefix.split('!')[1]
try:
del self.db[ih]
irc.replySuccess()
except KeyError:
irc.error("No entry for %s exists." % ircutils.bold('*!'+ih), Raise=True)
Class = WorldTime
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: