File tree Expand file tree Collapse file tree 2 files changed +38
-9
lines changed Expand file tree Collapse file tree 2 files changed +38
-9
lines changed Original file line number Diff line number Diff line change 1
1
import argparse
2
+ from importlib .metadata import PackageNotFoundError , version
2
3
from typing import List
3
4
4
- from .proxies import GenericProxyConfig , WebshareProxyConfig
5
+ from ._api import FetchedTranscript , TranscriptList , YouTubeTranscriptApi
5
6
from .formatters import FormatterLoader
7
+ from .proxies import GenericProxyConfig , WebshareProxyConfig
8
+
6
9
7
- from ._api import YouTubeTranscriptApi , FetchedTranscript , TranscriptList
10
+ def get_version ():
11
+ try :
12
+ return version ("youtube-transcript-api" )
13
+ except PackageNotFoundError :
14
+ return "unknown"
8
15
9
16
10
17
class YouTubeTranscriptCli :
@@ -99,6 +106,9 @@ def _parse_args(self):
99
106
"other selenium based solutions do!"
100
107
)
101
108
)
109
+ parser .add_argument (
110
+ "--version" , action = "version" , version = f"%(prog)s, version { get_version ()} "
111
+ )
102
112
parser .add_argument (
103
113
"--list-transcripts" ,
104
114
action = "store_const" ,
Original file line number Diff line number Diff line change 1
- import pytest
1
+ import json
2
+ import subprocess
3
+ from importlib .metadata import version
2
4
from unittest import TestCase
3
5
from unittest .mock import MagicMock
4
6
5
- import json
7
+ import pytest
6
8
7
9
from youtube_transcript_api import (
8
- YouTubeTranscriptApi ,
9
- VideoUnavailable ,
10
10
FetchedTranscript ,
11
11
FetchedTranscriptSnippet ,
12
+ VideoUnavailable ,
13
+ YouTubeTranscriptApi ,
12
14
)
13
15
from youtube_transcript_api ._cli import YouTubeTranscriptCli
14
16
@@ -332,11 +334,28 @@ def test_run__generic_proxy_config(self):
332
334
"working due to YouTube changes."
333
335
)
334
336
def test_run__cookies (self ):
335
- YouTubeTranscriptCli (
336
- ("v1 v2 --languages de en " "--cookies blahblah.txt" ).split ()
337
- ).run ()
337
+ YouTubeTranscriptCli (("v1 v2 --languages de en --cookies blahblah.txt" ).split ()).run ()
338
338
339
339
YouTubeTranscriptApi .__init__ .assert_any_call (
340
340
proxy_config = None ,
341
341
cookie_path = "blahblah.txt" ,
342
342
)
343
+
344
+ def test_version_matches_metadata (self ):
345
+ """
346
+ `youtube_transcript_api --version` should return the same version as in the package metadata.
347
+ """
348
+ expected_version_msg = (
349
+ f"youtube_transcript_api, version { version ("youtube-transcript-api" )} "
350
+ )
351
+
352
+ cli_version_msg = subprocess .run (
353
+ ["youtube_transcript_api" , "--version" ],
354
+ capture_output = True ,
355
+ text = True ,
356
+ check = True ,
357
+ ).stdout .strip ()
358
+
359
+ assert (
360
+ cli_version_msg == expected_version_msg
361
+ ), f"Expected version '{ expected_version_msg } ', but got '{ cli_version_msg } '"
You can’t perform that action at this time.
0 commit comments