<s> import re <EOL> from uuid import uuid4 <EOL> try : <EOL> import xml . etree . cElementTree as ET <EOL> except ImportError : <EOL> import xml . etree . ElementTree as ET <EOL> from livestreamer . plugin import Plugin <EOL> from livestreamer . plugin . api import http <EOL> from livestreamer . stream import RTMPStream <EOL> from livestreamer . compat import urlparse <EOL> ITV_PLAYER_USER_AGENT = '<STR_LIT>' <EOL> LIVE_SWF_URL = "<STR_LIT>" <EOL> ONDEMAND_SWF_URL = "<STR_LIT>" <EOL> CHANNEL_MAP = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT:7> } <EOL> _url_re = re . compile ( r"<STR_LIT>" ) <EOL> class ITVPlayer ( Plugin ) : <EOL> def __init__ ( self , url ) : <EOL> Plugin . __init__ ( self , url ) <EOL> match = _url_re . match ( url ) <EOL> self . _stream = match and match . groupdict ( ) [ "<STR_LIT>" ] <EOL> @ classmethod <EOL> def can_handle_url ( self , url ) : <EOL> match = _url_re . match ( url ) <EOL> return match <EOL> @ property <EOL> def channel_id ( self ) : <EOL> if self . _stream in CHANNEL_MAP : <EOL> return CHANNEL_MAP [ self . _stream ] <EOL> @ property <EOL> def production_id ( self ) : <EOL> if self . _stream not in CHANNEL_MAP : <EOL> res = http . get ( self . url , verify = False ) <EOL> production_id_match = re . findall ( r'<STR_LIT>' , res . text , flags = re . DOTALL ) <EOL> if production_id_match : <EOL> return production_id_match [ <NUM_LIT:0> ] <EOL> def _get_streams ( self ) : <EOL> """<STR_LIT>""" <EOL> soap_message = self . _soap_request ( ) <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' . format ( len ( soap_message ) ) , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : ITV_PLAYER_USER_AGENT , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> res = http . post ( "<STR_LIT>" , <EOL> headers = headers , <EOL> data = soap_message ) <EOL> xmldoc = http . xml ( res ) <EOL> mediafiles = xmldoc . find ( "<STR_LIT>" ) <EOL> if not mediafiles : <EOL> return None <EOL> rtmp = mediafiles . attrib [ '<STR_LIT>' ] <EOL> streams = { } <EOL> for mediafile in mediafiles . findall ( "<STR_LIT>" ) : <EOL> playpath = mediafile . find ( "<STR_LIT>" ) . text <EOL> rtmp_url = urlparse ( rtmp ) <EOL> app = ( rtmp_url . path [ <NUM_LIT:1> : ] + '<STR_LIT:?>' + rtmp_url . query ) . rstrip ( '<STR_LIT:?>' ) <EOL> live = app == "<STR_LIT>" <EOL> params = dict ( rtmp = "<STR_LIT>" . format ( u = rtmp_url ) , <EOL> app = app . rstrip ( '<STR_LIT:?>' ) , <EOL> playpath = playpath , <EOL> swfVfy = LIVE_SWF_URL if live else ONDEMAND_SWF_URL , <EOL> timeout = <NUM_LIT:10> ) <EOL> if live : <EOL> params [ '<STR_LIT>' ] = True <EOL> bitrate = int ( mediafile . attrib [ '<STR_LIT>' ] ) / <NUM_LIT:1000> <EOL> quality = "<STR_LIT>" . format ( bitrate ) <EOL> streams [ quality ] = RTMPStream ( self . session , params ) <EOL> return streams <EOL> def _soap_request ( self ) : <EOL> def sub_ns ( parent , tag , ns ) : <EOL> return ET . SubElement ( parent , "<STR_LIT>" % ( ns , tag ) ) <EOL> def sub_common ( parent , tag ) : <EOL> return sub_ns ( parent , tag , "<STR_LIT>" ) <EOL> def sub_soap ( parent , tag ) : <EOL> return sub_ns ( parent , tag , "<STR_LIT>" ) <EOL> def sub_item ( parent , tag ) : <EOL> return sub_ns ( parent , tag , "<STR_LIT>" ) <EOL> def sub_itv ( parent , tag ) : <EOL> return sub_ns ( parent , tag , "<STR_LIT>" ) <EOL> production_id = self . production_id <EOL> channel_id = self . channel_id <EOL> ET . register_namespace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ET . register_namespace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ET . register_namespace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ET . register_namespace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> root = ET . Element ( "<STR_LIT>" ) <EOL> sub_soap ( root , "<STR_LIT>" ) <EOL> body = sub_soap ( root , "<STR_LIT>" ) <EOL> get_playlist = sub_item ( body , "<STR_LIT>" ) <EOL> request = sub_item ( get_playlist , "<STR_LIT>" ) <EOL> prode = sub_itv ( request , "<STR_LIT>" ) <EOL> if production_id : <EOL> prode . text = production_id <EOL> sub_itv ( request , "<STR_LIT>" ) . text = str ( uuid4 ( ) ) . upper ( ) <EOL> vodcrid = sub_itv ( request , "<STR_LIT>" ) <EOL> vod_id = sub_common ( vodcrid , "<STR_LIT>" ) <EOL> sub_common ( vodcrid , "<STR_LIT>" ) . text = "<STR_LIT>" <EOL> if channel_id : <EOL> vod_id . text = "<STR_LIT>" . format ( channel_id ) <EOL> userinfo = sub_item ( get_playlist , "<STR_LIT>" ) <EOL> sub_itv ( userinfo , "<STR_LIT>" ) . text = "<STR_LIT>" <EOL> sub_itv ( userinfo , "<STR_LIT>" ) . text = "<STR_LIT>" <EOL> sub_itv ( userinfo , "<STR_LIT>" ) <EOL> sub_itv ( userinfo , "<STR_LIT>" ) <EOL> sub_itv ( userinfo , "<STR_LIT>" ) <EOL> siteinfo = sub_item ( get_playlist , "<STR_LIT>" ) <EOL> sub_itv ( siteinfo , "<STR_LIT>" ) . text = "<STR_LIT:None>" <EOL> sub_itv ( siteinfo , "<STR_LIT>" ) . text = "<STR_LIT>" <EOL> sub_itv ( siteinfo , "<STR_LIT>" ) . text = "<STR_LIT>" <EOL> sub_itv ( siteinfo , <EOL> "<STR_LIT>" ) . text = "<STR_LIT>" <EOL> sub_itv ( siteinfo , "<STR_LIT>" ) <EOL> sub_itv ( siteinfo , "<STR_LIT>" ) . text = "<STR_LIT>" <EOL> sub_itv ( siteinfo , "<STR_LIT>" ) . text = "<STR_LIT>" <EOL> deviceinfo = sub_item ( get_playlist , "<STR_LIT>" ) <EOL> sub_itv ( deviceinfo , "<STR_LIT>" ) . text = "<STR_LIT>" <EOL> playerinfo = sub_item ( get_playlist , "<STR_LIT>" ) <EOL> sub_itv ( playerinfo , "<STR_LIT>" ) . text = "<STR_LIT:2>" <EOL> return ET . tostring ( root ) <EOL> __plugin__ = ITVPlayer </s>
