Skip to content

Commit

Permalink
Create ydl._request_director when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
pukkandan committed Feb 25, 2024
1 parent 5eedc20 commit 069b2ae
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion yt_dlp/YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,6 @@ def process_color_policy(stream):
self.params['http_headers'] = HTTPHeaderDict(std_headers, self.params.get('http_headers'))
self._load_cookies(self.params['http_headers'].get('Cookie')) # compat
self.params['http_headers'].pop('Cookie', None)
self._request_director = self.build_request_director(_REQUEST_HANDLERS.values(), _RH_PREFERENCES)

if auto_init and auto_init != 'no_verbose_header':
self.print_debug_header()
Expand Down Expand Up @@ -964,6 +963,7 @@ def __exit__(self, *args):
def close(self):
self.save_cookies()
self._request_director.close()
del self._request_director

def trouble(self, message=None, tb=None, is_error=True):
"""Determine action to take when a download problem appears.
Expand Down Expand Up @@ -4160,6 +4160,10 @@ def build_request_director(self, handlers, preferences=None):
director.preferences.add(lambda rh, _: 500 if rh.RH_KEY == 'Urllib' else 0)
return director

@functools.cached_property
def _request_director(self):
return self.build_request_director(_REQUEST_HANDLERS.values(), _RH_PREFERENCES)

def encode(self, s):
if isinstance(s, bytes):
return s # Already encoded
Expand Down
1 change: 1 addition & 0 deletions yt_dlp/networking/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, logger, verbose=False):
def close(self):
for handler in self.handlers.values():
handler.close()
self.handlers = {}

def add_handler(self, handler: RequestHandler):
"""Add a handler. If a handler of the same RH_KEY exists, it will overwrite it"""
Expand Down

0 comments on commit 069b2ae

Please sign in to comment.