File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Expand file tree Collapse file tree 2 files changed +41
-1
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
5
from .proxies import GenericProxyConfig , WebshareProxyConfig
@@ -91,6 +92,12 @@ def _fetch_transcript(
91
92
92
93
return transcript .fetch ()
93
94
95
+ def _get_version (self ):
96
+ try :
97
+ return version ("youtube-transcript-api" )
98
+ except PackageNotFoundError :
99
+ return "unknown"
100
+
94
101
def _parse_args (self ):
95
102
parser = argparse .ArgumentParser (
96
103
description = (
@@ -99,6 +106,11 @@ def _parse_args(self):
99
106
"other selenium based solutions do!"
100
107
)
101
108
)
109
+ parser .add_argument (
110
+ "--version" ,
111
+ action = "version" ,
112
+ version = f"%(prog)s, version { self ._get_version ()} " ,
113
+ )
102
114
parser .add_argument (
103
115
"--list-transcripts" ,
104
116
action = "store_const" ,
Original file line number Diff line number Diff line change 1
1
import pytest
2
+ from importlib .metadata import PackageNotFoundError , version
2
3
from unittest import TestCase
3
- from unittest .mock import MagicMock
4
+ from unittest .mock import MagicMock , patch
4
5
5
6
import json
7
+ import subprocess
6
8
7
9
from youtube_transcript_api import (
8
10
YouTubeTranscriptApi ,
@@ -340,3 +342,29 @@ def test_run__cookies(self):
340
342
proxy_config = None ,
341
343
cookie_path = "blahblah.txt" ,
342
344
)
345
+
346
+ def test_version_matches_metadata (self ):
347
+ """
348
+ `youtube_transcript_api --version` should return the same version as in the package metadata.
349
+ """
350
+ expected_version_msg = (
351
+ f"youtube_transcript_api, version { version ('youtube-transcript-api' )} "
352
+ )
353
+
354
+ cli_version_msg = subprocess .run (
355
+ ["youtube_transcript_api" , "--version" ],
356
+ capture_output = True ,
357
+ text = True ,
358
+ check = True ,
359
+ ).stdout .strip ()
360
+
361
+ assert (
362
+ cli_version_msg == expected_version_msg
363
+ ), f"Expected version '{ expected_version_msg } ', but got '{ cli_version_msg } '"
364
+
365
+ def test_get_version_package_not_found (self ):
366
+ with patch (
367
+ "youtube_transcript_api._cli.version" , side_effect = PackageNotFoundError
368
+ ):
369
+ cli = YouTubeTranscriptCli ([])
370
+ assert cli ._get_version () == "unknown"
You can’t perform that action at this time.
0 commit comments