-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
111 lines (96 loc) · 3.25 KB
/
server.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
from pytube import Search
from pytube import YouTube
import youtube_dl
from flask import Flask, send_file, request
import os
from flask_cors import CORS
from flask_restful import Api, Resource
app = Flask(__name__)
app.config.from_object(__name__)
CORS(app, resources={r"/*":{'origins':"*"}})
api = Api(app)
dic = {"users": []}
print(youtube_dl.__file__)
@app.route('/search/<query>', methods=['GET'])
def ReturnSearchQuery(query):
s = Search(query)
print(query)
first_result = str(s.results[0])
id = first_result.split('=')[1].split(">")[0]
print(id)
url = ("https://www.youtube.com/watch?v=" + id)
print(url)
vid = YouTube(url)
print("task 1 complete")
video = vid.streams.filter(only_audio=True).first()
print("task 2 complete")
# out_file = video.download(output_path="C:/Users/Ephre/Documents/adonias projects/VueApps/Mezmur/Mezmur_frontend/src/views/")
# base, ext = os.path.splitext(out_file)
# new_file = base + '.mp3'
# os.rename(out_file, new_file)
ydl_options = {'formate': "bestaudio"}
with youtube_dl.YoutubeDL(ydl_options) as dl:
info = dl.extract_info(url, download = False)
print("task 3 complete")
url2 = info['formats'][0]['url']
print('Task Completed!')
url3 = {"url": url2}
return url3
@app.route('/searchList/<search>', methods=['GET'])
def ReturnResults(search):
query = Search(search)
results = query.results
returns = {"one":[], "two":[], "three":[], "four":[], "five": [], "six":[]}
for x in range(6):
info = str(results[x]).split('=')[1].split(">")[0]
url = ("https://www.youtube.com/watch?v=" + info)
result = YouTube(url)
title = result.title
author = result.author
dic = {"title": title, "author": author}
if x == 0:
returns["one"].append(dic)
if x == 1:
returns["two"].append(dic)
if x == 2:
returns["three"].append(dic)
if x == 3:
returns["four"].append(dic)
if x == 4:
returns["five"].append(dic)
if x == 5:
returns["six"].append(dic)
return returns
@app.route('/Create/<username>/<password>', methods=['GET'])
def CreateUser(username, password):
user = {"name": username, "password": password, "playlists": []}
for x in dic["users"]:
print(x)
if username == x["name"]:
return ("username already taken")
dic["users"].append(user)
return dic
@app.route('/SignIn/<username>/<password>', methods=['GET'])
def SignInUser(username, password):
for x in dic["users"]:
print(x)
if username == x["name"]:
if password == x["password"]:
return("success")
return("invalid")
@app.route('/createPlaylist/<Id>/<Playlist_name>', methods=['GET'])
def CreatePlaylist(Id, Playlist_name):
playlist = {"pl_name": Playlist_name, "list": []}
for x in dic["users"]:
if Id == x["name"]:
x["playlists"].append(playlist)
return dic
@app.route('/playlists/<id>/')
def returnPlaylist():
for x in dic["users"]:
if id == x["name"]:
return x["playlists"]
return("invalid")
if __name__ == "__main__":
app.run(debug=True)
print("running!")