<s> """<STR_LIT>""" <EOL> import base64 <EOL> import re <EOL> import time <EOL> import urllib <EOL> import urllib2 <EOL> import sys <EOL> import traceback <EOL> import socket <EOL> from SocketServer import ThreadingMixIn <EOL> from BaseHTTPServer import HTTPServer , BaseHTTPRequestHandler <EOL> from urllib import * <EOL> import urlparse <EOL> from f4mDownloader import F4MDownloader <EOL> from interalSimpleDownloader import interalSimpleDownloader <EOL> from hlsDownloader import HLSDownloader <EOL> import xbmc <EOL> import thread <EOL> import zlib <EOL> from StringIO import StringIO <EOL> import hmac <EOL> import hashlib <EOL> import base64 <EOL> import threading <EOL> import xbmcgui , xbmcplugin <EOL> import xbmc <EOL> import hashlib <EOL> g_stopEvent = None <EOL> g_downloader = None <EOL> class MyHandler ( BaseHTTPRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def do_HEAD ( self ) : <EOL> print "<STR_LIT>" <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> rtype = "<STR_LIT>" <EOL> self . end_headers ( ) <EOL> """<STR_LIT>""" <EOL> def do_GET ( s ) : <EOL> print "<STR_LIT>" <EOL> s . answer_request ( True ) <EOL> def answer_request ( self , sendData ) : <EOL> global g_stopEvent <EOL> global g_downloader <EOL> try : <EOL> request_path = self . path [ <NUM_LIT:1> : ] <EOL> request_path = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , request_path ) <EOL> if request_path . lower ( ) == "<STR_LIT>" : <EOL> sys . exit ( ) <EOL> return <EOL> if request_path . lower ( ) == "<STR_LIT>" : <EOL> print '<STR_LIT>' <EOL> self . wfile . close ( ) <EOL> return <EOL> ( url , proxy , use_proxy_for_chunks , maxbitrate , simpledownloader , auth , streamtype ) = self . decode_url ( request_path ) <EOL> print '<STR_LIT>' , simpledownloader <EOL> if streamtype == '<STR_LIT>' or streamtype == None or streamtype == '<STR_LIT:none>' : streamtype = '<STR_LIT>' <EOL> if streamtype == '<STR_LIT>' : <EOL> print '<STR_LIT>' , url , proxy , use_proxy_for_chunks , maxbitrate <EOL> downloader = None <EOL> if not downloader or downloader . live == True or not ( downloader . init_done and downloader . init_url == url ) : <EOL> downloader = F4MDownloader ( ) <EOL> if not downloader . init ( self . wfile , url , proxy , use_proxy_for_chunks , g_stopEvent , maxbitrate , auth ) : <EOL> print '<STR_LIT>' <EOL> return <EOL> g_downloader = downloader <EOL> print '<STR_LIT>' <EOL> enableSeek = False <EOL> requested_range = self . headers . getheader ( "<STR_LIT>" ) <EOL> if requested_range == None : requested_range = "<STR_LIT>" <EOL> srange , erange = ( None , None ) <EOL> if downloader . live == False and len ( requested_range ) > <NUM_LIT:0> and not requested_range == "<STR_LIT>" : <EOL> enableSeek = True <EOL> ( srange , erange ) = self . get_range_request ( requested_range , downloader . total_frags ) <EOL> print '<STR_LIT>' , downloader . live , enableSeek , requested_range , downloader . total_frags , srange , erange <EOL> enableSeek = False <EOL> framgementToSend = <NUM_LIT:0> <EOL> inflate = <NUM_LIT> <EOL> if enableSeek : <EOL> self . send_response ( <NUM_LIT> ) <EOL> rtype = "<STR_LIT>" <EOL> self . send_header ( "<STR_LIT:Content-Type>" , rtype ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> print '<STR_LIT>' , downloader . total_frags <EOL> totalsize = downloader . total_frags * inflate <EOL> framgementToSend = <NUM_LIT:1> <EOL> erange = srange + framgementToSend * inflate <EOL> if erange >= totalsize : <EOL> erange = totalsize - <NUM_LIT:1> <EOL> crange = "<STR_LIT>" + str ( srange ) + "<STR_LIT:->" + str ( int ( erange ) ) + "<STR_LIT>" <EOL> print srange / inflate , erange / inflate , totalsize / inflate <EOL> self . send_header ( "<STR_LIT>" , str ( totalsize ) ) <EOL> self . send_header ( "<STR_LIT>" , crange ) <EOL> etag = self . generate_ETag ( url ) <EOL> self . send_header ( "<STR_LIT>" , etag ) <EOL> print crange <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . send_header ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> else : <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> rtype = "<STR_LIT>" <EOL> self . send_header ( "<STR_LIT:Content-Type>" , rtype ) <EOL> srange = None <EOL> elif streamtype == '<STR_LIT>' or simpledownloader : <EOL> downloader = interalSimpleDownloader ( ) ; <EOL> if not downloader . init ( self . wfile , url , proxy , g_stopEvent , maxbitrate ) : <EOL> print '<STR_LIT>' <EOL> return <EOL> srange , framgementToSend = ( None , None ) <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> rtype = "<STR_LIT>" <EOL> self . send_header ( "<STR_LIT:Content-Type>" , rtype ) <EOL> srange = None <EOL> elif streamtype == '<STR_LIT>' or simpledownloader : <EOL> downloader = HLSDownloader ( ) <EOL> if not downloader . init ( self . wfile , url , proxy , use_proxy_for_chunks , g_stopEvent , maxbitrate , auth ) : <EOL> print '<STR_LIT>' <EOL> return <EOL> srange , framgementToSend = ( None , None ) <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> rtype = "<STR_LIT>" <EOL> self . send_header ( "<STR_LIT:Content-Type>" , rtype ) <EOL> srange = None <EOL> self . end_headers ( ) <EOL> if not srange == None : <EOL> srange = srange / inflate <EOL> if sendData : <EOL> downloader . keep_sending_video ( self . wfile , srange , framgementToSend ) <EOL> print '<STR_LIT>' , srange , framgementToSend <EOL> xbmc . sleep ( <NUM_LIT> ) <EOL> while not downloader . status == "<STR_LIT>" : <EOL> xbmc . sleep ( <NUM_LIT:200> ) ; <EOL> except : <EOL> traceback . print_exc ( ) <EOL> g_stopEvent . set ( ) <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . wfile . close ( ) <EOL> return <EOL> self . wfile . close ( ) <EOL> return <EOL> def generate_ETag ( self , url ) : <EOL> md = hashlib . md5 ( ) <EOL> md . update ( url ) <EOL> return md . hexdigest ( ) <EOL> def get_range_request ( self , hrange , file_size ) : <EOL> if hrange == None : <EOL> srange = <NUM_LIT:0> <EOL> erange = None <EOL> else : <EOL> try : <EOL> hrange = str ( hrange ) <EOL> splitRange = hrange . split ( "<STR_LIT:=>" ) [ <NUM_LIT:1> ] . split ( "<STR_LIT:->" ) <EOL> srange = int ( splitRange [ <NUM_LIT:0> ] ) <EOL> erange = splitRange [ <NUM_LIT:1> ] <EOL> if erange == "<STR_LIT>" : <EOL> erange = int ( file_size ) - <NUM_LIT:1> <EOL> except : <EOL> srange = <NUM_LIT:0> <EOL> erange = int ( file_size - <NUM_LIT:1> ) ; <EOL> return ( srange , erange ) <EOL> def decode_url ( self , url ) : <EOL> print '<STR_LIT>' <EOL> params = urlparse . parse_qs ( url ) <EOL> print '<STR_LIT>' , params <EOL> received_url = params [ '<STR_LIT:url>' ] [ <NUM_LIT:0> ] <EOL> use_proxy_for_chunks = False <EOL> proxy = None <EOL> try : <EOL> proxy = params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> use_proxy_for_chunks = params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> except : pass <EOL> maxbitrate = <NUM_LIT:0> <EOL> try : <EOL> maxbitrate = int ( params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> except : pass <EOL> auth = None <EOL> try : <EOL> auth = params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> except : pass <EOL> if auth == '<STR_LIT:None>' and auth == '<STR_LIT>' : <EOL> auth = None <EOL> if proxy == '<STR_LIT:None>' or proxy == '<STR_LIT>' : <EOL> proxy = None <EOL> if use_proxy_for_chunks == '<STR_LIT:False>' : <EOL> use_proxy_for_chunks = False <EOL> simpledownloader = False <EOL> try : <EOL> simpledownloader = params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if simpledownloader . lower ( ) == '<STR_LIT:true>' : <EOL> print '<STR_LIT>' , params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> simpledownloader = True <EOL> else : <EOL> simpledownloader = False <EOL> except : pass <EOL> streamtype = '<STR_LIT>' <EOL> try : <EOL> streamtype = params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> except : pass <EOL> if streamtype == '<STR_LIT:None>' and streamtype == '<STR_LIT>' : streamtype = '<STR_LIT>' <EOL> return ( received_url , proxy , use_proxy_for_chunks , maxbitrate , simpledownloader , auth , streamtype ) <EOL> """<STR_LIT>""" <EOL> class Server ( HTTPServer ) : <EOL> """<STR_LIT>""" <EOL> def get_request ( self ) : <EOL> """<STR_LIT>""" <EOL> self . socket . settimeout ( <NUM_LIT> ) <EOL> result = None <EOL> while result is None : <EOL> try : <EOL> result = self . socket . accept ( ) <EOL> except socket . timeout : <EOL> pass <EOL> result [ <NUM_LIT:0> ] . settimeout ( <NUM_LIT:1000> ) <EOL> return result <EOL> class ThreadedHTTPServer ( ThreadingMixIn , Server ) : <EOL> """<STR_LIT>""" <EOL> HOST_NAME = '<STR_LIT:127.0.0.1>' <EOL> PORT_NUMBER = <NUM_LIT> <EOL> class f4mProxy ( ) : <EOL> def start ( self , stopEvent , port = PORT_NUMBER ) : <EOL> global PORT_NUMBER <EOL> global HOST_NAME <EOL> global g_stopEvent <EOL> print '<STR_LIT:port>' , port , '<STR_LIT>' , HOST_NAME <EOL> g_stopEvent = stopEvent <EOL> socket . setdefaulttimeout ( <NUM_LIT:10> ) <EOL> server_class = ThreadedHTTPServer <EOL> MyHandler . protocol_version = "<STR_LIT>" <EOL> httpd = server_class ( ( HOST_NAME , port ) , MyHandler ) <EOL> print "<STR_LIT>" % ( HOST_NAME , port ) <EOL> while ( True and not stopEvent . isSet ( ) ) : <EOL> httpd . handle_request ( ) <EOL> httpd . server_close ( ) <EOL> print "<STR_LIT>" % ( HOST_NAME , port ) <EOL> def prepare_url ( self , url , proxy = None , use_proxy_for_chunks = True , port = PORT_NUMBER , maxbitrate = <NUM_LIT:0> , simpleDownloader = False , auth = None , streamtype = '<STR_LIT>' ) : <EOL> global PORT_NUMBER <EOL> global PORT_NUMBER <EOL> newurl = urllib . urlencode ( { '<STR_LIT:url>' : url , '<STR_LIT>' : proxy , '<STR_LIT>' : use_proxy_for_chunks , '<STR_LIT>' : maxbitrate , '<STR_LIT>' : simpleDownloader , '<STR_LIT>' : auth , '<STR_LIT>' : streamtype } ) <EOL> link = '<STR_LIT>' + HOST_NAME + ( '<STR_LIT>' % str ( port ) ) + newurl <EOL> return ( link ) <EOL> class f4mProxyHelper ( ) : <EOL> def playF4mLink ( self , url , name , proxy = None , use_proxy_for_chunks = False , maxbitrate = <NUM_LIT:0> , simpleDownloader = False , auth = None , streamtype = '<STR_LIT>' , setResolved = False ) : <EOL> print "<STR_LIT>" + url <EOL> stopPlaying = threading . Event ( ) <EOL> progress = xbmcgui . DialogProgress ( ) <EOL> f4m_proxy = f4mProxy ( ) <EOL> stopPlaying . clear ( ) <EOL> runningthread = thread . start_new_thread ( f4m_proxy . start , ( stopPlaying , ) ) <EOL> progress . create ( '<STR_LIT>' ) <EOL> stream_delay = <NUM_LIT:1> <EOL> progress . update ( <NUM_LIT:20> , "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" ) <EOL> xbmc . sleep ( stream_delay * <NUM_LIT:1000> ) <EOL> progress . update ( <NUM_LIT:100> , "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" ) <EOL> url_to_play = f4m_proxy . prepare_url ( url , proxy , use_proxy_for_chunks , maxbitrate = maxbitrate , simpleDownloader = simpleDownloader , auth = auth , streamtype = streamtype ) <EOL> listitem = xbmcgui . ListItem ( name , path = url_to_play ) <EOL> listitem . setInfo ( '<STR_LIT>' , { '<STR_LIT>' : name } ) <EOL> if setResolved : <EOL> return url_to_play , listitem <EOL> mplayer = MyPlayer ( ) <EOL> mplayer . stopPlaying = stopPlaying <EOL> progress . close ( ) <EOL> mplayer . play ( url_to_play , listitem ) <EOL> firstTime = True <EOL> while True : <EOL> if stopPlaying . isSet ( ) : <EOL> break ; <EOL> xbmc . log ( '<STR_LIT>' ) <EOL> xbmc . sleep ( <NUM_LIT:200> ) <EOL> stopPlaying . isSet ( ) <EOL> print '<STR_LIT>' <EOL> return <EOL> def start_proxy ( self , url , name , proxy = None , use_proxy_for_chunks = False , maxbitrate = <NUM_LIT:0> , simpleDownloader = False , auth = None , streamtype = '<STR_LIT>' ) : <EOL> print "<STR_LIT>" + url <EOL> stopPlaying = threading . Event ( ) <EOL> f4m_proxy = f4mProxy ( ) <EOL> stopPlaying . clear ( ) <EOL> runningthread = thread . start_new_thread ( f4m_proxy . start , ( stopPlaying , ) ) <EOL> stream_delay = <NUM_LIT:1> <EOL> xbmc . sleep ( stream_delay * <NUM_LIT:1000> ) <EOL> url_to_play = f4m_proxy . prepare_url ( url , proxy , use_proxy_for_chunks , maxbitrate = maxbitrate , simpleDownloader = simpleDownloader , auth = auth , streamtype = streamtype ) <EOL> return url_to_play , stopPlaying <EOL> class MyPlayer ( xbmc . Player ) : <EOL> def __init__ ( self ) : <EOL> xbmc . Player . __init__ ( self ) <EOL> def play ( self , url , listitem ) : <EOL> print '<STR_LIT>' % url <EOL> self . stopPlaying . clear ( ) <EOL> xbmc . Player ( xbmc . PLAYER_CORE_DVDPLAYER ) . play ( url , listitem ) <EOL> def onPlayBackEnded ( self ) : <EOL> print "<STR_LIT>" <EOL> self . stopPlaying . set ( ) ; <EOL> print "<STR_LIT>" <EOL> def onPlayBackStopped ( self ) : <EOL> print "<STR_LIT>" <EOL> self . stopPlaying . set ( ) ; <EOL> print "<STR_LIT>" </s>
