1
0
mirror of https://git.sr.ht/~cadence/NewLeaf synced 2024-11-21 15:17:29 +00:00

Remove with requests when it is unnecessary

This commit is contained in:
Cadence Ember 2022-01-16 21:51:26 +13:00
parent 73b4fbabf7
commit 68cfbb809f
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
6 changed files with 244 additions and 244 deletions

View File

@ -16,7 +16,7 @@ def extract_captions_from_dict(captions, *, lang=None, label=None):
return captions
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()
# 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):

View File

@ -18,7 +18,7 @@ def extract_channel(ucid):
return channel_cache[ucid]
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()
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:
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:
cherrypy.response.status = 404
return {

View File

@ -11,7 +11,7 @@ def extract_manifest(id):
return video
if video["second__providedDashUrl"]:
with requests.get(video["second__providedDashUrl"]) as r:
r = requests.get(video["second__providedDashUrl"])
r.raise_for_status()
return r

View File

@ -17,7 +17,7 @@ ytdl = yt_dlp.YoutubeDL(ytdl_opts)
def extract_search(q):
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()
content = r.content.decode("utf8")
yt_initial_data = extract_yt_initial_data(content)

View File

@ -20,7 +20,7 @@ def extract_search_suggestions(q):
"xhr": "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()
response = r.json()
result = {

View File

@ -123,14 +123,14 @@ class NewLeaf(object):
@cherrypy.expose
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()
cherrypy.response.headers["content-type"] = r.headers["content-type"]
return next(r.iter_content(chunk_size=None))
@cherrypy.expose
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()
cherrypy.response.headers["content-type"] = r.headers["content-type"]
return next(r.iter_content(chunk_size=None))