Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support headers #316

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ Using the CLI:
youtube_transcript_api <first_video_id> <second_video_id> --cookies /path/to/your/cookies.txt
```

## Headers

You can pass additional headers to the `list_transcripts` method by providing a dictionary of headers as the `headers` parameter. This can be useful for various purposes, such as specifying the accepted encoding, setting a custom user agent, or including any other necessary headers for the request.

```python
headers = {'Accept-Encoding': 'gzip, deflate'}
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id, headers=headers)
```

## Warning

Expand Down
5 changes: 4 additions & 1 deletion youtube_transcript_api/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class YouTubeTranscriptApi(object):
@classmethod
def list_transcripts(cls, video_id, proxies=None, cookies=None):
def list_transcripts(cls, video_id, proxies=None, cookies=None, headers=None):
"""
Retrieves the list of transcripts which are available for a given video. It returns a `TranscriptList` object
which is iterable and provides methods to filter the list of transcripts for specific languages. While iterating
Expand Down Expand Up @@ -61,13 +61,16 @@ def list_transcripts(cls, video_id, proxies=None, cookies=None):
:type proxies: {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
:param cookies: a string of the path to a text file containing youtube authorization cookies
:type cookies: str
:param headers: a dictionary of additional headers to include in the requests
:type headers: dict
:return: the list of available transcripts
:rtype TranscriptList:
"""
with requests.Session() as http_client:
if cookies:
http_client.cookies = cls._load_cookies(cookies, video_id)
http_client.proxies = proxies if proxies else {}
http_client.headers.update(headers) if headers else {}
return TranscriptListFetcher(http_client).fetch(video_id)

@classmethod
Expand Down
6 changes: 4 additions & 2 deletions youtube_transcript_api/_transcripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def _fetch_video_html(self, video_id):
return html

def _fetch_html(self, video_id):
response = self._http_client.get(WATCH_URL.format(video_id=video_id), headers={'Accept-Language': 'en-US'})
self._http_client.headers.update({'Accept-Language': 'en-US'})
response = self._http_client.get(WATCH_URL.format(video_id=video_id))
return unescape(_raise_http_errors(response, video_id).text)


Expand Down Expand Up @@ -288,7 +289,8 @@ def fetch(self, preserve_formatting=False):
:return: a list of dictionaries containing the 'text', 'start' and 'duration' keys
:rtype [{'text': str, 'start': float, 'end': float}]:
"""
response = self._http_client.get(self._url, headers={'Accept-Language': 'en-US'})
self._http_client.headers.update({'Accept-Language': 'en-US'})
response = self._http_client.get(self._url)
return _TranscriptParser(preserve_formatting=preserve_formatting).parse(
_raise_http_errors(response, self.video_id).text,
)
Expand Down
6 changes: 6 additions & 0 deletions youtube_transcript_api/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ def test_get_transcript__with_cookies(self):
]
)

def test_get_transcript__with_headers(self):
headers = {'Accept-Encoding': 'gzip, deflate'}
transcript_list = YouTubeTranscriptApi.list_transcripts('GJLlxj_dtq8', headers=headers)
language_codes = {transcript.language_code for transcript in transcript_list}
self.assertGreater(len(language_codes), 0)

def test_get_transcript__assertionerror_if_input_not_string(self):
with self.assertRaises(AssertionError):
YouTubeTranscriptApi.get_transcript(['video_id_1', 'video_id_2'])
Expand Down