Merge lp:~keegan-csmith/ibid/uct into lp:ibid

Proposed by Keegan Carruthers-Smith
Status: Needs review
Proposed branch: lp:~keegan-csmith/ibid/uct
Merge into: lp:ibid
Diff against target: 123 lines (+108/-0)
2 files modified
ibid/plugins/__init__.py (+1/-0)
ibid/plugins/uct.py (+107/-0)
To merge this branch: bzr merge lp:~keegan-csmith/ibid/uct
Reviewer Review Type Date Requested Status
Ibid Core Team Pending
Review via email: mp+95383@code.launchpad.net
To post a comment you must log in.
lp:~keegan-csmith/ibid/uct updated
1050. By Keegan Carruthers-Smith

Don't load uct plugins by default.

1051. By Keegan Carruthers-Smith

Add copyright header and fix feature/help definition.

1052. By Keegan Carruthers-Smith

Add a UCT help category.

Unmerged revisions

1052. By Keegan Carruthers-Smith

Add a UCT help category.

1051. By Keegan Carruthers-Smith

Add copyright header and fix feature/help definition.

1050. By Keegan Carruthers-Smith

Don't load uct plugins by default.

1049. By Keegan Carruthers-Smith

UCT Club burger menu parser \o/

1048. By Keegan Carruthers-Smith

Rename jammie.py to uct.py

1047. By Keegan Carruthers-Smith

Be more relaxed for place name matching.

1046. By Keegan Carruthers-Smith

Use a more relaxed regex for jammie schedule matching.

1045. By Keegan Carruthers-Smith

Plugin for parsing the jammie shuttle schedule.

Jammie Shuttle is the UCT bus service. This plugin uses a beta
WhereIsMyTransport site. So is destined to break in the future.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid/plugins/__init__.py'
2--- ibid/plugins/__init__.py 2011-02-27 12:33:07 +0000
3+++ ibid/plugins/__init__.py 2012-03-01 15:54:17 +0000
4@@ -45,6 +45,7 @@
5 ('message', u'delivering messages', -5),
6 ('south africa', u'South African stuff', 10),
7 ('sysadmin', u'System Administration', 5),
8+ ('uct', u'University of Cape Town stuff', 10),
9 ):
10 if cat not in ibid.categories:
11 ibid.categories[cat] = {
12
13=== added file 'ibid/plugins/uct.py'
14--- ibid/plugins/uct.py 1970-01-01 00:00:00 +0000
15+++ ibid/plugins/uct.py 2012-03-01 15:54:17 +0000
16@@ -0,0 +1,107 @@
17+# Copyright (c) 2012, Keegan Carruthers-Smith
18+# Released under terms of the MIT/X/Expat Licence. See COPYING for details.
19+
20+import re
21+import urllib
22+import urllib2
23+
24+from BeautifulSoup import BeautifulSoup
25+from ibid.plugins import Processor, match
26+
27+features = {}
28+
29+features['jammie'] = {
30+ 'description': u'Find out where your Jammie Shuttle is',
31+ 'categories': ('lookup', 'south africa', 'uct'),
32+}
33+class Jammie(Processor):
34+ usage = u'''jammie from <depart> to <destination>'
35+ jammie from <depart>
36+ jammie to <destination>'''
37+ features = ('jammie',)
38+ autoload = False
39+
40+ places = ('bremner', 'claremont', 'clarinus', 'dean str', 'forest hill',
41+ 'glendower', 'groote schuur', 'health science', 'hiddingh hall',
42+ 'kopano', 'liesbeeck', 'mowbray', 'pinelands grove',
43+ 'rochester', 'sandown', 'tugwell', 'uct')
44+
45+ @match(r'jammie( from (\S+(:? \S+)?))?( to {any})?')
46+ def jammie(self, event, ignore1, depart, ignore2, ignore3, destination):
47+ dep = Jammie._search_for_place(depart)
48+ dest = Jammie._search_for_place(destination)
49+
50+ if dep is None or dest is None:
51+ if dep is None:
52+ place = depart
53+ else:
54+ place = destination
55+ event.addresponse(u"Sorry, I don't know the place '%s', however "
56+ u"I do know the places %s",
57+ (place, ', '.join(Jammie.places)))
58+ return
59+
60+ times = Jammie._get_times(dep, dest)
61+ if times['live']:
62+ event.addresponse(u'; '.join(times['live']))
63+ elif times['schedule']:
64+ event.addresponse(u'Schedule for %(dep)s to %(dest)s: '
65+ u'%(schedule)s',
66+ dict(dep=dep, dest=dest,
67+ schedule='; '.join(times['schedule'])))
68+ else:
69+ event.addresponse(u'No schedule or live data found for %(dep)s '
70+ u'to %(dest)s', dict(dep=dep, dest=dest))
71+
72+ @classmethod
73+ def _search_for_place(cls, place):
74+ '''Search for place in places.
75+ If not found try to match the first word for each place.'''
76+ if place is None:
77+ return 'uct'
78+ place = place.lower()
79+ if place in cls.places:
80+ return place
81+ if len(place) < 3:
82+ return None
83+ for p in cls.places:
84+ if p.startswith(place):
85+ return p
86+
87+ @classmethod
88+ def _get_times(cls, depart, destination):
89+ query = urllib.urlencode((('dep', depart.lower()),
90+ ('des', destination.lower())))
91+ resp = urllib2.urlopen(
92+ 'http://41.134.54.204:8014/MOBILEp/JAM3.pgm?' + query).read()
93+
94+ live = re.findall(r'(?:<h4>|<BR>)(.*?\(Live\).*?)<BR>', resp)
95+ schedule = re.findall(r'\(Schedule\)(.*)</h4>', resp)
96+ if schedule:
97+ schedule = schedule[0].split('<BR><BR>')[1:]
98+ return dict(live=live, schedule=schedule)
99+
100+
101+features['uctburger'] = {
102+ 'description': u'Find out what todays gourmet burger is at UCT Club',
103+ 'categories': ('lookup', 'south africa', 'uct'),
104+}
105+class UCTBurger(Processor):
106+ usage = u'what is the burger [at uct club]'
107+ features = ('uctburger',)
108+ autoload = False
109+
110+ @match(r'what is the (?:gourmet )?burger(?: at uct club(?: today)?)?\??')
111+ def burger(self, event):
112+ menu = urllib2.urlopen('http://www.uctclub.uct.ac.za/index.php?pid=43').read()
113+ for prev, next in zip(menu.splitlines(), menu.splitlines()[1:]):
114+ if 'gourmet burger' in prev.lower():
115+ break
116+ else:
117+ event.addresponse(u'Could not find the gourmet burger on the menu')
118+ return
119+ desc = re.findall(r'>([^<]*?)<br', next)[0]
120+ desc = BeautifulSoup(desc, convertEntities=BeautifulSoup.HTML_ENTITIES)
121+ event.addresponse(desc)
122+
123+# vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches