-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
110 lines (96 loc) · 3.58 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from bs4 import BeautifulSoup
import flask
from flask import request
import os
from random import choice
import json
import requests
from spotify import getTokenSpt, topTrack, getSongQ, searchSong
from genius import gTData, get_Lyric
from decouple import config
app = flask.Flask(__name__)
# My favorite artist ids list
MY_ARTIST = [
'0vR2qb8m9WHeZ5ByCbimq2', # Reik
'5Pwc4xIPtQLFEnJriah9YJ', # OneRepublic
'4VMYDCV2IEDYJArk749S6m', # Daddy Yankee
'4wLXwxDeWQ8mtUIRPxGiD6', # Marc Anthony
'6eUKZXaKkcviH0Ku9w2n3V', # Ed Sheeran
'1XXUv8GRyRqOXVuDwB5QaS', # Leoni Torres
'2cy1zPcrFcXAJTP0APWewL', # Gente de Zona
]
@app.route('/', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
artist_name01 = (request.form.get('artist_name')).lower()
song_name01 = (request.form.get('song_name')).lower()
if not artist_name01 or not song_name01:
return
# token
token = getTokenSpt()
# Random artis ID
artistId = choice(MY_ARTIST)
data_from_search = searchSong(song_name01, artist_name01, token)
try:
trackName = data_from_search['name']
artistName = data_from_search['artists'][0]['name']
except:
return flask.render_template("search.html")
# the string to uses to get de genius data
songString = getSongQ(trackName, artistName)
# Genius data to have the Lyrics
geniusData = gTData(songString)
# I did this to have the song lyrics. OLD
# myLyrics = scrapSongUrl(geniusData['url'])
# I did this to have the song lyrics.
myLyrics02 = get_Lyric(artistName, trackName)
return flask.render_template(
"index.html",
songName = trackName,
artistName = artistName,
songPreviewUrl = data_from_search['preview_url'],
songImageUrl = data_from_search['album']['images'][1]['url'],
lyricsUrl = geniusData['url'],
artistImageUrl = geniusData['primary_artist']['image_url'],
myLyrics02 = myLyrics02
)
# otherwise handle the GET request
return flask.render_template(
"search.html")
@app.route('/play')
def index():
# token
token = getTokenSpt()
#Random artis ID
artistId = choice(MY_ARTIST)
#With the token and artist id to get the top tracks.
topTrackData = topTrack(token, artistId)
#from the top track data obtain the track name accessing to that with name.
trackName = topTrackData['name']
# the JSON return will help to have the artis name.
artistName = topTrackData['artists'][0]['name']
# the string to uses to get de genius data
songString = getSongQ(trackName, artistName)
# Genius data to have the Lyrics
geniusData = gTData(songString)
# I did this to have the song lyrics. OLD
#myLyrics = scrapSongUrl(geniusData['url'])
# I did this to have the song lyrics.
myLyrics02 = get_Lyric(artistName, trackName)
# this is the return part
return flask.render_template(
"index.html",
songName = trackName,
artistName = artistName,
songPreviewUrl = topTrackData['preview_url'],
songImageUrl = topTrackData['album']['images'][1]['url'],
lyricsUrl = geniusData['url'],
artistImageUrl = geniusData['primary_artist']['image_url'],
myLyrics02 = myLyrics02
)
# server and ips to run the app
app.run(
port=int(os.getenv('PORT', 3001)),
host=os.getenv('IP', '0.0.0.0'),
debug=True
)