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: return make_error_response(msg=str(e), endpoint='/api/v1/suggest')
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'] retval = get_download_link_youtube(vid_id, 'm4a/bestaudio') 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(): 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 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 search_v2(): try: search_term = request.args.get('q') raw_html = get_search_results_html(search_term) vids = get_videos(raw_html) ret_vids = [] for _ in vids: temp = get_video_attrs(_, removeLongResult=False) if temp: temp['get_url'] = '/api/v2' + temp['get_url'] temp['stream_url'] = '/api/v2' + temp['stream_url'] temp['suggest_url'] = temp['get_url'].replace( 'v2/g?', 'v1/suggest?', 1) ret_vids.append(temp) ret_dict = make_search_api_response(search_term, ret_vids, '/api/v2/search') except Exception as e: return make_error_response(msg=str(e), endpoint='/api/v2/search') return jsonify(ret_dict)
def search(): """ Search youtube and return results """ try: search_term = request.args.get('q') raw_html = get_search_results_html(search_term) vids = get_videos(raw_html) ret_vids = [] for _ in vids: temp = get_video_attrs(_) if temp: temp['get_url'] = '/api/v1' + temp['get_url'] temp['stream_url'] = '/api/v1' + temp['stream_url'] temp['suggest_url'] = temp['get_url'].replace('/g?', '/suggest?', 1) ret_vids.append(temp) ret_dict = make_search_api_response(search_term, ret_vids, '/api/v1/search') except Exception as e: logger.info(traceback.format_exc()) return make_error_response(msg=str(e), endpoint='/api/v1/search') return jsonify(ret_dict)