-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (68 loc) · 2.54 KB
/
main.py
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
from flask import send_file, Flask, jsonify
from flask_cors import CORS, cross_origin
from youtube_search import YoutubeSearch
from spotify import SpotifyAPI
import re
import pafy
# import requests
''' do not do pip install pafy, do - pip install -e git+git://github.com/mohamed-challal/pafy.git@develop#egg=pafy '''
app = Flask(__name__)
CORS(app, support_credentials=True)
client_id = '574113ae79c246db956bfc33471fdf0d'
client_secret = '60c1057e87c14c7595044d7a0a51be51'
spotify = SpotifyAPI(client_id, client_secret)
@app.route("/info/<query>")
@cross_origin(supports_credentials=True)
def info(query):
results = spotify.search(query)
result_length = len(results['tracks']['items'])
if result_length == 0:
pass
else:
unsorted_search_results = []
popularity_index = []
count = 0
for result in range(result_length):
song = results['tracks']['items'][result]['name']
artist = results['tracks']['items'][result]['album']['artists'][0]['name']
popularity = results['tracks']['items'][result]['popularity']
re_string = f"/{song}{artist.title()}"
redirect = re.sub('[^A-Za-z0-9]+', '', re_string.lower())
popularity_index.append(popularity)
unsorted_search_results.append({
'name': f"{song} - {artist.title()}",
'popularity': int(popularity)}
)
count += 1
search_results = []
for x in popularity_index:
max_index = popularity_index.index(max(popularity_index))
search_results.insert(-1, unsorted_search_results[max_index])
popularity_index.pop(max_index)
unsorted_search_results.pop(max_index)
top_song = search_results.pop()
search_results.insert(0, top_song)
return jsonify(search_results)
@app.route("/intro.pdf")
@cross_origin(supports_credentials=True)
def pdf():
return send_file('intro.pdf')
@app.route("/")
@cross_origin(supports_credentials=True)
def home():
return("search on spotify - /info/query<br>get youtube meta data and mp3 - /id/query")
@app.route("/id/<query>")
@cross_origin(supports_credentials=True)
def link(query):
data = YoutubeSearch(query, max_results=1).to_dict()
print(data)
id = data[0]['id']
url = "www.youtube.com/watch?v=" + id
audio = pafy.new(url).getbestaudio().url
""" try:
requests.get(audio)
except requests.ConnectionError or urllib3.exceptions.LocationParseError or requests.exceptions.InvalidURL or requests.exceptions.MissingSchema:
return "The selected song can't be played at the moment" """
result = ({"audio" : audio, "info": data[0]})
return jsonify(result)
app.run(host='0.0.0.0',debug=True)