def get_list_popular_music(request): perido = request.GET.get('period') client = Client() client.init() if perido == '1': popular_music = client.popular(period=Client.DAILY) else: popular_music = client.popular(period=Client.MONTHLY) musicas = [] for musica in popular_music: musicas.append(musica.export()) return HttpResponse(json.dumps(musicas), mimetype="application/json")
# -*- coding:utf-8 -*- # # Copyright (C) 2012, Maximilian Köhl <*****@*****.**> # # 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. from __future__ import print_function import subprocess from grooveshark import Client client = Client() client.init() for song in client.popular(): print(song) subprocess.call(['mplayer', song.stream.url])
class Grooveshark(Application): __URLS__ = { '/desktop/.*|/icons/.*': 'www', '/request/popular': 'popular', '/request/search': 'search', '/request/stream': 'stream' } www = Directory(os.path.join(os.path.dirname(__file__), 'www')) def __init__(self): super().__init__() self.client = Client() self.client.init() self._cache = {} self._cache['streams'] = {} def _respond_json(self, data, response): response.headers['Content-Type'] = 'application/json; charset=UTF-8' response.body.append(json.dumps(data).encode('utf-8')) def _bad_request(self, message, response): response.status = 400 response.message = 'ERROR' response.headers['Content-Type'] = 'application/json; charset=UTF-8' response.body.append( json.dumps({ 'status': 'error', 'result': message }).encode('utf-8')) def popular(self, request, response): if 'popular' not in self._cache: self._cache['popular'] = (time.time(), [ song.export() for song in self.client.popular() ]) if time.time() - self._cache['popular'][0] > 7200: self._cache['popular'] = (time.time(), [ song.export() for song in self.client.popular() ]) self._respond_json( { 'status': 'success', 'result': self._cache['popular'][1] }, response) def search(self, request, response): if 'type' not in request.query: request.query['type'] = [SEARCH_TYPE_SONGS] if 'query' in request.query: if not request.query['type'][0] in (Client.SONGS, Client.ALBUMS, Client.ARTISTS): self._bad_request('unknown type', response) else: result = [ object.export() for object in self.client.search( request.query['query'][0], request.query['type'][0]) ] self._respond_json({ 'status': 'success', 'result': result }, response) else: self._bad_request('missing query argument', response) def stream(self, request, response): song = Song.from_export(json.loads(request.query['song'][0]), self.client.connection) if song.id in self._cache['streams']: stream, cache = self._cache['streams'][song.id] else: stream = song.stream cache = Cache(stream.data, stream.size) self._cache['streams'][song.id] = stream, cache if 'Range' in request.headers: response.status = 206 start_byte, end_byte = request.headers['Range'].replace( 'bytes=', '').split('-') if start_byte: start_byte = int(start_byte) else: start_byte = 0 if end_byte: end_byte = int(end_byte) else: end_byte = stream.size cache.offset = start_byte if 'download' in request.query: response.headers[ 'Content-Disposition'] = 'attachment; filename="{} - {} - {}.mp3"'.format( song.name, song.album.name, song.artist.name) response.headers['Accept-Ranges'] = 'bytes' response.headers['Content-Type'] = stream.data.info( )['Content-Type'] response.headers['Content-Length'] = str(stream.size) response.headers['Content-Range'] = 'bytes {}-{}/{}'.format( start_byte, end_byte, stream.size) response.body = FileWrapper(cache) else: cache.reset() if 'download' in request.query: response.headers[ 'Content-Disposition'] = 'attachment; filename="{} - {} - {}.mp3"'.format( song.name, song.album.name, song.artist.name) response.headers['Accept-Ranges'] = 'bytes' response.headers['Content-Type'] = stream.data.info( )['Content-Type'] response.headers['Content-Length'] = str(stream.size) response.body = FileWrapper(cache)
class Grooveshark(Application): __URLS__ = {'/desktop/.*|/icons/.*' : 'www', '/request/popular' : 'popular', '/request/search' : 'search', '/request/stream' : 'stream'} www = Directory(os.path.join(os.path.dirname(__file__), 'www')) def __init__(self): super().__init__() self.client = Client() self.client.init() self._cache = {} self._cache['streams'] = {} def _respond_json(self, data, response): response.headers['Content-Type'] = 'application/json; charset=UTF-8' response.body.append(json.dumps(data).encode('utf-8')) def _bad_request(self, message, response): response.status = 400 response.message = 'ERROR' response.headers['Content-Type'] = 'application/json; charset=UTF-8' response.body.append(json.dumps({'status' : 'error', 'result' : message}).encode('utf-8')) def popular(self, request, response): if not 'popular' in self._cache: self._cache['popular'] = (time.time(), [song.export() for song in self.client.popular()]) if time.time() - self._cache['popular'][0] > 7200: self._cache['popular'] = (time.time(), [song.export() for song in self.client.popular()]) self._respond_json({'status' : 'success', 'result' : self._cache['popular'][1]}, response) def search(self, request, response): if not 'type' in request.query: request.qery['type'] = [SEARCH_TYPE_SONGS] if 'query' in request.query: if not request.query['type'][0] in (Client.SONGS, Client.ALBUMS, Client.ARTISTS): self._bad_request('unknown type', response) else: result = [object.export() for object in self.client.search(request.query['query'][0], request.query['type'][0])] self._respond_json({'status' : 'success', 'result' : result}, response) else: self._bad_request('missing query argument', response) def stream(self, request, response): song = Song.from_export(json.loads(request.query['song'][0]), self.client.connection) if song.id in self._cache['streams']: stream, cache = self._cache['streams'][song.id] else: stream = song.stream cache = Cache(stream.data, stream.size) self._cache['streams'][song.id] = stream, cache if 'Range' in request.headers: response.status = 206 start_byte, end_byte = request.headers['Range'].replace('bytes=', '').split('-') if start_byte: start_byte = int(start_byte) else: start_byte = 0 if end_byte: end_byte = int(end_byte) else: end_byte = stream.size cache.offset = start_byte if 'download' in request.query: response.headers['Content-Disposition'] = 'attachment; filename="{} - {} - {}.mp3"'.format(song.name, song.album.name, song.artist.name) response.headers['Accept-Ranges'] = 'bytes' response.headers['Content-Type'] = stream.data.info()['Content-Type'] response.headers['Content-Length'] = str(stream.size) response.headers['Content-Range'] = 'bytes {}-{}/{}'.format(start_byte, end_byte, stream.size) response.body = FileWrapper(cache) else: cache.reset() if 'download' in request.query: response.headers['Content-Disposition'] = 'attachment; filename="{} - {} - {}.mp3"'.format(song.name, song.album.name, song.artist.name) response.headers['Accept-Ranges'] = 'bytes' response.headers['Content-Type'] = stream.data.info()['Content-Type'] response.headers['Content-Length'] = str(stream.size) response.body = FileWrapper(cache)
class Grooveshark(Application): __URLS__ = { "/desktop/.*|/icons/.*": "www", "/request/popular": "popular", "/request/search": "search", "/request/stream": "stream", } www = Directory(os.path.join(os.path.dirname(__file__), "www")) def __init__(self): super().__init__() self.client = Client() self.client.init() self._cache = {} self._cache["streams"] = {} def _respond_json(self, data, response): response.headers["Content-Type"] = "application/json; charset=UTF-8" response.body.append(json.dumps(data).encode("utf-8")) def _bad_request(self, message, response): response.status = 400 response.message = "ERROR" response.headers["Content-Type"] = "application/json; charset=UTF-8" response.body.append(json.dumps({"status": "error", "result": message}).encode("utf-8")) def popular(self, request, response): if "popular" not in self._cache: self._cache["popular"] = (time.time(), [song.export() for song in self.client.popular()]) if time.time() - self._cache["popular"][0] > 7200: self._cache["popular"] = (time.time(), [song.export() for song in self.client.popular()]) self._respond_json({"status": "success", "result": self._cache["popular"][1]}, response) def search(self, request, response): if "type" not in request.query: request.query["type"] = [SEARCH_TYPE_SONGS] if "query" in request.query: if not request.query["type"][0] in (Client.SONGS, Client.ALBUMS, Client.ARTISTS): self._bad_request("unknown type", response) else: result = [ object.export() for object in self.client.search(request.query["query"][0], request.query["type"][0]) ] self._respond_json({"status": "success", "result": result}, response) else: self._bad_request("missing query argument", response) def stream(self, request, response): song = Song.from_export(json.loads(request.query["song"][0]), self.client.connection) if song.id in self._cache["streams"]: stream, cache = self._cache["streams"][song.id] else: stream = song.stream cache = Cache(stream.data, stream.size) self._cache["streams"][song.id] = stream, cache if "Range" in request.headers: response.status = 206 start_byte, end_byte = request.headers["Range"].replace("bytes=", "").split("-") if start_byte: start_byte = int(start_byte) else: start_byte = 0 if end_byte: end_byte = int(end_byte) else: end_byte = stream.size cache.offset = start_byte if "download" in request.query: response.headers["Content-Disposition"] = 'attachment; filename="{} - {} - {}.mp3"'.format( song.name, song.album.name, song.artist.name ) response.headers["Accept-Ranges"] = "bytes" response.headers["Content-Type"] = stream.data.info()["Content-Type"] response.headers["Content-Length"] = str(stream.size) response.headers["Content-Range"] = "bytes {}-{}/{}".format(start_byte, end_byte, stream.size) response.body = FileWrapper(cache) else: cache.reset() if "download" in request.query: response.headers["Content-Disposition"] = 'attachment; filename="{} - {} - {}.mp3"'.format( song.name, song.album.name, song.artist.name ) response.headers["Accept-Ranges"] = "bytes" response.headers["Content-Type"] = stream.data.info()["Content-Type"] response.headers["Content-Length"] = str(stream.size) response.body = FileWrapper(cache)