File: jamapi.py

package info (click to toggle)
exaile 0.3.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,564 kB
  • sloc: python: 35,424; makefile: 265; sh: 58
file content (166 lines) | stat: -rw-r--r-- 6,602 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Copyright (C) 2009-2010 Erin Drummond
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# The developers of the Exaile media player hereby grant permission
# for non-GPL compatible GStreamer and Exaile plugins to be used and
# distributed together with GStreamer and Exaile. This permission is
# above and beyond the permissions granted by the GPL license by which
# Exaile is covered. If you modify this code, you may extend this
# exception to your version of the code, but you are not obligated to
# do so. If you do not wish to do so, delete this exception statement
# from your version.

import simplejson as json
import urllib
import jamtree
import threading

#Gets a list of jamtree.Artist objects matching the specified criteria
class get_artist_list(threading.Thread):

    def __init__(self, search_term, order_by, num_results, callback):
        threading.Thread.__init__(self)
        self.search_term = search_term
        self.order_by = order_by
        self.num_results = num_results
        self.callback = callback

    def run(self):
        url = "http://api.jamendo.com/get2/name+id/artist/json/?searchquery=%s&order=%s&n=%s" % (self.search_term, self.order_by, self.num_results)
        #print('get_artist_list: %s' % url)
        results = json.load(urllib.urlopen(url))
        artists = []
        for result in results:
            item = jamtree.Artist(result['id'], result['name'].strip())
            artists.append(item)
        if artists == []:
            artists = None

        self.callback(artists)

#Gets a list of jamtree.Album objects matching the specified criteria
class get_album_list(threading.Thread):

    def __init__(self, search_term, order_by, num_results, callback):
        threading.Thread.__init__(self)
        self.search_term = search_term
        self.order_by = order_by
        self.num_results = num_results
        self.callback = callback

    def run(self):
        url = "http://api.jamendo.com/get2/name+id/album/json/?searchquery=%s&order=%s&n=%s" % (self.search_term, self.order_by, self.num_results)
        results = json.load(urllib.urlopen(url))
        albums = []
        for result in results:
            ar = jamtree.Album(result['id'], result['name'].strip())
            albums.append(ar)

        if albums == []:
            albums = None

        self.callback(albums)

#Gets a list of jamtree.Artist objects matching the specified criteria
class get_artist_list_by_genre(threading.Thread):
    def __init__(self, search_term, order_by, num_results, callback):
        threading.Thread.__init__(self)
        self.search_term = search_term
        self.order_by = order_by
        self.num_results = num_results
        self.callback = callback

    def run(self):
        url = "http://api.jamendo.com/get2/name+id/artist/json/?tag_idstr=%s&order=%s&n=%s" % (self.search_term, self.order_by, self.num_results)
        results = json.load(urllib.urlopen(url))
        artists = []
        for result in results:
            item = jamtree.Artist(result['id'], result['name'].strip())
            artists.append(item)
        if artists == []:
            artists = None

        self.callback(artists)

#Gets a list of jamtree.Track objects matching the specified criteria
class get_track_list(threading.Thread):

    def __init__(self, search_term, order_by, num_results, callback):
        threading.Thread.__init__(self)
        self.search_term = search_term
        self.order_by = order_by
        self.num_results = num_results
        self.callback = callback

    def run(self):
        url = "http://api.jamendo.com/get2/id+name+stream+album_id+album_name/track/json/?searchquery=%s&order=%s&n=%s&streamencoding=ogg2" % (self.search_term, self.order_by, self.num_results)
        #print('get_track_list: %s' % url)
        tracks = json.load(urllib.urlopen(url))
        track_list = []
        for track in tracks:
            item = jamtree.Track(track['id'], track['name'].strip(), track['stream'])
            item.album_name = track['album_name']
            track_list.append(item)
        if track_list == []:
            track_list = None

        self.callback(track_list)


#Gets a list of jamtree.Album objects for the specified jamtree.Artist
class get_albums(threading.Thread):

    def __init__(self, artist, callback, add_to_playlist = False):
        threading.Thread.__init__(self)
        self._artist = artist
        self._callback = callback
        self._add_to_playlist = add_to_playlist

    def run(self):
        url = "http://api.jamendo.com/get2/id+name/album/json/?artist_id=%s" % self._artist.id
        #print('get_albums: %s' % url)
        albumresults = json.load(urllib.urlopen(url))
        for albumresult in albumresults:
            item = jamtree.Album(albumresult['id'], albumresult['name'].strip())
            self._artist.add_album(item)

        self._callback(self._artist, self._add_to_playlist)


#Gets a list of jamtree.Track objects for the specified jamtree.Album
class get_tracks(threading.Thread):

    def __init__(self, album, callback, add_to_playlist=False):
        threading.Thread.__init__(self)
        self._album = album
        self._callback = callback
        self._add_to_playlist = add_to_playlist

    def run(self):
        url = "http://api.jamendo.com/get2/id+name+stream/track/json/?album_id=%s&streamencoding=ogg2" % self._album.id
        #print('get_tracks: %s' % url)
        tracks = json.load(urllib.urlopen(url))
        for track in tracks:
            item = jamtree.Track(track['id'], track['name'].strip(), track['stream'])
            self._album.add_track(item)
        self._callback(self._album, self._add_to_playlist)

#Gets the URL for an album image based on a track id
def get_album_image_url_from_track(track_id):
    url = "http://api.jamendo.com/get2/album_image/track/json/?id=%s&album_imagesize=400" % track_id
    imageurl = json.load(urllib.urlopen(url))
    return "".join(imageurl)