code:
import getpass
import json
import re
import time
import urllib.error
import xml.etree.ElementTree as etree

from .common import InfoExtractor
from ..compat import compat_urlparse
from ..utils import (
    NO_DEFAULT,
    ExtractorError,
    unescapeHTML,
    unified_timestamp,
    urlencode_postdata,
)

MSO_INFO = {
    'DTV': {
        'name': 'DIRECTV',
        'username_field': 'username',
        'password_field': 'password',
    },
    'ATT': {
        'name': 'AT&T U-verse',
        'username_field': 'userid',
        'password_field': 'password',
    },
    'ATTOTT': {
        'name': 'DIRECTV NOW',
        'username_field': 'email',
        'password_field': 'loginpassword',
    },
    'RCN': {
        'name': 'RCN',
        'username_field': 'username',
        'password_field': 'password',
    },
    'Rogers': {
        'name': 'Rogers',
        'username_field': 'UserName',
        'password_field': 'UserPassword',
    },
    'Comcast_SSO': {
        'name': 'Comcast XFINITY',
        'username_field': 'user',
        'password_field': 'passwd',
    },
    'TWC': {
        'name': 'Time Warner Cable | Spectrum',
        'username_field': 'Ecom_User_ID',
        'password_field': 'Ecom_Password',
    },
    'Brighthouse': {
        'name': 'Bright House Networks | Spectrum',
        'username_field': 'j_username',
        'password_field': 'j_password',
    },
    'Charter_Direct': {
        'name': 'Charter Spectrum',
        'username_field': 'IDToken1',
        'password_field': 'IDToken2',
    },
    'Spectrum': {
        'name': 'Spectrum',
        'username_field': 'IDToken1',
        'password_field': 'IDToken2',
    },
    'Philo': {
        'name': 'Philo',
        'username_field': 'ident'
    },
    'Verizon': {
        'name': 'Verizon FiOS',
        'username_field': 'IDToken1',
        'password_field': 'IDToken2',
    },
    'Cablevision': {
        'name': 'Optimum/Cablevision',
        'username_field': 'j_username',
        'password_field': 'j_password',
    },
    'thr030': {
        'name': '3 Rivers Communications'
    },
    'com140': {
        'name': 'Access Montana'
    },
    'acecommunications': {
        'name': 'AcenTek'
    },
    'acm010': {
        'name': 'Acme Communications'
    },
    'ada020': {
        'name': 'Adams Cable Service'
    },
    'alb020': {
        'name': 'Albany Mutual Telephone'
    },
    'algona': {
        'name': 'Algona Municipal Utilities'
    },
    'allwest': {
        'name': 'All West Communications'
    },
    'all025': {
        'name': 'Allen\'s Communications'
    },
    'spl010': {
        'name': 'Alliance Communications'
    },
    'all070': {
        'name': 'ALLO Communications'
    },
    'alpine': {
        'name': 'Alpine Communications'
    },
    'hun015': {
        'name': 'American Broadband'
    },
    'nwc010': {
        'name': 'American Broadband Missouri'
    },
    'com130-02': {
        'name': 'American Community Networks'
    },
    'com130-01': {
        'name': 'American Warrior Networks'
    },
    'tom020': {
        'name': 'Amherst Telephone/Tomorrow Valley'
    },
    'tvc020': {
        'name': 'Andycable'
    },
    'arkwest': {
        'name': 'Arkwest Communications'
    },
    'art030': {
        'name': 'Arthur Mutual Telephone Company'
    },
    'arvig': {
        'name': 'Arvig'
    },
    'nttcash010': {
        'name': 'Ashland Home Net'
    },
    'astound': {
        'name': 'Astound (now Wave)'
    },
    'dix030': {
        'name': 'ATC Broadband'
    },
    'ara010': {
        'name': 'ATC Communications'
    },
    'she030-02': {
        'name': 'Ayersville Communications'
    },
    'baldwin': {
        'name': 'Baldwin Lightstream'
    },
    'bal040': {
        'name': 'Ballard TV'
    },
    'cit025': {
        'name': 'Bardstown Cable TV'
    },
    'bay030': {
        'name': 'Bay Country Communications'
    },
    'tel095': {
        'name': 'Beaver Creek Cooperative Telephone'
    },
    'bea020': {
        'name': 'Beaver Valley Cable'
    },
    'bee010': {
        'name': 'Bee Line Cable'
    },
    'wir030': {
        'name': 'Beehive Broadband'
    },
    'bra020': {
        'name': 'BELD'
    },
    'bel020': {
        'name': 'Bellevue Municipal Cable'
    },
    'vol040-01': {
        'name': 'Ben Lomand Connect / BLTV'
    },
    'bev010': {
        'name': 'BEVCOMM'
    },
    'big020': {
        'name': 'Big Sandy Broadband'
    },
    'ble020': {
        'name': 'Bledsoe Telephone Cooperative'
    },
    'bvt010': {
        'name': 'Blue Valley Tele-Communications'
    },
    'bra050': {
        'name': 'Brandenburg Telephone Co.'
    },
    'bte010': {
        'name': 'Bristol Tennessee Essential Services'
    },
    'annearundel': {
        'name': 'Broadstripe'
    },
    'btc010': {
        'name': 'BTC Communications'
    },
    'btc040': {
        'name': 'BTC Vision - Nahunta'
    },
    'bul010': {
        'name': 'Bulloch Telephone Cooperative'
    },
    'but010': {
        'name': 'Butler-Bremer Communications'
    },
    'tel160-csp': {
        'name': 'C Spire SNAP'
    },
    'csicable': {
        'name': 'Cable Services Inc.'
    },
    'cableamerica': {
        'name': 'CableAmerica'
    },
    'cab038': {
        'name': 'CableSouth Media 3'
    },
    'weh010-camtel': {
        'name': 'Cam-Tel Company'
    },
    'car030': {
        'name': 'Cameron Communications'
    },
    'canbytel': {
        'name': 'Canby Telcom'
    },
    'crt020': {
        'name': 'CapRock Tv'
    },
    'car050': {
        'name': 'Carnegie Cable'
    },
    'cas': {
        'name': 'CAS Cable'
    },
    'casscomm': {
        'name': 'CASSCOMM'
    },
    'mid180-02': {
        'name': 'Catalina Broadband Solutions'
    },
    'cccomm': {
        'name': 'CC Communications'
    },
    'nttccde010': {
        'name': 'CDE Lightband'
    },
    'cfunet': {
        'name': 'Cedar Falls Utilities'
    },
    'dem010-01': {
        'name': 'Celect-Bloomer Telephone Area'
    },
    'dem010-02': {
        'name': 'Celect-Bruce Telephone Area'
    },
    'dem010-03': {
        'name': 'Celect-Citizens Connected Area'
    },
    'dem010-04': {
        'name': 'Celect-Elmwood/Spring Valley Area'
    },
    'dem010-06': {
        'name': 'Celect-Mosaic Telecom'
    },
    'dem010-05': {
        'name': 'Celect-West WI Telephone Area'
    },
    'net010-02': {
        'name': 'Cellcom/Nsight Telservices'
    },
    'cen100': {
        'name': 'CentraCom'
    },
    'nttccst010': {
        'name': 'Central Scott / CSTV'
    },
    'cha035': {
        'name': 'Chaparral CableVision'
    },
    'cha050': {
        'name': 'Chariton Valley Communication Corporation, Inc.'
    },
    'cha060': {
        'name': 'Chatmoss Cablevision'
    },
    'nttcche010': {
        'name': 'Cherokee Communications'
    },
    'che050': {
        'name': 'Chesapeake Bay Communications'
    },
    'cimtel': {
        'name': 'Cim-Tel Cable, LLC.'
    },
    'cit180': {
        'name': 'Citizens Cablevision - Floyd, VA'
    },
    'cit210': {
        'name': 'Citizens Cablevision, Inc.'
    },
    'cit040': {
        'name': 'Citizens Fiber'
    },
    'cit250': {
        'name': 'Citizens Mutual'
    },
    'war040': {
        'name': 'Citizens Telephone Corporation'
    },
    'wat025': {
        'name': 'City Of Monroe'
    },
    'wadsworth': {
        'name': 'CityLink'
    },
    'nor100': {
        'name': 'CL Tel'
    },
    'cla010': {
        'name': 'Clarence Telephone and Cedar Communications'
    },
    'ser060': {
        'name': 'Clear Choice Communications'
    },
    'tac020': {
        'name': 'Click! Cable TV'
    },
    'war020': {
        'name': 'CLICK1.NET'
    },
    'cml010': {
        'name': 'CML Telephone Cooperative Association'
    },
    'cns': {
        'name': 'CNS'
    },
    'com160': {
        'name': 'Co-Mo Connect'
    },
    'coa020': {
        'name': 'Coast Communications'
    },
    'coa030': {
        'name': 'Coaxial Cable TV'
    },
    'mid055': {
        'name': 'Cobalt TV (Mid-State Community TV)'
    },
    'col070': {
        'name': 'Columbia Power & Water Systems'
    },
    'col080': {
        'name': 'Columbus Telephone'
    },
    'nor105': {
        'name': 'Communications 1 Cablevision, Inc.'
    },
    'com150': {
        'name': 'Community Cable & Broadband'
    },
    'com020': {
        'name': 'Community Communications Company'
    },
    'coy010': {
        'name': 'commZoom'
    },
    'com025': {
        'name': 'Complete Communication Services'
    },
    'cat020': {
        'name': 'Comporium'
    },
    'com071': {
        'name': 'ComSouth Telesys'
    },
    'consolidatedcable': {
        'name': 'Consolidated'
    },
    'conwaycorp': {
        'name': 'Conway Corporation'
    },
    'coo050': {
        'name': 'Coon Valley Telecommunications Inc'
    },
    'coo080': {
        'name': 'Cooperative Telephone Company'
    },
    'cpt010': {
        'name': 'CP-TEL'
    },
    'cra010': {
        'name': 'Craw-Kan Telephone'
    },
    'crestview': {
        'name': 'Crestview Cable Communications'
    },
    'cross': {
        'name': 'Cross TV'
    },
    'cro030': {
        'name': 'Crosslake Communications'
    },
    'ctc040': {
        'name': 'CTC - Brainerd MN'
    },
    'phe030': {
        'name': 'CTV-Beam - East Alabama'
    },
    'cun010': {
        'name': 'Cunningham Telephone & Cable'
    },
    'dpc010': {
        'name': 'D & P Communications'
    },
    'dak030': {
        'name': 'Dakota Central Telecommunications'
    },
    'nttcdel010': {
        'name': 'Delcambre Telephone LLC'
    },
    'tel160-del': {
        'name': 'Delta Telephone Company'
    },
    'sal040': {
        'name': 'DiamondNet'
    },
    'ind060-dc': {
        'name': 'Direct Communications'
    },
    'doy010': {
        'name': 'Doylestown Cable TV'
    },
    'dic010': {
        'name': 'DRN'
    },
    'dtc020': {
        'name': 'DTC'
    },
    'dtc010': {
        'name': 'DTC Cable (Delhi)'
    },
    'dum010': {
        'name': 'Dumont Telephone Company'
    },
    'dun010': {
        'name': 'Dunkerton Telephone Cooperative'
    },
    'cci010': {
        'name': 'Duo County Telecom'
    },
    'eagle': {
        'name': 'Eagle Communications'
    },
    'weh010-east': {
        'name': 'East Arkansas Cable TV'
    },
    'eatel': {
        'name': 'EATEL Video, LLC'
    },
    'ell010': {
        'name': 'ECTA'
    },
    'emerytelcom': {
        'name': 'Emery Telcom Video LLC'
    },
    'nor200': {
        'name': 'Empire Access'
    },
    'endeavor': {
        'name': 'Endeavor Communications'
    },
    'sun045': {
        'name': 'Enhanced Telecommunications Corporation'
    },
    'mid030': {
        'name': 'enTouch'
    },
    'epb020': {
        'name': 'EPB Smartnet'
    },
    'jea010': {
        'name': 'EPlus Broadband'
    },
    'com065': {
        'name': 'ETC'
    },
    'ete010': {
        'name': 'Etex Communications'
    },
    'fbc-tele': {
        'name': 'F&B Communications'
    },
    'fal010': {
        'name': 'Falcon Broadband'
    },
    'fam010': {
        'name': 'FamilyView CableVision'
    },
    'far020': {
        'name': 'Farmers Mutual Telephone Company'
    },
    'fay010': {
        'name': 'Fayetteville Public Utilities'
    },
    'sal060': {
        'name': 'fibrant'
    },
    'fid010': {
        'name': 'Fidelity Communications'
    },
    'for030': {
        'name': 'FJ Communications'
    },
    'fli020': {
        'name': 'Flint River Communications'
    },
    'far030': {
        'name': 'FMT - Jesup'
    },
    'foo010': {
        'name': 'Foothills Communications'
    },
    'for080': {
        'name': 'Forsyth CableNet'
    },
    'fbcomm': {
        'name': 'Frankfort Plant Board'
    },
    'tel160-fra': {
        'name': 'Franklin Telephone Company'
    },
    'nttcftc010': {
        'name': 'FTC'
    },
    'fullchannel': {
        'name': 'Full Channel, Inc.'
    },
    'gar040': {
        'name': 'Gardonville Cooperative Telephone Association'
    },
    'gbt010': {
        'name': 'GBT Communications, Inc.'
    },
    'tec010': {
        'name': 'Genuine Telecom'
    },
    'clr010': {
        'name': 'Giant Communications'
    },
    'gla010': {
        'name': 'Glasgow EPB'
    },
    'gle010': {
        'name': 'Glenwood Telecommunications'
    },
    'gra060': {
        'name': 'GLW Broadband Inc.'
    },
    'goldenwest': {
        'name': 'Golden West Cablevision'
    },
    'vis030': {
        'name': 'Grantsburg Telcom'
    },
    'gpcom': {
        'name': 'Great Plains Communications'
    },
    'gri010': {
        'name': 'Gridley Cable Inc'
    },
    'hbc010': {
        'name': 'H&B Cable Services'
    },
    'hae010': {
        'name': 'Haefele TV Inc.'
    },
    'htc010': {
        'name': 'Halstad Telephone Company'
    },
    'har005': {
        'name': 'Harlan Municipal Utilities'
    },
    'har020': {
        'name': 'Hart Communications'
    },
    'ced010': {
        'name': 'Hartelco TV'
    },
    'hea040': {
        'name': 'Heart of Iowa Communications Cooperative'
    },
    'htc020': {
        'name': 'Hickory Telephone Company'
    },
    'nttchig010': {
        'name': 'Highland Communication Services'
    },
    'hig030': {
        'name': 'Highland Media'
    },
    'spc010': {
        'name': 'Hilliary Communications'
    },
    'hin020': {
        'name': 'Hinton CATV Co.'
    },
    'hometel': {
        'name': 'HomeTel Entertainment, Inc.'
    },
    'hoodcanal': {
        'name': 'Hood Canal Communications'
    },
    'weh010-hope': {
        'name': 'Hope - Prescott Cable TV'
    },
    'horizoncable': {
        'name': 'Horizon Cable TV, Inc.'
    },
    'hor040': {
        'name': 'Horizon Chillicothe Telephone'
    },
    'htc030': {
        'name': 'HTC Communications Co. - IL'
    },
    'htccomm': {
        'name': 'HTC Communications, Inc. - IA'
    },
    'wal005': {
        'name': 'Huxley Communications'
    },
    'imon': {
        'name': 'ImOn Communications'
    },
    'ind040': {
        'name': 'Independence Telecommunications'
    },
    'rrc010': {
        'name': 'Inland Networks'
    },
    'stc020': {
        'name': 'Innovative Cable TV St Croix'
    },
    'car100': {
        'name': 'Innovative Cable TV St Thomas-St John'
    },
    'icc010': {
        'name': 'Inside Connect Cable'
    },
    'int100': {
        'name': 'Integra Telecom'
    },
    'int050': {
        'name': 'Interstate Telecommunications Coop'
    },
    'irv010': {
        'name': 'Irvine Cable'
    },
    'k2c010': {
        'name': 'K2 Communications'
    },
    'kal010': {
        'name': 'Kalida Telephone Company, Inc.'
    },
    'kal030': {
        'name': 'Kalona Cooperative Telephone Company'
    },
    'kmt010': {
        'name': 'KMTelecom'
    },
    'kpu010': {
        'name': 'KPU Telecommunications'
    },
    'kuh010': {
        'name': 'Kuhn Communications, Inc.'
    },
    'lak130': {
        'name': 'Lakeland Communications'
    },
    'lan010': {
        'name': 'Langco'
    },
    'lau020': {
        'name': 'Laurel Highland Total Communications, Inc.'
    },
    'leh010': {
        'name': 'Lehigh Valley Cooperative Telephone'
    },
    'bra010': {
        'name': 'Limestone Cable/Bracken Cable'
    },
    'loc020': {
        'name': 'LISCO'
    },
    'lit020': {
        'name': 'Litestream'
    },
    'tel140': {
        'name': 'LivCom'
    },
    'loc010': {
        'name': 'LocalTel Communications'
    },
    'weh010-longview': {
        'name': 'Longview - Kilgore Cable TV'
    },
    'lon030': {
        'name': 'Lonsdale Video Ventures, LLC'
    },
    'lns010': {
        'name': 'Lost Nation-Elwood Telephone Co.'
    },
    'nttclpc010': {
        'name': 'LPC Connect'
    },
    'lumos': {
        'name': 'Lumos Networks'
    },
    'madison': {
        'name': 'Madison Communications'
    },
    'mad030': {
        'name': 'Madison County Cable Inc.'
    },
    'nttcmah010': {
        'name': 'Mahaska Communication Group'
    },
    'mar010': {
        'name': 'Marne & Elk Horn Telephone Company'
    },
    'mcc040': {
        'name': 'McClure Telephone Co.'
    },
    'mctv': {
        'name': 'MCTV'
    },
    'merrimac': {
        'name': 'Merrimac Communications Ltd.'
    },
    'metronet': {
        'name': 'Metronet'
    },
    'mhtc': {
        'name': 'MHTC'
    },
    'midhudson': {
        'name': 'Mid-Hudson Cable'
    },
    'midrivers': {
        'name': 'Mid-Rivers Communications'
    },
    'mid045': {
        'name': 'Midstate Communications'
    },
    'mil080': {
        'name': 'Milford Communications'
    },
    'min030': {
        'name': 'MINET'
    },
    'nttcmin010': {
        'name': 'Minford TV'
    },
    'san040-02': {
        'name': 'Mitchell Telecom'
    },
    'mlg010': {
        'name': 'MLGC'
    },
    'mon060': {
        'name': 'Mon-Cre TVE'
    },
    'mou110': {
        'name': 'Mountain Telephone'
    },
    'mou050': {
        'name': 'Mountain Village Cable'
    },
    'mtacomm': {
        'name': 'MTA Communications, LLC'
    },
    'mtc010': {
        'name': 'MTC Cable'
    },
    'med040': {
        'name': 'MTC Technologies'
    },
    'man060': {
        'name': 'MTCC'
    },
    'mtc030': {
        'name': 'MTCO Communications'
    },
    'mul050': {
        'name': 'Mulberry Telecommunications'
    },
    'mur010': {
        'name': 'Murray Electric System'
    },
    'musfiber': {
        'name': 'MUS FiberNET'
    },
    'mpw': {
        'name': 'Muscatine Power & Water'
    },
    'nttcsli010': {
        'name': 'myEVTV.com'
    },
    'nor115': {
        'name': 'NCC'
    },
    'nor260': {
        'name': 'NDTC'
    },
    'nctc': {
        'name': 'Nebraska Central Telecom, Inc.'
    },
    'nel020': {
        'name': 'Nelsonville TV Cable'
    },
    'nem010': {
        'name': 'Nemont'
    },
    'new075': {
        'name': 'New Hope Telephone Cooperative'
    },
    'nor240': {
        'name': 'NICP'
    },
    'cic010': {
        'name': 'NineStar Connect'
    },
    'nktelco': {
        'name': 'NKTelco'
    },
    'nortex': {
        'name': 'Nortex Communications'
    },
    'nor140': {
        'name': 'North Central Telephone Cooperative'
    },
    'nor030': {
        'name': 'Northland Communications'
    },
    'nor075': {
        'name': 'Northwest Communications'
    },
    'nor125': {
        'name': 'Norwood Light Broadband'
    },
    'net010': {
        'name': 'Nsight Telservices'
    },
    'dur010': {
        'name': 'Ntec'
    },
    'nts010': {
        'name': 'NTS Communications'
    },
    'new045': {
        'name': 'NU-Telecom'
    },
    'nulink': {
        'name': 'NuLink'
    },
    'jam030': {
        'name': 'NVC'
    },
    'far035': {
        'name': 'OmniTel Communications'
    },
    'onesource': {
        'name': 'OneSource Communications'
    },
    'cit230': {
        'name': 'Opelika Power Services'
    },
    'daltonutilities': {
        'name': 'OptiLink'
    },
    'mid140': {
        'name': 'OPTURA'
    },
    'ote010': {
        'name': 'OTEC Communication Company'
    },
    'cci020': {
        'name': 'Packerland Broadband'
    },
    'pan010': {
        'name': 'Panora Telco/Guthrie Center Communications'
    },
    'otter': {
        'name': 'Park Region Telephone & Otter Tail Telcom'
    },
    'mid050': {
        'name': 'Partner Communications Cooperative'
    },
    'fib010': {
        'name': 'Pathway'
    },
    'paulbunyan': {
        'name': 'Paul Bunyan Communications'
    },
    'pem020': {
        'name': 'Pembroke Telephone Company'
    },
    'mck010': {
        'name': 'Peoples Rural Telephone Cooperative'
    },
    'pul010': {
        'name': 'PES Energize'
    },
    'phi010': {
        'name': 'Philippi Communications System'
    },
    'phonoscope': {
        'name': 'Phonoscope Cable'
    },
    'pin070': {
        'name': 'Pine Belt Communications, Inc.'
    },
    'weh010-pine': {
        'name': 'Pine Bluff Cable TV'
    },
    'pin060': {
        'name': 'Pineland Telephone Cooperative'
    },
    'cam010': {
        'name': 'Pinpoint Communications'
    },
    'pio060': {
        'name': 'Pioneer Broadband'
    },
    'pioncomm': {
        'name': 'Pioneer Communications'
    },
    'pioneer': {
        'name': 'Pioneer DTV'
    },
    'pla020': {
        'name': 'Plant TiftNet, Inc.'
    },
    'par010': {
        'name': 'PLWC'
    },
    'pro035': {
        'name': 'PMT'
    },
    'vik011': {
        'name': 'Polar Cablevision'
    },
    'pottawatomie': {
        'name': 'Pottawatomie Telephone Co.'
    },
    'premiercomm': {
        'name': 'Premier Communications'
    },
    'psc010': {
        'name': 'PSC'
    },
    'pan020': {
        'name': 'PTCI'
    },
    'qco010': {
        'name': 'QCOL'
    },
    'qua010': {
        'name': 'Quality Cablevision'
    },
    'rad010': {
        'name': 'Radcliffe Telephone Company'
    },
    'car040': {
        'name': 'Rainbow Communications'
    },
    'rai030': {
        'name': 'Rainier Connect'
    },
    'ral010': {
        'name': 'Ralls Technologies'
    },
    'rct010': {
        'name': 'RC Technologies'
    },
    'red040': {
        'name': 'Red River Communications'
    },
    'ree010': {
        'name': 'Reedsburg Utility Commission'
    },
    'mol010': {
        'name': 'Reliance Connects- Oregon'
    },
    'res020': {
        'name': 'Reserve Telecommunications'
    },
    'weh010-resort': {
        'name': 'Resort TV Cable'
    },
    'rld010': {
        'name': 'Richland Grant Telephone Cooperative, Inc.'
    },
    'riv030': {
        'name': 'River Valley Telecommunications Coop'
    },
    'rockportcable': {
        'name': 'Rock Port Cablevision'
    },
    'rsf010': {
        'name': 'RS Fiber'
    },
    'rtc': {
        'name': 'RTC Communication Corp'
    },
    'res040': {
        'name': 'RTC-Reservation Telephone Coop.'
    },
    'rte010': {
        'name': 'RTEC Communications'
    },
    'stc010': {
        'name': 'S&T'
    },
    'san020': {
        'name': 'San Bruno Cable TV'
    },
    'san040-01': {
        'name': 'Santel'
    },
    'sav010': {
        'name': 'SCI Broadband-Savage Communications Inc.'
    },
    'sco050': {
        'name': 'Scottsboro Electric Power Board'
    },
    'scr010': {
        'name': 'Scranton Telephone Company'
    },
    'selco': {
        'name': 'SELCO'
    },
    'she010': {
        'name': 'Shentel'
    },
    'she030': {
        'name': 'Sherwood Mutual Telephone Association, Inc.'
    },
    'ind060-ssc': {
        'name': 'Silver Star Communications'
    },
    'sjoberg': {
        'name': 'Sjoberg\'s Inc.'
    },
    'sou025': {
        'name': 'SKT'
    },
    'sky050': {
        'name': 'SkyBest TV'
    },
    'nttcsmi010': {
        'name': 'Smithville Communications'
    },
    'woo010': {
        'name': 'Solarus'
    },
    'sou075': {
        'name': 'South Central Rural Telephone Cooperative'
    },
    'sou065': {
        'name': 'South Holt Cablevision, Inc.'
    },
    'sou035': {
        'name': 'South Slope Cooperative Communications'
    },
    'spa020': {
        'name': 'Spanish Fork Community Network'
    },
    'spe010': {
        'name': 'Spencer Municipal Utilities'
    },
    'spi005': {
        'name': 'Spillway Communications, Inc.'
    },
    'srt010': {
        'name': 'SRT'
    },
    'cccsmc010': {
        'name': 'St. Maarten Cable TV'
    },
    'sta025': {
        'name': 'Star Communications'
    },
    'sco020': {
        'name': 'STE'
    },
    'uin010': {
        'name': 'STRATA Networks'
    },
    'sum010': {
        'name': 'Sumner Cable TV'
    },
    'pie010': {
        'name': 'Surry TV/PCSI TV'
    },
    'swa010': {
        'name': 'Swayzee Communications'
    },
    'sweetwater': {
        'name': 'Sweetwater Cable Television Co'
    },
    'weh010-talequah': {
        'name': 'Tahlequah Cable TV'
    },
    'tct': {
        'name': 'TCT'
    },
    'tel050': {
        'name': 'Tele-Media Company'
    },
    'com050': {
        'name': 'The Community Agency'
    },
    'thr020': {
        'name': 'Three River'
    },
    'cab140': {
        'name': 'Town & Country Technologies'
    },
    'tra010': {
        'name': 'Trans-Video'
    },
    'tre010': {
        'name': 'Trenton TV Cable Company'
    },
    'tcc': {
        'name': 'Tri County Communications Cooperative'
    },
    'tri025': {
        'name': 'TriCounty Telecom'
    },
    'tri110': {
        'name': 'TrioTel Communications, Inc.'
    },
    'tro010': {
        'name': 'Troy Cablevision, Inc.'
    },
    'tsc': {
        'name': 'TSC'
    },
    'cit220': {
        'name': 'Tullahoma Utilities Board'
    },
    'tvc030': {
        'name': 'TV Cable of Rensselaer'
    },
    'tvc015': {
        'name': 'TVC Cable'
    },
    'cab180': {
        'name': 'TVision'
    },
    'twi040': {
        'name': 'Twin Lakes'
    },
    'tvtinc': {
        'name': 'Twin Valley'
    },
    'uis010': {
        'name': 'Union Telephone Company'
    },
    'uni110': {
        'name': 'United Communications - TN'
    },
    'uni120': {
        'name': 'United Services'
    },
    'uss020': {
        'name': 'US Sonet'
    },
    'cab060': {
        'name': 'USA Communications'
    },
    'she005': {
        'name': 'USA Communications/Shellsburg, IA'
    },
    'val040': {
        'name': 'Valley TeleCom Group'
    },
    'val025': {
        'name': 'Valley Telecommunications'
    },
    'val030': {
        'name': 'Valparaiso Broadband'
    },
    'cla050': {
        'name': 'Vast Broadband'
    },
    'sul015': {
        'name': 'Venture Communications Cooperative, Inc.'
    },
    'ver025': {
        'name': 'Vernon Communications Co-op'
    },
    'weh010-vicksburg': {
        'name': 'Vicksburg Video'
    },
    'vis070': {
        'name': 'Vision Communications'
    },
    'volcanotel': {
        'name': 'Volcano Vision, Inc.'
    },
    'vol040-02': {
        'name': 'VolFirst / BLTV'
    },
    'ver070': {
        'name': 'VTel'
    },
    'nttcvtx010': {
        'name': 'VTX1'
    },
    'bci010-02': {
        'name': 'Vyve Broadband'
    },
    'wab020': {
        'name': 'Wabash Mutual Telephone'
    },
    'waitsfield': {
        'name': 'Waitsfield Cable'
    },
    'wal010': {
        'name': 'Walnut Communications'
    },
    'wavebroadband': {
        'name': 'Wave'
    },
    'wav030': {
        'name': 'Waverly Communications Utility'
    },
    'wbi010': {
        'name': 'WBI'
    },
    'web020': {
        'name': 'Webster-Calhoun Cooperative Telephone Association'
    },
    'wes005': {
        'name': 'West Alabama TV Cable'
    },
    'carolinata': {
        'name': 'West Carolina Communications'
    },
    'wct010': {
        'name': 'West Central Telephone Association'
    },
    'wes110': {
        'name': 'West River Cooperative Telephone Company'
    },
    'ani030': {
        'name': 'WesTel Systems'
    },
    'westianet': {
        'name': 'Western Iowa Networks'
    },
    'nttcwhi010': {
        'name': 'Whidbey Telecom'
    },
    'weh010-white': {
        'name': 'White County Cable TV'
    },
    'wes130': {
        'name': 'Wiatel'
    },
    'wik010': {
        'name': 'Wiktel'
    },
    'wil070': {
        'name': 'Wilkes Communications, Inc./RiverStreet Networks'
    },
    'wil015': {
        'name': 'Wilson Communications'
    },
    'win010': {
        'name': 'Windomnet/SMBS'
    },
    'win090': {
        'name': 'Windstream Cable TV'
    },
    'wcta': {
        'name': 'Winnebago Cooperative Telecom Association'
    },
    'wtc010': {
        'name': 'WTC'
    },
    'wil040': {
        'name': 'WTC Communications, Inc.'
    },
    'wya010': {
        'name': 'Wyandotte Cable'
    },
    'hin020-02': {
        'name': 'X-Stream Services'
    },
    'xit010': {
        'name': 'XIT Communications'
    },
    'yel010': {
        'name': 'Yelcot Communications'
    },
    'mid180-01': {
        'name': 'yondoo'
    },
    'cou060': {
        'name': 'Zito Media'
    },
    'slingtv': {
        'name': 'Sling TV',
        'username_field': 'username',
        'password_field': 'password',
    },
    'Suddenlink': {
        'name': 'Suddenlink',
        'username_field': 'username',
        'password_field': 'password',
    },
    'AlticeOne': {
        'name': 'Optimum TV',
        'username_field': 'j_username',
        'password_field': 'j_password',
    },
}

class AdobePassIE(InfoExtractor):
    _SERVICE_PROVIDER_TEMPLATE = 'https://sp.auth.adobe.com/adobe-services/%s'
    _USER_AGENT = 'Mozilla/5.0 (X11; Linux i686; rv:47.0) Gecko/20100101 Firefox/47.0'
    _MVPD_CACHE = 'ap-mvpd'

    _DOWNLOADING_LOGIN_PAGE = 'Downloading Provider Login Page'

    def _download_webpage_handle(self, *args, **kwargs):
        headers = self.geo_verification_headers()
        headers.update(kwargs.get('headers', {}))
        kwargs['headers'] = headers
        return super(AdobePassIE, self)._download_webpage_handle(
            *args, **kwargs)

    @staticmethod
    def _get_mvpd_resource(provider_id, title, guid, rating):
        channel = etree.Element('channel')
        channel_title = etree.SubElement(channel, 'title')
        channel_title.text = provider_id
        item = etree.SubElement(channel, 'item')
        resource_title = etree.SubElement(item, 'title')
        resource_title.text = title
        resource_guid = etree.SubElement(item, 'guid')
        resource_guid.text = guid
        resource_rating = etree.SubElement(item, 'media:rating')
        resource_rating.attrib = {'scheme': 'urn:v-chip'}
        resource_rating.text = rating
        return '<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">' + etree.tostring(channel).decode() + '</rss>'

    def _extract_mvpd_auth(self, url, video_id, requestor_id, resource):
        def xml_text(xml_str, tag):
            return self._search_regex(
                '<%s>(.+?)</%s>' % (tag, tag), xml_str, tag)

        def is_expired(token, date_ele):
            token_expires = unified_timestamp(re.sub(r'[_ ]GMT', '', xml_text(token, date_ele)))
            return token_expires and token_expires <= int(time.time())

        def post_form(form_page_res, note, data={}):
            form_page, urlh = form_page_res
            post_url = self._html_search_regex(r'<form[^>]+action=(["\'])(?P<url>.+?)\1', form_page, 'post url', group='url')
            if not re.match(r'https?://', post_url):
                post_url = compat_urlparse.urljoin(urlh.geturl(), post_url)
            form_data = self._hidden_inputs(form_page)
            form_data.update(data)
            return self._download_webpage_handle(
                post_url, video_id, note, data=urlencode_postdata(form_data), headers={
                    'Content-Type': 'application/x-www-form-urlencoded',
                })

        def raise_mvpd_required():
            raise ExtractorError(
                'This video is only available for users of participating TV providers. '
                'Use --ap-mso to specify Adobe Pass Multiple-system operator Identifier '
                'and --ap-username and --ap-password or --netrc to provide account credentials.', expected=True)

        def extract_redirect_url(html, url=None, fatal=False):
            # TODO: eliminate code duplication with generic extractor and move
            # redirection code into _download_webpage_handle
            REDIRECT_REGEX = r'[0-9]{,2};\s*(?:URL|url)=\'?([^\'"]+)'
            redirect_url = self._search_regex(
                r'(?i)<meta\s+(?=(?:[a-z-]+="[^"]+"\s+)*http-equiv="refresh")'
                r'(?:[a-z-]+="[^"]+"\s+)*?content="%s' % REDIRECT_REGEX,
                html, 'meta refresh redirect',
                default=NO_DEFAULT if fatal else None, fatal=fatal)
            if not redirect_url:
                return None
            if url:
                redirect_url = compat_urlparse.urljoin(url, unescapeHTML(redirect_url))
            return redirect_url

        mvpd_headers = {
            'ap_42': 'anonymous',
            'ap_11': 'Linux i686',
            'ap_z': self._USER_AGENT,
            'User-Agent': self._USER_AGENT,
        }

        guid = xml_text(resource, 'guid') if '<' in resource else resource
        count = 0
        while count < 2:
            requestor_info = self.cache.load(self._MVPD_CACHE, requestor_id) or {}
            authn_token = requestor_info.get('authn_token')
            if authn_token and is_expired(authn_token, 'simpleTokenExpires'):
                authn_token = None
            if not authn_token:
                mso_id = self.get_param('ap_mso')
                if mso_id:
                    username, password = self._get_login_info('ap_username', 'ap_password', mso_id)
                    if not username or not password:
                        raise_mvpd_required()
                    mso_info = MSO_INFO[mso_id]

                    provider_redirect_page_res = self._download_webpage_handle(
                        self._SERVICE_PROVIDER_TEMPLATE % 'authenticate/saml', video_id,
                        'Downloading Provider Redirect Page', query={
                            'noflash': 'true',
                            'mso_id': mso_id,
                            'requestor_id': requestor_id,
                            'no_iframe': 'false',
                            'domain_name': 'adobe.com',
                            'redirect_url': url,
                        })
                elif not self._cookies_passed:
                    raise_mvpd_required()

                if not mso_id:
                    pass
                elif mso_id == 'Comcast_SSO':
                    # Comcast page flow varies by video site and whether you
                    # are on Comcast's network.
                    provider_redirect_page, urlh = provider_redirect_page_res
                    if 'automatically signing you in' in provider_redirect_page:
                        oauth_redirect_url = self._html_search_regex(
                            r'window\.location\s*=\s*[\'"]([^\'"]+)',
                            provider_redirect_page, 'oauth redirect')
                        self._download_webpage(
                            oauth_redirect_url, video_id, 'Confirming auto login')
                    elif 'automatically signed in with' in provider_redirect_page:
                        # Seems like comcast is rolling up new way of automatically signing customers
                        oauth_redirect_url = self._html_search_regex(
                            r'continue:\s*"(https://oauth.xfinity.com/oauth/authorize\?.+)"', provider_redirect_page,
                            'oauth redirect (signed)')
                        # Just need to process the request. No useful data comes back
                        self._download_webpage(oauth_redirect_url, video_id, 'Confirming auto login')
                    else:
                        if '<form name="signin"' in provider_redirect_page:
                            provider_login_page_res = provider_redirect_page_res
                        elif 'http-equiv="refresh"' in provider_redirect_page:
                            oauth_redirect_url = extract_redirect_url(
                                provider_redirect_page, fatal=True)
                            provider_login_page_res = self._download_webpage_handle(
                                oauth_redirect_url, video_id,
                                self._DOWNLOADING_LOGIN_PAGE)
                        else:
                            provider_login_page_res = post_form(
                                provider_redirect_page_res,
                                self._DOWNLOADING_LOGIN_PAGE)

                        mvpd_confirm_page_res = post_form(
                            provider_login_page_res, 'Logging in', {
                                mso_info['username_field']: username,
                                mso_info['password_field']: password,
                            })
                        mvpd_confirm_page, urlh = mvpd_confirm_page_res
                        if '<button class="submit" value="Resume">Resume</button>' in mvpd_confirm_page:
                            post_form(mvpd_confirm_page_res, 'Confirming Login')
                elif mso_id == 'Philo':
                    # Philo has very unique authentication method
                    self._download_webpage(
                        'https://idp.philo.com/auth/init/login_code', video_id, 'Requesting auth code', data=urlencode_postdata({
                            'ident': username,
                            'device': 'web',
                            'send_confirm_link': False,
                            'send_token': True
                        }))
                    philo_code = getpass.getpass('Type auth code you have received [Return]: ')
                    self._download_webpage(
                        'https://idp.philo.com/auth/update/login_code', video_id, 'Submitting token', data=urlencode_postdata({
                            'token': philo_code
                        }))
                    mvpd_confirm_page_res = self._download_webpage_handle('https://idp.philo.com/idp/submit', video_id, 'Confirming Philo Login')
                    post_form(mvpd_confirm_page_res, 'Confirming Login')
                elif mso_id == 'Verizon':
                    # In general, if you're connecting from a Verizon-assigned IP,
                    # you will not actually pass your credentials.
                    provider_redirect_page, urlh = provider_redirect_page_res
                    # From non-Verizon IP, still gave 'Please wait', but noticed N==Y; will need to try on Verizon IP
                    if 'Please wait ...' in provider_redirect_page and '\'N\'== "Y"' not in provider_redirect_page:
                        saml_redirect_url = self._html_search_regex(
                            r'self\.parent\.location=(["\'])(?P<url>.+?)\1',
                            provider_redirect_page,
                            'SAML Redirect URL', group='url')
                        saml_login_page = self._download_webpage(
                            saml_redirect_url, video_id,
                            'Downloading SAML Login Page')
                    elif 'Verizon FiOS - sign in' in provider_redirect_page:
                        # FXNetworks from non-Verizon IP
                        saml_login_page_res = post_form(
                            provider_redirect_page_res, 'Logging in', {
                                mso_info['username_field']: username,
                                mso_info['password_field']: password,
                            })
                        saml_login_page, urlh = saml_login_page_res
                        if 'Please try again.' in saml_login_page:
                            raise ExtractorError(
                                'We\'re sorry, but either the User ID or Password entered is not correct.')
                    else:
                        # ABC from non-Verizon IP
                        saml_redirect_url = self._html_search_regex(
                            r'var\surl\s*=\s*(["\'])(?P<url>.+?)\1',
                            provider_redirect_page,
                            'SAML Redirect URL', group='url')
                        saml_redirect_url = saml_redirect_url.replace(r'\/', '/')
                        saml_redirect_url = saml_redirect_url.replace(r'\-', '-')
                        saml_redirect_url = saml_redirect_url.replace(r'\x26', '&')
                        saml_login_page = self._download_webpage(
                            saml_redirect_url, video_id,
                            'Downloading SAML Login Page')
                        saml_login_page, urlh = post_form(
                            [saml_login_page, saml_redirect_url], 'Logging in', {
                                mso_info['username_field']: username,
                                mso_info['password_field']: password,
                            })
                        if 'Please try again.' in saml_login_page:
                            raise ExtractorError(
                                'Failed to login, incorrect User ID or Password.')
                    saml_login_url = self._search_regex(
                        r'xmlHttp\.open\("POST"\s*,\s*(["\'])(?P<url>.+?)\1',
                        saml_login_page, 'SAML Login URL', group='url')
                    saml_response_json = self._download_json(
                        saml_login_url, video_id, 'Downloading SAML Response',
                        headers={'Content-Type': 'text/xml'})
                    self._download_webpage(
                        saml_response_json['targetValue'], video_id,
                        'Confirming Login', data=urlencode_postdata({
                            'SAMLResponse': saml_response_json['SAMLResponse'],
                            'RelayState': saml_response_json['RelayState']
                        }), headers={
                            'Content-Type': 'application/x-www-form-urlencoded'
                        })
                elif mso_id == 'Spectrum':
                    # Spectrum's login for is dynamically loaded via JS so we need to hardcode the flow
                    # as a one-off implementation.
                    provider_redirect_page, urlh = provider_redirect_page_res
                    provider_login_page_res = post_form(
                        provider_redirect_page_res, self._DOWNLOADING_LOGIN_PAGE)
                    saml_login_page, urlh = provider_login_page_res
                    relay_state = self._search_regex(
                        r'RelayState\s*=\s*"(?P<relay>.+?)";',
                        saml_login_page, 'RelayState', group='relay')
                    saml_request = self._search_regex(
                        r'SAMLRequest\s*=\s*"(?P<saml_request>.+?)";',
                        saml_login_page, 'SAMLRequest', group='saml_request')
                    login_json = {
                        mso_info['username_field']: username,
                        mso_info['password_field']: password,
                        'RelayState': relay_state,
                        'SAMLRequest': saml_request,
                    }
                    saml_response_json = self._download_json(
                        'https://tveauthn.spectrum.net/tveauthentication/api/v1/manualAuth', video_id,
                        'Downloading SAML Response',
                        data=json.dumps(login_json).encode(),
                        headers={
                            'Content-Type': 'application/json',
                            'Accept': 'application/json',
                        })
                    self._download_webpage(
                        saml_response_json['SAMLRedirectUri'], video_id,
                        'Confirming Login', data=urlencode_postdata({
                            'SAMLResponse': saml_response_json['SAMLResponse'],
                            'RelayState': relay_state,
                        }), headers={
                            'Content-Type': 'application/x-www-form-urlencoded'
                        })
                elif mso_id == 'slingtv':
                    # SlingTV has a meta-refresh based authentication, but also
                    # looks at the tab history to count the number of times the
                    # browser has been on a page

                    first_bookend_page, urlh = provider_redirect_page_res

                    hidden_data = self._hidden_inputs(first_bookend_page)
                    hidden_data['history'] = 1

                    provider_login_page_res = self._download_webpage_handle(
                        urlh.geturl(), video_id, 'Sending first bookend',
                        query=hidden_data)

                    provider_association_redirect, urlh = post_form(
                        provider_login_page_res, 'Logging in', {
                            mso_info['username_field']: username,
                            mso_info['password_field']: password
                        })

                    provider_refresh_redirect_url = extract_redirect_url(
                        provider_association_redirect, url=urlh.geturl())

                    last_bookend_page, urlh = self._download_webpage_handle(
                        provider_refresh_redirect_url, video_id,
                        'Downloading Auth Association Redirect Page')
                    hidden_data = self._hidden_inputs(last_bookend_page)
                    hidden_data['history'] = 3

                    mvpd_confirm_page_res = self._download_webpage_handle(
                        urlh.geturl(), video_id, 'Sending final bookend',
                        query=hidden_data)

                    post_form(mvpd_confirm_page_res, 'Confirming Login')
                elif mso_id == 'Suddenlink':
                    # Suddenlink is similar to SlingTV in using a tab history count and a meta refresh,
                    # but they also do a dynmaic redirect using javascript that has to be followed as well
                    first_bookend_page, urlh = post_form(
                        provider_redirect_page_res, 'Pressing Continue...')

                    hidden_data = self._hidden_inputs(first_bookend_page)
                    hidden_data['history_val'] = 1

                    provider_login_redirect_page_res = self._download_webpage_handle(
                        urlh.geturl(), video_id, 'Sending First Bookend',
                        query=hidden_data)

                    provider_login_redirect_page, urlh = provider_login_redirect_page_res

                    # Some website partners seem to not have the extra ajaxurl redirect step, so we check if we already
                    # have the login prompt or not
                    if 'id="password" type="password" name="password"' in provider_login_redirect_page:
                        provider_login_page_res = provider_login_redirect_page_res
                    else:
                        provider_tryauth_url = self._html_search_regex(
                            r'url:\s*[\'"]([^\'"]+)', provider_login_redirect_page, 'ajaxurl')
                        provider_tryauth_page = self._download_webpage(
                            provider_tryauth_url, video_id, 'Submitting TryAuth',
                            query=hidden_data)

                        provider_login_page_res = self._download_webpage_handle(
                            f'https://authorize.suddenlink.net/saml/module.php/authSynacor/login.php?AuthState={provider_tryauth_page}',
                            video_id, 'Getting Login Page',
                            query=hidden_data)

                    provider_association_redirect, urlh = post_form(
                        provider_login_page_res, 'Logging in', {
                            mso_info['username_field']: username,
                            mso_info['password_field']: password
                        })

                    provider_refresh_redirect_url = extract_redirect_url(
                        provider_association_redirect, url=urlh.geturl())

                    last_bookend_page, urlh = self._download_webpage_handle(
                        provider_refresh_redirect_url, video_id,
                        'Downloading Auth Association Redirect Page')

                    hidden_data = self._hidden_inputs(last_bookend_page)
                    hidden_data['history_val'] = 3

                    mvpd_confirm_page_res = self._download_webpage_handle(
                        urlh.geturl(), video_id, 'Sending Final Bookend',
                        query=hidden_data)

                    post_form(mvpd_confirm_page_res, 'Confirming Login')
                else:
                    # Some providers (e.g. DIRECTV NOW) have another meta refresh
                    # based redirect that should be followed.
                    provider_redirect_page, urlh = provider_redirect_page_res
                    provider_refresh_redirect_url = extract_redirect_url(
                        provider_redirect_page, url=urlh.geturl())
                    if provider_refresh_redirect_url:
                        provider_redirect_page_res = self._download_webpage_handle(
                            provider_refresh_redirect_url, video_id,
                            'Downloading Provider Redirect Page (meta refresh)')
                    provider_login_page_res = post_form(
                        provider_redirect_page_res, self._DOWNLOADING_LOGIN_PAGE)
                    form_data = {
                        mso_info.get('username_field', 'username'): username,
                        mso_info.get('password_field', 'password'): password
                    }
                    if mso_id in ('Cablevision', 'AlticeOne'):
                        form_data['_eventId_proceed'] = ''
                    mvpd_confirm_page_res = post_form(provider_login_page_res, 'Logging in', form_data)
                    if mso_id != 'Rogers':
                        post_form(mvpd_confirm_page_res, 'Confirming Login')

                try:
                    session = self._download_webpage(
                        self._SERVICE_PROVIDER_TEMPLATE % 'session', video_id,
                        'Retrieving Session', data=urlencode_postdata({
                            '_method': 'GET',
                            'requestor_id': requestor_id,
                        }), headers=mvpd_headers)
                except ExtractorError as e:
                    if not mso_id and isinstance(e.cause, urllib.error.HTTPError) and e.cause.code == 401:
                        raise_mvpd_required()
                    raise
                if '<pendingLogout' in session:
                    self.cache.store(self._MVPD_CACHE, requestor_id, {})
                    count += 1
                    continue
                authn_token = unescapeHTML(xml_text(session, 'authnToken'))
                requestor_info['authn_token'] = authn_token
                self.cache.store(self._MVPD_CACHE, requestor_id, requestor_info)

            authz_token = requestor_info.get(guid)
            if authz_token and is_expired(authz_token, 'simpleTokenTTL'):
                authz_token = None
            if not authz_token:
                authorize = self._download_webpage(
                    self._SERVICE_PROVIDER_TEMPLATE % 'authorize', video_id,
                    'Retrieving Authorization Token', data=urlencode_postdata({
                        'resource_id': resource,
                        'requestor_id': requestor_id,
                        'authentication_token': authn_token,
                        'mso_id': xml_text(authn_token, 'simpleTokenMsoID'),
                        'userMeta': '1',
                    }), headers=mvpd_headers)
                if '<pendingLogout' in authorize:
                    self.cache.store(self._MVPD_CACHE, requestor_id, {})
                    count += 1
                    continue
                if '<error' in authorize:
                    raise ExtractorError(xml_text(authorize, 'details'), expected=True)
                authz_token = unescapeHTML(xml_text(authorize, 'authzToken'))
                requestor_info[guid] = authz_token
                self.cache.store(self._MVPD_CACHE, requestor_id, requestor_info)

            mvpd_headers.update({
                'ap_19': xml_text(authn_token, 'simpleSamlNameID'),
                'ap_23': xml_text(authn_token, 'simpleSamlSessionIndex'),
            })

            short_authorize = self._download_webpage(
                self._SERVICE_PROVIDER_TEMPLATE % 'shortAuthorize',
                video_id, 'Retrieving Media Token', data=urlencode_postdata({
                    'authz_token': authz_token,
                    'requestor_id': requestor_id,
                    'session_guid': xml_text(authn_token, 'simpleTokenAuthenticationGuid'),
                    'hashed_guid': 'false',
                }), headers=mvpd_headers)
            if '<pendingLogout' in short_authorize:
                self.cache.store(self._MVPD_CACHE, requestor_id, {})
                count += 1
                continue
            return short_authorize


code:
import functools
import re

from .common import InfoExtractor
from ..compat import compat_str
from ..utils import (
    float_or_none,
    int_or_none,
    ISO639Utils,
    join_nonempty,
    OnDemandPagedList,
    parse_duration,
    str_or_none,
    str_to_int,
    unified_strdate,
)

class AdobeTVBaseIE(InfoExtractor):
    def _call_api(self, path, video_id, query, note=None):
        return self._download_json(
            'http://tv.adobe.com/api/v4/' + path,
            video_id, note, query=query)['data']

    def _parse_subtitles(self, video_data, url_key):
        subtitles = {}
        for translation in video_data.get('translations', []):
            vtt_path = translation.get(url_key)
            if not vtt_path:
                continue
            lang = translation.get('language_w3c') or ISO639Utils.long2short(translation['language_medium'])
            subtitles.setdefault(lang, []).append({
                'ext': 'vtt',
                'url': vtt_path,
            })
        return subtitles

    def _parse_video_data(self, video_data):
        video_id = compat_str(video_data['id'])
        title = video_data['title']

        s3_extracted = False
        formats = []
        for source in video_data.get('videos', []):
            source_url = source.get('url')
            if not source_url:
                continue
            f = {
                'format_id': source.get('quality_level'),
                'fps': int_or_none(source.get('frame_rate')),
                'height': int_or_none(source.get('height')),
                'tbr': int_or_none(source.get('video_data_rate')),
                'width': int_or_none(source.get('width')),
                'url': source_url,
            }
            original_filename = source.get('original_filename')
            if original_filename:
                if not (f.get('height') and f.get('width')):
                    mobj = re.search(r'_(\d+)x(\d+)', original_filename)
                    if mobj:
                        f.update({
                            'height': int(mobj.group(2)),
                            'width': int(mobj.group(1)),
                        })
                if original_filename.startswith('s3://') and not s3_extracted:
                    formats.append({
                        'format_id': 'original',
                        'quality': 1,
                        'url': original_filename.replace('s3://', 'https://s3.amazonaws.com/'),
                    })
                    s3_extracted = True
            formats.append(f)
        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': title,
            'description': video_data.get('description'),
            'thumbnail': video_data.get('thumbnail'),
            'upload_date': unified_strdate(video_data.get('start_date')),
            'duration': parse_duration(video_data.get('duration')),
            'view_count': str_to_int(video_data.get('playcount')),
            'formats': formats,
            'subtitles': self._parse_subtitles(video_data, 'vtt'),
        }

class AdobeTVEmbedIE(AdobeTVBaseIE):
    IE_NAME = 'adobetv:embed'
    _VALID_URL = r'https?://tv\.adobe\.com/embed/\d+/(?P<id>\d+)'
    _TEST = {
        'url': 'https://tv.adobe.com/embed/22/4153',
        'md5': 'c8c0461bf04d54574fc2b4d07ac6783a',
        'info_dict': {
            'id': '4153',
            'ext': 'flv',
            'title': 'Creating Graphics Optimized for BlackBerry',
            'description': 'md5:eac6e8dced38bdaae51cd94447927459',
            'thumbnail': r're:https?://.*\.jpg$',
            'upload_date': '20091109',
            'duration': 377,
            'view_count': int,
        },
    }

    def _real_extract(self, url):
        video_id = self._match_id(url)

        video_data = self._call_api(
            'episode/' + video_id, video_id, {'disclosure': 'standard'})[0]
        return self._parse_video_data(video_data)

class AdobeTVIE(AdobeTVBaseIE):
    IE_NAME = 'adobetv'
    _VALID_URL = r'https?://tv\.adobe\.com/(?:(?P<language>fr|de|es|jp)/)?watch/(?P<show_urlname>[^/]+)/(?P<id>[^/]+)'

    _TEST = {
        'url': 'http://tv.adobe.com/watch/the-complete-picture-with-julieanne-kost/quick-tip-how-to-draw-a-circle-around-an-object-in-photoshop/',
        'md5': '9bc5727bcdd55251f35ad311ca74fa1e',
        'info_dict': {
            'id': '10981',
            'ext': 'mp4',
            'title': 'Quick Tip - How to Draw a Circle Around an Object in Photoshop',
            'description': 'md5:99ec318dc909d7ba2a1f2b038f7d2311',
            'thumbnail': r're:https?://.*\.jpg$',
            'upload_date': '20110914',
            'duration': 60,
            'view_count': int,
        },
    }

    def _real_extract(self, url):
        language, show_urlname, urlname = self._match_valid_url(url).groups()
        if not language:
            language = 'en'

        video_data = self._call_api(
            'episode/get', urlname, {
                'disclosure': 'standard',
                'language': language,
                'show_urlname': show_urlname,
                'urlname': urlname,
            })[0]
        return self._parse_video_data(video_data)

class AdobeTVPlaylistBaseIE(AdobeTVBaseIE):
    _PAGE_SIZE = 25

    def _fetch_page(self, display_id, query, page):
        page += 1
        query['page'] = page
        for element_data in self._call_api(
                self._RESOURCE, display_id, query, 'Download Page %d' % page):
            yield self._process_data(element_data)

    def _extract_playlist_entries(self, display_id, query):
        return OnDemandPagedList(functools.partial(
            self._fetch_page, display_id, query), self._PAGE_SIZE)

class AdobeTVShowIE(AdobeTVPlaylistBaseIE):
    IE_NAME = 'adobetv:show'
    _VALID_URL = r'https?://tv\.adobe\.com/(?:(?P<language>fr|de|es|jp)/)?show/(?P<id>[^/]+)'

    _TEST = {
        'url': 'http://tv.adobe.com/show/the-complete-picture-with-julieanne-kost',
        'info_dict': {
            'id': '36',
            'title': 'The Complete Picture with Julieanne Kost',
            'description': 'md5:fa50867102dcd1aa0ddf2ab039311b27',
        },
        'playlist_mincount': 136,
    }
    _RESOURCE = 'episode'
    _process_data = AdobeTVBaseIE._parse_video_data

    def _real_extract(self, url):
        language, show_urlname = self._match_valid_url(url).groups()
        if not language:
            language = 'en'
        query = {
            'disclosure': 'standard',
            'language': language,
            'show_urlname': show_urlname,
        }

        show_data = self._call_api(
            'show/get', show_urlname, query)[0]

        return self.playlist_result(
            self._extract_playlist_entries(show_urlname, query),
            str_or_none(show_data.get('id')),
            show_data.get('show_name'),
            show_data.get('show_description'))

class AdobeTVChannelIE(AdobeTVPlaylistBaseIE):
    IE_NAME = 'adobetv:channel'
    _VALID_URL = r'https?://tv\.adobe\.com/(?:(?P<language>fr|de|es|jp)/)?channel/(?P<id>[^/]+)(?:/(?P<category_urlname>[^/]+))?'

    _TEST = {
        'url': 'http://tv.adobe.com/channel/development',
        'info_dict': {
            'id': 'development',
        },
        'playlist_mincount': 96,
    }
    _RESOURCE = 'show'

    def _process_data(self, show_data):
        return self.url_result(
            show_data['url'], 'AdobeTVShow', str_or_none(show_data.get('id')))

    def _real_extract(self, url):
        language, channel_urlname, category_urlname = self._match_valid_url(url).groups()
        if not language:
            language = 'en'
        query = {
            'channel_urlname': channel_urlname,
            'language': language,
        }
        if category_urlname:
            query['category_urlname'] = category_urlname

        return self.playlist_result(
            self._extract_playlist_entries(channel_urlname, query),
            channel_urlname)

class AdobeTVVideoIE(AdobeTVBaseIE):
    IE_NAME = 'adobetv:video'
    _VALID_URL = r'https?://video\.tv\.adobe\.com/v/(?P<id>\d+)'
    _EMBED_REGEX = [r'<iframe[^>]+src=[\'"](?P<url>(?:https?:)?//video\.tv\.adobe\.com/v/\d+[^"]+)[\'"]']

    _TEST = {
        # From https://helpx.adobe.com/acrobat/how-to/new-experience-acrobat-dc.html?set=acrobat--get-started--essential-beginners
        'url': 'https://video.tv.adobe.com/v/2456/',
        'md5': '43662b577c018ad707a63766462b1e87',
        'info_dict': {
            'id': '2456',
            'ext': 'mp4',
            'title': 'New experience with Acrobat DC',
            'description': 'New experience with Acrobat DC',
            'duration': 248.667,
        },
    }

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)

        video_data = self._parse_json(self._search_regex(
            r'var\s+bridge\s*=\s*([^;]+);', webpage, 'bridged data'), video_id)
        title = video_data['title']

        formats = []
        sources = video_data.get('sources') or []
        for source in sources:
            source_src = source.get('src')
            if not source_src:
                continue
            formats.append({
                'filesize': int_or_none(source.get('kilobytes') or None, invscale=1000),
                'format_id': join_nonempty(source.get('format'), source.get('label')),
                'height': int_or_none(source.get('height') or None),
                'tbr': int_or_none(source.get('bitrate') or None),
                'width': int_or_none(source.get('width') or None),
                'url': source_src,
            })
        self._sort_formats(formats)

        # For both metadata and downloaded files the duration varies among
        # formats. I just pick the max one
        duration = max(filter(None, [
            float_or_none(source.get('duration'), scale=1000)
            for source in sources]))

        return {
            'id': video_id,
            'formats': formats,
            'title': title,
            'description': video_data.get('description'),
            'thumbnail': video_data.get('video', {}).get('poster'),
            'duration': duration,
            'subtitles': self._parse_subtitles(video_data, 'vttPath'),
        }

repo_name: Lesmiscore/ytdl


code:
import json

from .turner import TurnerBaseIE
from ..utils import (
    determine_ext,
    float_or_none,
    int_or_none,
    mimetype2ext,
    parse_age_limit,
    parse_iso8601,
    strip_or_none,
    try_get,
)

class AdultSwimIE(TurnerBaseIE):
    _VALID_URL = r'https?://(?:www\.)?adultswim\.com/videos/(?P<show_path>[^/?#]+)(?:/(?P<episode_path>[^/?#]+))?'

    _TESTS = [{
        'url': 'http://adultswim.com/videos/rick-and-morty/pilot',
        'info_dict': {
            'id': 'rQxZvXQ4ROaSOqq-or2Mow',
            'ext': 'mp4',
            'title': 'Rick and Morty - Pilot',
            'description': 'Rick moves in with his daughter\'s family and establishes himself as a bad influence on his grandson, Morty.',
            'timestamp': 1543294800,
            'upload_date': '20181127',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
        'expected_warnings': ['Unable to download f4m manifest'],
    }, {
        'url': 'http://www.adultswim.com/videos/tim-and-eric-awesome-show-great-job/dr-steve-brule-for-your-wine/',
        'info_dict': {
            'id': 'sY3cMUR_TbuE4YmdjzbIcQ',
            'ext': 'mp4',
            'title': 'Tim and Eric Awesome Show Great Job! - Dr. Steve Brule, For Your Wine',
            'description': 'Dr. Brule reports live from Wine Country with a special report on wines.  \nWatch Tim and Eric Awesome Show Great Job! episode #20, "Embarrassed" on Adult Swim.',
            'upload_date': '20080124',
            'timestamp': 1201150800,
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
        'skip': '404 Not Found',
    }, {
        'url': 'http://www.adultswim.com/videos/decker/inside-decker-a-new-hero/',
        'info_dict': {
            'id': 'I0LQFQkaSUaFp8PnAWHhoQ',
            'ext': 'mp4',
            'title': 'Decker - Inside Decker: A New Hero',
            'description': 'The guys recap the conclusion of the season. They announce a new hero, take a peek into the Victorville Film Archive and welcome back the talented James Dean.',
            'timestamp': 1469480460,
            'upload_date': '20160725',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
        'expected_warnings': ['Unable to download f4m manifest'],
    }, {
        'url': 'http://www.adultswim.com/videos/attack-on-titan',
        'info_dict': {
            'id': 'attack-on-titan',
            'title': 'Attack on Titan',
            'description': 'md5:41caa9416906d90711e31dc00cb7db7e',
        },
        'playlist_mincount': 12,
    }, {
        'url': 'http://www.adultswim.com/videos/streams/williams-stream',
        'info_dict': {
            'id': 'd8DEBj7QRfetLsRgFnGEyg',
            'ext': 'mp4',
            'title': r're:^Williams Stream \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'description': 'original programming',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
        'skip': '404 Not Found',
    }]

    def _real_extract(self, url):
        show_path, episode_path = self._match_valid_url(url).groups()
        display_id = episode_path or show_path
        query = '''query {
  getShowBySlug(slug:"%s") {
    %%s
  }
}''' % show_path
        if episode_path:
            query = query % '''title
    getVideoBySlug(slug:"%s") {
      _id
      auth
      description
      duration
      episodeNumber
      launchDate
      mediaID
      seasonNumber
      poster
      title
      tvRating
    }''' % episode_path
            ['getVideoBySlug']
        else:
            query = query % '''metaDescription
    title
    videos(first:1000,sort:["episode_number"]) {
      edges {
        node {
           _id
           slug
        }
      }
    }'''
        show_data = self._download_json(
            'https://www.adultswim.com/api/search', display_id,
            data=json.dumps({'query': query}).encode(),
            headers={'Content-Type': 'application/json'})['data']['getShowBySlug']
        if episode_path:
            video_data = show_data['getVideoBySlug']
            video_id = video_data['_id']
            episode_title = title = video_data['title']
            series = show_data.get('title')
            if series:
                title = '%s - %s' % (series, title)
            info = {
                'id': video_id,
                'title': title,
                'description': strip_or_none(video_data.get('description')),
                'duration': float_or_none(video_data.get('duration')),
                'formats': [],
                'subtitles': {},
                'age_limit': parse_age_limit(video_data.get('tvRating')),
                'thumbnail': video_data.get('poster'),
                'timestamp': parse_iso8601(video_data.get('launchDate')),
                'series': series,
                'season_number': int_or_none(video_data.get('seasonNumber')),
                'episode': episode_title,
                'episode_number': int_or_none(video_data.get('episodeNumber')),
            }

            auth = video_data.get('auth')
            media_id = video_data.get('mediaID')
            if media_id:
                info.update(self._extract_ngtv_info(media_id, {
                    # CDN_TOKEN_APP_ID from:
                    # https://d2gg02c3xr550i.cloudfront.net/assets/asvp.e9c8bef24322d060ef87.bundle.js
                    'appId': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6ImFzLXR2ZS1kZXNrdG9wLXB0enQ2bSIsInByb2R1Y3QiOiJ0dmUiLCJuZXR3b3JrIjoiYXMiLCJwbGF0Zm9ybSI6ImRlc2t0b3AiLCJpYXQiOjE1MzI3MDIyNzl9.BzSCk-WYOZ2GMCIaeVb8zWnzhlgnXuJTCu0jGp_VaZE',
                }, {
                    'url': url,
                    'site_name': 'AdultSwim',
                    'auth_required': auth,
                }))

            if not auth:
                extract_data = self._download_json(
                    'https://www.adultswim.com/api/shows/v1/videos/' + video_id,
                    video_id, query={'fields': 'stream'}, fatal=False) or {}
                assets = try_get(extract_data, lambda x: x['data']['video']['stream']['assets'], list) or []
                for asset in assets:
                    asset_url = asset.get('url')
                    if not asset_url:
                        continue
                    ext = determine_ext(asset_url, mimetype2ext(asset.get('mime_type')))
                    if ext == 'm3u8':
                        info['formats'].extend(self._extract_m3u8_formats(
                            asset_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
                    elif ext == 'f4m':
                        continue
                        # info['formats'].extend(self._extract_f4m_formats(
                        #     asset_url, video_id, f4m_id='hds', fatal=False))
                    elif ext in ('scc', 'ttml', 'vtt'):
                        info['subtitles'].setdefault('en', []).append({
                            'url': asset_url,
                        })
            self._sort_formats(info['formats'])

            return info
        else:
            entries = []
            for edge in show_data.get('videos', {}).get('edges', []):
                video = edge.get('node') or {}
                slug = video.get('slug')
                if not slug:
                    continue
                entries.append(self.url_result(
                    'http://adultswim.com/videos/%s/%s' % (show_path, slug),
                    'AdultSwim', video.get('_id')))
            return self.playlist_result(
                entries, show_path, show_data.get('title'),
                strip_or_none(show_data.get('metaDescription')))


code:
from .theplatform import ThePlatformIE
from ..utils import (
    ExtractorError,
    GeoRestrictedError,
    int_or_none,
    update_url_query,
    urlencode_postdata,
)

class AENetworksBaseIE(ThePlatformIE):
    _BASE_URL_REGEX = r'''(?x)https?://
        (?:(?:www|play|watch)\.)?
        (?P<domain>
            (?:history(?:vault)?|aetv|mylifetime|lifetimemovieclub)\.com|
            fyi\.tv
        )/'''
    _THEPLATFORM_KEY = '43jXaGRQud'
    _THEPLATFORM_SECRET = 'S10BPXHMlb'
    _DOMAIN_MAP = {
        'history.com': ('HISTORY', 'history'),
        'aetv.com': ('AETV', 'aetv'),
        'mylifetime.com': ('LIFETIME', 'lifetime'),
        'lifetimemovieclub.com': ('LIFETIMEMOVIECLUB', 'lmc'),
        'fyi.tv': ('FYI', 'fyi'),
        'historyvault.com': (None, 'historyvault'),
        'biography.com': (None, 'biography'),
    }

    def _extract_aen_smil(self, smil_url, video_id, auth=None):
        query = {
            'mbr': 'true',
            'formats': 'M3U+none,MPEG-DASH+none,MPEG4,MP3',
        }
        if auth:
            query['auth'] = auth
        TP_SMIL_QUERY = [{
            'assetTypes': 'high_video_ak',
            'switch': 'hls_high_ak',
        }, {
            'assetTypes': 'high_video_s3',
        }, {
            'assetTypes': 'high_video_s3',
            'switch': 'hls_high_fastly',
        }]
        formats = []
        subtitles = {}
        last_e = None
        for q in TP_SMIL_QUERY:
            q.update(query)
            m_url = update_url_query(smil_url, q)
            m_url = self._sign_url(m_url, self._THEPLATFORM_KEY, self._THEPLATFORM_SECRET)
            try:
                tp_formats, tp_subtitles = self._extract_theplatform_smil(
                    m_url, video_id, 'Downloading %s SMIL data' % (q.get('switch') or q['assetTypes']))
            except ExtractorError as e:
                if isinstance(e, GeoRestrictedError):
                    raise
                last_e = e
                continue
            formats.extend(tp_formats)
            subtitles = self._merge_subtitles(subtitles, tp_subtitles)
        if last_e and not formats:
            raise last_e
        self._sort_formats(formats)
        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
        }

    def _extract_aetn_info(self, domain, filter_key, filter_value, url):
        requestor_id, brand = self._DOMAIN_MAP[domain]
        result = self._download_json(
            'https://feeds.video.aetnd.com/api/v2/%s/videos' % brand,
            filter_value, query={'filter[%s]' % filter_key: filter_value})['results'][0]
        title = result['title']
        video_id = result['id']
        media_url = result['publicUrl']
        theplatform_metadata = self._download_theplatform_metadata(self._search_regex(
            r'https?://link\.theplatform\.com/s/([^?]+)', media_url, 'theplatform_path'), video_id)
        info = self._parse_theplatform_metadata(theplatform_metadata)
        auth = None
        if theplatform_metadata.get('AETN$isBehindWall'):
            resource = self._get_mvpd_resource(
                requestor_id, theplatform_metadata['title'],
                theplatform_metadata.get('AETN$PPL_pplProgramId') or theplatform_metadata.get('AETN$PPL_pplProgramId_OLD'),
                theplatform_metadata['ratings'][0]['rating'])
            auth = self._extract_mvpd_auth(
                url, video_id, requestor_id, resource)
        info.update(self._extract_aen_smil(media_url, video_id, auth))
        info.update({
            'title': title,
            'series': result.get('seriesName'),
            'season_number': int_or_none(result.get('tvSeasonNumber')),
            'episode_number': int_or_none(result.get('tvSeasonEpisodeNumber')),
        })
        return info

class AENetworksIE(AENetworksBaseIE):
    IE_NAME = 'aenetworks'
    IE_DESC = 'A+E Networks: A&E, Lifetime, History.com, FYI Network and History Vault'
    _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'''(?P<id>
        shows/[^/]+/season-\d+/episode-\d+|
        (?:
            (?:movie|special)s/[^/]+|
            (?:shows/[^/]+/)?videos
        )/[^/?#&]+
    )'''
    _TESTS = [{
        'url': 'http://www.history.com/shows/mountain-men/season-1/episode-1',
        'info_dict': {
            'id': '22253814',
            'ext': 'mp4',
            'title': 'Winter is Coming',
            'description': 'md5:641f424b7a19d8e24f26dea22cf59d74',
            'timestamp': 1338306241,
            'upload_date': '20120529',
            'uploader': 'AENE-NEW',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
        'add_ie': ['ThePlatform'],
        'skip': 'This video is only available for users of participating TV providers.',
    }, {
        'url': 'http://www.aetv.com/shows/duck-dynasty/season-9/episode-1',
        'info_dict': {
            'id': '600587331957',
            'ext': 'mp4',
            'title': 'Inlawful Entry',
            'description': 'md5:57c12115a2b384d883fe64ca50529e08',
            'timestamp': 1452634428,
            'upload_date': '20160112',
            'uploader': 'AENE-NEW',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
        'add_ie': ['ThePlatform'],
    }, {
        'url': 'http://www.fyi.tv/shows/tiny-house-nation/season-1/episode-8',
        'only_matching': True
    }, {
        'url': 'http://www.mylifetime.com/shows/project-runway-junior/season-1/episode-6',
        'only_matching': True
    }, {
        'url': 'http://www.mylifetime.com/movies/center-stage-on-pointe/full-movie',
        'only_matching': True
    }, {
        'url': 'https://watch.lifetimemovieclub.com/movies/10-year-reunion/full-movie',
        'only_matching': True
    }, {
        'url': 'http://www.history.com/specials/sniper-into-the-kill-zone/full-special',
        'only_matching': True
    }, {
        'url': 'https://www.aetv.com/specials/hunting-jonbenets-killer-the-untold-story/preview-hunting-jonbenets-killer-the-untold-story',
        'only_matching': True
    }, {
        'url': 'http://www.history.com/videos/history-of-valentines-day',
        'only_matching': True
    }, {
        'url': 'https://play.aetv.com/shows/duck-dynasty/videos/best-of-duck-dynasty-getting-quack-in-shape',
        'only_matching': True
    }]

    def _real_extract(self, url):
        domain, canonical = self._match_valid_url(url).groups()
        return self._extract_aetn_info(domain, 'canonical', '/' + canonical, url)

class AENetworksListBaseIE(AENetworksBaseIE):
    def _call_api(self, resource, slug, brand, fields):
        return self._download_json(
            'https://yoga.appsvcs.aetnd.com/graphql',
            slug, query={'brand': brand}, data=urlencode_postdata({
                'query': '''{
  %s(slug: "%s") {
    %s
  }
}''' % (resource, slug, fields),
            }))['data'][resource]

    def _real_extract(self, url):
        domain, slug = self._match_valid_url(url).groups()
        _, brand = self._DOMAIN_MAP[domain]
        playlist = self._call_api(self._RESOURCE, slug, brand, self._FIELDS)
        base_url = 'http://watch.%s' % domain

        entries = []
        for item in (playlist.get(self._ITEMS_KEY) or []):
            doc = self._get_doc(item)
            canonical = doc.get('canonical')
            if not canonical:
                continue
            entries.append(self.url_result(
                base_url + canonical, AENetworksIE.ie_key(), doc.get('id')))

        description = None
        if self._PLAYLIST_DESCRIPTION_KEY:
            description = playlist.get(self._PLAYLIST_DESCRIPTION_KEY)

        return self.playlist_result(
            entries, playlist.get('id'),
            playlist.get(self._PLAYLIST_TITLE_KEY), description)

class AENetworksCollectionIE(AENetworksListBaseIE):
    IE_NAME = 'aenetworks:collection'
    _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'(?:[^/]+/)*(?:list|collections)/(?P<id>[^/?#&]+)/?(?:[?#&]|$)'
    _TESTS = [{
        'url': 'https://watch.historyvault.com/list/america-the-story-of-us',
        'info_dict': {
            'id': '282',
            'title': 'America The Story of Us',
        },
        'playlist_mincount': 12,
    }, {
        'url': 'https://watch.historyvault.com/shows/america-the-story-of-us-2/season-1/list/america-the-story-of-us',
        'only_matching': True
    }, {
        'url': 'https://www.historyvault.com/collections/mysteryquest',
        'only_matching': True
    }]
    _RESOURCE = 'list'
    _ITEMS_KEY = 'items'
    _PLAYLIST_TITLE_KEY = 'display_title'
    _PLAYLIST_DESCRIPTION_KEY = None
    _FIELDS = '''id
    display_title
    items {
      ... on ListVideoItem {
        doc {
          canonical
          id
        }
      }
    }'''

    def _get_doc(self, item):
        return item.get('doc') or {}

class AENetworksShowIE(AENetworksListBaseIE):
    IE_NAME = 'aenetworks:show'
    _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'shows/(?P<id>[^/?#&]+)/?(?:[?#&]|$)'
    _TESTS = [{
        'url': 'http://www.history.com/shows/ancient-aliens',
        'info_dict': {
            'id': 'SERIES1574',
            'title': 'Ancient Aliens',
            'description': 'md5:3f6d74daf2672ff3ae29ed732e37ea7f',
        },
        'playlist_mincount': 150,
    }]
    _RESOURCE = 'series'
    _ITEMS_KEY = 'episodes'
    _PLAYLIST_TITLE_KEY = 'title'
    _PLAYLIST_DESCRIPTION_KEY = 'description'
    _FIELDS = '''description
    id
    title
    episodes {
      canonical
      id
    }'''

    def _get_doc(self, item):
        return item

class HistoryTopicIE(AENetworksBaseIE):
    IE_NAME = 'history:topic'
    IE_DESC = 'History.com Topic'
    _VALID_URL = r'https?://(?:www\.)?history\.com/topics/[^/]+/(?P<id>[\w+-]+?)-video'
    _TESTS = [{
        'url': 'https://www.history.com/topics/valentines-day/history-of-valentines-day-video',
        'info_dict': {
            'id': '40700995724',
            'ext': 'mp4',
            'title': "History of Valentine’s Day",
            'description': 'md5:7b57ea4829b391995b405fa60bd7b5f7',
            'timestamp': 1375819729,
            'upload_date': '20130806',
            'uploader': 'AENE-NEW',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
        'add_ie': ['ThePlatform'],
    }]

    def _real_extract(self, url):
        display_id = self._match_id(url)
        return self.url_result(
            'http://www.history.com/videos/' + display_id,
            AENetworksIE.ie_key())

class HistoryPlayerIE(AENetworksBaseIE):
    IE_NAME = 'history:player'
    _VALID_URL = r'https?://(?:www\.)?(?P<domain>(?:history|biography)\.com)/player/(?P<id>\d+)'
    _TESTS = []

    def _real_extract(self, url):
        domain, video_id = self._match_valid_url(url).groups()
        return self._extract_aetn_info(domain, 'id', video_id, url)

class BiographyIE(AENetworksBaseIE):
    _VALID_URL = r'https?://(?:www\.)?biography\.com/video/(?P<id>[^/?#&]+)'
    _TESTS = [{
        'url': 'https://www.biography.com/video/vincent-van-gogh-full-episode-2075049808',
        'info_dict': {
            'id': '30322987',
            'ext': 'mp4',
            'title': 'Vincent Van Gogh - Full Episode',
            'description': 'A full biography about the most influential 20th century painter, Vincent Van Gogh.',
            'timestamp': 1311970571,
            'upload_date': '20110729',
            'uploader': 'AENE-NEW',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
        'add_ie': ['ThePlatform'],
    }]

    def _real_extract(self, url):
        display_id = self._match_id(url)
        webpage = self._download_webpage(url, display_id)
        player_url = self._search_regex(
            r'<phoenix-iframe[^>]+src="(%s)' % HistoryPlayerIE._VALID_URL,
            webpage, 'player URL')
        return self.url_result(player_url, HistoryPlayerIE.ie_key())