~diegosarmentero/+junk/u1-scope

« back to all changes in this revision

Viewing changes to src/unity_soundcloud_daemon.py

  • Committer: David Callé
  • Date: 2013-01-16 18:29:15 UTC
  • Revision ID: davidc@framli.eu-20130116182915-7suk9yf1tleuigrm
* Init SoundCloud scope
* Add tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Copyright (C) 2012 name <email>
 
5
# This program is free software: you can redistribute it and/or modify it 
 
6
# under the terms of the GNU General Public License version 3, as published 
 
7
# by the Free Software Foundation.
 
8
 
9
# This program is distributed in the hope that it will be useful, but 
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of 
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
12
# PURPOSE.  See the GNU General Public License for more details.
 
13
 
14
# You should have received a copy of the GNU General Public License along 
 
15
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
from gi.repository import GLib, GObject, Gio
 
18
from gi.repository import Unity, UnityExtras
 
19
import urllib
 
20
import urllib2
 
21
import simplejson
 
22
import gettext
 
23
 
 
24
APP_NAME = 'unity-scope-soundcloud'
 
25
LOCAL_PATH = '/usr/share/locale/'
 
26
gettext.bindtextdomain(APP_NAME, LOCAL_PATH)
 
27
gettext.textdomain(APP_NAME)
 
28
_ = gettext.gettext
 
29
 
 
30
BUS_NAME = 'com.canonical.Unity.Scope.Music.Soundcloud'
 
31
BUS_PATH = '/com/canonical/unity/scope/music/soundcloud'
 
32
 
 
33
SVG_DIR = '/usr/share/icons/unity-icon-theme/places/svg/'
 
34
CAT_0_ICON = Gio.ThemedIcon.new(SVG_DIR + 'group-music.svg')
 
35
CAT_0_TITLE = _('Categorytitle')
 
36
 
 
37
NO_RESULTS_HINT = _('Sorry, there are no Soundcloud results that match your search.')
 
38
SEARCH_HINT = _('Search Soundcloud')
 
39
SEARCH_URI = 'https://api.soundcloud.com/'
 
40
API_KEY = '916e2a744323e1f28e8f1fe50728f86d'
 
41
 
 
42
 
 
43
class Daemon:
 
44
    def __init__ (self):
 
45
        self.scope = Unity.Scope.new (BUS_PATH, 'soundcloud')
 
46
        self.scope.props.search_hint = SEARCH_HINT
 
47
        self.scope.search_in_global = True
 
48
        cats = []
 
49
        cats.append(Unity.Category.new('cat_0',
 
50
                                       CAT_0_TITLE,
 
51
                                       CAT_0_ICON,
 
52
                                       Unity.CategoryRenderer.VERTICAL_TILE))
 
53
        self.scope.props.categories = cats
 
54
        self.preferences = Unity.PreferencesManager.get_default()
 
55
        self.preferences.connect('notify::remote-content-search', self._on_preference_changed)
 
56
        self.scope.connect('search-changed', self.on_search_changed)
 
57
        self.scope.export()
 
58
 
 
59
    def _on_preference_changed(self, *_):
 
60
        self.scope.queue_search_changed(Unity.SearchType.DEFAULT)
 
61
 
 
62
    def on_search_changed(self, scope, search, search_type, *_):
 
63
        model = search.props.results_model
 
64
        model.clear()
 
65
        if self.preferences.props.remote_content_search != Unity.PreferencesManagerRemoteContent.ALL:
 
66
            search.finished()
 
67
            return
 
68
        search_string = search.props.search_string.strip()
 
69
        print ('Search changed to \'%s\'' % search_string)
 
70
        self.update_results_model(search_string, model)
 
71
        search.set_reply_hint('no-results-hint',
 
72
                              GLib.Variant.new_string(NO_RESULTS_HINT))
 
73
        search.finished()
 
74
 
 
75
    def update_results_model(self, search, results):
 
76
        checks = ['permalink_url', 'artwork_url',
 
77
                  'title', 'description']
 
78
        for r in self.soundcloud_search(search):
 
79
            for c in checks:
 
80
                if not r[c]:
 
81
                    if c == 'artwork_url':
 
82
                        r[c] = ''
 
83
                    else:
 
84
                        r[c] = ''
 
85
            results.append(uri=r['permalink_url'],
 
86
                           icon_hint=r['artwork_url'],
 
87
                           category=0,
 
88
                           mimetype='text/html',
 
89
                           title=r['title'],
 
90
                           comment=r['description'],
 
91
                           dnd_uri=r['permalink_url'],
 
92
                           result_type=Unity.ResultType.DEFAULT)
 
93
 
 
94
    def soundcloud_search(self, search_string):
 
95
        query = urllib.quote(search_string)
 
96
        data = []
 
97
        if query:
 
98
            uri = ("%stracks.json?consumer_key=%s&q=%s" % (SEARCH_URI, API_KEY, query))
 
99
            print uri
 
100
            try:
 
101
                response = urllib2.urlopen(uri).read()
 
102
                data = simplejson.loads(response)
 
103
            except Exception as error:
 
104
                print "SoundCloud error : ", error
 
105
        return data
 
106
 
 
107
if __name__ == '__main__':
 
108
    daemon = UnityExtras.dbus_own_name (BUS_NAME, Daemon, None)
 
109
    if daemon:
 
110
        GLib.unix_signal_add(0, 2, lambda x: daemon.quit(), None)
 
111
        daemon.run([])