Updated etymology interface
parent
6198b30d76
commit
c5fe137b01
|
@ -7,17 +7,25 @@ Licensed under the Eiffel Forum License 2.
|
||||||
http://inamidst.com/phenny/
|
http://inamidst.com/phenny/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re, urllib
|
||||||
import web
|
import web
|
||||||
from tools import deprecated
|
from tools import deprecated
|
||||||
|
|
||||||
etyuri = 'http://etymonline.com/?term=%s'
|
etysite = 'http://www.etymonline.com/index.php?'
|
||||||
etysearch = 'http://etymonline.com/?search=%s'
|
etyuri = etysite + 'allowed_in_frame=0&term=%s'
|
||||||
|
etysearch = etysite + 'allowed_in_frame=0&search=%s'
|
||||||
|
|
||||||
r_definition = re.compile(r'(?ims)<dd[^>]*>.*?</dd>')
|
r_definition = re.compile(r'(?ims)<dd[^>]*>.*?</dd>')
|
||||||
r_tag = re.compile(r'<(?!!)[^>]+>')
|
r_tag = re.compile(r'<(?!!)[^>]+>')
|
||||||
r_whitespace = re.compile(r'[\t\r\n ]+')
|
r_whitespace = re.compile(r'[\t\r\n ]+')
|
||||||
|
|
||||||
|
class Grab(urllib.URLopener):
|
||||||
|
def __init__(self, *args):
|
||||||
|
self.version = 'Mozilla/5.0 (Phenny)'
|
||||||
|
urllib.URLopener.__init__(self, *args)
|
||||||
|
def http_error_default(self, url, fp, errcode, errmsg, headers):
|
||||||
|
return urllib.addinfourl(fp, [headers, errcode], "http:" + url)
|
||||||
|
|
||||||
abbrs = [
|
abbrs = [
|
||||||
'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp',
|
'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp',
|
||||||
'Fr', 'N', 'E', 'S', 'W', 'L', 'Gen', 'J.C', 'dial', 'Gk',
|
'Fr', 'N', 'E', 'S', 'W', 'L', 'Gen', 'J.C', 'dial', 'Gk',
|
||||||
|
@ -46,7 +54,11 @@ def etymology(word):
|
||||||
raise ValueError("Word too long: %s[...]" % word[:10])
|
raise ValueError("Word too long: %s[...]" % word[:10])
|
||||||
word = {'axe': 'ax/axe'}.get(word, word)
|
word = {'axe': 'ax/axe'}.get(word, word)
|
||||||
|
|
||||||
|
grab = urllib._urlopener
|
||||||
|
urllib._urlopener = Grab()
|
||||||
|
urllib._urlopener.addheader("Referer", "http://www.etymonline.com/")
|
||||||
bytes = web.get(etyuri % web.urllib.quote(word))
|
bytes = web.get(etyuri % web.urllib.quote(word))
|
||||||
|
urllib._urlopener = grab
|
||||||
definitions = r_definition.findall(bytes)
|
definitions = r_definition.findall(bytes)
|
||||||
|
|
||||||
if not definitions:
|
if not definitions:
|
||||||
|
@ -62,6 +74,7 @@ def etymology(word):
|
||||||
sentence = unicode(sentence, 'iso-8859-1')
|
sentence = unicode(sentence, 'iso-8859-1')
|
||||||
sentence = sentence.encode('utf-8')
|
sentence = sentence.encode('utf-8')
|
||||||
except: pass
|
except: pass
|
||||||
|
sentence = web.decode(sentence)
|
||||||
|
|
||||||
maxlength = 275
|
maxlength = 275
|
||||||
if len(sentence) > maxlength:
|
if len(sentence) > maxlength:
|
||||||
|
@ -71,7 +84,7 @@ def etymology(word):
|
||||||
sentence = ' '.join(words) + ' [...]'
|
sentence = ' '.join(words) + ' [...]'
|
||||||
|
|
||||||
sentence = '"' + sentence.replace('"', "'") + '"'
|
sentence = '"' + sentence.replace('"', "'") + '"'
|
||||||
return sentence + ' - ' + (etyuri % word)
|
return sentence + ' - etymonline.com'
|
||||||
|
|
||||||
@deprecated
|
@deprecated
|
||||||
def f_etymology(self, origin, match, args):
|
def f_etymology(self, origin, match, args):
|
||||||
|
|
Loading…
Reference in New Issue