-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
142 lines (119 loc) · 4.61 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from flask import Flask, render_template, request, jsonify
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from ytmusicapi import YTMusic
import json
import youtube_dl
from pytube import YouTube
from colorthief import ColorThief
from urllib.request import urlopen
from io import BytesIO
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///luna.db'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
class Luna(db.Model):
sno = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(500), nullable=False)
desc = db.Column(db.String(500), nullable=False)
created = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self) -> str:
return f"{self.sno} - {self.title}"
# Routing
@app.route('/', methods=['GET'])
def homepage():
home_data = ytmusic.get_home(10)
artists = []
genre = ytmusic.get_mood_categories()
recommended_playlists = []
recommended_music_videos = []
quick_picks = []
for data in home_data:
if data['title'] == 'Recommended playlists':
recommended_playlists = data['contents']
elif data['title'] == 'Quick picks':
quick_picks = data['contents']
index=0;
while artists==[]:
try:
artists = ytmusic.get_artist(quick_picks[index]["artists"][0]["id"])[
'related']['results']
json_object = json.dumps(artists, indent=4)
f = open("trial.txt", 'w')
f.write(json_object)
except:
index+=1
return render_template('index.html', quick_picks=quick_picks, artists=artists, recommended_playlists=recommended_playlists, recommended_music_videos=recommended_music_videos, genre_category=genre)
@app.route('/watch')
def watch():
return render_template('now-playing.html')
# API
@app.route('/recommendedQueue', methods=['GET'])
def queue():
id = request.args.get("videoId")
watch_playlist = ytmusic.get_watch_playlist(videoId=id)
return watch_playlist
@app.route('/search', methods=['GET'])
def search():
search = request.args.get("search")
result=ytmusic.search(search,limit=100)
songs=[]
albums=[]
community_playlist=[]
artists=[]
for element in result:
if(element['category']=='Songs'):
songs.append(element)
elif(element['category']=='Community playlists'):
community_playlist.append(element)
elif(element['category']=='Albums'):
albums.append(element)
elif(element['category']=='Artists'):
artists.append(element)
return render_template('search.html',songs=songs,albums=albums,community_playlist=community_playlist,artists=artists)
@app.route('/getSong', methods=['GET'])
def getSong():
id = request.args.get("videoId")
song_detail = ytmusic.get_song(videoId=id)
song_url = f"https://music.youtube.com/watch?v={id}"
print('waiting')
try:
ydl_opts = {
'cachedir': 'D:\Web dev\Luna\cache',
'youtube_skip_dash_manifest': True,
# 'format': 'bestaudio/best',
'outtmpl': '%(title)s.%(ext)s',
'quiet': True,
'no_warnings': True,
'extractaudio': True,
'audioformat': 'mp3',
'preferredcodec': 'mp3',
# 'preferredquality': '192',
'nocheckcertificate': True,
'format_limit': 1,
'max_downloads': 1,
'verbose': True
}
url = ''
# Download the song and extract the streaming URL
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(song_url, download=False)
print('done')
url = info['formats'][0]['url']
except:
yt = YouTube(f'https://www.youtube.com/watch?v={id}')
url = yt.streams.filter(only_audio=True).first().url
print(url)
# json_object = json.dumps(song_detail, indent=4)
# f = open("trial.txt", 'w')
# f.write(json_object)
# Create a new ColorThief object with the image
image_data = urlopen(song_detail['videoDetails']['thumbnail']['thumbnails'][len(song_detail['videoDetails']['thumbnail']['thumbnails'])-1]['url']).read()
image = BytesIO(image_data)
color_thief = ColorThief(image)
dominant_color = color_thief.get_color(quality=1)
print(dominant_color)
return jsonify({'url': url, 'songDetail': song_detail,'color':dominant_color})
if __name__ == "__main__":
ytmusic = YTMusic('D:\Web dev\Luna\python\headers_auth.json')
app.run(debug=False,host='0.0.0.0')