@ -18,10 +18,11 @@ from get_languages import alpha2_from_alpha3
gc . enable ( )
gc . enable ( )
def store_subtitles ( file ) :
def store_subtitles ( file ) :
# languages = []
logging . debug ( ' BAZARR started subtitles indexing for this file: ' + file )
actual_subtitles = [ ]
actual_subtitles = [ ]
if os . path . exists ( file ) :
if os . path . exists ( file ) :
if os . path . splitext ( file ) [ 1 ] == ' .mkv ' :
if os . path . splitext ( file ) [ 1 ] == ' .mkv ' :
logging . debug ( " BAZARR is trying to index embedded subtitles. " )
try :
try :
with open ( file , ' rb ' ) as f :
with open ( file , ' rb ' ) as f :
mkv = enzyme . MKV ( f )
mkv = enzyme . MKV ( f )
@ -29,25 +30,36 @@ def store_subtitles(file):
for subtitle_track in mkv . subtitle_tracks :
for subtitle_track in mkv . subtitle_tracks :
try :
try :
if alpha2_from_alpha3 ( subtitle_track . language ) != None :
if alpha2_from_alpha3 ( subtitle_track . language ) != None :
actual_subtitles . append ( [ str ( alpha2_from_alpha3 ( subtitle_track . language ) ) , None ] )
lang = str ( alpha2_from_alpha3 ( subtitle_track . language ) )
logging . debug ( " BAZARR embedded subtitles detected: " + lang )
actual_subtitles . append ( [ lang , None ] )
except :
except :
logging . debug ( " BAZARR unable to index this unrecognized language: " + subtitle_track . language )
pass
pass
except :
except Exception as e :
logging . exception ( " BAZARR error when trying to analyze this mkv file: " + file )
pass
pass
else :
logging . debug ( " BAZARR This file isn ' t an .mkv file. " )
brazilian_portuguese = [ " .pt-br " , " .pob " , " pb " ]
brazilian_portuguese = [ " .pt-br " , " .pob " , " pb " ]
try :
try :
subtitles = core . search_external_subtitles ( file )
subtitles = core . search_external_subtitles ( file )
except :
except Exception as e :
logging . exception ( " BAZARR unable to index external subtitles. " )
pass
pass
else :
else :
for subtitle , language in subtitles . iteritems ( ) :
for subtitle , language in subtitles . iteritems ( ) :
if str ( os . path . splitext ( subtitle ) [ 0 ] ) . lower ( ) . endswith ( tuple ( brazilian_portuguese ) ) is True :
if str ( os . path . splitext ( subtitle ) [ 0 ] ) . lower ( ) . endswith ( tuple ( brazilian_portuguese ) ) is True :
logging . debug ( " BAZARR external subtitles detected: " + " pb " )
actual_subtitles . append ( [ str ( " pb " ) , path_replace_reverse ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
actual_subtitles . append ( [ str ( " pb " ) , path_replace_reverse ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
elif str ( language ) != ' und ' :
elif str ( language ) != ' und ' :
logging . debug ( " BAZARR external subtitles detected: " + str ( language ) )
actual_subtitles . append ( [ str ( language ) , path_replace_reverse ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
actual_subtitles . append ( [ str ( language ) , path_replace_reverse ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
else :
else :
if os . path . splitext ( subtitle ) [ 1 ] != " .sub " :
logging . debug ( " BAZARR falling back to file content analysis to detect language. " )
with open ( path_replace ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) , ' r ' ) as f :
with open ( path_replace ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) , ' r ' ) as f :
text = list ( islice ( f , 100 ) )
text = list ( islice ( f , 100 ) )
text = ' ' . join ( text )
text = ' ' . join ( text )
@ -56,27 +68,33 @@ def store_subtitles(file):
text = text . decode ( encoding . original_encoding )
text = text . decode ( encoding . original_encoding )
detected_language = langdetect . detect ( text )
detected_language = langdetect . detect ( text )
except Exception as e :
except Exception as e :
logging . exception ( ' BAZARR Error trying to detect character encoding for this subtitles file: ' + path_replace ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again. ' )
logging . exception ( ' BAZARR Error trying to detect language for this subtitles file: ' + path_replace ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again. ' )
else :
else :
if len ( detected_language ) > 0 :
if len ( detected_language ) > 0 :
logging . debug ( " BAZARR external subtitles detected and analysis guessed this language: " + str ( detected_language ) )
actual_subtitles . append ( [ str ( detected_language ) , path_replace_reverse ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
actual_subtitles . append ( [ str ( detected_language ) , path_replace_reverse ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
conn_db = sqlite3 . connect ( os . path . join ( config_dir , ' db/bazarr.db ' ) , timeout = 30 )
conn_db = sqlite3 . connect ( os . path . join ( config_dir , ' db/bazarr.db ' ) , timeout = 30 )
c_db = conn_db . cursor ( )
c_db = conn_db . cursor ( )
logging . debug ( " BAZARR storing those languages to DB: " + str ( actual_subtitles ) )
c_db . execute ( " UPDATE table_episodes SET subtitles = ? WHERE path = ? " , ( str ( actual_subtitles ) , path_replace_reverse ( file ) ) )
c_db . execute ( " UPDATE table_episodes SET subtitles = ? WHERE path = ? " , ( str ( actual_subtitles ) , path_replace_reverse ( file ) ) )
conn_db . commit ( )
conn_db . commit ( )
c_db . close ( )
c_db . close ( )
else :
logging . debug ( " BAZARR this file doesn ' t seems to exist or isn ' t accessible. " )
logging . debug ( ' BAZARR ended subtitles indexing for this file: ' + file )
return actual_subtitles
return actual_subtitles
def store_subtitles_movie ( file ) :
def store_subtitles_movie ( file ) :
# languages = []
logging . debug ( ' BAZARR started subtitles indexing for this file: ' + file )
actual_subtitles = [ ]
actual_subtitles = [ ]
if os . path . exists ( file ) :
if os . path . exists ( file ) :
if os . path . splitext ( file ) [ 1 ] == ' .mkv ' :
if os . path . splitext ( file ) [ 1 ] == ' .mkv ' :
logging . debug ( " BAZARR is trying to index embedded subtitles. " )
try :
try :
with open ( file , ' rb ' ) as f :
with open ( file , ' rb ' ) as f :
mkv = enzyme . MKV ( f )
mkv = enzyme . MKV ( f )
@ -84,22 +102,36 @@ def store_subtitles_movie(file):
for subtitle_track in mkv . subtitle_tracks :
for subtitle_track in mkv . subtitle_tracks :
try :
try :
if alpha2_from_alpha3 ( subtitle_track . language ) != None :
if alpha2_from_alpha3 ( subtitle_track . language ) != None :
actual_subtitles . append ( [ str ( alpha2_from_alpha3 ( subtitle_track . language ) ) , None ] )
lang = str ( alpha2_from_alpha3 ( subtitle_track . language ) )
logging . debug ( " BAZARR embedded subtitles detected: " + lang )
actual_subtitles . append ( [ lang , None ] )
except :
except :
logging . debug ( " BAZARR unable to index this unrecognized language: " + subtitle_track . language )
pass
pass
except :
except Exception as e :
logging . exception ( " BAZARR error when trying to analyze this mkv file: " + file )
pass
pass
else :
logging . debug ( " BAZARR This file isn ' t an .mkv file. " )
subtitles = core . search_external_subtitles ( file )
brazilian_portuguese = [ " .pt-br " , " .pob " , " pb " ]
brazilian_portuguese = [ " .pt-br " , " .pob " , " pb " ]
try :
subtitles = core . search_external_subtitles ( file )
except Exception as e :
logging . exception ( " BAZARR unable to index external subtitles. " )
pass
else :
for subtitle , language in subtitles . iteritems ( ) :
for subtitle , language in subtitles . iteritems ( ) :
if str ( os . path . splitext ( subtitle ) [ 0 ] ) . lower ( ) . endswith ( tuple ( brazilian_portuguese ) ) is True :
if str ( os . path . splitext ( subtitle ) [ 0 ] ) . lower ( ) . endswith ( tuple ( brazilian_portuguese ) ) is True :
logging . debug ( " BAZARR external subtitles detected: " + " pb " )
actual_subtitles . append ( [ str ( " pb " ) , path_replace_reverse_movie ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
actual_subtitles . append ( [ str ( " pb " ) , path_replace_reverse_movie ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
elif str ( language ) != ' und ' :
elif str ( language ) != ' und ' :
logging . debug ( " BAZARR external subtitles detected: " + str ( language ) )
actual_subtitles . append ( [ str ( language ) , path_replace_reverse_movie ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
actual_subtitles . append ( [ str ( language ) , path_replace_reverse_movie ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
else :
else :
if os . path . splitext ( subtitle ) [ 1 ] != " .sub " :
if os . path . splitext ( subtitle ) [ 1 ] != " .sub " :
logging . debug ( " BAZARR falling back to file content analysis to detect language. " )
with open ( path_replace_movie ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) , ' r ' ) as f :
with open ( path_replace_movie ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) , ' r ' ) as f :
text = list ( islice ( f , 100 ) )
text = list ( islice ( f , 100 ) )
text = ' ' . join ( text )
text = ' ' . join ( text )
@ -108,18 +140,23 @@ def store_subtitles_movie(file):
text = text . decode ( encoding . original_encoding )
text = text . decode ( encoding . original_encoding )
detected_language = langdetect . detect ( text )
detected_language = langdetect . detect ( text )
except Exception as e :
except Exception as e :
logging . exception ( ' BAZARR Error trying to detect character encoding for this subtitles file: ' + path_replac e_movi e( os . path . join ( os . path . dirname ( file ) , subtitle ) ) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again. ' )
logging . exception ( ' BAZARR Error trying to detect language for this subtitles file: ' + path_replac e( os . path . join ( os . path . dirname ( file ) , subtitle ) ) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again. ' )
else :
else :
if len ( detected_language ) > 0 :
if len ( detected_language ) > 0 :
logging . debug ( " BAZARR external subtitles detected and analysis guessed this language: " + str ( detected_language ) )
actual_subtitles . append ( [ str ( detected_language ) , path_replace_reverse_movie ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
actual_subtitles . append ( [ str ( detected_language ) , path_replace_reverse_movie ( os . path . join ( os . path . dirname ( file ) , subtitle ) ) ] )
conn_db = sqlite3 . connect ( os . path . join ( config_dir , ' db/bazarr.db ' ) , timeout = 30 )
conn_db = sqlite3 . connect ( os . path . join ( config_dir , ' db/bazarr.db ' ) , timeout = 30 )
c_db = conn_db . cursor ( )
c_db = conn_db . cursor ( )
logging . debug ( " BAZARR storing those languages to DB: " + str ( actual_subtitles ) )
c_db . execute ( " UPDATE table_movies SET subtitles = ? WHERE path = ? " , ( str ( actual_subtitles ) , path_replace_reverse_movie ( file ) ) )
c_db . execute ( " UPDATE table_movies SET subtitles = ? WHERE path = ? " , ( str ( actual_subtitles ) , path_replace_reverse_movie ( file ) ) )
conn_db . commit ( )
conn_db . commit ( )
c_db . close ( )
c_db . close ( )
else :
logging . debug ( " BAZARR this file doesn ' t seems to exist or isn ' t accessible. " )
logging . debug ( ' BAZARR ended subtitles indexing for this file: ' + file )
return actual_subtitles
return actual_subtitles