1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# Copyright 2020 Quentin Kniep <[email protected]> |
4
|
|
|
# Distributed under terms of the MIT license. |
5
|
|
|
|
6
|
|
|
import os.path |
7
|
|
|
from threading import Thread |
8
|
|
|
import urllib.parse |
9
|
|
|
|
10
|
|
|
from flask import Flask, redirect, render_template, url_for |
11
|
|
|
import googleapiclient.discovery as google |
12
|
|
|
|
13
|
|
|
import config |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
app = Flask(__name__) |
17
|
|
|
youtube = google.build('youtube', 'v3', developerKey=config.YOUTUBE_API_KEY) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def url_enc(title): |
21
|
|
|
return urllib.parse.quote(urllib.parse.quote(title, safe='')) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def url_dec(title): |
25
|
|
|
return urllib.parse.unquote(urllib.parse.unquote(title)) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def youtube_search(query, maxres, recs=False): |
29
|
|
|
if recs: |
30
|
|
|
search_response = youtube.search().list( |
31
|
|
|
relatedToVideoId=query, |
32
|
|
|
part='id,snippet', |
33
|
|
|
maxResults=maxres, |
34
|
|
|
type='video', |
35
|
|
|
topicId='/m/04rlf', |
36
|
|
|
).execute() |
37
|
|
|
else: |
38
|
|
|
search_response = youtube.search().list( |
39
|
|
|
q=query, |
40
|
|
|
part='id,snippet', |
41
|
|
|
maxResults=maxres, |
42
|
|
|
type='video', |
43
|
|
|
topicId='/m/04rlf', |
44
|
|
|
).execute() |
45
|
|
|
|
46
|
|
|
videos = [] |
47
|
|
|
|
48
|
|
|
for search_result in search_response.get('items', []): |
49
|
|
|
if search_result['id']['kind'] == 'youtube#video': |
50
|
|
|
videos.append([search_result['id']['videoId'], |
51
|
|
|
search_result['snippet']['title'], |
52
|
|
|
url_enc(search_result['snippet']['title'])]) |
53
|
|
|
return videos |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
@app.route('/') |
57
|
|
|
def index(): |
58
|
|
|
return render_template('main.html', videos=queue, |
|
|
|
|
59
|
|
|
numVids=len(queue), current=currentlyPlaying, |
|
|
|
|
60
|
|
|
# volume=int(player.volume)) |
61
|
|
|
volume=100) |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
@app.route('/search/<keyword>') |
65
|
|
|
def search_results(keyword): |
66
|
|
|
keyword = url_dec(keyword) |
67
|
|
|
vids = youtube_search(keyword, 50) |
68
|
|
|
return render_template('results.html', videos=vids, |
69
|
|
|
header='Search Results', subheader='For: '+keyword) |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
@app.route('/rec/<ytid>/<title>') |
73
|
|
|
def yt_recommendations(ytid, title): |
74
|
|
|
title = url_dec(title) |
75
|
|
|
vids = youtube_search(ytid, 50, recs=True) |
76
|
|
|
return render_template('results.html', videos=vids, |
77
|
|
|
header='Recommendations', |
78
|
|
|
subheader='Based on: '+title) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
@app.route('/add/<ytid>/<title>') |
82
|
|
|
def add_song(ytid, title): |
83
|
|
|
title = url_dec(title) |
84
|
|
|
already_downloaded = False |
85
|
|
|
if os.path.isfile('downloads/'+ytid): |
86
|
|
|
already_downloaded = True |
87
|
|
|
queue.append([ytid, title, url_enc(title), already_downloaded]) |
|
|
|
|
88
|
|
|
return render_template('added') |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
@app.route('/remove/<ytid>') |
92
|
|
|
def remove_song(ytid): |
93
|
|
|
global queue |
94
|
|
|
queue = list(filter(lambda x: x[0] != ytid, queue)) |
95
|
|
|
return redirect(url_for('/')) |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
# @app.route('/skip') |
99
|
|
|
# def skip_song(): |
100
|
|
|
# player.seek(100, 'absolute-percent') |
101
|
|
|
# return redirect(url_for('/')) |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
# @app.route('/volume/<vol>') |
105
|
|
|
# def change_volume(vol): |
106
|
|
|
# if int(vol) > player.volume_max: |
107
|
|
|
# player.volume = player.volume_max |
108
|
|
|
# else: |
109
|
|
|
# player.volume = vol |
110
|
|
|
# return redirect(url_for('/')) |
111
|
|
|
|