Skip to content

Commit 63880b0

Browse files
authored
Use curl User-Agent to avoid GDPR consent redirects (#647)
1 parent bc5ace3 commit 63880b0

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/youtubeaio/youtube.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ async def is_short(self, video_id: str) -> bool:
304304
self.session = ClientSession()
305305
self._close_session = True
306306
async with asyncio.timeout(self.session_timeout):
307-
response = await self._api_head_request(self.session, _url)
307+
response = await self.session.head(
308+
_url,
309+
allow_redirects=False,
310+
headers={"User-Agent": "curl/x.x"},
311+
)
308312
if response.status == 200:
309313
return True
310314
if response.status in {303, 302, 301}:

tests/test_short.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Tests for the YouTube client."""
22

3+
from typing import cast
4+
35
import aiohttp
6+
import aiohttp.web
47
from aresponses import ResponsesMockServer
58

69
from youtubeaio.youtube import YouTube
@@ -35,7 +38,7 @@ async def test_is_not_short(
3538
"/shorts/KXaMtA6kWXU",
3639
"HEAD",
3740
aresponses.Response(
38-
status=303,
41+
status=302,
3942
headers={
4043
"Content-Type": "application/binary",
4144
"Location": "https://www.youtube.com/watch?v=KXaMtA6kWXU",
@@ -47,3 +50,21 @@ async def test_is_not_short(
4750
response = await youtube.is_short("KXaMtA6kWXU")
4851
assert not response
4952
await youtube.close()
53+
54+
55+
async def test_is_short_sends_curl_user_agent(
56+
aresponses: ResponsesMockServer,
57+
) -> None:
58+
"""Test that is_short sends a curl User-Agent to avoid GDPR consent redirects."""
59+
60+
async def _assert_user_agent(
61+
request: aiohttp.web.Request,
62+
) -> aiohttp.web.Response:
63+
assert request.headers.get("User-Agent") == "curl/x.x"
64+
return cast("aiohttp.web.Response", aresponses.Response(status=200))
65+
66+
aresponses.add("www.youtube.com", "/shorts/2YUPfsi8PF4", "HEAD", _assert_user_agent)
67+
async with aiohttp.ClientSession() as session:
68+
youtube = YouTube(session=session)
69+
await youtube.is_short("2YUPfsi8PF4")
70+
await youtube.close()

0 commit comments

Comments
 (0)