summaryrefslogtreecommitdiff
path: root/modules/tracking.py
blob: 70c85b09ed53516664bf30d1a2e69415dac3ca42 (plain)
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
info = {
	'author': 'Jon Bergli Heier',
	'title': 'Posten.no tracking',
	'description': 'Fetches tracking info from posten.no.',
}

cfg_section = 'module/tracking'

import urllib2, datetime, re, pytz, json, urllib
from xml.etree import ElementTree as ET
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relation, backref
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql import or_, and_
import dateutil.parser
import easypost
import requests

engine = Session = None
Base = declarative_base()

class Consignment(Base):
	__tablename__ = 'consignment'

	id = Column(Integer, primary_key = True)
	server = Column(String, nullable = False)
	nick = Column(String, nullable = False)
	channel = Column(String) # NULL for private
	code = Column(String, nullable = False, index = True)
	label = Column(String, index = True)
	added = Column(DateTime, nullable = False)
	last = Column(DateTime)
	type = Column(String, nullable = False) # Tracking module

	packages = relation('Package', backref = 'consignment', primaryjoin = 'Package.consignment_id == Consignment.id')

	__table_args__ = (UniqueConstraint('code', 'type', name = 'consignment_code_type_uc'),)

	def __init__(self, server, nick, channel, type, code, label, added):
		self.server = server
		self.nick = nick
		self.channel = channel
		self.type = type
		self.code = code
		self.label = label
		self.added = added

	def __str__(self):
		s = '\002%s\002' % self.code.encode('utf-8')
		if self.label:
			s += ' (%s)' % self.label.encode('utf-8')
		return s

	def __unicode__(self):
		return str(self).decode('utf-8')

class Package(Base):
	__tablename__ = 'package'

	id = Column(Integer, primary_key = True)
	consignment_id = Column(Integer, ForeignKey('consignment.id'))
	code = Column(String, nullable = False, index = True)
	last = Column(DateTime) # Last update
	status = Column(String) # Last status

	def __init__(self, consignment_id, code):
		self.consignment_id = consignment_id
		self.code = code

	def __str__(self):
		s = '\002%s\002' % self.code.encode('utf-8')
		if self.consignment.label:
			s += ' (%s)' % self.consignment.label.encode('utf-8')
		return s

	def __unicode__(self):
		return str(self).decode('utf-8')

class PackageError(Exception): pass
class NoPackageFound(PackageError): pass
class UnknownTypeError(PackageError): pass

class TrackingResult:
	def __init__(self, code, date, desc, delivered = False, previous_code = None):
		self.code = code
		self.date = date
		self.desc = desc
		self.delivered = delivered
		# Set when a package changes tracking code.
		self.previous_code = previous_code

	def __str__(self):
		return '\002{code}\002 {date} - {desc}'.format(code = self.code, date = self.date, desc = self.desc)

tracking_modules = {}

class TrackingModuleMeta(type):
	def __init__(cls, name, bases, dict):
		if name != 'TrackingModule':
			tracking_modules[cls.name] = cls

class TrackingModule(object):
	__metaclass__ = TrackingModuleMeta
	config_section = 'module/tracking'

	def splitcode(self, code):
		return code_split(code)[-1]

class PostenModule(TrackingModule):
	name = 'posten'
	url = 'http://sporing.posten.no/sporing.xml?q=%s'

	def get_xml(self, url):
		try:
			u = urllib2.urlopen(url, timeout = config.getfloat(cfg_section, 'timeout'))
		except urllib2.HTTPError as e:
			if e.code == 404:
				raise NoPackageFound('No packages found')
			raise PackageError(str(e))
		except Exception as e:
			raise PackageError(str(e))
		if u.headers['content-type'].startswith('application/xml'):
			xml = ET.parse(u)
		else:
			xml = None
		u.close()
		return xml

	def get_url(self, code = None):
		url = 'http://sporing.posten.no/'
		if code:
			url += 'sporing.html?q=' + code
		return url

	def track(self, code):
		code = self.splitcode(code)
		xml = self.get_xml(self.url % code)
		if not xml:
			return

		ns = 'http://www.bring.no/sporing/1.0'
		packages = xml.find('.//{%s}PackageSet' % (ns,))
		if packages is None:
			raise NoPackageFound('No packages found for \002%s\002.' % code)

		results = []
		for package in packages:
			code = package.attrib['packageId']
			if 'previousPackageId' in package.attrib:
				previous_code = package.attrib['previousPackageId']
			else:
				previous_code = None
			eventset = package.find('{%s}EventSet' % ns)
			if not len(eventset):
				continue
			last = eventset[0]
			desc = last.find('{%s}Description' % ns).text.replace('&lt;', '<').replace('<br/>', ' ')
			desc = re.sub(r'<[^>]*?>', '', desc).encode('utf8').strip()
			isodate = last.find('{%s}OccuredAtIsoDateTime' % ns).text[:-10]
			isodate = datetime.datetime.strptime(isodate, '%Y-%m-%dT%H:%M:%S')
			city = last.find('{%s}City' % ns).text
			status = last.find('{%s}Status' % ns).text.encode('utf-8')
			if city:
				desc = '%s (%s)' % (desc, city.encode('utf8'))
			date = last.find('{%s}OccuredAtDisplayDate' % ns).text + ' ' + last.find('{%s}OccuredAtDisplayTime' % ns).text
			results.append(TrackingResult(code, isodate, desc, status == 'DELIVERED', previous_code = previous_code))
		return results

class FedexModule(TrackingModule):
	name = 'fedex'

	def get_url(self, code = None):
		url = 'https://www.fedex.com/fedextrack/index.html'
		if code:
			url += '?tracknumbers=' + code
		return url

	def track(self, code):
		code = self.splitcode(code)
		data = {'TrackPackagesRequest': {'appType': 'wtrk', 'uniqueKey': '', 'processingParameters': {'anonymousTransaction': True, 'clientId': 'WTRK', 'returnDetailedErrors': True, 'returnLocalizedDateTime': False}, 'trackingInfoList': [{'trackNumberInfo': {'trackingNumber': code, 'trackingQualifier': '', 'trackingCarrier': ''}}]}}
		url = 'https://www.fedex.com/trackingCal/track?' + urllib.urlencode({'data': json.dumps(data), 'action': 'trackpackages', 'locale': 'en_US', 'format': 'json', 'version': 99})
		try:
			request = urllib2.Request(url, headers = {'User-Agent': 'fot'})
			u = urllib2.urlopen(request)
		except urllib2.HTTPError as e:
			raise PackageError(str(e))
		data = u.read()
		u.close()
		for m in re.findall(r'(\\x[a-fA-F\d]{2})', data):
			data = data.replace(m, chr(int(m[-2:], 16)))
		data = json.loads(data)
		response = data['TrackPackagesResponse']
		packages = response['packageList']
		results = []
		for package in packages:
			lastevent = package['scanEventList'][0]
			# Ignore events with missing date or time, this seems to be a temporary thing.
			if not lastevent['date'] or not lastevent['time']:
				continue
			date = datetime.datetime.strptime('%s %s' % (lastevent['date'], lastevent['time']), '%Y-%m-%d %H:%M:%S')
			if lastevent['gmtOffset']:
				tz = pytz.timezone('Etc/GMT' + ('+' if lastevent['gmtOffset'][0] == '-' else '-') + str(int(lastevent['gmtOffset'][1:3])))
				date = tz.localize(date).astimezone(pytz.timezone(config.get(cfg_section, 'local_timezone'))).replace(tzinfo = None)
			status = lastevent['status'].encode('utf-8')
			if lastevent['scanDetails']:
				status += ' - %s' % lastevent['scanDetails'].encode('utf-8')
			if lastevent['scanLocation']:
				status += ' (%s)' % lastevent['scanLocation'].encode('utf-8')
			results.append(TrackingResult(code, date, status, lastevent['isDelivered']))
		return results

class PostNordModule(TrackingModule):
	name = 'postnord'

	def get_url(self, code = None):
		url = 'http://www.postnordlogistics.no/minside/SOPS/'
		if code:
			url += code
		return url

	def track(self, code):
		code = self.splitcode(code)
		url = 'http://www.postnordlogistics.no/XMLServer/rest/trackandtrace?' + urllib.urlencode({'q': code})
		try:
			u = urllib2.urlopen(url)
		except urllib2.HTTPError as e:
			raise PackageError(str(e))
		data = json.load(u)
		u.close()
		response = data['TrackingInformationResponse']
		results = []
		for shipment in response['shipments']:
			for item in shipment['items']:
				code = item['itemId']
				delivered = (item.get('status') == 'DELIVERED')
				lastevent = item['events'][-1]
				date = datetime.datetime.strptime(lastevent['eventTime'], '%Y-%m-%dT%H:%M:%S')
				event = lastevent['eventDescription']
				location = lastevent.get('location')
				if location:
					location = lastevent['location'].get('displayName') or lastevent['location'].get('city')
				if location:
					status = '%s (%s)' % (event, location)
				else:
					status = event
				results.append(TrackingResult(code, date, status.encode('utf-8'), delivered))
		return results

class UpsModule(TrackingModule):
	name = 'ups'

	def get_url(self, code = None):
		if code is None:
			url = 'http://www.ups.com/WebTracking/track'
		else:
			data = {
				'TypeOfInquiryNumber': 'T',
				'InquiryNumber1': code,
			}
			url = 'http://www.ups.com/WebTracking/processInputRequest?' + urllib.urlencode(data)
		return url

	def track(self, code):
		code = self.splitcode(code)
		
		access_key = config.get(self.config_section, 'ups_access_key')
		username = config.get(self.config_section, 'ups_username')
		password = config.get(self.config_section, 'ups_password')

		url = 'https://wwwcie.ups.com/ups.app/xml/Track'

		request = """<?xml version="1.0" ?>
			<AccessRequest xml:lang='en-US'>
				<AccessLicenseNumber>%s</AccessLicenseNumber>
					<UserId>%s</UserId>
					<Password>%s</Password>
				</AccessRequest>
			<?xml version="1.0" ?>
			<TrackRequest>
				<Request>
				<RequestAction>Track</RequestAction>
				</Request>
				<TrackingNumber>%s</TrackingNumber>
			</TrackRequest>""" % (access_key, username, password, code)

		try:
			u = urllib2.urlopen(url, request)
		except urllib2.HTTPError as e:
			raise PackageError(str(e))

		xml = ET.parse(u)

		packages = xml.findall('Shipment/Package')
		if packages is None:
			raise NoPackageFound('No packages found for \002%s\002.' % code)

		results = []
		for package in packages:
			code = package.find('TrackingNumber').text

			activity = package.find('Activity')
			if activity is None:
				continue

			address = activity.find('ActivityLocation/Address')
			city = address.find('City')
			cc = address.find('CountryCode')
			if city is not None and cc is not None:
				location_text = "%s, %s" % (city.text, cc.text)
			else:
				location_text = None
			statuscode = activity.find('Status/StatusType/Code').text
			event_text = activity.find('Status/StatusType/Description').text
			date = activity.find('Date').text
			time = activity.find('Time').text
			year, month, day = int(date[0:4]), int(date[4:6]), int(date[6:8])
			hour, minute, second = int(time[0:2]), int(time[2:4]), int(time[4:6])
			isodate = datetime.datetime(year, month, day, hour, minute, second)
			desc = event_text
			if location_text:
				desc += " (%s)" % location_text
			results.append(TrackingResult(code, isodate, desc, statuscode == 'D'))
		return results

class EasypostModule:
	def track(self, code):
		code = self.splitcode(code)
		
		try:
			tracker = (t for t in easypost.Tracker.all()['trackers'] if t.tracking_code == code).next()
		except StopIteration:
			tracker = easypost.Tracker.create(tracking_code = code, carrier = self.easypost_carrier)
		
		results = []
		
		if tracker.tracking_details:
			delivered = tracker.status == 'delivered'
			lastevent = tracker.tracking_details[-1]
			date = datetime.datetime.strptime(lastevent.datetime, '%Y-%m-%dT%H:%M:%SZ')
			status = lastevent.message
			location = []
			for f in ('city', 'state', 'country'):
				if lastevent['tracking_location'][f]:
					location.append(lastevent['tracking_location'][f])
			if location:
				status += ' (%s)' % ', '.join(location)
			results.append(TrackingResult(code, date, status.encode('utf-8'), delivered))
		
		return results

class DHLModule(TrackingModule, EasypostModule):
	name = 'dhl'
	easypost_carrier = 'DHLExpress'
	
	def get_url(self, code = None):
		url = 'http://www.dhl.no/no/express/sporing.html?AWB='
		if code:
			url += code
		return url

class DHLGMModule(TrackingModule, EasypostModule):
	name = 'dhlgm'
	easypost_carrier = 'DHLGlobalMail'

	def get_url(self, code = None):
		url = 'http://webtrack.dhlglobalmail.com/?trackingnumber='
		if code:
			url += code
		return url

class PitneyBowes(TrackingModule):
	name = 'pb'

	def track(self, code):
		code = self.splitcode(code)
		url = 'https://parceltracking.pb.com/tracking/services/v1/track-packages/' + code
		try:
			response = requests.get(url, timeout = config.getfloat(cfg_section, 'timeout'), verify = False)
		except Exception as e:
			raise PackageError(str(e))
		data = response.json()
		date = dateutil.parser.parse('%sT%s' % (data['currentStatus']['eventDate'], data['currentStatus']['eventTime']))
		date = date.astimezone(pytz.timezone(config.get(cfg_section, 'local_timezone'))).replace(tzinfo = None)
		desc = data['currentStatus']['eventDescription']
		location = data['currentStatus']['eventLocation'].get('city') or ''
		if location and data['currentStatus']['eventLocation'].get('country'):
			location += u', ' + data['currentStatus']['eventLocation']['country']
		if location:
			desc += u' (%s)' % location
		desc = desc.encode('utf-8')
		return [TrackingResult(code, date, desc, data['currentStatus']['packageStatus'] == 'DELIVERED')]

	def get_url(self, code = None):
		url = 'https://parceltracking.pb.com/app/#/dashboard/'
		if code:
			url += code
		return url

def code_split(code):
	if ':' in code:
		return code.split(':')
	else:
		return 'posten', code

def get_tracking_module(arg):
	type, code = code_split(arg)

	if not type in tracking_modules:
		raise UnknownTypeError('Unknown type "%s"' % type)

	return tracking_modules[type]()

class Module:
	def __init__(self, bot):
		easypost.api_key = config.get(cfg_section, 'easypost_api_key')
		self.setup_db()
		self.irc = bot
		self.tracking = []
		self.lc = LoopingCall(self.lc_callback)
		if bot:
			self.lc.start(config.getfloat(cfg_section, 'interval'), False)
			self.irc.register_keyword('!track', self)

	def stop(self):
		try:
			self.lc.stop()
		except:
			pass

	def setup_db(self):
		global engine, Session

		if engine and Session:
			return

		engine = create_engine(config.get(cfg_section, 'db_path'))
		Base.metadata.create_all(engine)
		Session = sessionmaker(bind = engine, autoflush = True, autocommit = False)

	def track_start(self, code, label, nick, channel):
		type, code = code_split(code)
		if not type in tracking_modules:
			return '\002%s\002 is an invalid type.' % type
		if not len(code):
			return 'Missing tracking number.'

		msg = None
		try:
			session = Session()
			consignment = Consignment(self.irc.factory.server, nick, channel, type, code, label.decode('utf8'), datetime.datetime.utcnow())
			session.add(consignment)
			session.commit()
			msg = 'Now tracking \002%s\002.' % code
			reactor.callLater(1, self.track_update, type, code, True)
		except IntegrityError as e:
			msg = 'Already tracking \002%s\002.' % code
		finally:
			session.close()
		return msg

	def track_stop(self, code, nick, channel):
		type, code = code_split(code)
		if not len(code):
			return 'Missing tracking number or label.'
		msg = None
		try:
			session = Session()
			consignments = session.query(Consignment).outerjoin(Consignment.packages) \
				.filter(and_(Consignment.server == self.irc.factory.server,
				or_(Consignment.code == code, Consignment.label == code, Package.code == code)))
			if nick:
				consignments = consignments.filter(Consignment.nick == nick)
			deleted = []
			consignments = consignments.all()
			if not len(consignments):
				raise NoResultFound
			# we should have exactly one consignment, but need to handle cases when there are more
			for consignment in consignments:
				code = str(consignment)
				for p in consignment.packages:
					session.delete(p)
				deleted.append(code)
				session.delete(consignment)
			session.commit()
			msg = 'No longer tracking %s' % ', '.join(deleted)
		except NoResultFound:
			msg = '\002%s\002 is not being tracked.' % code.decode('utf8')
		finally:
			session.close()
		return msg

	def track_status(self, code, nick, channel):
		# don't code_split() here; this is done later if no results are returned
		msg = []
		try:
			session = Session()
			consignments = session.query(Consignment).filter(and_(Consignment.server == self.irc.factory.server,
				or_(Consignment.code.like(code + '%'), Consignment.label.like((code + '%')))))
			if nick:
				consignments = consignments.filter(Consignment.nick == nick)
			results = []
			for row in consignments:
				try:
					self.track_update(row.type, row.code, propagate_error = True)
				except NoPackageFound:
					results.append('No packages found for %s' % unicode(row))
					continue
				except PackageError as e:
					results.append('Failed to fetch data for %s.' % unicode(row))
					continue
				i = 0

				for package in row.packages:
					i += 1
					date = package.last
					desc = package.status

					if date and desc:
						s = '%s %s - %s' % (unicode(package), date, desc)
					else:
						s = 'No tracking info found for %s' % unicode(package)

					results.append(s)

				if i == 0:
					results.append('No packages found for %s' % unicode(row))

			if len(results):
				msg = results
			elif len(code):
				try:
					# type:code is handled by get_tracking_module()
					tm = get_tracking_module(code)
					data = tm.track(code)
					if data:
						for package in data:
							msg.append('\002%s\002 %s - %s' % (package.code, package.date, package.desc.decode('utf8')))
					elif code:
						msg = 'Failed to fetch tracking data for \002%s\002.' % code.decode('utf8')
				except PackageError as e:
					msg = str(e)
			else:
				msg = 'No tracking number given or registered.'
		finally:
			session.close()
		return msg

	def track_label(self, code, label, nick, channel):
		type, code = code_split(code)
		if not len(code):
			return 'Missing tracking number or label.'

		msg = None
		try:
			session = Session()
			consignment = session.query(Consignment).filter(and_(Consignment.server == self.irc.factory.server, Consignment.code == code, Consignment.nick == nick)).one()
			consignment.label = label.decode('utf8')
			session.add(consignment)
			session.commit()
			msg = '\002%s\002 now labelled \002%s\002' % (code, label.decode('utf8'))
		finally:
			session.close()
		return msg

	def track_list(self, nick):
		msg = None
		try:
			session = Session()
			trackings = []
			consignments = session.query(Consignment).filter(and_(Consignment.server == self.irc.factory.server, Consignment.nick == nick))
			for row in consignments:
				trackings.append(str(row))
			if len(trackings):
				msg = 'Trackings for %s: %s' % (nick, ', '.join(trackings))
			else:
				msg = 'No trackings for %s' % nick
		except NoResultFound:
			msg = 'No trackings registered for %s.'
		finally:
			session.close()
		return msg

	def track_url(self, code, nick, channel):
		msg = []
		try:
			session = Session()
			consignments = session.query(Consignment).filter(and_(Consignment.server == self.irc.factory.server,
				or_(Consignment.code.like(code + '%'), Consignment.label.like(code+  '%'))))
			if nick:
				consignments = consignments.filter(Consignment.nick == nick)
			results = []
			for consignment in consignments:
				m = get_tracking_module('%s:%s' % (consignment.type, consignment.code))
				msg.append(m.get_url(consignment.code))
			if not len(msg):
				# Rreturn an error only if we got a code to filter with.
				if code:
					msg.append('No packages found.')
				else:
					m = get_tracking_module('')
					msg = [m.get_url()]
		finally:
			session.close()
		return msg

	def keyword(self, nick, channel, kw, msg):
		usage = 'Usage: !track (start TRACKINGNO [LABEL])|(stop|status TRACKINGNO|LABEL) - See !track help for full list'
		args = msg.split()
		if len(args) < 1:
			self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], usage)
			return

		mode = args[0]
		if mode.lower() in ('start', 'add', 'stop', 'status'):
			if len(args) < 2 and mode.lower() != 'status':
				self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], usage)
				return
			if mode.lower() in ('start', 'add'):
				code = args[1]
				label = ' '.join(args[2:]) if len(args) > 2 else ''
			else:
				code = ' '.join(args[1:])
		elif mode.lower() == 'label':
			if len(args) < 3:
				self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], usage)
				return
			code = args[1]
			label = ' '.join(args[2:])
		elif mode.lower() == 'url':
			code = ' '.join(args[1:]) if len(args) > 1 else ''
		elif mode.lower() == 'help':
			self.irc.msg(nick.split('!')[0], '''
start  TRACKINGNO [LABEL] - Start tracking package with optional label (alias: add)
stop   TRACKINGNO|LABEL - Stop tracking package given by either tracking number or label
status TRACKINGNO|LABEL - Show status for registered matches, or look up tracking number if no matches (implicitly updates status)
list   - List all registered tracking numbers without fetching status
label  TRACKINGNO LABEL - Changes the label for the given tracking number
url    [TRACKINGNO] - Lists URLs for matches to the corresponding website''')
			return

		msg = None
		if mode.lower() in ('start', 'add'):
			msg = self.track_start(code, label, nick.split('!')[0], channel if not channel == self.irc.nickname else None)
		elif mode.lower() == 'stop':
			msg = self.track_stop(code, nick.split('!')[0], channel if not channel == self.irc.nickname else None)
		elif mode.lower() == 'status':
			msg = self.track_status(code, nick.split('!')[0], channel if not channel == self.irc.nickname else None)
		elif mode.lower() == 'list':
			msg = self.track_list(nick.split('!')[0])
		elif mode.lower() == 'label':
			msg = self.track_label(code, label, nick.split('!')[0], channel if not channel == self.irc.nickname else None)
		elif mode.lower() == 'url':
			msg = self.track_url(code, nick.split('!')[0], channel if not channel == self.irc.nickname else None)
		else:
			msg = 'Invalid mode "%s".' % mode

		if msg:
			if type(msg) == list:
				for i in msg:
					if isinstance(i, unicode):
						i = i.encode('utf-8')
					self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], i)
			else:
				if isinstance(msg, unicode):
					msg = msg.encode('utf-8')
				self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], msg)
		else:
			self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], 'No data returned (this is a bug).')

	# called by start and status
	def track_update(self, type, code, announce = False, propagate_error = False):
		try:
			session = Session()
			consignment = session.query(Consignment).filter(and_(Consignment.server == self.irc.factory.server, Consignment.type == type, Consignment.code == code)).one()
			self.update_consignment(session, consignment, announce, propagate_error)
			session.commit()
		finally:
			session.close()

	def update_consignment(self, session, consignment, announce = True, propagate_error = False):
		now = datetime.datetime.utcnow()
		td = datetime.timedelta(seconds = config.getint(cfg_section, 'cache_time'))
		# return if consignment was cached within cache_time seconds
		if consignment.last and consignment.last > now - td:
			return
		consignment.last = now
		target = consignment.channel or consignment.nick
		target = target.encode('utf-8')
		removed = False
		tm = tracking_modules[consignment.type]()
		try:
			package_results = tm.track(consignment.code) or []
		except PackageError as e:
			if propagate_error:
				raise e
			# ignore, set results to an empty list so that we can reach the stale check at the end
			package_results = []
		for data in package_results:
			try:
				package = session.query(Package).filter(Package.consignment_id == consignment.id)
				if data.previous_code:
					package = package.filter(Package.code.in_((data.code, data.previous_code)))
				else:
					package = package.filter(Package.code == data.code)
				package = package.one()
			except NoResultFound:
				# don't add delivered packages
				if data.delivered:
					continue
				try:
					conflicts = session.query(Consignment).join(Consignment.packages) \
						.filter(and_(Package.code == data.code, Consignment.type != consignment.type)).all()
				except NoResultFound:
					pass
				else:
					if len(conflicts):
						self.irc.msg(target, '%s: %s conflicts with another consignment' % consignment.nick.encode('utf-8'), consignment)
						removed = True
						break
				package = Package(consignment.id, data.code)
				session.add(package)
				try:
					session.commit()
				except IntegrityError:
					# assume several packets within the same consignment was added as a consignment
					session.rollback()
					removed = True
					self.irc.msg(target, '%s: %s conflicts with another consignment' % (consignment.nick.encode('utf-8'), consignment))
					break
				package = session.query(Package).filter(and_(Package.consignment_id == consignment.id, Package.code == data.code)).one()

			# This happens when the package changes code.
			if package.code != data.code:
				# Always announce.
				msg = '%s: %s has changed code to \002%s\002' % (consignment.nick.encode('utf-8'), package, data.code)
				self.irc.msg(target, msg)
				package.code = data.code
				session.add(package)

			if package.last == None or data.date > package.last:
				code = data.code
				last = data.date
				desc = data.desc
				msg = '%s: %s %s - %s' % (consignment.nick.encode('utf-8'), package, last, desc)
				if data.delivered:
					session.delete(package)
					msg += ' (Package delivered - tracking stopped)'
					removed = True
				else:
					package.last = last
					package.status = desc
					session.add(package)
				if announce or removed:
					self.irc.msg(target, msg)
			elif package.last != None and package.last < datetime.datetime.utcnow() - datetime.timedelta(hours = 24*30):
				msg = '%s: Removing stale package %s' % (consignment.nick.encode('utf-8'), consignment)
				self.irc.msg(target, msg)
				session.delete(package)
				removed = True
		# empty consignment and 30 days old
		if len(consignment.packages) == 0 and consignment.added < datetime.datetime.utcnow() - datetime.timedelta(hours = 24*30):
			msg = '%s: Removing stale consignment %s' % (consignment.nick.encode('utf-8'), consignment)
			self.irc.msg(target, msg)
			session.delete(consignment)
		if removed and len(consignment.packages) == 0:
			msg = '%s: %s is no longer being tracked' % (consignment.nick.encode('utf-8'), consignment)
			self.irc.msg(target, msg)
			session.delete(consignment)

	def lc_callback(self):
		try:
			session = Session()
			consignments = session.query(Consignment).filter(and_(Consignment.server == self.irc.factory.server,
				or_(Consignment.channel.in_(config.get(self.irc.factory.server, 'channels').split()), Consignment.channel == None)))
			for row in consignments:
				self.update_consignment(session, row)
			session.commit()
		except Exception:
			import traceback
			print 'Exception as %s' % datetime.datetime.now()
			traceback.print_exc()
		finally:
			session.close()

if __name__ == '__main__':
	import sys, ConfigParser, os
	config = ConfigParser.ConfigParser()
	config.read([os.path.expanduser('~/.fot')])
	m = Module(None)
	for code in sys.argv[1:]:
		tm = get_tracking_module(code)
		tracking = tm.track(code)
		if tracking:
			for track in tracking:
				if type(track) == list:
					print '\n'.join(track)
				else:
					print track

# vim: noet ts=4