<s> from __future__ import absolute_import , unicode_literals <EOL> import collections <EOL> import datetime <EOL> import errno <EOL> import io <EOL> import itertools <EOL> import json <EOL> import locale <EOL> import os <EOL> import platform <EOL> import re <EOL> import shutil <EOL> import subprocess <EOL> import socket <EOL> import sys <EOL> import time <EOL> import traceback <EOL> if os . name == '<STR_LIT>' : <EOL> import ctypes <EOL> from . compat import ( <EOL> compat_cookiejar , <EOL> compat_expanduser , <EOL> compat_http_client , <EOL> compat_kwargs , <EOL> compat_str , <EOL> compat_urllib_error , <EOL> compat_urllib_request , <EOL> ) <EOL> from . utils import ( <EOL> escape_url , <EOL> ContentTooShortError , <EOL> date_from_str , <EOL> DateRange , <EOL> DEFAULT_OUTTMPL , <EOL> determine_ext , <EOL> DownloadError , <EOL> encodeFilename , <EOL> ExtractorError , <EOL> format_bytes , <EOL> formatSeconds , <EOL> get_term_width , <EOL> locked_file , <EOL> make_HTTPS_handler , <EOL> MaxDownloadsReached , <EOL> PagedList , <EOL> PostProcessingError , <EOL> platform_name , <EOL> preferredencoding , <EOL> SameFileError , <EOL> sanitize_filename , <EOL> subtitles_filename , <EOL> takewhile_inclusive , <EOL> UnavailableVideoError , <EOL> url_basename , <EOL> version_tuple , <EOL> write_json_file , <EOL> write_string , <EOL> YoutubeDLHandler , <EOL> prepend_extension , <EOL> args_to_str , <EOL> age_restricted , <EOL> ) <EOL> from . cache import Cache <EOL> from . extractor import get_info_extractor , gen_extractors <EOL> from . downloader import get_suitable_downloader <EOL> from . downloader . rtmp import rtmpdump_version <EOL> from . postprocessor import ( <EOL> FFmpegFixupStretchedPP , <EOL> FFmpegMergerPP , <EOL> FFmpegPostProcessor , <EOL> get_postprocessor , <EOL> ) <EOL> from . version import __version__ <EOL> class YoutubeDL ( object ) : <EOL> """<STR_LIT>""" <EOL> params = None <EOL> _ies = [ ] <EOL> _pps = [ ] <EOL> _download_retcode = None <EOL> _num_downloads = None <EOL> _screen_file = None <EOL> def __init__ ( self , params = None , auto_init = True ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> self . _ies = [ ] <EOL> self . _ies_instances = { } <EOL> self . _pps = [ ] <EOL> self . _progress_hooks = [ ] <EOL> self . _download_retcode = <NUM_LIT:0> <EOL> self . _num_downloads = <NUM_LIT:0> <EOL> self . _screen_file = [ sys . stdout , sys . stderr ] [ params . get ( '<STR_LIT>' , False ) ] <EOL> self . _err_file = sys . stderr <EOL> self . params = params <EOL> self . cache = Cache ( self ) <EOL> if params . get ( '<STR_LIT>' , False ) : <EOL> try : <EOL> import pty <EOL> master , slave = pty . openpty ( ) <EOL> width = get_term_width ( ) <EOL> if width is None : <EOL> width_args = [ ] <EOL> else : <EOL> width_args = [ '<STR_LIT>' , str ( width ) ] <EOL> sp_kwargs = dict ( <EOL> stdin = subprocess . PIPE , <EOL> stdout = slave , <EOL> stderr = self . _err_file ) <EOL> try : <EOL> self . _output_process = subprocess . Popen ( <EOL> [ '<STR_LIT>' ] + width_args , ** sp_kwargs <EOL> ) <EOL> except OSError : <EOL> self . _output_process = subprocess . Popen ( <EOL> [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' ] + width_args , ** sp_kwargs ) <EOL> self . _output_channel = os . fdopen ( master , '<STR_LIT:rb>' ) <EOL> except OSError as ose : <EOL> if ose . errno == <NUM_LIT:2> : <EOL> self . report_warning ( '<STR_LIT>' ) <EOL> else : <EOL> raise <EOL> if ( sys . version_info >= ( <NUM_LIT:3> , ) and sys . platform != '<STR_LIT:win32>' and <EOL> sys . getfilesystemencoding ( ) in [ '<STR_LIT:ascii>' , '<STR_LIT>' ] <EOL> and not params . get ( '<STR_LIT>' , False ) ) : <EOL> self . report_warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . params [ '<STR_LIT>' ] = True <EOL> if '<STR_LIT>' in self . params . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . report_warning ( '<STR_LIT>' ) <EOL> self . _setup_opener ( ) <EOL> if auto_init : <EOL> self . print_debug_header ( ) <EOL> self . add_default_info_extractors ( ) <EOL> for pp_def_raw in self . params . get ( '<STR_LIT>' , [ ] ) : <EOL> pp_class = get_postprocessor ( pp_def_raw [ '<STR_LIT:key>' ] ) <EOL> pp_def = dict ( pp_def_raw ) <EOL> del pp_def [ '<STR_LIT:key>' ] <EOL> pp = pp_class ( self , ** compat_kwargs ( pp_def ) ) <EOL> self . add_post_processor ( pp ) <EOL> for ph in self . params . get ( '<STR_LIT>' , [ ] ) : <EOL> self . add_progress_hook ( ph ) <EOL> def warn_if_short_id ( self , argv ) : <EOL> idxs = [ <EOL> i for i , a in enumerate ( argv ) <EOL> if re . match ( r'<STR_LIT>' , a ) ] <EOL> if idxs : <EOL> correct_argv = ( <EOL> [ '<STR_LIT>' ] + <EOL> [ a for i , a in enumerate ( argv ) if i not in idxs ] + <EOL> [ '<STR_LIT>' ] + [ argv [ i ] for i in idxs ] <EOL> ) <EOL> self . report_warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> args_to_str ( correct_argv ) ) <EOL> def add_info_extractor ( self , ie ) : <EOL> """<STR_LIT>""" <EOL> self . _ies . append ( ie ) <EOL> self . _ies_instances [ ie . ie_key ( ) ] = ie <EOL> ie . set_downloader ( self ) <EOL> def get_info_extractor ( self , ie_key ) : <EOL> """<STR_LIT>""" <EOL> ie = self . _ies_instances . get ( ie_key ) <EOL> if ie is None : <EOL> ie = get_info_extractor ( ie_key ) ( ) <EOL> self . add_info_extractor ( ie ) <EOL> return ie <EOL> def add_default_info_extractors ( self ) : <EOL> """<STR_LIT>""" <EOL> for ie in gen_extractors ( ) : <EOL> self . add_info_extractor ( ie ) <EOL> def add_post_processor ( self , pp ) : <EOL> """<STR_LIT>""" <EOL> self . _pps . append ( pp ) <EOL> pp . set_downloader ( self ) <EOL> def add_progress_hook ( self , ph ) : <EOL> """<STR_LIT>""" <EOL> self . _progress_hooks . append ( ph ) <EOL> def _bidi_workaround ( self , message ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> return message <EOL> assert hasattr ( self , '<STR_LIT>' ) <EOL> assert isinstance ( message , compat_str ) <EOL> line_count = message . count ( '<STR_LIT:\n>' ) + <NUM_LIT:1> <EOL> self . _output_process . stdin . write ( ( message + '<STR_LIT:\n>' ) . encode ( '<STR_LIT:utf-8>' ) ) <EOL> self . _output_process . stdin . flush ( ) <EOL> res = '<STR_LIT>' . join ( self . _output_channel . readline ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> for _ in range ( line_count ) ) <EOL> return res [ : - len ( '<STR_LIT:\n>' ) ] <EOL> def to_screen ( self , message , skip_eol = False ) : <EOL> """<STR_LIT>""" <EOL> return self . to_stdout ( message , skip_eol , check_quiet = True ) <EOL> def _write_string ( self , s , out = None ) : <EOL> write_string ( s , out = out , encoding = self . params . get ( '<STR_LIT>' ) ) <EOL> def to_stdout ( self , message , skip_eol = False , check_quiet = False ) : <EOL> """<STR_LIT>""" <EOL> if self . params . get ( '<STR_LIT>' ) : <EOL> self . params [ '<STR_LIT>' ] . debug ( message ) <EOL> elif not check_quiet or not self . params . get ( '<STR_LIT>' , False ) : <EOL> message = self . _bidi_workaround ( message ) <EOL> terminator = [ '<STR_LIT:\n>' , '<STR_LIT>' ] [ skip_eol ] <EOL> output = message + terminator <EOL> self . _write_string ( output , self . _screen_file ) <EOL> def to_stderr ( self , message ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( message , compat_str ) <EOL> if self . params . get ( '<STR_LIT>' ) : <EOL> self . params [ '<STR_LIT>' ] . error ( message ) <EOL> else : <EOL> message = self . _bidi_workaround ( message ) <EOL> output = message + '<STR_LIT:\n>' <EOL> self . _write_string ( output , self . _err_file ) <EOL> def to_console_title ( self , message ) : <EOL> if not self . params . get ( '<STR_LIT>' , False ) : <EOL> return <EOL> if os . name == '<STR_LIT>' and ctypes . windll . kernel32 . GetConsoleWindow ( ) : <EOL> ctypes . windll . kernel32 . SetConsoleTitleW ( ctypes . c_wchar_p ( message ) ) <EOL> elif '<STR_LIT>' in os . environ : <EOL> self . _write_string ( '<STR_LIT>' % message , self . _screen_file ) <EOL> def save_console_title ( self ) : <EOL> if not self . params . get ( '<STR_LIT>' , False ) : <EOL> return <EOL> if '<STR_LIT>' in os . environ : <EOL> self . _write_string ( '<STR_LIT>' , self . _screen_file ) <EOL> def restore_console_title ( self ) : <EOL> if not self . params . get ( '<STR_LIT>' , False ) : <EOL> return <EOL> if '<STR_LIT>' in os . environ : <EOL> self . _write_string ( '<STR_LIT>' , self . _screen_file ) <EOL> def __enter__ ( self ) : <EOL> self . save_console_title ( ) <EOL> return self <EOL> def __exit__ ( self , * args ) : <EOL> self . restore_console_title ( ) <EOL> if self . params . get ( '<STR_LIT>' ) is not None : <EOL> self . cookiejar . save ( ) <EOL> def trouble ( self , message = None , tb = None ) : <EOL> """<STR_LIT>""" <EOL> if message is not None : <EOL> self . to_stderr ( message ) <EOL> if self . params . get ( '<STR_LIT>' ) : <EOL> if tb is None : <EOL> if sys . exc_info ( ) [ <NUM_LIT:0> ] : <EOL> tb = '<STR_LIT>' <EOL> if hasattr ( sys . exc_info ( ) [ <NUM_LIT:1> ] , '<STR_LIT>' ) and sys . exc_info ( ) [ <NUM_LIT:1> ] . exc_info [ <NUM_LIT:0> ] : <EOL> tb += '<STR_LIT>' . join ( traceback . format_exception ( * sys . exc_info ( ) [ <NUM_LIT:1> ] . exc_info ) ) <EOL> tb += compat_str ( traceback . format_exc ( ) ) <EOL> else : <EOL> tb_data = traceback . format_list ( traceback . extract_stack ( ) ) <EOL> tb = '<STR_LIT>' . join ( tb_data ) <EOL> self . to_stderr ( tb ) <EOL> if not self . params . get ( '<STR_LIT>' , False ) : <EOL> if sys . exc_info ( ) [ <NUM_LIT:0> ] and hasattr ( sys . exc_info ( ) [ <NUM_LIT:1> ] , '<STR_LIT>' ) and sys . exc_info ( ) [ <NUM_LIT:1> ] . exc_info [ <NUM_LIT:0> ] : <EOL> exc_info = sys . exc_info ( ) [ <NUM_LIT:1> ] . exc_info <EOL> else : <EOL> exc_info = sys . exc_info ( ) <EOL> raise DownloadError ( message , exc_info ) <EOL> self . _download_retcode = <NUM_LIT:1> <EOL> def report_warning ( self , message ) : <EOL> '''<STR_LIT>''' <EOL> if self . params . get ( '<STR_LIT>' ) is not None : <EOL> self . params [ '<STR_LIT>' ] . warning ( message ) <EOL> else : <EOL> if self . params . get ( '<STR_LIT>' ) : <EOL> return <EOL> if self . _err_file . isatty ( ) and os . name != '<STR_LIT>' : <EOL> _msg_header = '<STR_LIT>' <EOL> else : <EOL> _msg_header = '<STR_LIT>' <EOL> warning_message = '<STR_LIT>' % ( _msg_header , message ) <EOL> self . to_stderr ( warning_message ) <EOL> def report_error ( self , message , tb = None ) : <EOL> '''<STR_LIT>''' <EOL> if self . _err_file . isatty ( ) and os . name != '<STR_LIT>' : <EOL> _msg_header = '<STR_LIT>' <EOL> else : <EOL> _msg_header = '<STR_LIT>' <EOL> error_message = '<STR_LIT>' % ( _msg_header , message ) <EOL> self . trouble ( error_message , tb ) <EOL> def report_file_already_downloaded ( self , file_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . to_screen ( '<STR_LIT>' % file_name ) <EOL> except UnicodeEncodeError : <EOL> self . to_screen ( '<STR_LIT>' ) <EOL> def prepare_filename ( self , info_dict ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> template_dict = dict ( info_dict ) <EOL> template_dict [ '<STR_LIT>' ] = int ( time . time ( ) ) <EOL> autonumber_size = self . params . get ( '<STR_LIT>' ) <EOL> if autonumber_size is None : <EOL> autonumber_size = <NUM_LIT:5> <EOL> autonumber_templ = '<STR_LIT>' + str ( autonumber_size ) + '<STR_LIT:d>' <EOL> template_dict [ '<STR_LIT>' ] = autonumber_templ % self . _num_downloads <EOL> if template_dict . get ( '<STR_LIT>' ) is not None : <EOL> template_dict [ '<STR_LIT>' ] = '<STR_LIT>' % ( len ( str ( template_dict [ '<STR_LIT>' ] ) ) , template_dict [ '<STR_LIT>' ] ) <EOL> if template_dict . get ( '<STR_LIT>' ) is None : <EOL> if template_dict . get ( '<STR_LIT:width>' ) and template_dict . get ( '<STR_LIT>' ) : <EOL> template_dict [ '<STR_LIT>' ] = '<STR_LIT>' % ( template_dict [ '<STR_LIT:width>' ] , template_dict [ '<STR_LIT>' ] ) <EOL> elif template_dict . get ( '<STR_LIT>' ) : <EOL> template_dict [ '<STR_LIT>' ] = '<STR_LIT>' % template_dict [ '<STR_LIT>' ] <EOL> elif template_dict . get ( '<STR_LIT:width>' ) : <EOL> template_dict [ '<STR_LIT>' ] = '<STR_LIT>' % template_dict [ '<STR_LIT:width>' ] <EOL> sanitize = lambda k , v : sanitize_filename ( <EOL> compat_str ( v ) , <EOL> restricted = self . params . get ( '<STR_LIT>' ) , <EOL> is_id = ( k == '<STR_LIT:id>' ) ) <EOL> template_dict = dict ( ( k , sanitize ( k , v ) ) <EOL> for k , v in template_dict . items ( ) <EOL> if v is not None ) <EOL> template_dict = collections . defaultdict ( lambda : '<STR_LIT>' , template_dict ) <EOL> outtmpl = self . params . get ( '<STR_LIT>' , DEFAULT_OUTTMPL ) <EOL> tmpl = compat_expanduser ( outtmpl ) <EOL> filename = tmpl % template_dict <EOL> return filename <EOL> except ValueError as err : <EOL> self . report_error ( '<STR_LIT>' + str ( err ) + '<STR_LIT>' + repr ( preferredencoding ( ) ) + '<STR_LIT:)>' ) <EOL> return None <EOL> def _match_entry ( self , info_dict ) : <EOL> """<STR_LIT>""" <EOL> video_title = info_dict . get ( '<STR_LIT:title>' , info_dict . get ( '<STR_LIT:id>' , '<STR_LIT>' ) ) <EOL> if '<STR_LIT:title>' in info_dict : <EOL> title = info_dict [ '<STR_LIT:title>' ] <EOL> matchtitle = self . params . get ( '<STR_LIT>' , False ) <EOL> if matchtitle : <EOL> if not re . search ( matchtitle , title , re . IGNORECASE ) : <EOL> return '<STR_LIT:">' + title + '<STR_LIT>' + matchtitle + '<STR_LIT:">' <EOL> rejecttitle = self . params . get ( '<STR_LIT>' , False ) <EOL> if rejecttitle : <EOL> if re . search ( rejecttitle , title , re . IGNORECASE ) : <EOL> return '<STR_LIT:">' + title + '<STR_LIT>' + rejecttitle + '<STR_LIT:">' <EOL> date = info_dict . get ( '<STR_LIT>' , None ) <EOL> if date is not None : <EOL> dateRange = self . params . get ( '<STR_LIT>' , DateRange ( ) ) <EOL> if date not in dateRange : <EOL> return '<STR_LIT>' % ( date_from_str ( date ) . isoformat ( ) , dateRange ) <EOL> view_count = info_dict . get ( '<STR_LIT>' , None ) <EOL> if view_count is not None : <EOL> min_views = self . params . get ( '<STR_LIT>' ) <EOL> if min_views is not None and view_count < min_views : <EOL> return '<STR_LIT>' % ( video_title , view_count , min_views ) <EOL> max_views = self . params . get ( '<STR_LIT>' ) <EOL> if max_views is not None and view_count > max_views : <EOL> return '<STR_LIT>' % ( video_title , view_count , max_views ) <EOL> if age_restricted ( info_dict . get ( '<STR_LIT>' ) , self . params . get ( '<STR_LIT>' ) ) : <EOL> return '<STR_LIT>' % title <EOL> if self . in_download_archive ( info_dict ) : <EOL> return '<STR_LIT>' % video_title <EOL> return None <EOL> @ staticmethod <EOL> def add_extra_info ( info_dict , extra_info ) : <EOL> '''<STR_LIT>''' <EOL> for key , value in extra_info . items ( ) : <EOL> info_dict . setdefault ( key , value ) <EOL> def extract_info ( self , url , download = True , ie_key = None , extra_info = { } , <EOL> process = True ) : <EOL> '''<STR_LIT>''' <EOL> if ie_key : <EOL> ies = [ self . get_info_extractor ( ie_key ) ] <EOL> else : <EOL> ies = self . _ies <EOL> for ie in ies : <EOL> if not ie . suitable ( url ) : <EOL> continue <EOL> if not ie . working ( ) : <EOL> self . report_warning ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> ie_result = ie . extract ( url ) <EOL> if ie_result is None : <EOL> break <EOL> if isinstance ( ie_result , list ) : <EOL> ie_result = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ie_result , <EOL> } <EOL> self . add_default_extra_info ( ie_result , ie , url ) <EOL> if process : <EOL> return self . process_ie_result ( ie_result , download , extra_info ) <EOL> else : <EOL> return ie_result <EOL> except ExtractorError as de : <EOL> self . report_error ( compat_str ( de ) , de . format_traceback ( ) ) <EOL> break <EOL> except MaxDownloadsReached : <EOL> raise <EOL> except Exception as e : <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> self . report_error ( compat_str ( e ) , tb = compat_str ( traceback . format_exc ( ) ) ) <EOL> break <EOL> else : <EOL> raise <EOL> else : <EOL> self . report_error ( '<STR_LIT>' % url ) <EOL> def add_default_extra_info ( self , ie_result , ie , url ) : <EOL> self . add_extra_info ( ie_result , { <EOL> '<STR_LIT>' : ie . IE_NAME , <EOL> '<STR_LIT>' : url , <EOL> '<STR_LIT>' : url_basename ( url ) , <EOL> '<STR_LIT>' : ie . ie_key ( ) , <EOL> } ) <EOL> def process_ie_result ( self , ie_result , download = True , extra_info = { } ) : <EOL> """<STR_LIT>""" <EOL> result_type = ie_result . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if result_type in ( '<STR_LIT:url>' , '<STR_LIT>' ) : <EOL> extract_flat = self . params . get ( '<STR_LIT>' , False ) <EOL> if ( ( extract_flat == '<STR_LIT>' and '<STR_LIT>' in extra_info ) or <EOL> extract_flat is True ) : <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> self . to_stdout ( json . dumps ( ie_result ) ) <EOL> return ie_result <EOL> if result_type == '<STR_LIT>' : <EOL> self . add_extra_info ( ie_result , extra_info ) <EOL> return self . process_video_result ( ie_result , download = download ) <EOL> elif result_type == '<STR_LIT:url>' : <EOL> return self . extract_info ( ie_result [ '<STR_LIT:url>' ] , <EOL> download , <EOL> ie_key = ie_result . get ( '<STR_LIT>' ) , <EOL> extra_info = extra_info ) <EOL> elif result_type == '<STR_LIT>' : <EOL> info = self . extract_info ( <EOL> ie_result [ '<STR_LIT:url>' ] , ie_key = ie_result . get ( '<STR_LIT>' ) , <EOL> extra_info = extra_info , download = False , process = False ) <EOL> force_properties = dict ( <EOL> ( k , v ) for k , v in ie_result . items ( ) if v is not None ) <EOL> for f in ( '<STR_LIT>' , '<STR_LIT:url>' ) : <EOL> if f in force_properties : <EOL> del force_properties [ f ] <EOL> new_result = info . copy ( ) <EOL> new_result . update ( force_properties ) <EOL> assert new_result . get ( '<STR_LIT>' ) != '<STR_LIT>' <EOL> return self . process_ie_result ( <EOL> new_result , download = download , extra_info = extra_info ) <EOL> elif result_type == '<STR_LIT>' or result_type == '<STR_LIT>' : <EOL> playlist = ie_result . get ( '<STR_LIT:title>' , None ) or ie_result . get ( '<STR_LIT:id>' , None ) <EOL> self . to_screen ( '<STR_LIT>' % playlist ) <EOL> playlist_results = [ ] <EOL> playliststart = self . params . get ( '<STR_LIT>' , <NUM_LIT:1> ) - <NUM_LIT:1> <EOL> playlistend = self . params . get ( '<STR_LIT>' , None ) <EOL> if playlistend == - <NUM_LIT:1> : <EOL> playlistend = None <EOL> ie_entries = ie_result [ '<STR_LIT>' ] <EOL> if isinstance ( ie_entries , list ) : <EOL> n_all_entries = len ( ie_entries ) <EOL> entries = ie_entries [ playliststart : playlistend ] <EOL> n_entries = len ( entries ) <EOL> self . to_screen ( <EOL> "<STR_LIT>" % <EOL> ( ie_result [ '<STR_LIT>' ] , playlist , n_all_entries , n_entries ) ) <EOL> elif isinstance ( ie_entries , PagedList ) : <EOL> entries = ie_entries . getslice ( <EOL> playliststart , playlistend ) <EOL> n_entries = len ( entries ) <EOL> self . to_screen ( <EOL> "<STR_LIT>" % <EOL> ( ie_result [ '<STR_LIT>' ] , playlist , n_entries ) ) <EOL> else : <EOL> entries = list ( itertools . islice ( <EOL> ie_entries , playliststart , playlistend ) ) <EOL> n_entries = len ( entries ) <EOL> self . to_screen ( <EOL> "<STR_LIT>" % <EOL> ( ie_result [ '<STR_LIT>' ] , playlist , n_entries ) ) <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> entries = entries [ : : - <NUM_LIT:1> ] <EOL> for i , entry in enumerate ( entries , <NUM_LIT:1> ) : <EOL> self . to_screen ( '<STR_LIT>' % ( i , n_entries ) ) <EOL> extra = { <EOL> '<STR_LIT>' : n_entries , <EOL> '<STR_LIT>' : playlist , <EOL> '<STR_LIT>' : ie_result . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT>' : ie_result . get ( '<STR_LIT:title>' ) , <EOL> '<STR_LIT>' : i + playliststart , <EOL> '<STR_LIT>' : ie_result [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : ie_result [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : url_basename ( ie_result [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ie_result [ '<STR_LIT>' ] , <EOL> } <EOL> reason = self . _match_entry ( entry ) <EOL> if reason is not None : <EOL> self . to_screen ( '<STR_LIT>' + reason ) <EOL> continue <EOL> entry_result = self . process_ie_result ( entry , <EOL> download = download , <EOL> extra_info = extra ) <EOL> playlist_results . append ( entry_result ) <EOL> ie_result [ '<STR_LIT>' ] = playlist_results <EOL> return ie_result <EOL> elif result_type == '<STR_LIT>' : <EOL> self . report_warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ie_result . get ( '<STR_LIT>' ) ) <EOL> def _fixup ( r ) : <EOL> self . add_extra_info ( <EOL> r , <EOL> { <EOL> '<STR_LIT>' : ie_result [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : ie_result [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : url_basename ( ie_result [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ie_result [ '<STR_LIT>' ] , <EOL> } <EOL> ) <EOL> return r <EOL> ie_result [ '<STR_LIT>' ] = [ <EOL> self . process_ie_result ( _fixup ( r ) , download , extra_info ) <EOL> for r in ie_result [ '<STR_LIT>' ] <EOL> ] <EOL> return ie_result <EOL> else : <EOL> raise Exception ( '<STR_LIT>' % result_type ) <EOL> def select_format ( self , format_spec , available_formats ) : <EOL> if format_spec == '<STR_LIT>' or format_spec is None : <EOL> return available_formats [ - <NUM_LIT:1> ] <EOL> elif format_spec == '<STR_LIT>' : <EOL> return available_formats [ <NUM_LIT:0> ] <EOL> elif format_spec == '<STR_LIT>' : <EOL> audio_formats = [ <EOL> f for f in available_formats <EOL> if f . get ( '<STR_LIT>' ) == '<STR_LIT:none>' ] <EOL> if audio_formats : <EOL> return audio_formats [ - <NUM_LIT:1> ] <EOL> elif format_spec == '<STR_LIT>' : <EOL> audio_formats = [ <EOL> f for f in available_formats <EOL> if f . get ( '<STR_LIT>' ) == '<STR_LIT:none>' ] <EOL> if audio_formats : <EOL> return audio_formats [ <NUM_LIT:0> ] <EOL> elif format_spec == '<STR_LIT>' : <EOL> video_formats = [ <EOL> f for f in available_formats <EOL> if f . get ( '<STR_LIT>' ) == '<STR_LIT:none>' ] <EOL> if video_formats : <EOL> return video_formats [ - <NUM_LIT:1> ] <EOL> elif format_spec == '<STR_LIT>' : <EOL> video_formats = [ <EOL> f for f in available_formats <EOL> if f . get ( '<STR_LIT>' ) == '<STR_LIT:none>' ] <EOL> if video_formats : <EOL> return video_formats [ <NUM_LIT:0> ] <EOL> else : <EOL> extensions = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if format_spec in extensions : <EOL> filter_f = lambda f : f [ '<STR_LIT>' ] == format_spec <EOL> else : <EOL> filter_f = lambda f : f [ '<STR_LIT>' ] == format_spec <EOL> matches = list ( filter ( filter_f , available_formats ) ) <EOL> if matches : <EOL> return matches [ - <NUM_LIT:1> ] <EOL> return None <EOL> def process_video_result ( self , info_dict , download = True ) : <EOL> assert info_dict . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' <EOL> if '<STR_LIT:id>' not in info_dict : <EOL> raise ExtractorError ( '<STR_LIT>' ) <EOL> if '<STR_LIT:title>' not in info_dict : <EOL> raise ExtractorError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in info_dict : <EOL> info_dict [ '<STR_LIT>' ] = None <EOL> info_dict [ '<STR_LIT>' ] = None <EOL> thumbnails = info_dict . get ( '<STR_LIT>' ) <EOL> if thumbnails : <EOL> thumbnails . sort ( key = lambda t : ( <EOL> t . get ( '<STR_LIT:width>' ) , t . get ( '<STR_LIT>' ) , t . get ( '<STR_LIT:url>' ) ) ) <EOL> for t in thumbnails : <EOL> if '<STR_LIT:width>' in t and '<STR_LIT>' in t : <EOL> t [ '<STR_LIT>' ] = '<STR_LIT>' % ( t [ '<STR_LIT:width>' ] , t [ '<STR_LIT>' ] ) <EOL> if thumbnails and '<STR_LIT>' not in info_dict : <EOL> info_dict [ '<STR_LIT>' ] = thumbnails [ - <NUM_LIT:1> ] [ '<STR_LIT:url>' ] <EOL> if '<STR_LIT>' not in info_dict and '<STR_LIT:id>' in info_dict : <EOL> info_dict [ '<STR_LIT>' ] = info_dict [ '<STR_LIT:id>' ] <EOL> if info_dict . get ( '<STR_LIT>' ) is None and info_dict . get ( '<STR_LIT>' ) is not None : <EOL> if info_dict [ '<STR_LIT>' ] < <NUM_LIT:0> and os . name == '<STR_LIT>' : <EOL> info_dict [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> upload_date = datetime . datetime . utcfromtimestamp ( <EOL> info_dict [ '<STR_LIT>' ] ) <EOL> info_dict [ '<STR_LIT>' ] = upload_date . strftime ( '<STR_LIT>' ) <EOL> if info_dict [ '<STR_LIT>' ] in [ '<STR_LIT>' ] : <EOL> if download : <EOL> self . process_info ( info_dict ) <EOL> return info_dict <EOL> if info_dict . get ( '<STR_LIT>' ) is None : <EOL> formats = [ info_dict ] <EOL> else : <EOL> formats = info_dict [ '<STR_LIT>' ] <EOL> if not formats : <EOL> raise ExtractorError ( '<STR_LIT>' ) <EOL> for i , format in enumerate ( formats ) : <EOL> if '<STR_LIT:url>' not in format : <EOL> raise ExtractorError ( '<STR_LIT>' % i ) <EOL> if format . get ( '<STR_LIT>' ) is None : <EOL> format [ '<STR_LIT>' ] = compat_str ( i ) <EOL> if format . get ( '<STR_LIT>' ) is None : <EOL> format [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> id = format [ '<STR_LIT>' ] , <EOL> res = self . format_resolution ( format ) , <EOL> note = '<STR_LIT>' . format ( format [ '<STR_LIT>' ] ) if format . get ( '<STR_LIT>' ) is not None else '<STR_LIT>' , <EOL> ) <EOL> if '<STR_LIT>' not in format : <EOL> format [ '<STR_LIT>' ] = determine_ext ( format [ '<STR_LIT:url>' ] ) . lower ( ) <EOL> format_limit = self . params . get ( '<STR_LIT>' , None ) <EOL> if format_limit : <EOL> formats = list ( takewhile_inclusive ( <EOL> lambda f : f [ '<STR_LIT>' ] != format_limit , formats <EOL> ) ) <EOL> if formats [ <NUM_LIT:0> ] is not info_dict : <EOL> info_dict [ '<STR_LIT>' ] = formats <EOL> if self . params . get ( '<STR_LIT>' , None ) : <EOL> self . list_formats ( info_dict ) <EOL> return <EOL> req_format = self . params . get ( '<STR_LIT>' ) <EOL> if req_format is None : <EOL> req_format = '<STR_LIT>' <EOL> formats_to_download = [ ] <EOL> if req_format in ( '<STR_LIT>' , '<STR_LIT:all>' ) : <EOL> formats_to_download = formats <EOL> else : <EOL> for rfstr in req_format . split ( '<STR_LIT:U+002C>' ) : <EOL> req_formats = rfstr . split ( '<STR_LIT:/>' ) <EOL> for rf in req_formats : <EOL> if re . match ( r'<STR_LIT>' , rf ) is not None : <EOL> format_1 , format_2 = rf . split ( '<STR_LIT:+>' ) <EOL> formats_info = ( self . select_format ( format_1 , formats ) , <EOL> self . select_format ( format_2 , formats ) ) <EOL> if all ( formats_info ) : <EOL> if formats_info [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) == '<STR_LIT:none>' : <EOL> self . report_error ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( format_2 , format_1 ) ) <EOL> return <EOL> output_ext = ( <EOL> formats_info [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> if self . params . get ( '<STR_LIT>' ) is None <EOL> else self . params [ '<STR_LIT>' ] ) <EOL> selected_format = { <EOL> '<STR_LIT>' : formats_info , <EOL> '<STR_LIT>' : rf , <EOL> '<STR_LIT>' : formats_info [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT:width>' : formats_info [ <NUM_LIT:0> ] . get ( '<STR_LIT:width>' ) , <EOL> '<STR_LIT>' : formats_info [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : formats_info [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : formats_info [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : formats_info [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : formats_info [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : formats_info [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : formats_info [ <NUM_LIT:1> ] . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : formats_info [ <NUM_LIT:1> ] . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : output_ext , <EOL> } <EOL> else : <EOL> selected_format = None <EOL> else : <EOL> selected_format = self . select_format ( rf , formats ) <EOL> if selected_format is not None : <EOL> formats_to_download . append ( selected_format ) <EOL> break <EOL> if not formats_to_download : <EOL> raise ExtractorError ( '<STR_LIT>' , <EOL> expected = True ) <EOL> if download : <EOL> if len ( formats_to_download ) > <NUM_LIT:1> : <EOL> self . to_screen ( '<STR_LIT>' % ( info_dict [ '<STR_LIT:id>' ] , len ( formats_to_download ) ) ) <EOL> for format in formats_to_download : <EOL> new_info = dict ( info_dict ) <EOL> new_info . update ( format ) <EOL> self . process_info ( new_info ) <EOL> info_dict . update ( formats_to_download [ - <NUM_LIT:1> ] ) <EOL> return info_dict <EOL> def process_info ( self , info_dict ) : <EOL> """<STR_LIT>""" <EOL> assert info_dict . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' <EOL> max_downloads = self . params . get ( '<STR_LIT>' ) <EOL> if max_downloads is not None : <EOL> if self . _num_downloads >= int ( max_downloads ) : <EOL> raise MaxDownloadsReached ( ) <EOL> info_dict [ '<STR_LIT>' ] = info_dict [ '<STR_LIT:title>' ] <EOL> if len ( info_dict [ '<STR_LIT:title>' ] ) > <NUM_LIT:200> : <EOL> info_dict [ '<STR_LIT:title>' ] = info_dict [ '<STR_LIT:title>' ] [ : <NUM_LIT> ] + '<STR_LIT>' <EOL> info_dict [ '<STR_LIT>' ] = info_dict [ '<STR_LIT:title>' ] <EOL> if '<STR_LIT>' not in info_dict : <EOL> info_dict [ '<STR_LIT>' ] = info_dict [ '<STR_LIT>' ] <EOL> reason = self . _match_entry ( info_dict ) <EOL> if reason is not None : <EOL> self . to_screen ( '<STR_LIT>' + reason ) <EOL> return <EOL> self . _num_downloads += <NUM_LIT:1> <EOL> filename = self . prepare_filename ( info_dict ) <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> self . to_stdout ( info_dict [ '<STR_LIT>' ] ) <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> self . to_stdout ( info_dict [ '<STR_LIT:id>' ] ) <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> if info_dict . get ( '<STR_LIT>' ) is not None : <EOL> for f in info_dict [ '<STR_LIT>' ] : <EOL> self . to_stdout ( f [ '<STR_LIT:url>' ] + f . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> else : <EOL> self . to_stdout ( info_dict [ '<STR_LIT:url>' ] + info_dict . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if self . params . get ( '<STR_LIT>' , False ) and info_dict . get ( '<STR_LIT>' ) is not None : <EOL> self . to_stdout ( info_dict [ '<STR_LIT>' ] ) <EOL> if self . params . get ( '<STR_LIT>' , False ) and info_dict . get ( '<STR_LIT:description>' ) is not None : <EOL> self . to_stdout ( info_dict [ '<STR_LIT:description>' ] ) <EOL> if self . params . get ( '<STR_LIT>' , False ) and filename is not None : <EOL> self . to_stdout ( filename ) <EOL> if self . params . get ( '<STR_LIT>' , False ) and info_dict . get ( '<STR_LIT>' ) is not None : <EOL> self . to_stdout ( formatSeconds ( info_dict [ '<STR_LIT>' ] ) ) <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> self . to_stdout ( info_dict [ '<STR_LIT>' ] ) <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> info_dict [ '<STR_LIT>' ] = filename <EOL> self . to_stdout ( json . dumps ( info_dict ) ) <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> info_dict [ '<STR_LIT>' ] = filename <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> return <EOL> if filename is None : <EOL> return <EOL> try : <EOL> dn = os . path . dirname ( encodeFilename ( filename ) ) <EOL> if dn and not os . path . exists ( dn ) : <EOL> os . makedirs ( dn ) <EOL> except ( OSError , IOError ) as err : <EOL> self . report_error ( '<STR_LIT>' + compat_str ( err ) ) <EOL> return <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> descfn = filename + '<STR_LIT>' <EOL> if self . params . get ( '<STR_LIT>' , False ) and os . path . exists ( encodeFilename ( descfn ) ) : <EOL> self . to_screen ( '<STR_LIT>' ) <EOL> elif info_dict . get ( '<STR_LIT:description>' ) is None : <EOL> self . report_warning ( '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> self . to_screen ( '<STR_LIT>' + descfn ) <EOL> with io . open ( encodeFilename ( descfn ) , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) as descfile : <EOL> descfile . write ( info_dict [ '<STR_LIT:description>' ] ) <EOL> except ( OSError , IOError ) : <EOL> self . report_error ( '<STR_LIT>' + descfn ) <EOL> return <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> annofn = filename + '<STR_LIT>' <EOL> if self . params . get ( '<STR_LIT>' , False ) and os . path . exists ( encodeFilename ( annofn ) ) : <EOL> self . to_screen ( '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> self . to_screen ( '<STR_LIT>' + annofn ) <EOL> with io . open ( encodeFilename ( annofn ) , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) as annofile : <EOL> annofile . write ( info_dict [ '<STR_LIT>' ] ) <EOL> except ( KeyError , TypeError ) : <EOL> self . report_warning ( '<STR_LIT>' ) <EOL> except ( OSError , IOError ) : <EOL> self . report_error ( '<STR_LIT>' + annofn ) <EOL> return <EOL> subtitles_are_requested = any ( [ self . params . get ( '<STR_LIT>' , False ) , <EOL> self . params . get ( '<STR_LIT>' ) ] ) <EOL> if subtitles_are_requested and '<STR_LIT>' in info_dict and info_dict [ '<STR_LIT>' ] : <EOL> subtitles = info_dict [ '<STR_LIT>' ] <EOL> sub_format = self . params . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for sub_lang in subtitles . keys ( ) : <EOL> sub = subtitles [ sub_lang ] <EOL> if sub is None : <EOL> continue <EOL> try : <EOL> sub_filename = subtitles_filename ( filename , sub_lang , sub_format ) <EOL> if self . params . get ( '<STR_LIT>' , False ) and os . path . exists ( encodeFilename ( sub_filename ) ) : <EOL> self . to_screen ( '<STR_LIT>' % ( sub_lang , sub_format ) ) <EOL> else : <EOL> self . to_screen ( '<STR_LIT>' + sub_filename ) <EOL> with io . open ( encodeFilename ( sub_filename ) , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) as subfile : <EOL> subfile . write ( sub ) <EOL> except ( OSError , IOError ) : <EOL> self . report_error ( '<STR_LIT>' + sub_filename ) <EOL> return <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> infofn = os . path . splitext ( filename ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> if self . params . get ( '<STR_LIT>' , False ) and os . path . exists ( encodeFilename ( infofn ) ) : <EOL> self . to_screen ( '<STR_LIT>' ) <EOL> else : <EOL> self . to_screen ( '<STR_LIT>' + infofn ) <EOL> try : <EOL> write_json_file ( info_dict , infofn ) <EOL> except ( OSError , IOError ) : <EOL> self . report_error ( '<STR_LIT>' + infofn ) <EOL> return <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> if info_dict . get ( '<STR_LIT>' ) is not None : <EOL> thumb_format = determine_ext ( info_dict [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> thumb_filename = os . path . splitext ( filename ) [ <NUM_LIT:0> ] + '<STR_LIT:.>' + thumb_format <EOL> if self . params . get ( '<STR_LIT>' , False ) and os . path . exists ( encodeFilename ( thumb_filename ) ) : <EOL> self . to_screen ( '<STR_LIT>' % <EOL> ( info_dict [ '<STR_LIT>' ] , info_dict [ '<STR_LIT:id>' ] ) ) <EOL> else : <EOL> self . to_screen ( '<STR_LIT>' % <EOL> ( info_dict [ '<STR_LIT>' ] , info_dict [ '<STR_LIT:id>' ] ) ) <EOL> try : <EOL> uf = self . urlopen ( info_dict [ '<STR_LIT>' ] ) <EOL> with open ( thumb_filename , '<STR_LIT:wb>' ) as thumbf : <EOL> shutil . copyfileobj ( uf , thumbf ) <EOL> self . to_screen ( '<STR_LIT>' % <EOL> ( info_dict [ '<STR_LIT>' ] , info_dict [ '<STR_LIT:id>' ] , thumb_filename ) ) <EOL> except ( compat_urllib_error . URLError , compat_http_client . HTTPException , socket . error ) as err : <EOL> self . report_warning ( '<STR_LIT>' % <EOL> ( info_dict [ '<STR_LIT>' ] , compat_str ( err ) ) ) <EOL> if not self . params . get ( '<STR_LIT>' , False ) : <EOL> try : <EOL> def dl ( name , info ) : <EOL> fd = get_suitable_downloader ( info ) ( self , self . params ) <EOL> for ph in self . _progress_hooks : <EOL> fd . add_progress_hook ( ph ) <EOL> if self . params . get ( '<STR_LIT>' ) : <EOL> self . to_stdout ( '<STR_LIT>' % info . get ( '<STR_LIT:url>' ) ) <EOL> return fd . download ( name , info ) <EOL> if info_dict . get ( '<STR_LIT>' ) is not None : <EOL> downloaded = [ ] <EOL> success = True <EOL> merger = FFmpegMergerPP ( self , not self . params . get ( '<STR_LIT>' ) ) <EOL> if not merger . _executable : <EOL> postprocessors = [ ] <EOL> self . report_warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> postprocessors = [ merger ] <EOL> for f in info_dict [ '<STR_LIT>' ] : <EOL> new_info = dict ( info_dict ) <EOL> new_info . update ( f ) <EOL> fname = self . prepare_filename ( new_info ) <EOL> fname = prepend_extension ( fname , '<STR_LIT>' % f [ '<STR_LIT>' ] ) <EOL> downloaded . append ( fname ) <EOL> partial_success = dl ( fname , new_info ) <EOL> success = success and partial_success <EOL> info_dict [ '<STR_LIT>' ] = postprocessors <EOL> info_dict [ '<STR_LIT>' ] = downloaded <EOL> else : <EOL> success = dl ( filename , info_dict ) <EOL> except ( compat_urllib_error . URLError , compat_http_client . HTTPException , socket . error ) as err : <EOL> self . report_error ( '<STR_LIT>' % str ( err ) ) <EOL> return <EOL> except ( OSError , IOError ) as err : <EOL> raise UnavailableVideoError ( err ) <EOL> except ( ContentTooShortError , ) as err : <EOL> self . report_error ( '<STR_LIT>' % ( err . expected , err . downloaded ) ) <EOL> return <EOL> if success : <EOL> stretched_ratio = info_dict . get ( '<STR_LIT>' ) <EOL> if stretched_ratio is not None and stretched_ratio != <NUM_LIT:1> : <EOL> fixup_policy = self . params . get ( '<STR_LIT>' ) <EOL> if fixup_policy is None : <EOL> fixup_policy = '<STR_LIT>' <EOL> if fixup_policy == '<STR_LIT>' : <EOL> self . report_warning ( '<STR_LIT>' % ( <EOL> info_dict [ '<STR_LIT:id>' ] , stretched_ratio ) ) <EOL> elif fixup_policy == '<STR_LIT>' : <EOL> stretched_pp = FFmpegFixupStretchedPP ( self ) <EOL> if stretched_pp . available : <EOL> info_dict . setdefault ( '<STR_LIT>' , [ ] ) <EOL> info_dict [ '<STR_LIT>' ] . append ( stretched_pp ) <EOL> else : <EOL> self . report_warning ( <EOL> '<STR_LIT>' % ( <EOL> info_dict [ '<STR_LIT:id>' ] , stretched_ratio ) ) <EOL> else : <EOL> assert fixup_policy == '<STR_LIT:ignore>' <EOL> try : <EOL> self . post_process ( filename , info_dict ) <EOL> except ( PostProcessingError ) as err : <EOL> self . report_error ( '<STR_LIT>' % str ( err ) ) <EOL> return <EOL> self . record_download_archive ( info_dict ) <EOL> def download ( self , url_list ) : <EOL> """<STR_LIT>""" <EOL> outtmpl = self . params . get ( '<STR_LIT>' , DEFAULT_OUTTMPL ) <EOL> if ( len ( url_list ) > <NUM_LIT:1> and <EOL> '<STR_LIT:%>' not in outtmpl <EOL> and self . params . get ( '<STR_LIT>' ) != <NUM_LIT:1> ) : <EOL> raise SameFileError ( outtmpl ) <EOL> for url in url_list : <EOL> try : <EOL> res = self . extract_info ( url ) <EOL> except UnavailableVideoError : <EOL> self . report_error ( '<STR_LIT>' ) <EOL> except MaxDownloadsReached : <EOL> self . to_screen ( '<STR_LIT>' ) <EOL> raise <EOL> else : <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> self . to_stdout ( json . dumps ( res ) ) <EOL> return self . _download_retcode <EOL> def download_with_info_file ( self , info_filename ) : <EOL> with io . open ( info_filename , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> info = json . load ( f ) <EOL> try : <EOL> self . process_ie_result ( info , download = True ) <EOL> except DownloadError : <EOL> webpage_url = info . get ( '<STR_LIT>' ) <EOL> if webpage_url is not None : <EOL> self . report_warning ( '<STR_LIT>' % webpage_url ) <EOL> return self . download ( [ webpage_url ] ) <EOL> else : <EOL> raise <EOL> return self . _download_retcode <EOL> def post_process ( self , filename , ie_info ) : <EOL> """<STR_LIT>""" <EOL> info = dict ( ie_info ) <EOL> info [ '<STR_LIT>' ] = filename <EOL> pps_chain = [ ] <EOL> if ie_info . get ( '<STR_LIT>' ) is not None : <EOL> pps_chain . extend ( ie_info [ '<STR_LIT>' ] ) <EOL> pps_chain . extend ( self . _pps ) <EOL> for pp in pps_chain : <EOL> keep_video = None <EOL> old_filename = info [ '<STR_LIT>' ] <EOL> try : <EOL> keep_video_wish , info = pp . run ( info ) <EOL> if keep_video_wish is not None : <EOL> if keep_video_wish : <EOL> keep_video = keep_video_wish <EOL> elif keep_video is None : <EOL> keep_video = keep_video_wish <EOL> except PostProcessingError as e : <EOL> self . report_error ( e . msg ) <EOL> if keep_video is False and not self . params . get ( '<STR_LIT>' , False ) : <EOL> try : <EOL> self . to_screen ( '<STR_LIT>' % old_filename ) <EOL> os . remove ( encodeFilename ( old_filename ) ) <EOL> except ( IOError , OSError ) : <EOL> self . report_warning ( '<STR_LIT>' ) <EOL> def _make_archive_id ( self , info_dict ) : <EOL> extractor = info_dict . get ( '<STR_LIT>' ) <EOL> if extractor is None : <EOL> if '<STR_LIT:id>' in info_dict : <EOL> extractor = info_dict . get ( '<STR_LIT>' ) <EOL> if extractor is None : <EOL> return None <EOL> return extractor . lower ( ) + '<STR_LIT:U+0020>' + info_dict [ '<STR_LIT:id>' ] <EOL> def in_download_archive ( self , info_dict ) : <EOL> fn = self . params . get ( '<STR_LIT>' ) <EOL> if fn is None : <EOL> return False <EOL> vid_id = self . _make_archive_id ( info_dict ) <EOL> if vid_id is None : <EOL> return False <EOL> try : <EOL> with locked_file ( fn , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as archive_file : <EOL> for line in archive_file : <EOL> if line . strip ( ) == vid_id : <EOL> return True <EOL> except IOError as ioe : <EOL> if ioe . errno != errno . ENOENT : <EOL> raise <EOL> return False <EOL> def record_download_archive ( self , info_dict ) : <EOL> fn = self . params . get ( '<STR_LIT>' ) <EOL> if fn is None : <EOL> return <EOL> vid_id = self . _make_archive_id ( info_dict ) <EOL> assert vid_id <EOL> with locked_file ( fn , '<STR_LIT:a>' , encoding = '<STR_LIT:utf-8>' ) as archive_file : <EOL> archive_file . write ( vid_id + '<STR_LIT:\n>' ) <EOL> @ staticmethod <EOL> def format_resolution ( format , default = '<STR_LIT>' ) : <EOL> if format . get ( '<STR_LIT>' ) == '<STR_LIT:none>' : <EOL> return '<STR_LIT>' <EOL> if format . get ( '<STR_LIT>' ) is not None : <EOL> return format [ '<STR_LIT>' ] <EOL> if format . get ( '<STR_LIT>' ) is not None : <EOL> if format . get ( '<STR_LIT:width>' ) is not None : <EOL> res = '<STR_LIT>' % ( format [ '<STR_LIT:width>' ] , format [ '<STR_LIT>' ] ) <EOL> else : <EOL> res = '<STR_LIT>' % format [ '<STR_LIT>' ] <EOL> elif format . get ( '<STR_LIT:width>' ) is not None : <EOL> res = '<STR_LIT>' % format [ '<STR_LIT:width>' ] <EOL> else : <EOL> res = default <EOL> return res <EOL> def _format_note ( self , fdict ) : <EOL> res = '<STR_LIT>' <EOL> if fdict . get ( '<STR_LIT>' ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> res += '<STR_LIT>' <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> res += fdict [ '<STR_LIT>' ] + '<STR_LIT:U+0020>' <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> res += '<STR_LIT>' % fdict [ '<STR_LIT>' ] <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> if res : <EOL> res += '<STR_LIT:U+002CU+0020>' <EOL> res += '<STR_LIT>' % fdict [ '<STR_LIT>' ] <EOL> if ( fdict . get ( '<STR_LIT>' ) is not None and <EOL> fdict . get ( '<STR_LIT>' ) != '<STR_LIT:none>' ) : <EOL> if res : <EOL> res += '<STR_LIT:U+002CU+0020>' <EOL> res += fdict [ '<STR_LIT>' ] <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> res += '<STR_LIT:@>' <EOL> elif fdict . get ( '<STR_LIT>' ) is not None and fdict . get ( '<STR_LIT>' ) is not None : <EOL> res += '<STR_LIT>' <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> res += '<STR_LIT>' % fdict [ '<STR_LIT>' ] <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> res += '<STR_LIT>' % fdict [ '<STR_LIT>' ] <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> if res : <EOL> res += '<STR_LIT:U+002CU+0020>' <EOL> if fdict [ '<STR_LIT>' ] == '<STR_LIT:none>' : <EOL> res += '<STR_LIT>' <EOL> else : <EOL> res += '<STR_LIT>' % fdict [ '<STR_LIT>' ] <EOL> elif fdict . get ( '<STR_LIT>' ) is not None : <EOL> if res : <EOL> res += '<STR_LIT:U+002CU+0020>' <EOL> res += '<STR_LIT>' <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> res += '<STR_LIT>' % fdict [ '<STR_LIT>' ] <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> res += '<STR_LIT>' % fdict [ '<STR_LIT>' ] <EOL> if fdict . get ( '<STR_LIT>' ) is not None : <EOL> if res : <EOL> res += '<STR_LIT:U+002CU+0020>' <EOL> res += format_bytes ( fdict [ '<STR_LIT>' ] ) <EOL> elif fdict . get ( '<STR_LIT>' ) is not None : <EOL> if res : <EOL> res += '<STR_LIT:U+002CU+0020>' <EOL> res += '<STR_LIT>' + format_bytes ( fdict [ '<STR_LIT>' ] ) <EOL> return res <EOL> def list_formats ( self , info_dict ) : <EOL> def line ( format , idlen = <NUM_LIT:20> ) : <EOL> return ( ( '<STR_LIT>' + compat_str ( idlen + <NUM_LIT:1> ) + '<STR_LIT>' ) % ( <EOL> format [ '<STR_LIT>' ] , <EOL> format [ '<STR_LIT>' ] , <EOL> self . format_resolution ( format ) , <EOL> self . _format_note ( format ) , <EOL> ) ) <EOL> formats = info_dict . get ( '<STR_LIT>' , [ info_dict ] ) <EOL> idlen = max ( len ( '<STR_LIT>' ) , <EOL> max ( len ( f [ '<STR_LIT>' ] ) for f in formats ) ) <EOL> formats_s = [ <EOL> line ( f , idlen ) for f in formats <EOL> if f . get ( '<STR_LIT>' ) is None or f [ '<STR_LIT>' ] >= - <NUM_LIT:1000> ] <EOL> if len ( formats ) > <NUM_LIT:1> : <EOL> formats_s [ <NUM_LIT:0> ] += ( '<STR_LIT:U+0020>' if self . _format_note ( formats [ <NUM_LIT:0> ] ) else '<STR_LIT>' ) + '<STR_LIT>' <EOL> formats_s [ - <NUM_LIT:1> ] += ( '<STR_LIT:U+0020>' if self . _format_note ( formats [ - <NUM_LIT:1> ] ) else '<STR_LIT>' ) + '<STR_LIT>' <EOL> header_line = line ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , idlen = idlen ) <EOL> self . to_screen ( '<STR_LIT>' % <EOL> ( info_dict [ '<STR_LIT:id>' ] , header_line , '<STR_LIT:\n>' . join ( formats_s ) ) ) <EOL> def urlopen ( self , req ) : <EOL> """<STR_LIT>""" <EOL> req_is_string = isinstance ( req , basestring if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) else compat_str ) <EOL> url = req if req_is_string else req . get_full_url ( ) <EOL> url_escaped = escape_url ( url ) <EOL> if url != url_escaped : <EOL> if req_is_string : <EOL> req = url_escaped <EOL> else : <EOL> req = compat_urllib_request . Request ( <EOL> url_escaped , data = req . data , headers = req . headers , <EOL> origin_req_host = req . origin_req_host , unverifiable = req . unverifiable ) <EOL> return self . _opener . open ( req , timeout = self . _socket_timeout ) <EOL> def print_debug_header ( self ) : <EOL> if not self . params . get ( '<STR_LIT>' ) : <EOL> return <EOL> if type ( '<STR_LIT>' ) is not compat_str : <EOL> self . report_warning ( <EOL> '<STR_LIT>' ) <EOL> stdout_encoding = getattr ( <EOL> sys . stdout , '<STR_LIT>' , '<STR_LIT>' % type ( sys . stdout ) . __name__ ) <EOL> encoding_str = ( <EOL> '<STR_LIT>' % ( <EOL> locale . getpreferredencoding ( ) , <EOL> sys . getfilesystemencoding ( ) , <EOL> stdout_encoding , <EOL> self . get_encoding ( ) ) ) <EOL> write_string ( encoding_str , encoding = None ) <EOL> self . _write_string ( '<STR_LIT>' + __version__ + '<STR_LIT:\n>' ) <EOL> try : <EOL> sp = subprocess . Popen ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> stdout = subprocess . PIPE , stderr = subprocess . PIPE , <EOL> cwd = os . path . dirname ( os . path . abspath ( __file__ ) ) ) <EOL> out , err = sp . communicate ( ) <EOL> out = out . decode ( ) . strip ( ) <EOL> if re . match ( '<STR_LIT>' , out ) : <EOL> self . _write_string ( '<STR_LIT>' + out + '<STR_LIT:\n>' ) <EOL> except : <EOL> try : <EOL> sys . exc_clear ( ) <EOL> except : <EOL> pass <EOL> self . _write_string ( '<STR_LIT>' % ( <EOL> platform . python_version ( ) , platform_name ( ) ) ) <EOL> exe_versions = FFmpegPostProcessor . get_versions ( ) <EOL> exe_versions [ '<STR_LIT>' ] = rtmpdump_version ( ) <EOL> exe_str = '<STR_LIT:U+002CU+0020>' . join ( <EOL> '<STR_LIT>' % ( exe , v ) <EOL> for exe , v in sorted ( exe_versions . items ( ) ) <EOL> if v <EOL> ) <EOL> if not exe_str : <EOL> exe_str = '<STR_LIT:none>' <EOL> self . _write_string ( '<STR_LIT>' % exe_str ) <EOL> proxy_map = { } <EOL> for handler in self . _opener . handlers : <EOL> if hasattr ( handler , '<STR_LIT>' ) : <EOL> proxy_map . update ( handler . proxies ) <EOL> self . _write_string ( '<STR_LIT>' + compat_str ( proxy_map ) + '<STR_LIT:\n>' ) <EOL> if self . params . get ( '<STR_LIT>' , False ) : <EOL> ipaddr = self . urlopen ( '<STR_LIT>' ) . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> self . _write_string ( '<STR_LIT>' % ipaddr ) <EOL> latest_version = self . urlopen ( <EOL> '<STR_LIT>' ) . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> if version_tuple ( latest_version ) > version_tuple ( __version__ ) : <EOL> self . report_warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> latest_version ) <EOL> def _setup_opener ( self ) : <EOL> timeout_val = self . params . get ( '<STR_LIT>' ) <EOL> self . _socket_timeout = <NUM_LIT> if timeout_val is None else float ( timeout_val ) <EOL> opts_cookiefile = self . params . get ( '<STR_LIT>' ) <EOL> opts_proxy = self . params . get ( '<STR_LIT>' ) <EOL> if opts_cookiefile is None : <EOL> self . cookiejar = compat_cookiejar . CookieJar ( ) <EOL> else : <EOL> self . cookiejar = compat_cookiejar . MozillaCookieJar ( <EOL> opts_cookiefile ) <EOL> if os . access ( opts_cookiefile , os . R_OK ) : <EOL> self . cookiejar . load ( ) <EOL> cookie_processor = compat_urllib_request . HTTPCookieProcessor ( <EOL> self . cookiejar ) <EOL> if opts_proxy is not None : <EOL> if opts_proxy == '<STR_LIT>' : <EOL> proxies = { } <EOL> else : <EOL> proxies = { '<STR_LIT:http>' : opts_proxy , '<STR_LIT>' : opts_proxy } <EOL> else : <EOL> proxies = compat_urllib_request . getproxies ( ) <EOL> if '<STR_LIT:http>' in proxies and '<STR_LIT>' not in proxies : <EOL> proxies [ '<STR_LIT>' ] = proxies [ '<STR_LIT:http>' ] <EOL> proxy_handler = compat_urllib_request . ProxyHandler ( proxies ) <EOL> debuglevel = <NUM_LIT:1> if self . params . get ( '<STR_LIT>' ) else <NUM_LIT:0> <EOL> https_handler = make_HTTPS_handler ( self . params , debuglevel = debuglevel ) <EOL> ydlh = YoutubeDLHandler ( self . params , debuglevel = debuglevel ) <EOL> opener = compat_urllib_request . build_opener ( <EOL> https_handler , proxy_handler , cookie_processor , ydlh ) <EOL> opener . addheaders = [ ] <EOL> self . _opener = opener <EOL> def encode ( self , s ) : <EOL> if isinstance ( s , bytes ) : <EOL> return s <EOL> try : <EOL> return s . encode ( self . get_encoding ( ) ) <EOL> except UnicodeEncodeError as err : <EOL> err . reason = err . reason + '<STR_LIT>' <EOL> raise <EOL> def get_encoding ( self ) : <EOL> encoding = self . params . get ( '<STR_LIT>' ) <EOL> if encoding is None : <EOL> encoding = preferredencoding ( ) <EOL> return encoding </s>
