~davidc3/onehundredscopes/songsterr

« back to all changes in this revision

Viewing changes to src/songsterr.py

  • Committer: David Call√©
  • Date: 2011-10-20 09:12:22 UTC
  • Revision ID: davidc@framli.eu-20111020091222-pz49ed9rl7knleb0
Branch init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
 
 
3
#    Copyright (c) 2011 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
import sys
 
18
from gi.repository import GLib, GObject, Gio
 
19
from gi.repository import Dee
 
20
_m = dir(Dee.SequenceModel)
 
21
from gi.repository import Unity
 
22
import socket, urllib, urllib2, simplejson
 
23
 
 
24
socket.setdefaulttimeout(5)
 
25
BUS_NAME = "net.launchpad.scope.music.score.songsterr"
 
26
 
 
27
class Daemon:
 
28
        def __init__ (self):
 
29
                self.scope = Unity.Scope.new ("/net/launchpad/scope/music/score/songsterr")
 
30
                self.scope.search_in_global = False
 
31
                self.scope.connect ("notify::active-search", self.on_search_changed)
 
32
                self.scope.connect ("filters-changed", self.on_search_changed);
 
33
                self.scope.export()
 
34
 
 
35
        def get_search_string (self):
 
36
                search = self.scope.props.active_search
 
37
                return search.props.search_string if search else None
 
38
 
 
39
        def on_search_changed (self, scope, param_spec=None):
 
40
                        search = self.get_search_string()
 
41
                        print "Search changed to: '%s'" % search
 
42
                        results = self.scope.props.results_model
 
43
                        results.clear()
 
44
                        self.update_results_model (search, results)
 
45
                        results.flush_revision_queue ()
 
46
 
 
47
        def update_results_model(self, search, model):
 
48
                for i in self.songsterr(search):
 
49
                        title = i[0]
 
50
                        comment = i[1]
 
51
                        uri = i[2]
 
52
                        icon_hint = i[3]
 
53
                        model.append (uri, icon_hint, 0,"text/html", title, comment, uri)
 
54
                        model.append (uri, icon_hint, 1,"text/html", title, comment, uri)
 
55
 
 
56
        #
 
57
        # Songsterr search
 
58
        # <http://www.songsterr.com/a/wa/api>
 
59
        #
 
60
        def songsterr(self, search):
 
61
                from xml.dom import minidom
 
62
                if search:
 
63
                        data_list = []
 
64
                        url = ('http://www.songsterr.com/a/ra/songs.xml?pattern=%s' % search)
 
65
                        print url
 
66
                        dom = minidom.parse(urllib2.urlopen(url))
 
67
                        if dom:
 
68
                                artistDict = {}
 
69
                                for node in dom.getElementsByTagName('Song'):
 
70
                                        item_list = []
 
71
                                        song_id = node.getAttribute('id')
 
72
                                        for node2 in node.getElementsByTagName('title'):
 
73
                                                title = node2.firstChild.data
 
74
                                        for node2 in node.getElementsByTagName('artist'):
 
75
                                                if node2.getAttribute('type')=='Artist':
 
76
                                                        artist_id = node2.getAttribute('id')
 
77
                                                for node3 in node2.getElementsByTagName('name'):
 
78
                                                                artist = ''
 
79
                                                                artist = node3.firstChild.data
 
80
                                                                if artist != '':
 
81
                                                                        artistDict[artist_id] = artist
 
82
                                        artist = artistDict[artist_id]
 
83
                                                
 
84
                                        item_list.append(title)
 
85
                                        item_list.append(artist)
 
86
                                        item_list.append('http://www.songsterr.com/a/wa/song?id=' + song_id)
 
87
                                        item_list.append('sound')
 
88
                                        data_list.append(item_list)
 
89
                                return data_list
 
90
                        else:
 
91
                                return []
 
92
                else:
 
93
                        return []
 
94
 
 
95
if __name__ == "__main__":
 
96
        session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
 
97
        session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
 
98
                                              'org.freedesktop.DBus',
 
99
                                              '/org/freedesktop/DBus',
 
100
                                              'org.freedesktop.DBus', None)
 
101
        result = session_bus.call_sync('RequestName',
 
102
                                       GLib.Variant ("(su)", (BUS_NAME, 0x4)),
 
103
                                       0, -1, None)
 
104
                                       
 
105
        # Unpack variant response with signature "(u)". 1 means we got it.
 
106
        result = result.unpack()[0]
 
107
        
 
108
        if result != 1 :
 
109
                print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
 
110
                raise SystemExit (1)
 
111
        
 
112
        daemon = Daemon()
 
113
        GObject.MainLoop().run()