<s> '''<STR_LIT>''' <EOL> import re <EOL> import json <EOL> import urllib <EOL> import urllib2 <EOL> import urlparse <EOL> import xbmc <EOL> import xbmcgui <EOL> import xbmcplugin <EOL> import xbmcvfs <EOL> import os <EOL> import inspect <EOL> def download ( name , image , url ) : <EOL> from resources . lib . libraries import control <EOL> if url == None : <EOL> return control . infoDialog ( control . lang ( <NUM_LIT> ) . encode ( '<STR_LIT:utf-8>' ) ) <EOL> try : headers = dict ( urlparse . parse_qsl ( url . rsplit ( '<STR_LIT:|>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] ) ) <EOL> except : headers = dict ( '<STR_LIT>' ) <EOL> url = url . split ( '<STR_LIT:|>' ) [ <NUM_LIT:0> ] <EOL> content = re . compile ( '<STR_LIT>' ) . findall ( name ) <EOL> transname = name . translate ( None , '<STR_LIT>' ) . strip ( '<STR_LIT:.>' ) <EOL> levels = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:..>' ] <EOL> if len ( content ) == <NUM_LIT:0> : <EOL> dest = control . setting ( '<STR_LIT>' ) <EOL> dest = control . transPath ( dest ) <EOL> for level in levels : <EOL> try : control . makeFile ( os . path . abspath ( os . path . join ( dest , level ) ) ) <EOL> except : pass <EOL> control . makeFile ( dest ) <EOL> dest = os . path . join ( dest , transname ) <EOL> control . makeFile ( dest ) <EOL> else : <EOL> dest = control . setting ( '<STR_LIT>' ) <EOL> dest = control . transPath ( dest ) <EOL> for level in levels : <EOL> try : control . makeFile ( os . path . abspath ( os . path . join ( dest , level ) ) ) <EOL> except : pass <EOL> control . makeFile ( dest ) <EOL> transtvshowtitle = content [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . translate ( None , '<STR_LIT>' ) . strip ( '<STR_LIT:.>' ) <EOL> dest = os . path . join ( dest , transtvshowtitle ) <EOL> control . makeFile ( dest ) <EOL> dest = os . path . join ( dest , '<STR_LIT>' % int ( content [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) ) <EOL> control . makeFile ( dest ) <EOL> ext = os . path . splitext ( urlparse . urlparse ( url ) . path ) [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> if not ext in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : ext = '<STR_LIT>' <EOL> dest = os . path . join ( dest , transname + '<STR_LIT:.>' + ext ) <EOL> sysheaders = urllib . quote_plus ( json . dumps ( headers ) ) <EOL> sysurl = urllib . quote_plus ( url ) <EOL> systitle = urllib . quote_plus ( name ) <EOL> sysimage = urllib . quote_plus ( image ) <EOL> sysdest = urllib . quote_plus ( dest ) <EOL> script = inspect . getfile ( inspect . currentframe ( ) ) <EOL> cmd = '<STR_LIT>' % ( script , sysurl , sysdest , systitle , sysimage , sysheaders ) <EOL> xbmc . executebuiltin ( cmd ) <EOL> def getResponse ( url , headers , size ) : <EOL> try : <EOL> if size > <NUM_LIT:0> : <EOL> size = int ( size ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' % size <EOL> req = urllib2 . Request ( url , headers = headers ) <EOL> resp = urllib2 . urlopen ( req , timeout = <NUM_LIT:30> ) <EOL> return resp <EOL> except : <EOL> return None <EOL> def done ( title , dest , downloaded ) : <EOL> playing = xbmc . Player ( ) . isPlaying ( ) <EOL> text = xbmcgui . Window ( <NUM_LIT> ) . getProperty ( '<STR_LIT>' ) <EOL> if len ( text ) > <NUM_LIT:0> : <EOL> text += '<STR_LIT>' <EOL> if downloaded : <EOL> text += '<STR_LIT>' % ( dest . rsplit ( os . sep ) [ - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> else : <EOL> text += '<STR_LIT>' % ( dest . rsplit ( os . sep ) [ - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> xbmcgui . Window ( <NUM_LIT> ) . setProperty ( '<STR_LIT>' , text ) <EOL> if ( not downloaded ) or ( not playing ) : <EOL> xbmcgui . Dialog ( ) . ok ( title , text ) <EOL> xbmcgui . Window ( <NUM_LIT> ) . clearProperty ( '<STR_LIT>' ) <EOL> def doDownload ( url , dest , title , image , headers ) : <EOL> headers = json . loads ( urllib . unquote_plus ( headers ) ) <EOL> url = urllib . unquote_plus ( url ) <EOL> title = urllib . unquote_plus ( title ) <EOL> image = urllib . unquote_plus ( image ) <EOL> dest = urllib . unquote_plus ( dest ) <EOL> file = dest . rsplit ( os . sep , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> resp = getResponse ( url , headers , <NUM_LIT:0> ) <EOL> if not resp : <EOL> xbmcgui . Dialog ( ) . ok ( title , dest , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return <EOL> try : content = int ( resp . headers [ '<STR_LIT>' ] ) <EOL> except : content = <NUM_LIT:0> <EOL> try : resumable = '<STR_LIT>' in resp . headers [ '<STR_LIT>' ] . lower ( ) <EOL> except : resumable = False <EOL> if resumable : <EOL> print "<STR_LIT>" <EOL> if content < <NUM_LIT:1> : <EOL> xbmcgui . Dialog ( ) . ok ( title , file , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return <EOL> size = <NUM_LIT> * <NUM_LIT> <EOL> mb = content / ( <NUM_LIT> * <NUM_LIT> ) <EOL> if content < size : <EOL> size = content <EOL> total = <NUM_LIT:0> <EOL> notify = <NUM_LIT:0> <EOL> errors = <NUM_LIT:0> <EOL> count = <NUM_LIT:0> <EOL> resume = <NUM_LIT:0> <EOL> sleep = <NUM_LIT:0> <EOL> if xbmcgui . Dialog ( ) . yesno ( title + '<STR_LIT>' , file , '<STR_LIT>' % mb , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT:1> : <EOL> return <EOL> print '<STR_LIT>' % ( mb , dest ) <EOL> f = xbmcvfs . File ( dest , '<STR_LIT:w>' ) <EOL> chunk = None <EOL> chunks = [ ] <EOL> while True : <EOL> downloaded = total <EOL> for c in chunks : <EOL> downloaded += len ( c ) <EOL> percent = min ( <NUM_LIT:100> * downloaded / content , <NUM_LIT:100> ) <EOL> if percent >= notify : <EOL> xbmc . executebuiltin ( "<STR_LIT>" % ( title + '<STR_LIT>' + str ( percent ) + '<STR_LIT:%>' , dest , <NUM_LIT> , image ) ) <EOL> print '<STR_LIT>' % ( str ( percent ) + '<STR_LIT:%>' , dest , mb , downloaded / <NUM_LIT> , content / <NUM_LIT> ) <EOL> notify += <NUM_LIT:10> <EOL> chunk = None <EOL> error = False <EOL> try : <EOL> chunk = resp . read ( size ) <EOL> if not chunk : <EOL> if percent < <NUM_LIT> : <EOL> error = True <EOL> else : <EOL> while len ( chunks ) > <NUM_LIT:0> : <EOL> c = chunks . pop ( <NUM_LIT:0> ) <EOL> f . write ( c ) <EOL> del c <EOL> f . close ( ) <EOL> print '<STR_LIT>' % ( dest ) <EOL> return done ( title , dest , True ) <EOL> except Exception , e : <EOL> print str ( e ) <EOL> error = True <EOL> sleep = <NUM_LIT:10> <EOL> errno = <NUM_LIT:0> <EOL> if hasattr ( e , '<STR_LIT>' ) : <EOL> errno = e . errno <EOL> if errno == <NUM_LIT> : <EOL> pass <EOL> if errno == <NUM_LIT> : <EOL> errors = <NUM_LIT:10> <EOL> sleep = <NUM_LIT:30> <EOL> if errno == <NUM_LIT> : <EOL> errors = <NUM_LIT:10> <EOL> sleep = <NUM_LIT:30> <EOL> if chunk : <EOL> errors = <NUM_LIT:0> <EOL> chunks . append ( chunk ) <EOL> if len ( chunks ) > <NUM_LIT:5> : <EOL> c = chunks . pop ( <NUM_LIT:0> ) <EOL> f . write ( c ) <EOL> total += len ( c ) <EOL> del c <EOL> if error : <EOL> errors += <NUM_LIT:1> <EOL> count += <NUM_LIT:1> <EOL> print '<STR_LIT>' % ( count , dest ) <EOL> xbmc . sleep ( sleep * <NUM_LIT:1000> ) <EOL> if ( resumable and errors > <NUM_LIT:0> ) or errors >= <NUM_LIT:10> : <EOL> if ( not resumable and resume >= <NUM_LIT:50> ) or resume >= <NUM_LIT> : <EOL> print '<STR_LIT>' % ( dest ) <EOL> return done ( title , dest , False ) <EOL> resume += <NUM_LIT:1> <EOL> errors = <NUM_LIT:0> <EOL> if resumable : <EOL> chunks = [ ] <EOL> print '<STR_LIT>' % ( resume , dest ) <EOL> resp = getResponse ( url , headers , total ) <EOL> else : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if '<STR_LIT>' in sys . argv [ <NUM_LIT:0> ] : <EOL> doDownload ( sys . argv [ <NUM_LIT:1> ] , sys . argv [ <NUM_LIT:2> ] , sys . argv [ <NUM_LIT:3> ] , sys . argv [ <NUM_LIT:4> ] , sys . argv [ <NUM_LIT:5> ] ) </s>
