33
33
def __init__ (self):
34
self.error_connect = False
35
# The path for the Lens *must* also match the one in our .lens file
36
34
self._lens = Unity.Lens.new ("/net/launchpad/lens/wikipedia", "wikipedia")
37
35
self._scope = Unity.Scope.new ("/net/launchpad/lens/wikipedia/main")
38
36
self._lens.props.search_hint = "Search Wikipedia"
39
37
self._lens.props.visible = True;
40
self._lens.props.search_in_global = False;
42
39
self._lens.add_local_scope (self._scope);
43
40
self._scope.connect("search-changed", self.on_search_changed)
44
41
svg_dir = "/usr/share/icons/unity-icon-theme/places/svg/"
48
43
cats.append (Unity.Category.new ("Articles",
49
44
Gio.ThemedIcon.new(svg_dir+"group-installed.svg"),
50
45
Unity.CategoryRenderer.HORIZONTAL_TILE))
51
46
self._lens.props.categories = cats
53
47
locale.setlocale(locale.LC_MESSAGES, '')
54
48
loc = locale.getlocale(locale.LC_MESSAGES)[0].split("_")[0]
55
self.wiki = "http://%s.wikipedia.org" % (loc)
49
self.wiki = ["http://%s.wikipedia.org" % (loc)]
57
50
self._lens.export ();
58
51
self._scope.export ();
61
53
def on_search_changed (self, scope, search, search_type, *_):
62
54
if search_type is Unity.SearchType.DEFAULT:
63
search_string = search.props.search_string.strip()
55
search_string = search.props.search_string.strip ()
64
56
print "Search changed to \"%s\"" % search_string
65
57
model = search.props.results_model
71
63
def update_results_model (self, model, search):
73
65
search = str(datetime.datetime.now().strftime("%B %d"))
74
for i in self.wikipedia_query(search):
76
image = i.childNodes[0].attributes["source"].value
77
text = i.childNodes[1].firstChild.data
78
comment = i.childNodes[2].firstChild.data
79
image = re.sub("/\d{1,3}px-", "/96px-", image)
81
image = "http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png"
82
text = i.childNodes[0].firstChild.data
83
comment = i.childNodes[1].firstChild.data
85
model.append("%s/wiki/%s" % (self.wiki, text),
91
"%s/wiki/%s" % (self.wiki, text))
94
def wikipedia_query(self,search):
66
for site in self.wiki:
67
for i in self.wikipedia_query(search, site):
69
image = i.childNodes[0].attributes["source"].value
70
text = i.childNodes[1].firstChild.data
71
comment = i.childNodes[2].firstChild.data
72
image = re.sub("/\d{1,3}px-", "/96px-", image)
74
image = "http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png"
75
text = i.childNodes[0].firstChild.data
76
comment = i.childNodes[1].firstChild.data
78
if comment.endswith(":"):
79
comment = "may refer to several subjects..."
80
comment = comment.replace ("()", "")
84
model.append("%s/wiki/%s" % (site, text),
90
"%s/wiki/%s" % (site, text))
92
def wikipedia_query(self,search, site):
96
94
search = search.replace(" ", "|")
97
url = ("%s/w/api.php?action=opensearch&limit=25&format=xml&search=%s" % (self.wiki, search))
95
url = ("%s/w/api.php?action=opensearch&limit=25&format=xml&search=%s" % (site, search))
98
96
results = parseString(urllib2.urlopen(url).read())
99
97
items = results.getElementsByTagName('Item')
100
print "Searching Wikipedia"
98
print "Searching Wikipedia: "+url
102
except (IOError, KeyError, urllib2.URLError, urllib2.HTTPError, simplejson.JSONDecodeError):
100
except (IOError, KeyError, urllib2.URLError, urllib2.HTTPError):
103
101
print "Error : Unable to search Wikipedia"