Пример #1
0
def art(id):
    id, artist, album, track, title, fullpath = catalog.get_by_id(id)
    status = 200
    headers = {
        'Content-Type': 'application/json'
    }
    params = {
        'cx': '013298434435660591847:iobys90t8ak',
        'imgSize': 'medium',
        'safe': 'medium',
        'searchType': 'image',
        'key': 'AIzaSyALp6qvMuW2uGx1qg_kvmE-UtFPyZvoVqA',
        'q': title + ' ' + album + ' ' + artist
    }
    response = requests.get('https://www.googleapis.com/customsearch/v1', params=params)
    return make_response(response.text, status, headers)
Пример #2
0
def download_song(id):
    id, artist, album, track, title, fullpath = catalog.get_by_id(id)
    filesize = os.path.getsize(fullpath)
    start = 0
    stop = filesize - 1
    length = stop - start + 1
    status = 200
    headers = {
        'Accept-Ranges': 'bytes',
        'Content-Type': 'audio/mpeg',
        'Content-Length': filesize,
        'Content-Range': 'bytes %s-%s/%s' % (start, stop, filesize),
        'Cache-Control': 'must-revalidate, post-check=0, pre-check=0',
        'Pragma': 'public',
    }
    http_range = request.headers.get('Range', None)
    if http_range:
        _, range = http_range.split('=')
        start, stop = range.split('-')
        start = start or 0
        stop = stop or filesize - 1
        start = int(start)
        stop = int(stop)
        stop = min(stop, filesize - 1)
        length = stop - start + 1
        if start > stop:
            # HTTP/1.1 416 Requested Range Not Satisfiable
            status = 416
            headers.update({
                'Content-Length': '0',
                'Content-Range': 'bytes */0'
            })
            return make_response('', status, headers)
        else:
            # HTTP/1.1 206 Partial Content
            status = 206
            headers.update({
                'Content-Length': length,
                'Content-Range': 'bytes %s-%s/%s' % (start, stop, length)
            })
    with open(fullpath, 'rb') as f:
        f.seek(start)
        bytes = f.read(length)
    return make_response(bytes, status, headers)