<s> import xml . etree . ElementTree as ET <EOL> import json <EOL> import re <EOL> from svtplay_dl . log import log <EOL> from svtplay_dl . utils import is_py2 , is_py3 , decode_html_entities <EOL> from svtplay_dl . utils . io import StringIO <EOL> from svtplay_dl . output import output <EOL> from requests import Session <EOL> from requests import __build__ as requests_version <EOL> import platform <EOL> class subtitle ( object ) : <EOL> def __init__ ( self , options , subtype , url ) : <EOL> self . url = url <EOL> self . subtitle = None <EOL> self . options = options <EOL> self . subtype = subtype <EOL> self . http = Session ( ) <EOL> def download ( self ) : <EOL> subdata = self . http . request ( "<STR_LIT>" , self . url , cookies = self . options . cookies ) <EOL> data = None <EOL> if self . subtype == "<STR_LIT>" : <EOL> data = self . tt ( subdata ) <EOL> if self . subtype == "<STR_LIT>" : <EOL> data = self . json ( subdata ) <EOL> if self . subtype == "<STR_LIT>" : <EOL> data = self . sami ( subdata ) <EOL> if self . subtype == "<STR_LIT>" : <EOL> data = self . smi ( subdata ) <EOL> if self . subtype == "<STR_LIT>" : <EOL> data = self . wrst ( subdata ) <EOL> if self . subtype == "<STR_LIT>" : <EOL> if is_py2 : <EOL> data = subdata . text . encode ( "<STR_LIT:utf-8>" ) <EOL> else : <EOL> data = subdata . text <EOL> if platform . system ( ) == "<STR_LIT>" and is_py3 : <EOL> file_d = output ( self . options , "<STR_LIT>" , mode = "<STR_LIT>" , encoding = "<STR_LIT:utf-8>" ) <EOL> else : <EOL> file_d = output ( self . options , "<STR_LIT>" , mode = "<STR_LIT>" ) <EOL> if hasattr ( file_d , "<STR_LIT>" ) is False : <EOL> return <EOL> file_d . write ( data ) <EOL> file_d . close ( ) <EOL> def tt ( self , subdata ) : <EOL> i = <NUM_LIT:1> <EOL> data = "<STR_LIT>" <EOL> if is_py2 : <EOL> subs = subdata . text . encode ( "<STR_LIT:utf8>" ) <EOL> else : <EOL> subs = subdata . text <EOL> subdata = re . sub ( '<STR_LIT>' , '<STR_LIT>' , subs , count = <NUM_LIT:1> ) <EOL> tree = ET . XML ( subdata ) <EOL> xml = tree . find ( "<STR_LIT:body>" ) . find ( "<STR_LIT>" ) <EOL> plist = list ( xml . findall ( "<STR_LIT:p>" ) ) <EOL> for node in plist : <EOL> tag = norm ( node . tag ) <EOL> if tag == "<STR_LIT:p>" or tag == "<STR_LIT>" : <EOL> begin = node . attrib [ "<STR_LIT>" ] <EOL> if not ( "<STR_LIT>" in node . attrib ) : <EOL> duration = node . attrib [ "<STR_LIT>" ] <EOL> else : <EOL> duration = node . attrib [ "<STR_LIT>" ] <EOL> if not ( "<STR_LIT:end>" in node . attrib ) : <EOL> begin2 = begin . split ( "<STR_LIT::>" ) <EOL> duration2 = duration . split ( "<STR_LIT::>" ) <EOL> try : <EOL> sec = float ( begin2 [ <NUM_LIT:2> ] ) + float ( duration2 [ <NUM_LIT:2> ] ) <EOL> except ValueError : <EOL> sec = <NUM_LIT> <EOL> end = "<STR_LIT>" % ( int ( begin [ <NUM_LIT:0> ] ) , int ( begin [ <NUM_LIT:1> ] ) , sec ) <EOL> else : <EOL> end = node . attrib [ "<STR_LIT:end>" ] <EOL> data += '<STR_LIT>' % ( i , begin . replace ( "<STR_LIT:.>" , "<STR_LIT:U+002C>" ) , end . replace ( "<STR_LIT:.>" , "<STR_LIT:U+002C>" ) ) <EOL> data = tt_text ( node , data ) <EOL> data += "<STR_LIT:\n>" <EOL> i += <NUM_LIT:1> <EOL> if is_py2 : <EOL> data = data . encode ( "<STR_LIT:utf8>" ) <EOL> return data <EOL> def json ( self , subdata ) : <EOL> data = json . loads ( subdata . text ) <EOL> number = <NUM_LIT:1> <EOL> subs = "<STR_LIT>" <EOL> for i in data : <EOL> subs += "<STR_LIT>" % ( number , timestr ( int ( i [ "<STR_LIT>" ] ) ) , timestr ( int ( i [ "<STR_LIT>" ] ) ) ) <EOL> if is_py2 : <EOL> subs += "<STR_LIT>" % i [ "<STR_LIT:text>" ] . encode ( "<STR_LIT:utf-8>" ) <EOL> else : <EOL> subs += "<STR_LIT>" % i [ "<STR_LIT:text>" ] <EOL> number += <NUM_LIT:1> <EOL> return subs <EOL> def sami ( self , subdata ) : <EOL> tree = ET . XML ( subdata . text . encode ( "<STR_LIT:utf8>" ) ) <EOL> subt = tree . find ( "<STR_LIT>" ) <EOL> subs = "<STR_LIT>" <EOL> n = <NUM_LIT:0> <EOL> for i in subt . getiterator ( ) : <EOL> if i . tag == "<STR_LIT>" : <EOL> n = i . attrib [ "<STR_LIT>" ] <EOL> if i . attrib [ "<STR_LIT>" ] == "<STR_LIT:1>" : <EOL> subs += "<STR_LIT>" % ( i . attrib [ "<STR_LIT>" ] , timecolon ( i . attrib [ "<STR_LIT>" ] ) , timecolon ( i . attrib [ "<STR_LIT>" ] ) ) <EOL> else : <EOL> subs += "<STR_LIT>" % ( i . attrib [ "<STR_LIT>" ] , timecolon ( i . attrib [ "<STR_LIT>" ] ) , timecolon ( i . attrib [ "<STR_LIT>" ] ) ) <EOL> else : <EOL> if int ( n ) > <NUM_LIT:0> : <EOL> subs += "<STR_LIT>" % i . text <EOL> if is_py2 : <EOL> subs = subs . encode ( '<STR_LIT:utf8>' ) <EOL> return subs <EOL> def smi ( self , subdata ) : <EOL> if requests_version < <NUM_LIT> : <EOL> if is_py2 : <EOL> subdata = subdata . content <EOL> else : <EOL> subdata = subdata . content . decode ( "<STR_LIT>" ) <EOL> else : <EOL> subdata . encoding = "<STR_LIT>" <EOL> subdata = subdata . text <EOL> ssubdata = StringIO ( subdata ) <EOL> timea = <NUM_LIT:0> <EOL> number = <NUM_LIT:1> <EOL> data = None <EOL> subs = "<STR_LIT>" <EOL> TAG_RE = re . compile ( r'<STR_LIT>' ) <EOL> bad_char = re . compile ( r'<STR_LIT>' ) <EOL> for i in ssubdata . readlines ( ) : <EOL> i = i . rstrip ( ) <EOL> sync = re . search ( r"<STR_LIT>" , i ) <EOL> if sync : <EOL> if int ( sync . group ( <NUM_LIT:1> ) ) != int ( timea ) : <EOL> if data and data != "<STR_LIT>" : <EOL> subs += "<STR_LIT>" % ( number , timestr ( timea ) , timestr ( sync . group ( <NUM_LIT:1> ) ) ) <EOL> text = "<STR_LIT>" % TAG_RE . sub ( '<STR_LIT>' , data . replace ( "<STR_LIT>" , "<STR_LIT:\n>" ) ) <EOL> if text [ len ( text ) - <NUM_LIT:2> ] != "<STR_LIT:\n>" : <EOL> text += "<STR_LIT:\n>" <EOL> subs += text <EOL> number += <NUM_LIT:1> <EOL> timea = sync . group ( <NUM_LIT:1> ) <EOL> text = re . search ( "<STR_LIT>" , i ) <EOL> if text : <EOL> data = text . group ( <NUM_LIT:1> ) <EOL> recomp = re . compile ( r'<STR_LIT:\r>' ) <EOL> text = bad_char . sub ( '<STR_LIT:->' , recomp . sub ( '<STR_LIT>' , subs ) ) . replace ( '<STR_LIT>' , '<STR_LIT:">' ) <EOL> if is_py2 and isinstance ( text , unicode ) : <EOL> return text . encode ( "<STR_LIT:utf-8>" ) <EOL> return text <EOL> def wrst ( self , subdata ) : <EOL> ssubdata = StringIO ( subdata . text ) <EOL> srt = "<STR_LIT>" <EOL> subtract = False <EOL> number_b = <NUM_LIT:1> <EOL> number = <NUM_LIT:0> <EOL> block = <NUM_LIT:0> <EOL> subnr = False <EOL> for i in ssubdata . readlines ( ) : <EOL> match = re . search ( r"<STR_LIT>" , i ) <EOL> match2 = re . search ( r"<STR_LIT>" , i ) <EOL> match3 = re . search ( r"<STR_LIT>" , i ) <EOL> if i [ : <NUM_LIT:6> ] == "<STR_LIT>" : <EOL> pass <EOL> elif match and number_b > <NUM_LIT:1> : <EOL> block = <NUM_LIT:0> <EOL> srt += "<STR_LIT:\n>" <EOL> elif match2 : <EOL> if not subnr : <EOL> srt += "<STR_LIT>" % number_b <EOL> matchx = re . search ( r'<STR_LIT>' , i ) <EOL> hour1 = int ( matchx . group ( <NUM_LIT:1> ) ) <EOL> hour2 = int ( matchx . group ( <NUM_LIT:4> ) ) <EOL> if int ( number ) == <NUM_LIT:1> : <EOL> if hour1 > <NUM_LIT:9> : <EOL> subtract = True <EOL> if subtract : <EOL> hour1 -= <NUM_LIT:10> <EOL> hour2 -= <NUM_LIT:10> <EOL> time = "<STR_LIT>" % ( hour1 , matchx . group ( <NUM_LIT:2> ) , matchx . group ( <NUM_LIT:3> ) . replace ( "<STR_LIT:.>" , "<STR_LIT:U+002C>" ) , hour2 , matchx . group ( <NUM_LIT:5> ) , matchx . group ( <NUM_LIT:6> ) . replace ( "<STR_LIT:.>" , "<STR_LIT:U+002C>" ) ) <EOL> srt += time <EOL> block = <NUM_LIT:1> <EOL> subnr = False <EOL> number_b += <NUM_LIT:1> <EOL> elif match3 and block == <NUM_LIT:0> : <EOL> number = match3 . group ( <NUM_LIT:1> ) <EOL> srt += "<STR_LIT>" % number <EOL> subnr = True <EOL> else : <EOL> sub = re . sub ( '<STR_LIT>' , '<STR_LIT>' , i ) <EOL> srt += sub . strip ( ) <EOL> srt += "<STR_LIT:\n>" <EOL> srt = decode_html_entities ( srt ) <EOL> if is_py2 : <EOL> return srt . encode ( "<STR_LIT:utf-8>" ) <EOL> return srt <EOL> def timestr ( msec ) : <EOL> """<STR_LIT>""" <EOL> sec = float ( msec ) / <NUM_LIT:1000> <EOL> hours = int ( sec / <NUM_LIT> ) <EOL> sec -= hours * <NUM_LIT> <EOL> minutes = int ( sec / <NUM_LIT> ) <EOL> sec -= minutes * <NUM_LIT> <EOL> output = "<STR_LIT>" % ( hours , minutes , sec ) <EOL> return output . replace ( "<STR_LIT:.>" , "<STR_LIT:U+002C>" ) <EOL> def timecolon ( data ) : <EOL> match = re . search ( r"<STR_LIT>" , data ) <EOL> return "<STR_LIT>" % ( match . group ( <NUM_LIT:1> ) , match . group ( <NUM_LIT:2> ) ) <EOL> def norm ( name ) : <EOL> if name [ <NUM_LIT:0> ] == "<STR_LIT:{>" : <EOL> _ , tag = name [ <NUM_LIT:1> : ] . split ( "<STR_LIT:}>" ) <EOL> return tag <EOL> else : <EOL> return name <EOL> def tt_text ( node , data ) : <EOL> if node . text : <EOL> data += "<STR_LIT>" % node . text . strip ( '<STR_LIT>' ) <EOL> for i in node : <EOL> if i . text : <EOL> data += "<STR_LIT>" % i . text . strip ( '<STR_LIT>' ) <EOL> if i . tail : <EOL> text = i . tail . strip ( '<STR_LIT>' ) <EOL> if text : <EOL> data += "<STR_LIT>" % text <EOL> return data </s>
