coherence.extern.youtubedl.youtubedl (module) — Cohen3 0.8.0 documentation

# coherence.extern.youtubedl.youtubedl (module)

`preferredencoding`()

  Get preferred encoding.

  Returns the best encoding scheme for the system, based on locale.getpreferredencoding() and some further tweaks.

exception `DownloadError`

  Bases: `Exception`

  Download Error exception.

  This exception may be thrown by FileDownloader objects if they are not configured to continue on errors. They will contain the appropriate error message.

exception `SameFileError`

  Bases: `Exception`

  Same File exception.

  This exception will be thrown by FileDownloader objects if they detect multiple files would have to be downloaded to the same file on disk.

exception `PostProcessingError`

  Bases: `Exception`

  Post Processing exception.

  This exception may be raised by PostProcessor’s .run() method to indicate an error in the postprocessing task.

exception `UnavailableFormatError`

  Bases: `Exception`

  Unavailable Format exception.

  This exception will be thrown when a video is requested in a format that is not available for that video.

exception `ContentTooShortError`(downloaded, expected)

  Bases: `Exception`

  Content Too Short exception.

  This exception may be raised by FileDownloader objects when a file they download is too small for what the server announced first, indicating the connection was probably interrupted.

  `downloaded` = None
  `expected` = None

class `FileDownloader`(params)

  Bases: `object`

  File Downloader class.

  File downloader objects are the ones responsible of downloading the actual video file and writing it to disk if the user has requested it, among some other tasks. In most cases there should be one per program. As, given a video URL, the downloader doesn’t know how to extract all the needed information, task that InfoExtractors do, it has to pass the URL to one of them.

  For this, file downloader objects have a method that allows InfoExtractors to be registered in a given order. When it is passed a URL, the file downloader handles it to the first InfoExtractor it finds that reports being able to handle it. The InfoExtractor extracts all the information about the video or videos the URL refers to, and asks the FileDownloader to process the video information, possibly downloading the video.

  File downloaders accept a lot of parameters. In order not to saturate the object constructor with arguments, it receives a dictionary of options instead. These options are available through the params attribute for the InfoExtractors to use. The FileDownloader also registers itself as the downloader in charge for the InfoExtractors that are added to it, so this is a “mutual registration”.

  Available options:

  username: Username for authentication purposes. password: Password for authentication purposes. usenetrc: Use netrc for authentication instead. quiet: Do not print messages to stdout. forceurl: Force printing final URL. forcetitle: Force printing title. simulate: Do not download the video files. format: Video format code. outtmpl: Template for output names. ignoreerrors: Do not stop on download errors. ratelimit: Download speed limit, in bytes/sec. nooverwrites: Prevent overwriting files. continuedl: Try to continue downloads if possible.

  Create a FileDownloader object with the given options.

  `__init__`(params)

    Create a FileDownloader object with the given options.

  `_ies` = []
  `_pps` = []
  `_download_retcode` = None
  `params` = None
  static `pmkdir`(filename)

    Create directory components in filename. Similar to Unix “mkdir -p”.

  static `format_bytes`(bytes)
  static `calc_percent`(byte_counter, data_len)
  static `calc_eta`(start, now, total, current)
  static `calc_speed`(start, now, bytes)
  static `best_block_size`(elapsed_time, bytes)
  static `parse_bytes`(bytestr)

    Parse a string indicating a byte quantity into a long integer.

  static `verify_url`(url)

    Verify a URL is valid and data could be downloaded. Return real data URL.

  `add_info_extractor`(ie)

    Add an InfoExtractor object to the end of the list.

  `add_post_processor`(pp)

    Add a PostProcessor object to the end of the chain.

  `to_stdout`(message, skip_eol=False)

    Print message to stdout if not in quiet mode.

  `to_stderr`(message)

    Print message to stderr.

  `fixed_template`()

    Checks if the output template is fixed.

  `trouble`(message=None)

    Determine action to take when a download problem appears.

    Depending on if the downloader has been configured to ignore download errors or not, this method may throw an exception or not when errors are found, after printing the message.

  `slow_down`(start_time, byte_counter)

    Sleep if the download speed is over the rate limit.

  `report_destination`(filename)

    Report destination filename.

  `report_progress`(percent_str, data_len_str, speed_str, eta_str)

    Report download progress.

  `report_resuming_byte`(resume_len)

    Report attemtp to resume at given byte.

  `report_file_already_downloaded`(file_name)

    Report file has already been fully downloaded.

  `report_unable_to_resume`()

    Report it was impossible to resume download.

  `report_finish`()

    Report download finished.

  `_do_download`(*args)
  `process_info`(info_dict)

    Process a single dictionary returned by an InfoExtractor.

  `download`(url_list)

    Download a given list of URLs.

  `get_real_urls`(url_list)

    Download a given list of URLs.

  `post_process`(filename, ie_info)

    Run the postprocessing chain on the given file.

class `InfoExtractor`(downloader=None)

  Bases: `object`

  Information Extractor class.

  Information extractors are the classes that, given a URL, extract information from the video (or videos) the URL refers to. This information includes the real video URL, the video title and simplified title, author and others. The information is stored in a dictionary which is then passed to the FileDownloader. The FileDownloader processes this information possibly downloading the video to the file system, among other possible outcomes. The dictionaries must include the following fields:

  id: Video identifier. url: Final video URL. uploader: Nickname of the video uploader. title: Literal title. stitle: Simplified title. ext: Video filename extension.

  Subclasses of this one should re-define the _real_initialize() and _real_extract() methods, as well as the suitable() static method. Probably, they should also be instantiated and added to the main downloader.

  Constructor. Receives an optional downloader.

  `_downloader` = None
  `__init__`(downloader=None)

    Constructor. Receives an optional downloader.

  `_ready` = False
  static `suitable`(url)

    Receives a URL and returns True if suitable for this IE.

  `initialize`()

    Initializes an instance (authentication, etc).

  `extract`(url)

    Extracts URL information and returns it in list of dicts.

  `set_downloader`(downloader)

    Sets the downloader for this IE.

  `to_stdout`(message)

    Print message to stdout if downloader is not in quiet mode.

  `to_stderr`(message)

    Print message to stderr.

  `_real_initialize`()

    Real initialization process. Redefine in subclasses.

  `_real_extract`(url)

    Real extraction process. Redefine in subclasses.

class `YoutubeIE`(downloader=None)

  Bases: `coherence.extern.youtubedl.youtubedl.InfoExtractor`

  Information extractor for youtube.com.

  Constructor. Receives an optional downloader.

  `_VALID_URL` = '^((?:https://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$'
  `_LANG_URL` = 'https://uk.youtube.com/?hl=en&persist_hl=1&gl=US&persist_gl=1&opt_out_ackd=1'
  `_LOGIN_URL` = 'https://www.youtube.com/signup?next=/&gl=US&hl=en'
  `_AGE_URL` = 'https://www.youtube.com/verify_age?next_url=/&gl=US&hl=en'
  `_NETRC_MACHINE` = 'youtube'
  `_available_formats` = ['22', '35', '18', '5', '17', '13', None]
  `_video_extensions` = {'13': '3gp', '17': 'mp4', '18': 'mp4', '22': 'mp4'}
  static `suitable`(url)

    Receives a URL and returns True if suitable for this IE.

  static `htmlentity_transform`(matchobj)

    Transforms an HTML entity to a Unicode character.

  `report_lang`()

    Report attempt to set language.

  `report_login`()

    Report attempt to log in.

  `report_age_confirmation`()

    Report attempt to confirm age.

  `report_video_info_webpage_download`(video_id)

    Report attempt to download video info webpage.

  `report_information_extraction`(video_id)

    Report attempt to extract video information.

  `report_unavailable_format`(video_id, format)

    Report extracted video URL.

  `report_video_url`(video_id, video_real_url)

    Report extracted video URL.

  `_real_initialize`()

    Real initialization process. Redefine in subclasses.

  `_real_extract`(url)

    Real extraction process. Redefine in subclasses.

class `MetacafeIE`(youtube_ie, downloader=None)

  Bases: `coherence.extern.youtubedl.youtubedl.InfoExtractor`

  Information Extractor for metacafe.com.

  `_VALID_URL` = '(?:https://)?(?:www\\.)?metacafe\\.com/watch/([^/]+)/([^/]+)/.*'
  `_DISCLAIMER` = 'https://www.metacafe.com/family_filter/'
  `_FILTER_POST` = 'https://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
  `_youtube_ie` = None
  static `suitable`(url)

    Receives a URL and returns True if suitable for this IE.

  `report_disclaimer`()

    Report disclaimer retrieval.

  `report_age_confirmation`()

    Report attempt to confirm age.

  `report_download_webpage`(video_id)

    Report webpage download.

  `report_extraction`(video_id)

    Report information extraction.

  `_real_initialize`()

    Real initialization process. Redefine in subclasses.

  `_real_extract`(url)

    Real extraction process. Redefine in subclasses.

class `YoutubeSearchIE`(youtube_ie, downloader=None)

  Bases: `coherence.extern.youtubedl.youtubedl.InfoExtractor`

  Information Extractor for YouTube search queries.

  `_VALID_QUERY` = 'ytsearch(\\d+|all)?:[\\s\\S]+'
  `_TEMPLATE_URL` = 'https://www.youtube.com/results?search_query=%s&page=%s&gl=US&hl=en'
  `_VIDEO_INDICATOR` = 'href="/watch\\?v=.+?"'
  `_MORE_PAGES_INDICATOR` = '(?m)>\\s*Next\\s*</a>'
  `_max_youtube_results` = 1000
  `_youtube_ie` = None
  static `suitable`(url)

    Receives a URL and returns True if suitable for this IE.

  `report_download_page`(query, pagenum)

    Report attempt to download playlist page with given number.

  `_real_initialize`()

    Real initialization process. Redefine in subclasses.

  `_real_extract`(query)

    Real extraction process. Redefine in subclasses.

  `_download_n_results`(query, n)

    Downloads a specified number of results for a query

class `YoutubePlaylistIE`(youtube_ie, downloader=None)

  Bases: `coherence.extern.youtubedl.youtubedl.InfoExtractor`

  Information Extractor for YouTube playlists.

  `_VALID_URL` = '(?:https://)?(?:\\w+\\.)?youtube.com/(?:view_play_list|my_playlists)\\?.*?p=([^&]+).*'
  `_TEMPLATE_URL` = 'https://www.youtube.com/view_play_list?p=%s&page=%s&gl=US&hl=en'
  `_VIDEO_INDICATOR` = '/watch\\?v=(.+?)&'
  `_MORE_PAGES_INDICATOR` = '/view_play_list?p=%s&page=%s'
  `_youtube_ie` = None
  static `suitable`(url)

    Receives a URL and returns True if suitable for this IE.

  `report_download_page`(playlist_id, pagenum)

    Report attempt to download playlist page with given number.

  `_real_initialize`()

    Real initialization process. Redefine in subclasses.

  `_real_extract`(url)

    Real extraction process. Redefine in subclasses.

class `PostProcessor`(downloader=None)

  Bases: `object`

  Post Processor class.

  PostProcessor objects can be added to downloaders with their add_post_processor() method. When the downloader has finished a successful download, it will take its internal chain of PostProcessors and start calling the run() method on each one of them, first with an initial argument and then with the returned value of the previous PostProcessor.

  The chain will be stopped if one of them ever returns None or the end of the chain is reached.

  PostProcessor objects follow a “mutual registration” process similar to InfoExtractor objects.

  `_downloader` = None
  `to_stdout`(message)

    Print message to stdout if downloader is not in quiet mode.

  `to_stderr`(message)

    Print message to stderr.

  `set_downloader`(downloader)

    Sets the downloader for this PP.

  `run`(information)

    Run the PostProcessor.

    The “information” argument is a dictionary like the ones composed by InfoExtractors. The only difference is that this one has an extra field called “filepath” that points to the downloaded file.

    When this method returns None, the postprocessing chain is stopped. However, this method may return an information dictionary that will be passed to the next postprocessing object in the chain. It can be the one it received after changing some fields.

    In addition, this method may raise a PostProcessingError exception that will be taken into account by the downloader it was called from.