-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyoutube_search_en.html
349 lines (349 loc) · 16.9 KB
/
youtube_search_en.html
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!DOCTYPE html>
<html lang = "en">
<head>
<title>YouTube Search</title>
<meta charset = "utf-8">
<script src = "https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js"></script>
<script src = "https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.min.js"></script>
<style>
html, body {
margin: 0px;
min-width: 520px;
}
#player {
display: flex;
flex-direction: column;
}
p {
font-family: Arial, sans-serif;
font-size: 20px;
margin: 20px;
}
h2 {
font-family: Arial, sans-serif;
font-size: 28px;
text-decoration: none;
font-weight: normal;
margin: 20px;
}
#load, #start {
margin: 0px 20px 20px;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
img {
margin: 0px 20px;
}
h1 {
font-family: Arial, sans-serif;
font-size: 36px;
font-weight: bold;
margin: 0px 20px 20px;
}
iframe {
margin: 0px 20px;
display: block;
}
input, button {
font-family: Arial, sans-serif;
font-size: 20px;
padding: 5px 7px;
margin: 0px 10px 20px;
border-radius: 0px;
text-decoration: none;
font-weight: normal;
background-color: #f8f8f8;
color: black;
-webkit-appearance: none;
border: 1px solid black;
}
input {
flex: 1;
width: 150px;
}
form, #vidops, #listops {
margin: 20px 10px 0px;
display: flex;
}
div, span {
margin: 0px;
}
table, tr, td {
border: 0px;
padding: 0px;
margin: 0px;
border-collapse: collapse;
}
td {
vertical-align: top;
}
</style>
</head>
<body onload = "brython (0)">
<noscript>
<p>JavaScript is required to use this website.</p>
</noscript>
<script type = "text/python">
from browser import document, ajax, html, window
from urllib import parse
from random import shuffle
global playlist, cors_url, player_pwd
document ["settings"].attrs ["hidden"] = "hidden"
def rfind (text, values, end = "}"):
pointer = 0
for j in values:
pointer += text [pointer : ].find (j)
if pointer == -1:
return "N/A"
return text [pointer + len (values [-1]) + 3 : pointer + text [pointer : ].find (end) - 1].replace ("\\n", "\n").replace ("\\u0026", "&").replace ('\\"', '"')
player_url = document.query.getvalue ("player_url")
if not player_url:
player_url = "https://www.youtube-nocookie.com/embed/%v?rel=0&playlist=%p&loop=1&modestbranding=1"
cors_url = document.query.getvalue ("cors_url")
if not cors_url:
cors_url = "https://corsproxy.io/?url=" # Default CORS proxy
playlist, player_pwd = [], ""
player_persist = False if document.query.getvalue ("player_persist") else True
show_id = False if document.query.getvalue ("show_id") else True
def complete (res):
if not player_persist:
remove_video ()
del document ["load"]
if "frame" in document and "vidops" not in document:
add_vidops ()
for i in res.text.split ("videoRenderer") [1 : ]:
vid_url = player_url.replace ("%v", rfind (i, ["videoId"], ",")).replace ("%p", rfind (i, ["videoId"], ","))
document ["results"] <= html.P (html.A (rfind (i, ["title", "text"]), tabindex = "0", href = vid_url, onclick = f'req_video ("{vid_url}")') + (f' ({rfind (i, ["videoId"], ",")})' if show_id else ""))
channel, user_url = rfind (i, ["ownerText", "text"], ","), rfind (i, ["webCommandMetadata", "url"], ",")
if channel != "N/A":
document ["results"] <= html.P (html.A (channel, tabindex = "0", href = f"https://www.youtube.com{user_url}/videos", onclick = f'req_user ("{user_url}", "{channel}")') + (f" ({user_url [1 : ]})" if show_id else ""))
views, timeago = rfind (i, ["accessibilityData", "label"]).split (" "), rfind (i, ["publishedTimeText", "simpleText"])
try:
views = views [views.index ("views") - 1]
except ValueError:
views = "N/A"
views = "" if views == "N/A" else f" • {views} views"
timeago = "" if timeago == "N/A" else f" • {timeago}"
document ["results"] <= html.P (f'{rfind (i, ["lengthText", "simpleText"])}{views}{timeago}')
document ["results"] <= html.A (html.IMG ("", src = f'https://i.ytimg.com/vi/{rfind (i, ["videoId"], ",")}/mqdefault.jpg', width = "480px"), tabindex = "-1", href = vid_url, onclick = f'req_video ("{vid_url}")')
document ["results"] <= html.DIV (style = "height: 20px;")
def req_search (homepage = False):
document ["settings"].attrs ["hidden"] = "hidden"
if "load" not in document:
document ["results"].clear ()
search = document ["search"].value
if homepage:
document ["enter"].textContent = "Settings"
document ["results"] <= html.H1 ("Homepage")
cors_get ("https://www.youtube.com/?app=desktop&hl=en&persist_app=1")
document ["player"] <= html.P ("Loading...", id = "load")
elif search:
document ["results"] <= html.H1 (f'Results for "{search}"')
cors_get ("https://www.youtube.com/results?app=desktop&hl=en&persist_app=1&search_query=" + parse.quote (search).replace (" ", "+") + "&sp=EgIQAQ%253D%253D")
document ["player"] <= html.P ("Loading...", id = "load")
else:
del document ["settings"].attrs ["hidden"]
window.scrollTo (0, 0)
def req_user (url, name):
window.event.preventDefault ()
if "load" not in document:
document ["results"].clear ()
document ["results"] <= (html.H1 (f'Videos by "{name}" ({url [1 : ]})'))
document ["player"] <= html.P ("Loading...", id = "load")
window.scrollTo (0, 0)
cors_get ("https://www.youtube.com" + url + "/videos?app=desktop&hl=en&persist_app=1")
def req_video (url):
window.event.preventDefault ()
if "load" not in document:
remove_video (False)
document ["player"] <= html.P ("Loading...", id = "load")
window.scrollTo (0, 0)
if player_persist and "frame" in document:
if document ["frame"].attrs ["height"] == "0px":
document ["frame"].attrs ["style"] = ""
document ["frame"].id = "player_old"
document ["player"] <= html.IFRAME (src = url, flex = "1", height = "0px", id = "frame", frameBorder = "0", allowfullscreen = "allowfullscreen", onload = "reload_frame ();")
def reload_frame ():
document ["frame"].onload = ""
document ["frame"].src += ""
del document ["load"]
if "player_old" in document:
del document ["player_old"]
document ["player"].attrs ["style"] = "height: 100vh; height: 100dvh;"
document ["frame"].attrs ["height"] = "100%"
document ["frame"].attrs ["style"] = ""
if "vidops" not in document:
add_vidops ()
def remove_video (frame = True):
if "vidops" in document:
del document ["vidops"]
document ["frame"].attrs ["style"] = "margin: 0px 20px 20px;"
if "frame" in document and frame:
del document ["frame"]
document ["player"].attrs ["style"] = ""
def search_id (imp = False):
url, run = document ["search"].value, ""
if url [ : 6] == "ytpid:" and len (url) > 16 and len (url) % 12 == 5:
if imp:
playlist.extend (url [6 : ].split (","))
list_manage ()
else:
req_video (player_url.replace ("%v", url [6 : 17]).replace ("%p", url [6 : ]))
elif not imp:
for i in url + " ":
if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_":
run += i
else:
if len (run) == 11:
req_video (player_url.replace ("%v", run).replace ("%p", run))
break
run = ""
else:
if "frame" in document:
listindex, frame_url = player_url.find ("%p") + 9, document ["frame"].src
document ["search"].value = "ytpid:" + frame_url [listindex : max (0, frame_url.find ("&", listindex))]
def hide_show ():
global player_pwd
if document ["visibility"].textContent == "Hide":
document ["frame"].attrs ["height"] = "0px"
document ["player"].attrs ["style"] = ""
document ["vidops"].attrs ["style"] = "margin: 0px 10px;"
document ["visibility"].textContent = "Show"
elif not player_pwd or player_pwd == window.prompt ("Enter player password:"):
document ["frame"].attrs ["height"] = "100%"
document ["player"].attrs ["style"] = "height: 100vh; height: 100dvh;"
document ["vidops"].attrs ["style"] = "margin: 20px 10px 0px;"
document ["visibility"].textContent = "Hide"
else:
window.alert ("Password incorrect.")
def list_change (change = True):
if "change" in document:
vid_id = document ["frame"].src [player_url.find ("%v") : player_url.find ("%v") + 11]
if change:
if vid_id in playlist:
playlist.remove (vid_id)
document ["change"].textContent = "Add"
else:
playlist.append (vid_id)
document ["change"].textContent = "Remove"
if "listops" in document:
list_manage ()
else:
document ["change"].textContent = ("Add", "Remove") [vid_id in playlist]
def list_manage ():
global player_pwd
if player_pwd and player_pwd != window.prompt ("Enter player password:"):
window.alert ("Password incorrect.")
return
document ["settings"].attrs ["hidden"] = "hidden"
list_change (False)
document ["results"].innerHTML = f"""
<h1>Managing Playlist</h1>
<h2>List length: {len (playlist)}<button style = "margin: 0px 0px 0px 20px;" onclick = "list_op ('clear');">Clear List</button></h2>
<div id = "listops">
<button style = "flex: 1;" onclick = "list_op ('reverse');">Reverse</button>
<button style = "flex: 1;" onclick = "list_op ('shuffle');">Shuffle</button>
<button style = "flex: 1;" onclick = "search_id (true);">Import</button>
<button style = "flex: 1;" onclick = "list_op ('export');">Export</button>
<button style = "flex: 1;" onclick = "list_op ('play');">Play</button>
</div>
"""
for j, i in enumerate (playlist):
document ["results"] <= html.TABLE (id = f"manage{j}", width = "520px")
vid_url = player_url.replace ("%v", i).replace ("%p", i)
document [f"manage{j}"].innerHTML = f"""
<tr style = "height: 1px;">
<td rowspan = 3><a href = {vid_url} onclick = "req_video ('{vid_url}')"><img src = "https://i.ytimg.com/vi/{i}/mqdefault.jpg" width = "360px" style = "margin: 0px 0px 20px 20px;"></img></a></td>
<td><button style = "margin: 0px 20px 20px 20px; width: 100px;" type = "button" onclick = "list_swap ({j}, {j - 1})">Up</button></td>
</tr>
<tr style = "height: 1px;">
<td><button style = "margin: 0px 20px 20px 20px; width: 100px;" type = "button" onclick = "list_swap ({j}, {j + 1})">Down</button></td>
</tr>
<tr>
<td><button style = "margin: 0px 20px; width: 100px;" type = "button" onclick = "list_swap ({j}, -2)">Delete</button></td>
</tr>
"""
def list_swap (a, b):
if b == -2:
playlist.pop (a)
elif b > -1 and b < len (playlist):
playlist [a], playlist [b] = playlist [b], playlist [a]
list_manage ()
def list_op (op):
if op == "play" and playlist:
req_video (player_url.replace ("%v", playlist [0]).replace ("%p", ",".join (playlist)))
elif op == "shuffle":
shuffle (playlist)
elif op == "reverse":
playlist.reverse ()
elif op == "export":
document ["search"].value = "ytpid:" * bool (len (playlist)) + ",".join (playlist)
window.scrollTo (0, 0)
elif op == "clear":
playlist.clear ()
list_manage ()
def add_vidops ():
document ["frame"].attrs ["style"] = ""
document ["player"] <= html.DIV (id = "vidops")
document ["vidops"] <= html.BUTTON ("Close", style = "flex: 1;", onclick = "remove_video ();")
document ["vidops"] <= html.BUTTON ("Hide", id = "visibility", style = "flex: 1;", onclick = "hide_show ();")
document ["vidops"] <= html.BUTTON ("Add", id = "change", style = "flex: 1;", onclick = "list_change ();")
list_change (False)
document ["vidops"] <= html.BUTTON ("Manage", style = "flex: 1;", onclick = "list_manage ();")
document ["vidops"] <= html.BUTTON ("Play", style = "flex: 1;", onclick = "list_op ('play');")
def check_empty ():
if document ["search"].value:
document ["enter"].textContent = "Search"
else:
document ["enter"].textContent = "Settings"
def cors_get (url, oncomplete = complete):
if cors_url.lower () != "none":
ajax.get (cors_url + window.encodeURIComponent (url), oncomplete = oncomplete)
else:
ajax.get (url, oncomplete = oncomplete)
def set_pwd ():
global player_pwd
if not player_pwd or player_pwd == window.prompt ("Enter the current password:"):
player_pwd = window.prompt ("Enter a new password (leave blank to remove):")
if player_pwd:
document ["setpwd"].textContent = "Change"
else:
document ["setpwd"].textContent = "Set"
else:
window.alert ("Password incorrect.")
window.req_search = req_search
window.req_user = req_user
window.req_video = req_video
window.search_id = search_id
window.reload_frame = reload_frame
window.remove_video = remove_video
window.hide_show = hide_show
window.list_change = list_change
window.list_manage = list_manage
window.list_swap = list_swap
window.list_op = list_op
window.check_empty = check_empty
window.set_pwd = set_pwd
document ["request"] <= html.BUTTON ("Home", type = "reset", onclick = "req_search (true);")
document ["request"] <= html.INPUT (type = "text", id = "search", placeholder = "Search YouTube", autofocus = "autofocus", oninput = "check_empty ();")
document ["request"] <= html.BUTTON ("Settings", id = "enter", type = "submit")
document ["request"] <= html.BUTTON ("URL", type = "button", onclick = "search_id ();")
req_search (True)
</script>
<div id = "player">
<form action = "javascript:void (0);" id = "request" onsubmit = "req_search ();"></form>
</div>
<script>
document.body.insertAdjacentHTML ("beforeend", `<div id = "results"><p id = "start">Starting Brython...</p></div>`)
</script>
<div hidden id = "settings">
<h1>Settings</h1>
<h2>Player Password<button id = "setpwd" style = "margin: 0px 0px 0px 20px;" onclick = "set_pwd ();">Set</button></h2>
<p>Text</p>
</div>
</body>
</html>