coherence.extern.youtubedl.youtubedl (module)

preferredencoding()[source]

Get preferred encoding.

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

exception DownloadError[source]

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[source]

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[source]

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[source]

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)[source]

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)[source]

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)[source]

Create a FileDownloader object with the given options.

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

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

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

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

static verify_url(url)[source]

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

add_info_extractor(ie)[source]

Add an InfoExtractor object to the end of the list.

add_post_processor(pp)[source]

Add a PostProcessor object to the end of the chain.

to_stdout(message, skip_eol=False)[source]

Print message to stdout if not in quiet mode.

to_stderr(message)[source]

Print message to stderr.

fixed_template()[source]

Checks if the output template is fixed.

trouble(message=None)[source]

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)[source]

Sleep if the download speed is over the rate limit.

report_destination(filename)[source]

Report destination filename.

report_progress(percent_str, data_len_str, speed_str, eta_str)[source]

Report download progress.

report_resuming_byte(resume_len)[source]

Report attemtp to resume at given byte.

report_file_already_downloaded(file_name)[source]

Report file has already been fully downloaded.

report_unable_to_resume()[source]

Report it was impossible to resume download.

report_finish()[source]

Report download finished.

_do_download(*args)[source]
process_info(info_dict)[source]

Process a single dictionary returned by an InfoExtractor.

download(url_list)[source]

Download a given list of URLs.

get_real_urls(url_list)[source]

Download a given list of URLs.

post_process(filename, ie_info)[source]

Run the postprocessing chain on the given file.

class InfoExtractor(downloader=None)[source]

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)[source]

Constructor. Receives an optional downloader.

_ready = False
static suitable(url)[source]

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

initialize()[source]

Initializes an instance (authentication, etc).

extract(url)[source]

Extracts URL information and returns it in list of dicts.

set_downloader(downloader)[source]

Sets the downloader for this IE.

to_stdout(message)[source]

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

to_stderr(message)[source]

Print message to stderr.

_real_initialize()[source]

Real initialization process. Redefine in subclasses.

_real_extract(url)[source]

Real extraction process. Redefine in subclasses.

class YoutubeIE(downloader=None)[source]

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)[source]

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

static htmlentity_transform(matchobj)[source]

Transforms an HTML entity to a Unicode character.

report_lang()[source]

Report attempt to set language.

report_login()[source]

Report attempt to log in.

report_age_confirmation()[source]

Report attempt to confirm age.

report_video_info_webpage_download(video_id)[source]

Report attempt to download video info webpage.

report_information_extraction(video_id)[source]

Report attempt to extract video information.

report_unavailable_format(video_id, format)[source]

Report extracted video URL.

report_video_url(video_id, video_real_url)[source]

Report extracted video URL.

_real_initialize()[source]

Real initialization process. Redefine in subclasses.

_real_extract(url)[source]

Real extraction process. Redefine in subclasses.

class MetacafeIE(youtube_ie, downloader=None)[source]

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)[source]

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

report_disclaimer()[source]

Report disclaimer retrieval.

report_age_confirmation()[source]

Report attempt to confirm age.

report_download_webpage(video_id)[source]

Report webpage download.

report_extraction(video_id)[source]

Report information extraction.

_real_initialize()[source]

Real initialization process. Redefine in subclasses.

_real_extract(url)[source]

Real extraction process. Redefine in subclasses.

class YoutubeSearchIE(youtube_ie, downloader=None)[source]

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)[source]

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

report_download_page(query, pagenum)[source]

Report attempt to download playlist page with given number.

_real_initialize()[source]

Real initialization process. Redefine in subclasses.

_real_extract(query)[source]

Real extraction process. Redefine in subclasses.

_download_n_results(query, n)[source]

Downloads a specified number of results for a query

class YoutubePlaylistIE(youtube_ie, downloader=None)[source]

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)[source]

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

report_download_page(playlist_id, pagenum)[source]

Report attempt to download playlist page with given number.

_real_initialize()[source]

Real initialization process. Redefine in subclasses.

_real_extract(url)[source]

Real extraction process. Redefine in subclasses.

class PostProcessor(downloader=None)[source]

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)[source]

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

to_stderr(message)[source]

Print message to stderr.

set_downloader(downloader)[source]

Sets the downloader for this PP.

run(information)[source]

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.