2
# -*- coding: utf-8 -*-
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.
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.
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/>.
17
from gi.repository import GLib, GObject, Gio
18
from gi.repository import Unity, UnityExtras
24
APP_NAME = 'unity-scope-soundcloud'
25
LOCAL_PATH = '/usr/share/locale/'
26
gettext.bindtextdomain(APP_NAME, LOCAL_PATH)
27
gettext.textdomain(APP_NAME)
30
BUS_NAME = 'com.canonical.Unity.Scope.Music.Soundcloud'
31
BUS_PATH = '/com/canonical/unity/scope/music/soundcloud'
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')
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'
45
self.scope = Unity.Scope.new (BUS_PATH, 'soundcloud')
46
self.scope.props.search_hint = SEARCH_HINT
47
self.scope.search_in_global = True
49
cats.append(Unity.Category.new('cat_0',
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)
59
def _on_preference_changed(self, *_):
60
self.scope.queue_search_changed(Unity.SearchType.DEFAULT)
62
def on_search_changed(self, scope, search, search_type, *_):
63
model = search.props.results_model
65
if self.preferences.props.remote_content_search != Unity.PreferencesManagerRemoteContent.ALL:
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))
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):
81
if c == 'artwork_url':
85
results.append(uri=r['permalink_url'],
86
icon_hint=r['artwork_url'],
90
comment=r['description'],
91
dnd_uri=r['permalink_url'],
92
result_type=Unity.ResultType.DEFAULT)
94
def soundcloud_search(self, search_string):
95
query = urllib.quote(search_string)
98
uri = ("%stracks.json?consumer_key=%s&q=%s" % (SEARCH_URI, API_KEY, query))
101
response = urllib2.urlopen(uri).read()
102
data = simplejson.loads(response)
103
except Exception as error:
104
print "SoundCloud error : ", error
107
if __name__ == '__main__':
108
daemon = UnityExtras.dbus_own_name (BUS_NAME, Daemon, None)
110
GLib.unix_signal_add(0, 2, lambda x: daemon.quit(), None)