<s> import logging <EOL> from lbrynet . core . client . StreamProgressManager import StreamProgressManager <EOL> from twisted . internet import defer <EOL> log = logging . getLogger ( __name__ ) <EOL> class LiveStreamProgressManager ( StreamProgressManager ) : <EOL> def __init__ ( self , finished_callback , blob_manager , download_manager , delete_blob_after_finished = False , <EOL> download_whole = True , max_before_skip_ahead = <NUM_LIT:5> ) : <EOL> self . download_whole = download_whole <EOL> self . max_before_skip_ahead = max_before_skip_ahead <EOL> StreamProgressManager . __init__ ( self , finished_callback , blob_manager , download_manager , <EOL> delete_blob_after_finished ) <EOL> def stream_position ( self ) : <EOL> blobs = self . download_manager . blobs <EOL> if not blobs : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> newest_known_blobnum = max ( blobs . iterkeys ( ) ) <EOL> position = newest_known_blobnum <EOL> oldest_relevant_blob_num = ( max ( <NUM_LIT:0> , newest_known_blobnum - self . max_before_skip_ahead + <NUM_LIT:1> ) ) <EOL> for i in xrange ( newest_known_blobnum , oldest_relevant_blob_num - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if i in blobs and ( not blobs [ i ] . is_validated ( ) and not i in self . provided_blob_nums ) : <EOL> position = i <EOL> return position <EOL> def needed_blobs ( self ) : <EOL> blobs = self . download_manager . blobs <EOL> stream_position = self . stream_position ( ) <EOL> if blobs : <EOL> newest_known_blobnum = max ( blobs . iterkeys ( ) ) <EOL> else : <EOL> newest_known_blobnum = - <NUM_LIT:1> <EOL> blobs_needed = [ ] <EOL> for i in xrange ( stream_position , newest_known_blobnum + <NUM_LIT:1> ) : <EOL> if i in blobs and not blobs [ i ] . is_validated ( ) and not i in self . provided_blob_nums : <EOL> blobs_needed . append ( blobs [ i ] ) <EOL> return blobs_needed <EOL> def _output_loop ( self ) : <EOL> from twisted . internet import reactor <EOL> if self . stopped is True : <EOL> if self . outputting_d is not None : <EOL> self . outputting_d . callback ( True ) <EOL> self . outputting_d = None <EOL> return <EOL> blobs = self . download_manager . blobs <EOL> log . info ( "<STR_LIT>" , str ( self . last_blob_outputted ) ) <EOL> if blobs : <EOL> log . debug ( "<STR_LIT>" , str ( max ( blobs . iterkeys ( ) ) ) ) <EOL> if self . outputting_d is None : <EOL> self . outputting_d = defer . Deferred ( ) <EOL> current_blob_num = self . last_blob_outputted + <NUM_LIT:1> <EOL> def finished_outputting_blob ( ) : <EOL> self . last_blob_outputted += <NUM_LIT:1> <EOL> final_blob_num = self . download_manager . final_blob_num ( ) <EOL> if final_blob_num is not None and final_blob_num == self . last_blob_outputted : <EOL> self . _finished_outputting ( ) <EOL> self . outputting_d . callback ( True ) <EOL> self . outputting_d = None <EOL> else : <EOL> reactor . callLater ( <NUM_LIT:0> , self . _output_loop ) <EOL> if current_blob_num in blobs and blobs [ current_blob_num ] . is_validated ( ) : <EOL> log . info ( "<STR_LIT>" , str ( current_blob_num ) ) <EOL> self . provided_blob_nums . append ( current_blob_num ) <EOL> d = self . download_manager . handle_blob ( current_blob_num ) <EOL> d . addCallback ( lambda _ : finished_outputting_blob ( ) ) <EOL> d . addCallback ( lambda _ : self . _finished_with_blob ( current_blob_num ) ) <EOL> elif blobs and max ( blobs . iterkeys ( ) ) > self . last_blob_outputted + self . max_before_skip_ahead - <NUM_LIT:1> : <EOL> self . last_blob_outputted += <NUM_LIT:1> <EOL> log . info ( "<STR_LIT>" , <EOL> str ( self . last_blob_outputted ) , str ( max ( blobs . iterkeys ( ) ) ) ) <EOL> self . _finished_with_blob ( current_blob_num ) <EOL> reactor . callLater ( <NUM_LIT:0> , self . _output_loop ) <EOL> else : <EOL> self . outputting_d . callback ( True ) <EOL> self . outputting_d = None </s>
