-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
192 lines (167 loc) · 6.83 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from flask import Flask, request, jsonify, render_template, redirect
from deta import Deta
import requests
deta = Deta()
app = Flask(__name__,
static_url_path='/static',
static_folder='static')
invidious_base_url = 'https://invidious.baczek.me/'
def get_invidious_url():
# inv_api="https://api.invidious.io/instances.json?pretty=1&sort_by=api,health"
# r = requests.get(inv_api)
# data = r.json()
#return(data[0][1]['uri'])
return "https://invidious.baczek.me/"
@app.route('/')
def index():
courses = deta.Base('courses').fetch().items
#invidious_base_url = get_invidious_url()
for i in courses:
i['complete_percentage'] = round((len(i['watched'])/i['videoCount'])*100)
return render_template('dashboard.html', courses=courses)
@app.route('/create_course', methods=['GET'])
def create_course():
return render_template('new_course.html')
@app.route('/new_course', methods=['POST'])
def new_course():
url = request.form['url']
if url.startswith('https://www.youtube.com/playlist?')==False:
return jsonify({'success': False, 'error': 'Please enter a valid YouTube playlist URL'})
else:
playlist_id= url.split('=')[1]
invidious_base_url = get_invidious_url()
invidious_url = f'{invidious_base_url}api/v1/playlists/{playlist_id}'
r = requests.get(invidious_url)
data = r.json()
watched = {'watched': []}
data.update(watched)
data['playlistThumbnail'] = data['playlistThumbnail'].replace('hqdefault', 'maxresdefault')
base = deta.Base('courses')
base.put(data)
return jsonify({'success': True})
@app.route('/course/new_from_id', methods=['POST'])
def new_from_id():
playlist_id = request.form['playlist_id']
invidious_base_url = get_invidious_url()
invidious_url = f'{invidious_base_url}api/v1/playlists/{playlist_id}'
r = requests.get(invidious_url)
data = r.json()
watched = {'watched': []}
data.update(watched)
data['playlistThumbnail'] = data['playlistThumbnail'].replace('hqdefault', 'maxresdefault')
base = deta.Base('courses')
base.put(data)
return redirect('/')
@app.route('/course/delete', methods=['POST'])
def delete():
base = deta.Base('courses')
id = request.form['id']
base.delete(id)
return redirect('/')
@app.route('/course/<id>', methods=['GET'])
def course(id):
invidious_base_url = get_invidious_url()
course = deta.Base('courses').fetch({'key': id}).items[0]
all_videos = course['videos']
watched = course['watched']
unwatched = [i for i in all_videos if i not in watched]
current_video = unwatched[0]
try:
next_video = unwatched[1]
except:
next_video = None
try:
previous_video = watched[-1]
except:
previous_video = None
complete_percentage = round((len(watched)/course['videoCount'])*100)
return render_template('course.html', course=course, current_video=current_video, next=next_video, prev=previous_video, complete_percentage=complete_percentage, inv_url=get_invidious_url())
@app.route('/course/<id>/video/<video_id>/watched', methods=['GET'])
def watched(id, video_id):
base = deta.Base('courses')
course = base.fetch({'key': id}).items[0]
watched = course['watched']
video = [i for i in course['videos'] if i['videoId'] == video_id][0]
watched.append(video)
base.update({'watched': watched}, id)
all_videos = course['videos']
unwatched = [i for i in all_videos if i not in watched]
try:
next_video = unwatched[1]
except:
next_video = None
return redirect(f'/course/{id}/video/{next_video["videoId"]}')
@app.route('/course/<id>/unwatched', methods=['POST'])
def unwatched(id):
base = deta.Base('courses')
course = base.fetch({'key': id}).items[0]
watched = course['watched']
watched.remove(request.form['video'])
base.update({'watched': watched}, id)
return jsonify({'success': True})
@app.route('/course/<id>/video/<video_id>', methods=['GET'])
def video(id, video_id):
invidious_base_url = get_invidious_url()
course = deta.Base('courses').fetch({'key': id}).items[0]
all_videos = course['videos']
video = [i for i in all_videos if i['videoId'] == video_id][0]
video_api_url = f'{invidious_base_url}api/v1/videos/{video_id}'
print(video_api_url)
r = requests.get(video_api_url)
video_data = r.json()
description = video_data['description'].replace('\n', '<br>')
return render_template('vid.html', course=course, video=video, inv_url=invidious_base_url, description=description, all_videos=all_videos)
@app.route('/course/<id>/notes', methods=['POST'])
def notes(id):
updated_notes = request.form['textarea']
base = deta.Base('courses')
base.update({'notes': updated_notes}, id)
course_notes = base.fetch({'key': id}).items[0]['notes']
print(course_notes)
return jsonify({'success': True})
@app.route('/course/notes/<id>', methods=['GET'])
def get_notes(id):
course = deta.Base('courses').fetch({'key': id}).items[0]
try:
notes = course['notes']
except:
notes = "Notes"
return render_template('course_notes.html', course=course, notes=notes)
@app.route('/__space/v0/actions', methods=['POST'])
def actions():
action = request.json
if action['event']['id'] == 'update_playlists':
all_courses = deta.Base('courses').fetch().items
for i in all_courses:
playlist_id = i['playlistId']
invidious_base_url = get_invidious_url()
invidious_url = f'{invidious_base_url}api/v1/playlists/{playlist_id}'
r = requests.get(invidious_url)
data = r.json()
videos = data['videos']
video_count = data['videoCount']
base = deta.Base('courses')
base.update({'videos': videos, 'videoCount': video_count}, i['key'])
return jsonify({'success': True})
@app.route("/search", methods=['GET'])
def search():
query = request.args.get('q')
invidious_base_url = get_invidious_url()
search_url = f"{invidious_base_url}api/v1/search?q={query}&type=playlist"
r = requests.get(search_url)
data = r.json()
return render_template('search.html',inv_url=invidious_base_url, results=data, q=query)
@app.route("/api/search/suggestions", methods=['GET'])
def search_suggestions():
query = request.args.get('q')
invidious_base_url = get_invidious_url()
search_url = f'{invidious_base_url}api/v1/search/suggestions?q={query}'
r = requests.get(search_url)
data = r.json()
suggestions = data['suggestions']
return jsonify(suggestions)
@app.route("/course/search", methods=['GET'])
def search_course():
return render_template('search.html')
def search_results(data):
return render_template('search_results.html', results=data)