1- #!/usr/bin/env python3 
2- # vim: ai ts=4 sts=4 et sw=4 nu 
3- 
4- from  __future__ import  annotations 
5- 
61import  pathlib 
72import  subprocess 
83from  concurrent .futures  import  Future , ThreadPoolExecutor 
9- from  typing  import  ClassVar 
4+ from  typing  import  Any ,  ClassVar 
105
116import  requests 
127import  requests .adapters 
138import  requests .structures 
149import  urllib3 .util 
15- import  yt_dlp  as  youtube_dl 
10+ import  yt_dlp  as  youtube_dl    # pyright: ignore[reportMissingTypeStubs] 
1611
1712from  zimscraperlib  import  logger 
1813from  zimscraperlib .constants  import  DEFAULT_WEB_REQUESTS_TIMEOUT 
@@ -31,24 +26,24 @@ def __init__(self, threads: int | None = 1) -> None:
3126    def  __enter__ (self ):
3227        return  self 
3328
34-     def  __exit__ (self , * args ):
29+     def  __exit__ (self , * _ :  Any ):
3530        self .shutdown ()
3631
3732    def  shutdown (self ) ->  None :
3833        """shuts down the executor, awaiting completion""" 
3934        self .executor .shutdown (wait = True )
4035
41-     def  _run_youtube_dl (self , url : str , options : dict ) ->  None :
36+     def  _run_youtube_dl (self , url : str , options : dict [ str ,  Any ] ) ->  None :
4237        with  youtube_dl .YoutubeDL (options ) as  ydl :
43-             ydl .download ([url ])
38+             ydl .download ([url ])   # pyright: ignore[reportUnknownMemberType] 
4439
4540    def  download (
4641        self ,
4742        url : str ,
48-         options : dict  |  None ,
43+         options : dict [ str ,  Any ]  |  None ,
4944        * ,
5045        wait : bool  |  None  =  True ,
51-     ) ->  bool  |  Future :
46+     ) ->  bool  |  Future [ Any ] :
5247        """Downloads video using initialized executor. 
5348
5449        url: URL or Video ID 
@@ -66,7 +61,7 @@ def download(
6661        return  True 
6762
6863
69- class  YoutubeConfig (dict ):
64+ class  YoutubeConfig (dict [ str ,  str   |   bool   |   int   |   None ] ):
7065    options : ClassVar [dict [str , str  |  bool  |  int  |  None ]] =  {}
7166    defaults : ClassVar [dict [str , str  |  bool  |  int  |  None ]] =  {
7267        "writethumbnail" : True ,
@@ -82,7 +77,7 @@ class YoutubeConfig(dict):
8277        "outtmpl" : "video.%(ext)s" ,
8378    }
8479
85-     def  __init__ (self , ** kwargs ):
80+     def  __init__ (self , ** kwargs :  str   |   bool   |   int   |   None ):
8681        super ().__init__ (self , ** type (self ).defaults )
8782        self .update (self .options )
8883        self .update (kwargs )
@@ -92,7 +87,7 @@ def get_options(
9287        cls ,
9388        target_dir : pathlib .Path  |  None  =  None ,
9489        filepath : pathlib .Path  |  None  =  None ,
95-         ** options ,
90+         ** options :  str   |   bool   |   int   |   None ,
9691    ):
9792        if  "outtmpl"  not  in   options :
9893            outtmpl  =  cls .options .get ("outtmpl" , cls .defaults ["outtmpl" ])
@@ -143,9 +138,10 @@ def save_large_file(url: str, fpath: pathlib.Path) -> None:
143138    )
144139
145140
146- def  _get_retry_adapter (
141+ def  get_retry_adapter (
147142    max_retries : int  |  None  =  5 ,
148143) ->  requests .adapters .BaseAdapter :
144+     """A requests adapter to automatically retry on known HTTP status that can be""" 
149145    retries  =  urllib3 .util .retry .Retry (
150146        total = max_retries ,  # total number of retries 
151147        connect = max_retries ,  # connection errors 
@@ -169,7 +165,7 @@ def _get_retry_adapter(
169165def  get_session (max_retries : int  |  None  =  5 ) ->  requests .Session :
170166    """Session to hold cookies and connection pool together""" 
171167    session  =  requests .Session ()
172-     session .mount ("http" , _get_retry_adapter (max_retries ))  # tied to http and https 
168+     session .mount ("http" , get_retry_adapter (max_retries ))  # tied to http and https 
173169    return  session 
174170
175171
0 commit comments