-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurldef.py
39 lines (30 loc) · 874 Bytes
/
curldef.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
#!/usr/bin/python
class Test:
def __init__(self):
self.contents = ''
def body_callback(self, buf):
self.contents = self.contents + buf
def stringify(multiline):
return ''.join(multiline.split('\n'))
def wiktionaryword(page):
try:
m = re.search('<ol>(.*?)</ol>', stringify(page))
definitions = m.group(0)
print '!!!' + stringify(page)
m = re.search('<li>(.*?)</li>', definitions)
definitions = m.group()
return str(definitions)
except AttributeError:
return False
def curldef(word):
t = Test()
c = pycurl.Curl()
c.setopt(c.URL, 'http://en.wiktionary.org/wiki/'+word)
c.setopt(c.WRITEFUNCTION, t.body_callback)
c.perform()
c.close()
defs = [wiktionaryword(t.contents)]
if defs[0]:
return defs[0]
else:
return "nodef"