Merge branch 'master' into feature/ISSUE-23

This commit is contained in:
Jonas Depoix 2021-03-22 18:54:07 +01:00
commit 3781907943
3 changed files with 47 additions and 31 deletions

View File

@ -1,9 +1,9 @@
# YouTube Transcript/Subtitle API (including automatically generated subtitles and subtitle translations) # YouTube Transcript/Subtitle API (including automatically generated subtitles and subtitle translations)
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=BAENLEW8VUJ6G&source=url) [![Build Status](https://travis-ci.org/jdepoix/youtube-transcript-api.svg)](https://travis-ci.org/jdepoix/youtube-transcript-api) [![Coverage Status](https://coveralls.io/repos/github/jdepoix/youtube-transcript-api/badge.svg?branch=master)](https://coveralls.io/github/jdepoix/youtube-transcript-api?branch=master) [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](http://opensource.org/licenses/MIT) [![image](https://img.shields.io/pypi/v/youtube-transcript-api.svg)](https://pypi.org/project/youtube-transcript-api/) [![image](https://img.shields.io/pypi/pyversions/youtube-transcript-api.svg)](https://pypi.org/project/youtube-transcript-api/) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=BAENLEW8VUJ6G&source=url) [![Build Status](https://travis-ci.com/jdepoix/youtube-transcript-api.svg)](https://travis-ci.com/jdepoix/youtube-transcript-api) [![Coverage Status](https://coveralls.io/repos/github/jdepoix/youtube-transcript-api/badge.svg?branch=master)](https://coveralls.io/github/jdepoix/youtube-transcript-api?branch=master) [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](http://opensource.org/licenses/MIT) [![image](https://img.shields.io/pypi/v/youtube-transcript-api.svg)](https://pypi.org/project/youtube-transcript-api/) [![image](https://img.shields.io/pypi/pyversions/youtube-transcript-api.svg)](https://pypi.org/project/youtube-transcript-api/)
This is an python API which allows you to get the transcripts/subtitles for a given YouTube video. It also works for automatically generated subtitles, supports translating subtitles and it does not require a headless browser, like other selenium based solutions do! This is a python API which allows you to get the transcript/subtitles for a given YouTube video. It also works for automatically generated subtitles, supports translating subtitles and it does not require a headless browser, like other selenium based solutions do!
## Install ## Install
@ -271,6 +271,12 @@ If you are not sure which languages are available for a given video you can call
youtube_transcript_api --list-transcripts <first_video_id> youtube_transcript_api --list-transcripts <first_video_id>
``` ```
If a video's ID starts with a hyphen you'll have to mask the hyphen using `\` to prevent the CLI from mistaking it for a argument name. For example to get the transcript for the video with the ID `-abc123` run:
```
youtube_transcript_api "\-abc123"
```
## Proxy ## Proxy
You can specify a https/http proxy, which will be used during the requests to YouTube: You can specify a https/http proxy, which will be used during the requests to YouTube:

View File

@ -128,4 +128,8 @@ class YouTubeTranscriptCli(object):
help='The cookie file that will be used for authorization with youtube.' help='The cookie file that will be used for authorization with youtube.'
) )
return parser.parse_args(self._args) return self._sanitize_video_ids(parser.parse_args(self._args))
def _sanitize_video_ids(self, args):
args.video_ids = [video_id.replace('\\', '') for video_id in args.video_ids]
return args

View File

@ -81,6 +81,12 @@ class TestYouTubeTranscriptCli(TestCase):
self.assertEqual(parsed_args.format, 'pretty') self.assertEqual(parsed_args.format, 'pretty')
self.assertEqual(parsed_args.languages, ['en']) self.assertEqual(parsed_args.languages, ['en'])
def test_argument_parsing__video_ids_starting_with_dash(self):
parsed_args = YouTubeTranscriptCli('\-v1 \-\-v2 \--v3'.split())._parse_args()
self.assertEqual(parsed_args.video_ids, ['-v1', '--v2', '--v3'])
self.assertEqual(parsed_args.json, False)
self.assertEqual(parsed_args.languages, ['en'])
def test_argument_parsing__fail_without_video_ids(self): def test_argument_parsing__fail_without_video_ids(self):
with self.assertRaises(SystemExit): with self.assertRaises(SystemExit):
YouTubeTranscriptCli('--format json'.split())._parse_args() YouTubeTranscriptCli('--format json'.split())._parse_args()