~davidc3/onehundredscopes/europeana

« back to all changes in this revision

Viewing changes to unity-lens-europeana

  • Committer: David Callé
  • Date: 2012-11-05 12:19:54 UTC
  • Revision ID: davidc@framli.eu-20121105121954-ald7m5q7zwhzm80z
Initial work around the Europeana API

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
 
 
3
#    Copyright(c) 2012 David Calle <davidc@framli.eu>
 
4
 
 
5
#    This program is free software: you can redistribute it and/or modify
 
6
#    it under the terms of the GNU General Public License as published by
 
7
#    the Free Software Foundation, either version 3 of the License, or
 
8
#   (at your option) any later version.
 
9
 
 
10
#    This program is distributed in the hope that it will be useful,
 
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#    GNU General Public License for more details.
 
14
 
 
15
#    You should have received a copy of the GNU General Public License
 
16
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
import sys
 
19
from gi.repository import GLib, GObject, Gio
 
20
from gi.repository import Unity
 
21
import urllib2, urllib
 
22
import re
 
23
import simplejson
 
24
import random
 
25
 
 
26
BUS_NAME = "net.launchpad.lens.europeana"
 
27
 
 
28
 
 
29
class Daemon:
 
30
 
 
31
    def __init__(self):
 
32
        self._lens = Unity.Lens.new("/net/launchpad/lens/europeana",
 
33
                                     "europeana")
 
34
        self._scope = Unity.Scope.new("/net/launchpad/lens/europeana/main")
 
35
        self._lens.props.search_hint = "Search Europeana"
 
36
        self._lens.props.visible = True
 
37
 
 
38
        self._lens.add_local_scope(self._scope)
 
39
        self._scope.connect("search-changed", self.on_search_changed)
 
40
        self._scope.connect('preview-uri', self.on_preview_uri)
 
41
        svg_dir = "/usr/share/icons/unity-icon-theme/places/svg/"
 
42
        cats = []
 
43
        cats.append(Unity.Category.new("Items",
 
44
                                       Gio.ThemedIcon.new(svg_dir+"group-installed.svg"),
 
45
                                       Unity.CategoryRenderer.VERTICAL_TILE))
 
46
        self._lens.props.categories = cats
 
47
        
 
48
        self.preferences = Unity.PreferencesManager.get_default()
 
49
        self.preferences.connect("notify::remote-content-search", self._on_preference_changed)
 
50
        self._lens.export();
 
51
        self._scope.export();
 
52
 
 
53
    def _on_preference_changed(self, *_):
 
54
        self._scope.queue_search_changed(Unity.SearchType.DEFAULT)
 
55
 
 
56
    def on_search_changed(self, scope, search, search_type, *_):
 
57
        model = search.props.results_model
 
58
        model.clear()
 
59
        if self.preferences.props.remote_content_search != Unity.PreferencesManagerRemoteContent.ALL:
 
60
            search.finished()
 
61
            return
 
62
 
 
63
        if search_type is Unity.SearchType.DEFAULT:
 
64
            search_string = search.props.search_string.strip()
 
65
            print "Search changed to \"%s\"" % search_string
 
66
            self.update_results_model(model, search_string)
 
67
            search.set_reply_hint("no-results-hint", GLib.Variant.new_string("Sorry, there are no Europeana items that match your search."))
 
68
        search.finished()
 
69
 
 
70
 
 
71
    def update_results_model(self, model, search):
 
72
        if not search:
 
73
            search = ""
 
74
        q = urllib.quote(search)
 
75
        qscope = urllib.quote('where:\"%s\" OR who:\"%s\" OR when:\"%s\" OR what:\"%s\"' % (q,q,q,q))
 
76
        url = "http://preview.europeana.eu/api/v2/search.json?wskey=QLShGrLqe&query=%s&start=1&rows=200&profile=standard" % (qscope)
 
77
        print url
 
78
        results = simplejson.load(urllib2.urlopen(url))
 
79
        if results['success']:
 
80
            if "items" in results:
 
81
                for item in results['items']:
 
82
                    dprovider, provider, title, image, guid, creator = '','','','','', ''
 
83
                    for e in item:
 
84
                        if e == "dataProvider":
 
85
                            dprovider =  item['dataProvider'][0]
 
86
                        elif e == "provider":
 
87
                            provider = item['provider'][0]
 
88
                        elif e == "title":
 
89
                            title = item['title'][0]
 
90
                        elif e == "thumbnails":
 
91
                            image = item['thumbnails'][0]
 
92
                        elif e == "guid":
 
93
                            guid = item['guid']
 
94
                        elif e == "dcCreator":
 
95
                            creator = item['dcCreator'][0]
 
96
                        else:
 
97
                            pass
 
98
                    print item
 
99
                    if image != '' and item['type'] == "IMAGE":
 
100
                        title = title.split(";")[-1]
 
101
                        if title == '':
 
102
                            title = creator
 
103
                        model.append(guid,image,0, "text/html", title, provider, guid)
 
104
 
 
105
 
 
106
 
 
107
    def on_preview_uri(self, scope, uri):
 
108
        """Preview request handler"""
 
109
        preview = None
 
110
        model = self._scope.props.results_model
 
111
        iter = model.get_first_iter()
 
112
        end_iter = model.get_last_iter()
 
113
        while iter != end_iter:
 
114
            if model.get_value(iter, 0) == uri:
 
115
                title  = model.get_value(iter, 4)
 
116
                amount = ''
 
117
                description = ''
 
118
                for r in self.recipes:
 
119
                    if title == r[0]:
 
120
                        print r
 
121
                        if ',' in r[3]:
 
122
                            amount = r[3].split(',')[-1]
 
123
                        description = r[4]
 
124
 
 
125
                image = self.createRecipeImage(title)
 
126
                
 
127
                gicon_icon = Gio.FileIcon.new(Gio.file_new_for_uri(model.get_value(iter, 1)))
 
128
                if self.is_file(CACHE+image):
 
129
                    gicon_recipe = Gio.FileIcon.new(Gio.file_new_for_path(CACHE+image))
 
130
                else:
 
131
                    gicon_recipe = ''
 
132
                preview = Unity.ApplicationPreview.new(title.strip(),'' ,description,gicon_icon, gicon_recipe)
 
133
                if amount != '':
 
134
                    preview.add_info(Unity.InfoHint.new("Production", "Production", None, amount))
 
135
#                gfile_icon = Gio.file_new_for_path("/usr/share/icons/unity-icon-theme/places/svg/service-picasa.svg")
 
136
#                gicon = Gio.FileIcon.new(gfile_icon)
 
137
#                view_action = Unity.PreviewAction.new("view", _("View"), gicon)
 
138
#                view_action.connect('activated', self.view_action)
 
139
#                preview.add_action(view_action)
 
140
                break
 
141
            iter = model.next(iter)
 
142
        if preview == None:
 
143
            print("Couldn't find model row for requested preview uri: '%s'", uri)
 
144
        return preview
 
145
 
 
146
    def view_action(self, scope, uri):
 
147
        """On item clicked, close the Dash and display the photo"""
 
148
        return
 
149
 
 
150
 
 
151
if __name__ == "__main__":
 
152
    session_bus_connection = Gio.bus_get_sync(Gio.BusType.SESSION, None)
 
153
    session_bus = Gio.DBusProxy.new_sync(session_bus_connection, 0, None,
 
154
                                          'org.freedesktop.DBus',
 
155
                                          '/org/freedesktop/DBus',
 
156
                                          'org.freedesktop.DBus', None)
 
157
    result = session_bus.call_sync('RequestName',
 
158
                                   GLib.Variant("(su)",(BUS_NAME, 0x4)),
 
159
                                   0, -1, None)
 
160
                                   
 
161
    result = result.unpack()[0]
 
162
 
 
163
    if result != 1:
 
164
        print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
 
165
        raise SystemExit(1)
 
166
 
 
167
    daemon = Daemon()
 
168
    GObject.MainLoop().run()