| commit 1: | 7a22233caa1f |
| parent 0: | 1e4c7f873843 |
| branch: | default |
| tags: | tip |
Look up the case at AltLaw and take the first result
7 months ago
Changed (Δ375 bytes):
raw changeset »
altlawbot.py (18 lines added, 12 lines removed)
| … | … | @@ -4,6 +4,8 @@ from waveapi import robot |
4 |
4 |
from waveapi import document |
5 |
5 |
import logging |
6 |
6 |
import re |
7 |
import feedparser |
|
8 |
from urllib import urlencode |
|
7 |
9 |
|
8 |
10 |
logger = logging.getLogger('AltLawBot') |
9 |
11 |
logger.setLevel(logging.DEBUG) |
| … | … | @@ -12,7 +14,7 @@ cases_re = re.compile('\[\[(.+?)\]\]') |
12 |
14 |
def OnRobotAdded(properties, context): |
13 |
15 |
"""Invoked when the robot has been added.""" |
14 |
16 |
root_wavelet = context.GetRootWavelet() |
15 |
root_wavelet.CreateBlip().GetDocument().SetText("Hello I am the AltLaw Bot! |
|
17 |
root_wavelet.CreateBlip().GetDocument().SetText("Hello I am the AltLaw Bot! Cases mentioned in the text like this: [[Marbury v. Madison]] will be automatically converted to links to the case at AltLaw.org") |
|
16 |
18 |
|
17 |
19 |
def OnBlipSubmitted(properties, context): |
18 |
20 |
blip = context.GetBlipById(properties['blipId']) |
| … | … | @@ -26,21 +28,25 @@ def OnBlipSubmitted(properties, context) |
26 |
28 |
pass |
27 |
29 |
cases = cases_re.findall(text) |
28 |
30 |
for case in cases: |
29 |
left = text.find(case, left) |
|
30 |
replaceRange = document.Range(left-2, left+len(case)+2) |
|
31 |
linkText = "Replaced" |
|
32 |
linkLocation = "http://www.google.com" |
|
33 |
doc.SetTextInRange(replaceRange, linkText) |
|
34 |
doc.SetAnnotation(document.Range(left-2, left+len(linkText)-2), "link/manual", linkLocation) |
|
35 |
newBlip = doc.AppendInlineBlip() |
|
36 |
newBlip.GetDocument().SetText(linkLocation) |
|
37 |
#text = text.replace(text[replaceRange.start:replaceRange.end], linkText, 1) |
|
38 |
#left += len(linkTxt) |
|
31 |
query = "http://altlaw.org/v1/search?"+urlencode({'q':case, 'command':'search cases', 'format':'atom'}) |
|
32 |
search_results = feedparser.parse(query) |
|
33 |
try: |
|
34 |
left = text.find(case, left) |
|
35 |
replaceRange = document.Range(left-2, left+len(case)+2) |
|
36 |
linkText = search_results['entries'][0].title.replace('\n','') |
|
37 |
linkLocation = search_results['entries'][0].link |
|
38 |
doc.SetTextInRange(replaceRange, linkText) |
|
39 |
doc.SetAnnotation(document.Range(left-2, left+len(linkText)-2), "link/manual", linkLocation) |
|
40 |
||
41 |
text = text.replace(text[replaceRange.start:replaceRange.end], linkText, 1) |
|
42 |
left += len(linkText) |
|
43 |
except IndexError: |
|
44 |
pass |
|
39 |
45 |
|
40 |
46 |
if __name__ == '__main__': |
41 |
47 |
myRobot = robot.Robot('AltLawBot', |
42 |
48 |
image_url='http://altlawbot.appspot.com/assets/icon.png', |
43 |
version='2. |
|
49 |
version='2.10', |
|
44 |
50 |
profile_url='http://altlawbot.appspot.com/') |
45 |
51 |
myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded) |
46 |
52 |
myRobot.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmitted) |
