def get_link(): """ Uses youtube-dl to fetch the direct link """ try: url = request.args.get('url') data = decode_data(get_key(), url) vid_id = data['id'] title = data['title'] format = 'm4a/bestaudio' retval = get_or_create_video_download_link(vid_id, format, get_download_link_youtube) if not LOCAL: retval = encode_data(get_key(), id=vid_id, title=title, url=retval, length=data['length']) retval = url_for('download_file', url=retval) ret_dict = { 'status': 200, 'requestLocation': '/api/v1/g', 'url': retval } return jsonify(ret_dict) except Exception as e: logger.info(traceback.format_exc()) return make_error_response(msg=str(e), endpoint='/api/v1/g')
def stream_handler(): url = request.args.get('url') try: url = decode_data(get_key(), url)['url'] except Exception: return 'Bad URL', 400 mime = 'audio/mp4' if url.find('mime=audio%2Fwebm') > -1: mime = 'audio/webm' range_header = request.headers.get('Range', None) if range_header: from_bytes, until_bytes = range_header.replace('bytes=', '').split('-') if not until_bytes: until_bytes = int(from_bytes) + int( 1024 * 1024 * 1) # 1MB * 1 = 1MB headers = {'Range': 'bytes=%s-%s' % (from_bytes, until_bytes)} r = requests.get(url, headers=headers) data = r.content rv = Response(data, 206, mimetype=mime, direct_passthrough=True) rv.headers.add('Content-Range', r.headers.get('Content-Range')) return rv r = requests.get(url, stream=True) return Response(generate_data(r), mimetype=mime)
def resolve_lyrics(self, info, track): data = decode_data(get_key(), track) title = data['title'] lyrics = Lyrics.get_lyrics(title) return Lyric(lyrics=lyrics)
def stream_v2(): url = request.args.get('url') try: vid_id = decode_data(get_key(), url)['id'] url = get_stream(vid_id) return jsonify(status=200, url=url) except Exception as e: return make_error_response(msg=str(e), endpoint='api/v2/stream')
def get_link_v2(): url = request.args.get('url') try: data = decode_data(get_key(), url) vid_id = data['id'] retval = get_download(vid_id) return jsonify(status=200, url=retval) except Exception as e: return make_error_response(msg=str(e), endpoint='/api/v2/g')
def download_file(): """ Download the file from the server. First downloads the file on the server using wget and then converts it using ffmpeg """ try: url = request.args.get('url') # decode info from url data = decode_data(get_key(), url) vid_id = data['id'] url = data['url'] filename = get_filename_from_title(data['title'], ext='') m4a_path = 'static/%s.m4a' % vid_id # ^^ vid_id regex is filename friendly [a-zA-Z0-9_-]{11} # Handle partial request range_header = request.headers.get('Range', None) if range_header: from_bytes, until_bytes = range_header.replace('bytes=', '').split('-') if not until_bytes: until_bytes = int(from_bytes) + int( 1024 * 1024 * 1) # 1MB * 1 = 1MB headers = {'Range': 'bytes=%s-%s' % (from_bytes, until_bytes)} resp = requests.get(url, headers=headers, stream=True) rv = Response(generate_data(resp), 206, mimetype='audio/mp4', direct_passthrough=True) rv.headers.add('Content-Range', resp.headers.get('Content-Range')) rv.headers.add('Content-Disposition', 'attachment; filename="%s"' % filename) rv.headers.add('Content-Length', resp.headers['Content-Length']) return rv resp = requests.get(url, stream=True) filename += '.m4a' response = Response(generate_data(resp), mimetype='audio/mp4') # set headers # http://stackoverflow.com/questions/93551/how-to-encode-the-filename- response.headers.add('Content-Disposition', 'attachment; filename="%s"' % filename) response.headers.add('Content-Length', resp.headers['Content-Length']) return response except Exception as e: logger.info(traceback.format_exc()) return 'Bad things have happened', 500
def stream(): url = request.args.get('url') stream_settings = { 'lo': 'webm[abr<=64]/webm[abr<=80]/m4a[abr<=64]/[abr<=96]/m4a', 'md': 'webm[abr>=64][abr<=96]/[abr>=64][abr<=96]/webm[abr>=96][abr<=128]/webm/m4a', 'hi': 'webm/m4a' } try: req = decode_data(get_key(), url) vid_id = req['id'] quality = req.get('quality', 'md') url = get_or_create_video_download_link(vid_id, stream_settings[quality], get_download_link_youtube) except Exception as e: logger.info(traceback.format_exc()) return make_error_response(msg=str(e), endpoint='api/v1/stream') return jsonify(status=200, url=url_for('stream_handler', url=encode_data(get_key(), url=url)))
def suggest_songs(): try: url = request.args.get('url') decoded_data = decode_data(get_key(), url) vid_id = decoded_data["id"] vids = get_suggestions(vid_id) count = len(vids) return jsonify({ "status": 200, "request_location": "/api/v1/suggest", "metadate": { "count": count }, "results": vids }) except Exception as e: logger.info(traceback.format_exc()) return make_error_response(msg=str(e), endpoint='/api/v1/suggest')