/
skybberbot.py
executable file
·713 lines (611 loc) · 26.9 KB
/
skybberbot.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# coding: utf-8
# SkybberBot: Astronomical jabber/xmpp bot
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import datetime
import time
import ephem
import math
import re
import sqlite3
import urllib.request
from .utils import *
from .jabberbot import botcmd
from .mucjabberbot import MUCJabberBot
from .satellitepass import SatellitePasses
from .user import User
from .typedetector import TypeDetector
from .location import Location
class CmdError(Exception):
""" Help class for handling command arguments errors
"""
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class MasterDBConnection():
""" Help class keeping DB connection
"""
SKYBBER_DB = 'skybber.db'
def __init__(self):
self.dbcon = None
def __enter__(self):
self.dbcon = sqlite3.connect(self.SKYBBER_DB)
return self.dbcon.cursor()
def __exit__(self, type, value, tb):
if tb is None:
self.dbcon.commit()
self.dbcon.close()
self.dbcon = None
else:
self.dbcon.rollback()
self.dbcon.close()
self.dbcon = None
class SkybberBot(MUCJabberBot):
AVAILABLE, AWAY, CHAT = None, 'away', 'chat'
DND, XA, OFFLINE = 'dnd', 'xa', 'unavailable'
MSG_AUTHORIZE_ME = 'Skybber: Please authorize my request.'
MSG_NOT_AUTHORIZED = 'You did not authorize my subscription request. Access denied.'
MSG_UNKNOWN_COMMAND = 'Unknown command: "%(command)s". Type "%(helpcommand)s" for available commands.'
# MSG_HELP_TAIL = 'Type %(helpcommand)s <command name> to get more info about that specific command.'
MSG_HELP_TAIL = ''
MSG_HELP_UNDEFINED_COMMAND = 'Undefined command.'
MSG_ERROR_OCCURRED = 'Unexpected error.'
MSG_NO_ASTRONOMICAL_NIGHT = 'No astronomical night'
MSG_FULL_ASTRONOMICAL_NIGHT = '24hrs astronomical night.'
PING_FREQUENCY = 60 # Set to the number of seconds, e.g. 60.
PING_TIMEOUT = 2 # Seconds to wait for a response.
MAX_USER_LOCATIONS = 10
RISET_OK = 0
NEVER_RISING = 1
NEVER_SETTING = 2
UNICODE_RISE = u'\u21E7'
UNICODE_SET = u'\u21E9'
def __init__(self, *args, **kwargs):
MUCJabberBot.__init__(self, *args, **kwargs)
self._obsr_default = ephem.Observer()
self._obsr_default.long, self._obsr_default.lat = '15.05728', '50.76111'
self._obsr_default.elevation = 400
self._arg_re = re.compile('[ \t]+')
def top_of_help_message(self):
""" Overridden from JabberBot
"""
return '\n This is astronomical jabber bot - SKYBBER!\n' + \
' version 0.1 \n\n'
def bottom_of_help_message(self):
return u'\n\nDATE FORMATS: \n ‘YYYY-MM-DD’ or ‘YYYY/MM/DD example: 2012/11/11’\n' + \
u'LOCATION FORMATS: \n' + \
u' - angular, example : 14.86524 50.78461\n' + \
u' - geographic coordinations, example : 50°46\'1.105"N 15°3\'52.885"E\n' + \
u' - user location, example: prague'
@botcmd(thread=True)
def satinfo(self, mess, args):
"""satinfo - information about satellite identified by satellite id
"""
(satid, _, reply) = self._checkArgSatId(args)
if satid is not None:
try:
req = urllib.request.Request('http://uhaapi-skybber.rhcloud.com/satellites/' + str(satid))
req.addheader('Accept', 'application/xml')
reply = urllib.request.urlopen(req).read()
except urllib2.URLError:
reply = 'Service disconnected.'
return reply
@botcmd(thread=True)
def satpass(self, mess, args):
"""satpass - show satellite passes identified by satellite id
"""
(satid, next_args, reply) = self._checkArgSatId(args)
if satid is not None:
reply = self._satteliteRequest(mess, next_args, str(satid))
else:
reply = 'Satellite ID missing.'
return reply
@botcmd(thread=True)
def iss(self, mess, args):
"""iss - show ISS passes
"""
return self._satteliteRequest(mess, args, '25544')
@botcmd
def tw(self, mess, args):
"""tw [date] [location] - show begin/end of current twilight
"""
jid, loc, dt = self._parseJidLocTime(mess, args)
# Set noon of if date is set
if dt != None:
dt = self._getNoonDateTimeFrom6To6ByDate(dt)
next_rising, next_setting, riset = self._getNextRiseSetting(jid, ephem.Sun(), dt=dt, loc=loc, horizon='-18.0')
if riset == SkybberBot.RISET_OK:
reply = SkybberBot.UNICODE_SET + formatLocalTime(next_setting) + ' - ' + SkybberBot.UNICODE_RISE + formatLocalTime(next_rising)
elif riset == SkybberBot.NEVER_SETTING:
reply = SkybberBot.MSG_NO_ASTRONOMICAL_NIGHT
else:
reply = SkybberBot.MSG_FULL_ASTRONOMICAL_NIGHT
return reply
@botcmd
def night(self, mess, args):
"""night [date] [location] - show the real night, taking into consideration the Moon rising/setting
"""
jid, loc, dt = self._parseJidLocTime(mess, args)
# Set noon of if date is set
if dt != None:
dt = self._getNoonDateTimeFrom6To6ByDate(dt)
next_sun_rising, next_sun_setting, riset_sun = self._getNextRiseSetting(jid, ephem.Sun(), dt=dt, loc=loc, horizon='-18.0')
next_moon_rising, next_moon_setting, riset_moon = self._getNextRiseSetting(jid, ephem.Moon(), dt=dt, loc=loc)
if riset_sun == SkybberBot.NEVER_SETTING:
return SkybberBot.MSG_NO_ASTRONOMICAL_NIGHT
full_night = False
if riset_sun == SkybberBot.NEVER_SETTING:
# TODO :
# tw_start = start day time
# tw_end = end day time
full_night = True
pass
else:
tw_start = next_sun_setting
tw_end = next_sun_rising
tw_middle_end = None
tw_middle_start = None
if riset_moon == SkybberBot.RISET_OK:
if next_moon_rising < next_moon_setting:
if next_moon_rising < next_sun_setting:
if next_moon_setting > next_sun_setting:
if next_moon_setting < next_sun_rising:
tw_start = next_moon_setting # MR - SS - MS - SR
else:
return SkybberBot.MSG_NO_ASTRONOMICAL_NIGHT # MR - SS - SR - MS
else:
pass # MR - MS - SS - SR
else:
if next_moon_rising < next_sun_rising:
if next_moon_setting < next_sun_rising:
# SS - MR - MS - SR
tw_middle_end = next_moon_rising
tw_middle_start = next_moon_rising
else:
tw_end = next_moon_rising # SS - MR - SR - MS
else:
pass # SS - SR - MR - MS
else:
if next_moon_setting < next_sun_setting:
if next_moon_rising > next_sun_setting:
if next_moon_rising < next_sun_rising:
tw_end = next_moon_rising # MS - SS - MR - SR
else:
pass # MS - SS - SR - MR
else:
return SkybberBot.MSG_NO_ASTRONOMICAL_NIGHT # MS - MR - SS - SR
else:
if next_moon_setting < next_sun_rising:
if next_moon_rising < next_sun_rising:
# SS - MS - MR - SR
tw_start = next_moon_setting
tw_end = next_moon_rising
else:
tw_start = next_moon_setting # SS - MS - SR - MR
else:
return SkybberBot.MSG_NO_ASTRONOMICAL_NIGHT # SS - SR - MS - MR
else:
if riset_moon == SkybberBot.NEVER_SETTING:
return SkybberBot.MSG_NO_ASTRONOMICAL_NIGHT
elif full_night:
return SkybberBot.MSG_FULL_ASTRONOMICAL_NIGHT
if tw_middle_start is None:
reply = SkybberBot.UNICODE_SET + formatLocalTime(tw_start) + ' - ' + SkybberBot.UNICODE_RISE + formatLocalTime(tw_end)
else:
reply = SkybberBot.UNICODE_SET + formatLocalTime(tw_start) + ' - ' + SkybberBot.UNICODE_RISE + formatLocalTime(tw_middle_end) + ' , ' + \
SkybberBot.UNICODE_SET + formatLocalTime(tw_middle_end) + ' - ' + SkybberBot.UNICODE_RISE + formatLocalTime(tw_end)
return reply
@botcmd
def sun(self, mess, args):
"""sun [date] [location] - show sun info
"""
return self._doBodyEphem(mess, args, u'\u2609', ephem.Sun(), with_constell_mag=False, rising_first=False)
@botcmd
def moon(self, mess, args):
"""moon [date] [location] - show Moon ephemeris
"""
body = ephem.Moon()
reply = self._doBodyEphem(mess, args, u'\u263D', body, with_constell_mag=False)
reply += ' Phase ' + ("%0.1f" % body.phase)
reply += ' [ ' + ephem.constellation(body)[1] + ' ]'
return reply
@botcmd
def mer(self, mess, args):
"""mer [date] [location] - show Mercury ephemeris
"""
return self._doInnerBodyEphem(mess, args, u'\u263F', ephem.Mercury())
@botcmd
def ven(self, mess, args):
"""ven [date] [location] - show Venus ephemeris
"""
return self._doInnerBodyEphem(mess, args, u'\u2640', ephem.Venus())
@botcmd
def mar(self, mess, args):
"""mar [date] [location] - show Mars ephemeris
"""
return self._doBodyEphem(mess, args, u'\u2642', ephem.Mars())
@botcmd
def jup(self, mess, args):
"""jup [date] [location] - show Jupiter ephemeris
"""
return self._doBodyEphem(mess, args, u'\u2643', ephem.Jupiter())
@botcmd
def sat(self, mess, args):
"""sat [date] [location] - show Saturn ephemeris
"""
return self._doBodyEphem(mess, args, u'\u2644', ephem.Saturn())
@botcmd
def reg(self, mess, args):
"""reg - register user into skybber
"""
with MasterDBConnection() as c:
str_jid = mess.getFrom().getStripped()
user = self._getUser(c, str_jid, reg_check=False)
if user is not None:
raise CmdError('User ' + user.getJID() + ' is already registered !')
user = User.createUser(c, mess.getFrom().getStripped())
reply = 'User ' + user.getJID() + ' is registered.'
return reply
@botcmd(allowed_roles={'registered'})
def unregister(self, mess, args):
"""unregister - unregister user from skybber
"""
with MasterDBConnection() as c:
user = self._getUser(c, mess.getFrom().getStripped())
user.delete(c)
reply = 'User ' + user.getJID() + ' was unregistered.'
return reply
@botcmd(allowed_roles={'registered'})
def prof(self, mess, args):
"""prof - show user profile
"""
with MasterDBConnection() as c:
user = self._getUser(c, mess.getFrom().getStripped())
reply = '\nJID :' + user.getJID() + '\nDescription: ' + user.getProfileDescription() + '\nDefault location: '
loc = user.getDefaultLocation(c)
if loc is None:
reply += 'undefined.'
else:
reply += loc.getName()
return reply
@botcmd(allowed_roles={'registered'})
def addloc(self, mess, args):
"""addloc <name> <longitude> <latitude> - add user location.
"""
with MasterDBConnection() as c:
user = self._getUser(c, mess.getFrom().getStripped())
pargs = self._arg_re.split(args.strip())
if len(pargs) == 3:
reply = self._doAddLoc(c, user, pargs[0].strip(), pargs[1].strip(), pargs[2].strip())
else:
reply = 'Invalid number of arguments.'
return reply
@botcmd(allowed_roles={'registered'})
def rmloc(self, mess, args):
"""rmloc <name> - remove location.
"""
with MasterDBConnection() as c:
user = self._getUser(c, mess.getFrom().getStripped())
pargs = self._arg_re.split(args.strip())
if len(pargs) == 1:
loc = user.getLocationByName(c, pargs[0])
loc.delete(c)
reply = 'Location "' + loc.getInfo() + '" was removed.'
elif len(pargs) == 0:
reply = 'Argument - location name - expected.'
else:
reply = 'Invalid number of arguments.'
return reply
@botcmd(allowed_roles={'registered'})
def loc(self, mess, args):
"""loc <name> - set the location as the new default location .
"""
with MasterDBConnection() as c:
user = self._getUser(c, mess.getFrom().getStripped())
pargs = self._arg_re.split(args.strip())
if len(pargs) == 1:
loc = user.getLocationByName(c, pargs[0])
if loc is not None:
user.setDefaultLocation(c, loc)
reply = loc.getInfo() + ' is your default location now.'
elif len(pargs) == 0:
reply = 'Argument - location name - expected.'
else:
reply = 'Invalid number of arguments.'
return reply
@botcmd(allowed_roles={'registered'})
def lsloc(self, mess, args):
"""lsloc - show the list of locations
"""
with MasterDBConnection() as c:
user = self._getUser(c, mess.getFrom().getStripped())
def_loc = user.getDefaultLocation(c)
reply = '\nUser locations : \n'
for loc in user.getUserLocationList(c):
reply += loc.getInfo()
if def_loc is not None and def_loc.getLocationId() == loc.getLocationId():
reply += ' *'
reply += '\n'
return reply
def check_role(self, allowed_roles, mess):
"""Overridden from JabberBot
check if user from message heas enough rights for specified role
"""
permit = True
if allowed_roles is not None:
user_roles = self._getUserRoles(mess.getFrom().getStripped())
if user_roles is None:
permit = False
else:
permit = not user_roles.isdisjoint(allowed_roles)
return permit
def execute_command(self, mess, cmd, args):
""" Overridden from JabberBot
"""
try:
reply = MUCJabberBot.execute_command(self, mess, cmd, args)
except CmdError as e:
reply = e.value
return reply
def _satteliteRequest(self, mess, args, satid):
""" TODO:
"""
jid, loc, _ = self._parseJidLocTime(mess, args)
lng, lat = self._getObserverStrCoord(jid, loc)
req = urllib.request.Request('http://uhaapi-skybber.rhcloud.com/satellites/' + satid + '/passes?lat=' + lat + '&lng=' + lng, None, {})
req.addheader('Accept', 'application/xml')
try:
reply = urllib.request.urlopen(req).read()
sp = SatellitePasses()
sp.parseFromXml(reply)
return sp.format()
except urllib2.URLError:
return 'Service disconnected.'
def _getUser(self, c, strjid, reg_check=True):
""" Return registered user
"""
user = User.getUserbyJID(c, strjid)
if reg_check and user is None :
raise CmdError('User ' + strjid + ' is not registered.')
return user
def _getObserverByName(self, jid, loc_name = None):
"""Create observer object initialized from location
1. It looks for location by location_name for given user(jid)
2. if not exists then it looks for user default location
3. if not exists then returns first user location
"""
observer = None
with MasterDBConnection() as c:
user = self._getUser(c, jid, reg_check=False)
if user is not None:
if loc_name is not None:
loc = user.getLocationByName(c, loc_name)
# TODO : return message if loc is None
else:
loc = user.getDefaultLocation(c)
if loc is None:
loc = user.getUserLocationList(c, 1)
if loc is not None:
observer = ephem.Observer()
observer.long, observer.lat = toradians(loc.getLng()), toradians(loc.getLat())
observer.elevation = 0
if observer is None:
observer = self._obsr_default
return observer
def _getObserver(self, jid, loc):
"""Return copy of observer object
"""
obsrv = ephem.Observer()
if loc is not None:
if loc.getName() is not None:
co = self._getObserverByName(jid, loc_name=loc.getName())
obsrv.long, obsrv.lat, obsrv.elevation = co.long, co.lat, co.elevation
else:
obsrv.long, obsrv.lat = loc.getLng(), loc.getLat()
else:
co = self._getObserverByName(jid)
obsrv.long, obsrv.lat, obsrv.elevation = co.long, co.lat, co.elevation
return obsrv
def _getObserverStrCoord(self, jid, loc):
"""Get observer's coordinations in string form
"""
observer = self._getObserver(jid, loc)
lng = todegrees(observer.long)
lat = todegrees(observer.lat)
return ("%0.3f" % lng), ("%0.3f" % lat)
def _checkArgSatId(self, args):
satid = None
next_args = None
pargs = self._arg_re.split(args.strip())
if pargs is None or len(pargs) == 0:
reply = 'Argument expected. Please specify satellite ID.'
else:
try:
satid = int(pargs[0])
reply = None
next_args = args[len(pargs[0]):]
except ValueError:
reply = 'Argument expected. Please specify satellite ID.'
return (satid, next_args, reply)
def _getUserRoles(self, jid):
"""Return list of user's roles
"""
with MasterDBConnection() as c:
user = self._getUser(c, jid, reg_check=False)
if user is not None:
return {'registered'}
return None
def _doAddLoc(self, c, user, loc_name, sval1, sval2):
""" Add location to list of locations. It reads geographic position in angle or geo format
"""
val1 = TypeDetector(sval1)
val2 = TypeDetector(sval2)
lng = 0.0
lat = 0.0
if val1.getType() == TypeDetector.FLOAT:
if val2.getType() == TypeDetector.FLOAT:
lng = val1.getTypeValue()
lat = val2.getTypeValue()
else:
return 'Invalid argument: "' + sval2 + '". Number is expected.'
elif val1.getType() == TypeDetector.LOCATION_LONG:
if val2.getType() == TypeDetector.LOCATION_LAT:
lng = val1.getTypeValue()
lat = val2.getTypeValue()
else:
return 'Invalid argument: "' + sval2 + u'". Latitude expected. Example: 15??3\'53.856"E'
elif val1.getType() == TypeDetector.LOCATION_LAT:
if val2.getType() == TypeDetector.LOCATION_LONG:
lat = val1.getTypeValue()
lng = val2.getTypeValue()
else:
return 'Invalid argument: "' + sval2 + u'". Longitude expected. Example: 50??46\'1.655"N'
else:
return 'Invalid format of argument value: "' + sval1 + '". Use help for .'
loc = user.getLocationByName(c, loc_name)
if loc is None:
user_locations = user.getUserLocationList(c)
if len(user_locations) < self.MAX_USER_LOCATIONS:
loc = user.createLocation(c, loc_name, lng, lat)
reply = 'Location "' + loc.getInfo() + '" added.'
# set default location if it is a first one
if len(user_locations) == 0:
user.setDefaultLocation(c, loc)
reply += ' Location set as default location.'
else:
reply = 'Add location failed. Number of user locations exceeded limit ' + self.MAX_USER_LOCATIONS
else:
reply = 'Location "' + loc.getInfo() + '" already exists.'
return reply
def _fmtRiSetFailMsg(self, body, riset):
result = body.name + ' '
if riset == SkybberBot.NEVER_RISING:
result += 'never rising.'
else:
result += 'never setting.'
def _doInnerBodyEphem(self, mess, args, unic_symb, body, with_constell_mag=True):
body.compute()
elong = math.degrees(body.elong)
jid, loc, dt = self._parseJidLocTime(mess, args)
next_rising, next_setting, riset = self._getNextRiseSetting(jid, body, dt=dt, loc=loc, horizon='0.0')
if riset == SkybberBot.RISET_OK:
if elong > 0.0:
result = unic_symb + ' ' + SkybberBot.UNICODE_RISE + formatLocalTime(next_setting)
else:
result = unic_symb + ' ' + SkybberBot.UNICODE_RISE + formatLocalTime(next_rising)
else:
result = self._fmtRiSetFailMsg(body, riset)
result += ' Elong ' + ("%0.2f" % elong)
if with_constell_mag:
result += ' ' + str(body.mag) + 'm'
result += ' [ ' + ephem.constellation(body)[1] + ' ]'
return result
def _doBodyEphem(self, mess, args, unic_symb, body, with_constell_mag=True, rising_first=True):
""" Return next rise/setting for specified body.
"""
body.compute()
jid, loc, dt = self._parseJidLocTime(mess, args)
next_rising, next_setting, riset = self._getNextRiseSetting(jid, body, dt=dt, loc=loc, horizon='0.0')
if riset == SkybberBot.RISET_OK:
if rising_first:
result = unic_symb + ' ' + SkybberBot.UNICODE_RISE + formatLocalTime(next_rising) + ' ' + SkybberBot.UNICODE_SET + formatLocalTime(next_setting)
else:
result = unic_symb + ' ' + SkybberBot.UNICODE_SET + formatLocalTime(next_setting) + ' ' + SkybberBot.UNICODE_RISE + formatLocalTime(next_rising)
else:
result = self._fmtRiSetFailMsg(body, riset)
if with_constell_mag:
result += ' ' + str(body.mag) + 'm'
result += ' [ ' + ephem.constellation(body)[1] + ' ]'
return result
def _getNextRiseSetting(self, jid, body, loc=None, dt=None, horizon = '0.0'):
""" Return next rising/setting time for given body, horizont and date
"""
observer = self._getObserver(jid, loc)
observer.horizon = horizon
if dt == None:
dt = self._getNoonDateTimeFrom6To6()
observer.date = ephem.Date(dt)
try:
next_rising = observer.next_rising(body)
next_setting = observer.next_setting(body)
riset = SkybberBot.RISET_OK
except ephem.NeverUpError:
next_rising = None
next_setting = None
riset = SkybberBot.NEVER_RISING
except ephem.AlwaysUpError:
next_rising = None
next_setting = None
riset = SkybberBot.NEVER_SETTING
return (next_rising, next_setting, riset)
def _getNoonDateTimeFrom6To6(self):
""" Return noon of day beetween 06:00 of that day to next day 06:00
"""
date = datetime.date.today()
if datetime.datetime.now().hour < 6:
date -= datetime.timedelta(1)
dt = datetime.datetime.combine(datetime.date.today(), datetime.time(12,0))
dt = dt + datetime.timedelta(0, time.timezone)
return dt
def _getNoonDateTimeFrom6To6ByDate(self, date):
""" Return noon of day beetween 06:00 of that day to next day 06:00
"""
dt = datetime.datetime.combine(date, datetime.time(12,0))
dt = dt + datetime.timedelta(0, time.timezone)
return dt
def _parseJidLocTime(self, mess, args, parse_date = True):
jid = mess.getFrom().getStripped()
args = args.strip()
if args is None or len(args) == 0:
return (jid, None, None)
loc = None
dt = None
lng = None
lat = None
loc_name = None
pargs = self._arg_re.split(args.strip())
if pargs is not None:
for arg in pargs:
parsed_arg = TypeDetector(arg)
if parse_date and parsed_arg.getType() == TypeDetector.DATE:
if dt is not None:
raise CmdError('Invalid double date argument: ' + arg)
dt = parsed_arg.getTypeValue()
elif parsed_arg.getType() == TypeDetector.LOCATION_LONG:
if lng is not None:
raise CmdError('Invalid double longitude argument: ' + arg)
lng = parsed_arg.getTypeValue()
elif parsed_arg.getType() == TypeDetector.LOCATION_LAT:
if lat is not None:
raise CmdError('Invalid double latitude argument: ' + arg)
lat = parsed_arg.getTypeValue()
elif parsed_arg.getType() == TypeDetector.STRING:
if loc_name is not None:
raise CmdError('Invalid double location name argument: ' + arg)
loc_name = parsed_arg.getValue()
else:
raise CmdError('Invalid argument: ' + arg)
if (lng is not None or lat is not None) and loc_name is not None:
raise CmdError('Invalid arguments. Please specify only one of longitude/latitude or location name.')
if lng is not None and lat is None:
raise CmdError('Invalid argument. Latitude missing.')
if lng is None and lat is not None:
raise CmdError('Invalid argument. Latitude missing.')
if lng is not None:
loc = Location(None, None, None, lng, lat)
elif loc_name is not None:
loc = Location(None, None, loc_name, None, None)
return (jid, loc, dt)