~davidc3/onehundredscopes/unity-lens-wikipedia-cards

« back to all changes in this revision

Viewing changes to src/unity-lens-wikipedia

  • Committer: David Callé
  • Date: 2012-05-10 00:12:06 UTC
  • Revision ID: davidc@framli.eu-20120510001206-95mcx0ritvxegpl7
Give the ability to the lens to read a list of websites

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
class Daemon:
32
32
 
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;
41
38
 
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/"
45
 
        
46
 
        # Populate categories
47
42
        cats = []
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
52
 
        
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)
56
 
        
 
49
        self.wiki = ["http://%s.wikipedia.org" % (loc)]
57
50
        self._lens.export ();
58
51
        self._scope.export ();
59
 
        
60
52
 
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
66
58
            model.clear ()
71
63
    def update_results_model (self, model, search):
72
64
        if search == '':
73
65
            search = str(datetime.datetime.now().strftime("%B %d"))
74
 
        for i in self.wikipedia_query(search):
75
 
            try:
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)
80
 
            except:
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
84
 
 
85
 
            model.append("%s/wiki/%s" % (self.wiki, text),
86
 
                        image,
87
 
                        0,
88
 
                        "text/html",
89
 
                        text,
90
 
                        comment,
91
 
                        "%s/wiki/%s" % (self.wiki, text))
92
 
 
93
 
 
94
 
    def wikipedia_query(self,search):
 
66
        for site in self.wiki:
 
67
            for i in self.wikipedia_query(search, site):
 
68
                try:
 
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)
 
73
                except:
 
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
 
77
                try:
 
78
                    if comment.endswith(":"):
 
79
                        comment = "may refer to several subjects..."
 
80
                    comment = comment.replace ("()", "")
 
81
                except:
 
82
                    pass
 
83
 
 
84
                model.append("%s/wiki/%s" % (site, text),
 
85
                            image,
 
86
                            0,
 
87
                            "text/html",
 
88
                            text,
 
89
                            comment,
 
90
                            "%s/wiki/%s" % (site, text))
 
91
 
 
92
    def wikipedia_query(self,search, site):
95
93
        try:
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
101
99
            return items
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"
104
102
            return []
105
103
        
113
111
                                   GLib.Variant ("(su)", (BUS_NAME, 0x4)),
114
112
                                   0, -1, None)
115
113
                                   
116
 
    # Unpack variant response with signature "(u)". 1 means we got it.
117
114
    result = result.unpack()[0]
118
115
    
119
116
    if result != 1 :