#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import base64
import datetime
import itertools
import netrc
import os
import re
import socket
import time
import email.utils
import xml.etree.ElementTree
import random
import math
from .utils import *
class InfoExtractor(object):
    """Information Extractor class.
    Information extractors are the classes that, given a URL, extract
    information about the video (or videos) the URL refers to. This
    information includes the real video URL, the video 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.
    title:          Video title, unescaped.
    ext:            Video filename extension.
    The following fields are optional:
    format:         The video format, defaults to ext (used for --get-format)
    thumbnail:      Full URL to a video thumbnail image.
    description:    One-line video description.
    uploader:       Full name of the video uploader.
    upload_date:    Video upload date (YYYYMMDD).
    uploader_id:    Nickname or id of the video uploader.
    location:       Physical location of the video.
    player_url:     SWF Player URL (used for rtmpdump).
    subtitles:      The .srt file contents.
    urlhandle:      [internal] The urlHandle to be used to download the file,
                    like returned by urllib.request.urlopen
    The fields should all be Unicode strings.
    Subclasses of this one should re-define the _real_initialize() and
    _real_extract() methods and define a _VALID_URL regexp.
    Probably, they should also be added to the list of extractors.
    _real_extract() must return a *list* of information dictionaries as
    described above.
    Finally, the _WORKING attribute should be set to False for broken IEs
    in order to warn the users and skip the tests.
    """
    _ready = False
    _downloader = None
    _WORKING = True
    def __init__(self, downloader=None):
        """Constructor. Receives an optional downloader."""
        self._ready = False
        self.set_downloader(downloader)
    def suitable(self, url):
        """Receives a URL and returns True if suitable for this IE."""
        return re.match(self._VALID_URL, url) is not None
    def working(self):
        """Getter method for _WORKING."""
        return self._WORKING
    def initialize(self):
        """Initializes an instance (authentication, etc)."""
        if not self._ready:
            self._real_initialize()
            self._ready = True
    def extract(self, url):
        """Extracts URL information and returns it in list of dicts."""
        self.initialize()
        return self._real_extract(url)
    def set_downloader(self, downloader):
        """Sets the downloader for this IE."""
        self._downloader = downloader
    def _real_initialize(self):
        """Real initialization process. Redefine in subclasses."""
        pass
    def _real_extract(self, url):
        """Real extraction process. Redefine in subclasses."""
        pass
    @property
    def IE_NAME(self):
        return type(self).__name__[:-2]
    def _request_webpage(self, url_or_request, video_id, note=None, errnote=None):
        """ Returns the response handle """
        if note is None:
            note = u'Downloading video webpage'
        self._downloader.to_screen(u'[%s] %s: %s' % (self.IE_NAME, video_id, note))
        try:
            return compat_urllib_request.urlopen(url_or_request)
        except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
            if errnote is None:
                errnote = u'Unable to download webpage'
            raise ExtractorError(u'%s: %s' % (errnote, compat_str(err)), sys.exc_info()[2])
    def _download_webpage(self, url_or_request, video_id, note=None, errnote=None):
        """ Returns the data of the page as a string """
        urlh = self._request_webpage(url_or_request, video_id, note, errnote)
        webpage_bytes = urlh.read()
        return webpage_bytes.decode('utf-8', 'replace')
class YoutubeIE(InfoExtractor):
    """Information extractor for youtube.com."""
    _VALID_URL = r"""^
                     (
                         (?:https?://)?                                       # http(s):// (optional)
                         (?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/|
                            tube\.majestyc\.net/)                             # the various hostnames, with wildcard subdomains
                         (?:.*?\#/)?                                          # handle anchor (#/) redirect urls
                         (?!view_play_list|my_playlists|artist|playlist)      # ignore playlist URLs
                         (?:                                                  # the various things that can precede the ID:
                             (?:(?:v|embed|e)/)                               # v/ or embed/ or e/
                             |(?:                                             # or the v= param in all its forms
                                 (?:watch(?:_popup)?(?:\.php)?)?              # preceding watch(_popup|.php) or nothing (like /?v=xxxx)
                                 (?:\?|\#!?)                                  # the params delimiter ? or # or #!
                                 (?:.*?&)?                                    # any other preceding param (like /?s=tuff&v=xxxx)
                                 v=
                             )
                         )?                                                   # optional -> youtube.com/xxxx is OK
                     )?                                                       # all until now is optional -> you can pass the naked ID
                     ([0-9A-Za-z_-]+)                                         # here is it! the YouTube video ID
                     (?(1).+)?                                                # if we found the ID, everything can follow
                     $"""
    _LANG_URL = r'http://www.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 = 'http://www.youtube.com/verify_age?next_url=/&gl=US&hl=en'
    _NEXT_URL_RE = r'[\?&]next_url=([^&]+)'
    _NETRC_MACHINE = 'youtube'
    # Listed in order of quality
    _available_formats = ['38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13']
    _available_formats_prefer_free = ['38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13']
    _video_extensions = {
        '13': '3gp',
        '17': 'mp4',
        '18': 'mp4',
        '22': 'mp4',
        '37': 'mp4',
        '38': 'video', # You actually don't know if this will be MOV, AVI or whatever
        '43': 'webm',
        '44': 'webm',
        '45': 'webm',
        '46': 'webm',
    }
    _video_dimensions = {
        '5': '240x400',
        '6': '???',
        '13': '???',
        '17': '144x176',
        '18': '360x640',
        '22': '720x1280',
        '34': '360x640',
        '35': '480x854',
        '37': '1080x1920',
        '38': '3072x4096',
        '43': '360x640',
        '44': '480x854',
        '45': '720x1280',
        '46': '1080x1920',
    }
    IE_NAME = u'youtube'
    def suitable(self, url):
        """Receives a URL and returns True if suitable for this IE."""
        return re.match(self._VALID_URL, url, re.VERBOSE) is not None
    def report_lang(self):
        """Report attempt to set language."""
        self._downloader.to_screen(u'[youtube] Setting language')
    def report_login(self):
        """Report attempt to log in."""
        self._downloader.to_screen(u'[youtube] Logging in')
    def report_age_confirmation(self):
        """Report attempt to confirm age."""
        self._downloader.to_screen(u'[youtube] Confirming age')
    def report_video_webpage_download(self, video_id):
        """Report attempt to download video webpage."""
        self._downloader.to_screen(u'[youtube] %s: Downloading video webpage' % video_id)
    def report_video_info_webpage_download(self, video_id):
        """Report attempt to download video info webpage."""
        self._downloader.to_screen(u'[youtube] %s: Downloading video info webpage' % video_id)
    def report_video_subtitles_download(self, video_id):
        """Report attempt to download video info webpage."""
        self._downloader.to_screen(u'[youtube] %s: Downloading video subtitles' % video_id)
    def report_information_extraction(self, video_id):
        """Report attempt to extract video information."""
        self._downloader.to_screen(u'[youtube] %s: Extracting video information' % video_id)
    def report_unavailable_format(self, video_id, format):
        """Report extracted video URL."""
        self._downloader.to_screen(u'[youtube] %s: Format %s not available' % (video_id, format))
    def report_rtmp_download(self):
        """Indicate the download will use the RTMP protocol."""
        self._downloader.to_screen(u'[youtube] RTMP download detected')
    def _closed_captions_xml_to_srt(self, xml_string):
        srt = ''
        texts = re.findall(r'