mirror of https://git.sr.ht/~cadence/NewLeaf
Remove `with requests` when it is unnecessary
This commit is contained in:
parent
73b4fbabf7
commit
68cfbb809f
|
@ -16,7 +16,7 @@ def extract_captions_from_dict(captions, *, lang=None, label=None):
|
||||||
return captions
|
return captions
|
||||||
|
|
||||||
url = next(caption["second__remoteUrl"] for caption in captions["captions"] if caption["languageCode"] == lang or caption["label"] == label)
|
url = next(caption["second__remoteUrl"] for caption in captions["captions"] if caption["languageCode"] == lang or caption["label"] == label)
|
||||||
with requests.get(url) as r:
|
r = requests.get(url)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
# remove extraneous " align:start position:0%" on timestamps lines on auto-generated captions
|
# remove extraneous " align:start position:0%" on timestamps lines on auto-generated captions
|
||||||
if (lang and "auto-generated" in lang) or (label and "auto-generated" in label):
|
if (lang and "auto-generated" in lang) or (label and "auto-generated" in label):
|
||||||
|
|
|
@ -18,7 +18,7 @@ def extract_channel(ucid):
|
||||||
return channel_cache[ucid]
|
return channel_cache[ucid]
|
||||||
|
|
||||||
channel_type = "channel" if len(ucid) == 24 and ucid[:2] == "UC" else "user"
|
channel_type = "channel" if len(ucid) == 24 and ucid[:2] == "UC" else "user"
|
||||||
with requests.get("https://www.youtube.com/{}/{}/videos?hl=en".format(channel_type, ucid), cookies=eu_consent_cookie()) as r:
|
r = requests.get("https://www.youtube.com/{}/{}/videos?hl=en".format(channel_type, ucid), cookies=eu_consent_cookie())
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
yt_initial_data = extract_yt_initial_data(r.content.decode("utf8"))
|
yt_initial_data = extract_yt_initial_data(r.content.decode("utf8"))
|
||||||
|
|
||||||
|
@ -167,7 +167,7 @@ def extract_channel_latest(ucid):
|
||||||
if ucid in channel_latest_cache:
|
if ucid in channel_latest_cache:
|
||||||
return channel_latest_cache[ucid]
|
return channel_latest_cache[ucid]
|
||||||
|
|
||||||
with requests.get("https://www.youtube.com/feeds/videos.xml?channel_id={}".format(ucid)) as r:
|
r = requests.get("https://www.youtube.com/feeds/videos.xml?channel_id={}".format(ucid))
|
||||||
if r.status_code == 404:
|
if r.status_code == 404:
|
||||||
cherrypy.response.status = 404
|
cherrypy.response.status = 404
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -11,7 +11,7 @@ def extract_manifest(id):
|
||||||
return video
|
return video
|
||||||
|
|
||||||
if video["second__providedDashUrl"]:
|
if video["second__providedDashUrl"]:
|
||||||
with requests.get(video["second__providedDashUrl"]) as r:
|
r = requests.get(video["second__providedDashUrl"])
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ ytdl = yt_dlp.YoutubeDL(ytdl_opts)
|
||||||
|
|
||||||
def extract_search(q):
|
def extract_search(q):
|
||||||
try:
|
try:
|
||||||
with requests.get("https://www.youtube.com/results", params={"q": q, "hl": "en"}, cookies=eu_consent_cookie()) as r:
|
r = requests.get("https://www.youtube.com/results", params={"q": q, "hl": "en"}, cookies=eu_consent_cookie())
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
content = r.content.decode("utf8")
|
content = r.content.decode("utf8")
|
||||||
yt_initial_data = extract_yt_initial_data(content)
|
yt_initial_data = extract_yt_initial_data(content)
|
||||||
|
|
|
@ -20,7 +20,7 @@ def extract_search_suggestions(q):
|
||||||
"xhr": "t",
|
"xhr": "t",
|
||||||
# "xssi": "t"
|
# "xssi": "t"
|
||||||
}
|
}
|
||||||
with requests.get("https://clients1.google.com/complete/search", params=params) as r:
|
r = requests.get("https://clients1.google.com/complete/search", params=params)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
response = r.json()
|
response = r.json()
|
||||||
result = {
|
result = {
|
||||||
|
|
4
index.py
4
index.py
|
@ -123,14 +123,14 @@ class NewLeaf(object):
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def vi(self, id, file):
|
def vi(self, id, file):
|
||||||
with requests.get("https://i.ytimg.com/vi/{}/{}".format(id, file), stream=True) as r:
|
r = requests.get("https://i.ytimg.com/vi/{}/{}".format(id, file), stream=True)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
cherrypy.response.headers["content-type"] = r.headers["content-type"]
|
cherrypy.response.headers["content-type"] = r.headers["content-type"]
|
||||||
return next(r.iter_content(chunk_size=None))
|
return next(r.iter_content(chunk_size=None))
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def ggpht(self, *path):
|
def ggpht(self, *path):
|
||||||
with requests.get("https://yt3.ggpht.com/{}".format("/".join(path)), stream=True) as r:
|
r = requests.get("https://yt3.ggpht.com/{}".format("/".join(path)), stream=True)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
cherrypy.response.headers["content-type"] = r.headers["content-type"]
|
cherrypy.response.headers["content-type"] = r.headers["content-type"]
|
||||||
return next(r.iter_content(chunk_size=None))
|
return next(r.iter_content(chunk_size=None))
|
||||||
|
|
Loading…
Reference in New Issue