@ -22,7 +22,7 @@ class Convert:
if len ( ids ) > 0 :
if len ( ids ) > 0 :
try :
try :
if len ( ids [ 0 ] ) > 0 :
if len ( ids [ 0 ] ) > 0 :
return ids[ 0 ] . split ( " , " ) if to_id == " imdbid " else int ( ids [ 0 ] )
return util. get_list ( ids [ 0 ] ) if to_id == " imdbid " else int ( ids [ 0 ] )
raise ValueError
raise ValueError
except ValueError :
except ValueError :
fail_text = f " Convert Error: No { util . pretty_ids [ to_id ] } ID found for AniDB ID: { input_id } "
fail_text = f " Convert Error: No { util . pretty_ids [ to_id ] } ID found for AniDB ID: { input_id } "
@ -30,7 +30,7 @@ class Convert:
fail_text = f " Convert Error: AniDB ID: { input_id } not found "
fail_text = f " Convert Error: AniDB ID: { input_id } not found "
if fail :
if fail :
raise Failed ( fail_text )
raise Failed ( fail_text )
return None
return [ ] if to_id == " imdbid " else None
@retry ( stop_max_attempt_number = 6 , wait_fixed = 10000 )
@retry ( stop_max_attempt_number = 6 , wait_fixed = 10000 )
def _request ( self , ids ) :
def _request ( self , ids ) :
@ -67,79 +67,43 @@ class Convert:
converted_ids . append ( anime_ids )
converted_ids . append ( anime_ids )
return converted_ids
return converted_ids
def anidb_to_ids ( self , anidb_list , language ):
def anidb_to_ids ( self , anidb_list ):
show_ids = [ ]
show_ids = [ ]
movie_ids = [ ]
movie_ids = [ ]
for anidb_id in anidb_list :
for anidb_id in anidb_list :
try :
imdb_ids = self . anidb_to_imdb ( anidb_id )
for imdb_id in self . anidb_to_imdb ( anidb_id , fail = True ) :
tmdb_ids = [ ]
tmdb_id , _ = self . imdb_to_ids ( imdb_id , language )
if imdb_ids :
for imdb_id in imdb_ids :
tmdb_id = self . imdb_to_tmdb ( imdb_id )
if tmdb_id :
if tmdb_id :
movie_ids . append ( tmdb_id )
tmdb_ids . append ( tmdb_id )
except Failed :
tvdb_id = self . anidb_to_tvdb ( anidb_id )
tvdb_id = self . anidb_to_tvdb ( anidb_id )
if tvdb_id :
if tvdb_id :
show_ids . append ( tvdb_id )
show_ids . append ( tvdb_id )
if tmdb_ids :
else :
movie_ids . extend ( tmdb_ids )
logger . error ( f " Convert Error: No TVDb ID or IMDb ID found for AniDB ID: { anidb_id } " )
if not tvdb_id and not tmdb_ids :
logger . error ( f " Convert Error: No TVDb ID or IMDb ID found for AniDB ID: { anidb_id } " )
return movie_ids , show_ids
return movie_ids , show_ids
def anilist_to_ids ( self , anilist_ids , language ):
def anilist_to_ids ( self , anilist_ids ):
anidb_ids = [ ]
anidb_ids = [ ]
for id_set in self . _arms_ids ( anilist_ids = anilist_ids ) :
for id_set in self . _arms_ids ( anilist_ids = anilist_ids ) :
if id_set [ " anidb " ] is not None :
if id_set [ " anidb " ] is not None :
anidb_ids . append ( id_set [ " anidb " ] )
anidb_ids . append ( id_set [ " anidb " ] )
else :
else :
logger . error ( f " Convert Error: AniDB ID not found for AniList ID: { id_set [ ' anilist ' ] } " )
logger . error ( f " Convert Error: AniDB ID not found for AniList ID: { id_set [ ' anilist ' ] } " )
return self . anidb_to_ids ( anidb_ids , language )
return self . anidb_to_ids ( anidb_ids )
def myanimelist_to_ids ( self , mal_ids , language ):
def myanimelist_to_ids ( self , mal_ids ):
anidb_ids = [ ]
anidb_ids = [ ]
for id_set in self . _arms_ids ( mal_ids = mal_ids ) :
for id_set in self . _arms_ids ( mal_ids = mal_ids ) :
if id_set [ " anidb " ] is not None :
if id_set [ " anidb " ] is not None :
anidb_ids . append ( id_set [ " anidb " ] )
anidb_ids . append ( id_set [ " anidb " ] )
else :
else :
logger . error ( f " Convert Error: AniDB ID not found for MyAnimeList ID: { id_set [ ' myanimelist ' ] } " )
logger . error ( f " Convert Error: AniDB ID not found for MyAnimeList ID: { id_set [ ' myanimelist ' ] } " )
return self . anidb_to_ids ( anidb_ids , language )
return self . anidb_to_ids ( anidb_ids )
def imdb_to_ids ( self , imdb_id , language ) :
update_tmdb = False
update_tvdb = False
if self . config . Cache :
tmdb_id , tvdb_id = self . config . Cache . get_ids_from_imdb ( imdb_id )
update_tmdb = False
if not tmdb_id :
tmdb_id , update_tmdb = self . config . Cache . get_tmdb_from_imdb ( imdb_id )
if update_tmdb :
tmdb_id = None
update_tvdb = False
if not tvdb_id :
tvdb_id , update_tvdb = self . config . Cache . get_tvdb_from_imdb ( imdb_id )
if update_tvdb :
tvdb_id = None
else :
tmdb_id = None
tvdb_id = None
from_cache = tmdb_id is not None or tvdb_id is not None
if not tmdb_id and not tvdb_id :
tmdb_id = self . imdb_to_tmdb ( imdb_id )
if not tmdb_id and not tvdb_id :
tvdb_id = self . imdb_to_tvdb ( imdb_id )
if tmdb_id and not from_cache :
try : self . config . TMDb . get_movie ( tmdb_id )
except Failed : tmdb_id = None
if tvdb_id and not from_cache :
try : self . config . TVDb . get_series ( language , tvdb_id )
except Failed : tvdb_id = None
if not tmdb_id and not tvdb_id :
raise Failed ( f " Convert Error: No TMDb ID or TVDb ID found for IMDb: { imdb_id } " )
if self . config . Cache :
if tmdb_id and update_tmdb is not False :
self . config . Cache . update_imdb ( " movie " , update_tmdb , imdb_id , tmdb_id )
if tvdb_id and update_tvdb is not False :
self . config . Cache . update_imdb ( " show " , update_tvdb , imdb_id , tvdb_id )
return tmdb_id , tvdb_id
def anidb_to_tvdb ( self , anidb_id , fail = False ) :
def anidb_to_tvdb ( self , anidb_id , fail = False ) :
return self . _anidb ( anidb_id , " tvdbid " , fail = fail )
return self . _anidb ( anidb_id , " tvdbid " , fail = fail )
@ -161,17 +125,25 @@ class Convert:
return None
return None
def imdb_to_tmdb ( self , imdb_id , is_movie = True , fail = False ) :
def imdb_to_tmdb ( self , imdb_id , is_movie = True , fail = False ) :
expired = False
if self . config . Cache and is_movie :
cache_id , expired = self . config . Cache . get_tmdb_from_imdb ( imdb_id )
if cache_id and not expired :
return cache_id
tmdb_id = None
try :
try :
return self . config . TMDb . convert_to ( imdb_id , " imdb_id " , is_movie )
tmdb_id = self . config . TMDb . convert_to ( imdb_id , " imdb_id " , is_movie )
except Failed :
except Failed :
if self . config . Trakt :
if self . config . Trakt :
try :
try :
return self . config . Trakt . convert ( imdb_id , " imdb " , " tmdb " , " movie " if is_movie else " show " )
tmdb_id = self . config . Trakt . convert ( imdb_id , " imdb " , " tmdb " , " movie " if is_movie else " show " )
except Failed :
except Failed :
pass
pass
if fail :
if fail and tmdb_id is None :
raise Failed ( f " Convert Error: No TMDb ID Found for IMDb ID: { imdb_id } " )
raise Failed ( f " Convert Error: No TMDb ID Found for IMDb ID: { imdb_id } " )
return None
if self . config . Cache and tmdb_id and is_movie :
self . config . Cache . update_imdb ( " movie " , expired , imdb_id , tmdb_id )
return tmdb_id
# TODO
# TODO
def tmdb_to_tvdb ( self , tmdb_id , fail = False ) :
def tmdb_to_tvdb ( self , tmdb_id , fail = False ) :
@ -215,33 +187,38 @@ class Convert:
return None
return None
def imdb_to_tvdb ( self , imdb_id , fail = False ) :
def imdb_to_tvdb ( self , imdb_id , fail = False ) :
expired = False
if self . config . Cache :
cache_id , expired = self . config . Cache . get_tvdb_from_imdb ( imdb_id )
if cache_id and not expired :
return cache_id
tvdb_id = None
try :
try :
return self . tmdb_to_tvdb ( self . imdb_to_tmdb ( imdb_id , False ) )
tvdb_id = self . tmdb_to_tvdb ( self . imdb_to_tmdb ( imdb_id , False ) )
except Failed :
except Failed :
if self . config . Trakt :
if self . config . Trakt :
try :
try :
return self . config . Trakt . convert ( imdb_id , " imdb " , " tvdb " , " show " )
tvdb_id = self . config . Trakt . convert ( imdb_id , " imdb " , " tvdb " , " show " )
except Failed :
except Failed :
pass
pass
if fail :
if fail and tvdb_id is None :
raise Failed ( f " Convert Error: No TVDb ID Found for IMDb ID: { imdb_id } " )
raise Failed ( f " Convert Error: No TVDb ID Found for IMDb ID: { imdb_id } " )
return None
if self . config . Cache and tvdb_id :
self . config . Cache . update_imdb ( " show " , expired , imdb_id , tvdb_id )
return tvdb_id
def get_id ( self , item , library , length ) :
def get_id ( self , item , library , length ) :
expired = None
expired = None
tmdb_id = None
imdb_id = None
tvdb_id = None
anidb_id = None
mal_id = None
error_message = None
if self . config . Cache :
if self . config . Cache :
if library . is_movie : tmdb_id , expired = self . config . Cache . get_tmdb_id ( " movie " , plex_guid = item . guid )
cache_id , media_type , expired = self . config . Cache . query_guid_map ( item . guid )
else : tvdb_id , expired = self . config . Cache . get_tvdb_id ( " show " , plex_guid = item . guid )
if cache_id and not expired :
if not tvdb_id and library . is_show :
media_id_type = " movie " if " movie " in media_type else " show "
tmdb_id , expired = self . config . Cache . get_tmdb_id ( " show " , plex_guid = item . guid )
return media_id_type , util . get_list ( cache_id , int_list = True )
anidb_id , expired = self . config . Cache . get_anidb_id ( " show " , plex_guid = item . guid )
try :
if expired or ( not tmdb_id and library . is_movie ) or ( not tvdb_id and not tmdb_id and library . is_show ) :
tmdb_id = None
imdb_id = None
tvdb_id = None
anidb_id = None
guid = requests . utils . urlparse ( item . guid )
guid = requests . utils . urlparse ( item . guid )
item_type = guid . scheme . split ( " . " ) [ - 1 ]
item_type = guid . scheme . split ( " . " ) [ - 1 ]
check_id = guid . netloc
check_id = guid . netloc
@ -250,97 +227,74 @@ class Convert:
tmdb_id = [ ]
tmdb_id = [ ]
imdb_id = [ ]
imdb_id = [ ]
tvdb_id = [ ]
tvdb_id = [ ]
if check_id == " movie " :
try :
try :
for guid_tag in library . get_guids ( item ) :
for guid_tag in library . get_guids ( item ) :
url_parsed = requests . utils . urlparse ( guid_tag . id )
url_parsed = requests . utils . urlparse ( guid_tag . id )
if url_parsed . scheme == " tvdb " : tvdb_id . append ( int ( url_parsed . netloc ) )
if url_parsed . scheme == " tmdb " : tmdb_id . append ( int ( url_parsed . netloc ) )
elif url_parsed . scheme == " imdb " : imdb_id . append ( url_parsed . netloc )
elif url_parsed . scheme == " imdb " : imdb_id . append ( url_parsed . netloc )
elif url_parsed . scheme == " tmdb " : tmdb_id . append ( int ( url_parsed . netloc ) )
except requests . exceptions . ConnectionError :
except requests . exceptions . ConnectionError :
util . print_stacktrace ( )
util . print_stacktrace ( )
logger . error ( f " { ' Cache | ! | ' if self . config . Cache else ' Mapping Error: ' } { item . guid : <46 } | No External GUIDs found for { item . title } " )
raise Failed ( " No External GUIDs found " )
return None , None
elif check_id == " show " :
try :
for guid_tag in library . get_guids ( item ) :
url_parsed = requests . utils . urlparse ( guid_tag . id )
if url_parsed . scheme == " tvdb " : tvdb_id . append ( int ( url_parsed . netloc ) )
elif url_parsed . scheme == " imdb " : imdb_id . append ( url_parsed . netloc )
elif url_parsed . scheme == " tmdb " : tmdb_id . append ( int ( url_parsed . netloc ) )
except requests . exceptions . ConnectionError :
util . print_stacktrace ( )
logger . error ( f " { ' Cache | ! | ' if self . config . Cache else ' Mapping Error: ' } { item . guid : <46 } | No External GUIDs found for { item . title } " )
return None , None
elif item_type == " imdb " : imdb_id = check_id
elif item_type == " imdb " : imdb_id = check_id
elif item_type == " thetvdb " : tvdb_id = int ( check_id )
elif item_type == " thetvdb " : tvdb_id = int ( check_id )
elif item_type == " themoviedb " : tmdb_id = int ( check_id )
elif item_type == " themoviedb " : tmdb_id = int ( check_id )
elif item_type == " hama " :
elif item_type == " hama " :
if check_id . startswith ( " tvdb " ) : tvdb_id = int ( re . search ( " -(.*) " , check_id ) . group ( 1 ) )
if check_id . startswith ( " tvdb " ) : tvdb_id = int ( re . search ( " -(.*) " , check_id ) . group ( 1 ) )
elif check_id . startswith ( " anidb " ) : anidb_id = re . search ( " -(.*) " , check_id ) . group ( 1 )
elif check_id . startswith ( " anidb " ) : anidb_id = re . search ( " -(.*) " , check_id ) . group ( 1 )
else : error_message = f " Hama Agent ID: { check_id } not supported "
else : raise Failed ( f " Hama Agent ID: { check_id } not supported " )
elif item_type == " myanimelist " : mal_id = check_id
elif item_type == " myanimelist " :
elif item_type == " local " : error_message = " No match in Plex "
anime_ids = self . _arms_ids ( mal_ids = check_id )
else : error_message = f " Agent { item_type } not supported "
if anime_ids [ 0 ] and anime_ids [ 0 ] [ " anidb " ] : anidb_id = anime_ids [ 0 ] [ " anidb " ]
else : raise Failed ( f " Unable to convert MyAnimeList ID: { check_id } to AniDB ID " )
elif item_type == " local " : raise Failed ( " No match in Plex " )
else : raise Failed ( f " Agent { item_type } not supported " )
if not error_message :
if anidb_id :
if mal_id and not anidb_id :
imdb_id = self . anidb_to_imdb ( anidb_id )
anime_ids = self . _arms_ids ( mal_ids = mal_id )
if not imdb_id :
if anime_ids [ 0 ] and anime_ids [ 0 ] [ " anidb " ] :
anidb_id = anime_ids [ 0 ] [ " anidb " ]
if anidb_id and not tvdb_id :
tvdb_id = self . anidb_to_tvdb ( anidb_id )
tvdb_id = self . anidb_to_tvdb ( anidb_id )
if anidb_id and not imdb_id :
if not imdb_id and not tvdb_id :
imdb_id = self . anidb_to_imdb ( anidb_id )
raise Failed ( f " Unable to convert AniDB ID: { anidb_id } to TVDb ID or IMDb ID " )
if not tmdb_id and imdb_id :
if isinstance ( imdb_id , list ) :
tmdb_id = [ ]
new_imdb_id = [ ]
for imdb in imdb_id :
try :
tmdb_id . append ( self . imdb_to_tmdb ( imdb_id , fail = True ) )
new_imdb_id . append ( imdb )
except Failed :
continue
imdb_id = new_imdb_id
else :
tmdb_id = self . imdb_to_tmdb ( imdb_id )
if not tmdb_id and tvdb_id and library . is_show : tmdb_id = self . tvdb_to_tmdb ( tvdb_id )
if not imdb_id and tmdb_id and library . is_movie : imdb_id = self . tmdb_to_imdb ( tmdb_id )
if not imdb_id and tvdb_id and library . is_show : imdb_id = self . tvdb_to_imdb ( tvdb_id )
if not tvdb_id and tmdb_id and library . is_show : tvdb_id = self . tmdb_to_tvdb ( tmdb_id )
if not tvdb_id and imdb_id and library . is_show : tvdb_id = self . imdb_to_tvdb ( imdb_id )
if ( not tmdb_id and library . is_movie ) or ( not tvdb_id and not ( anidb_id and tmdb_id ) and library . is_show ) :
if not tmdb_id and imdb_id :
service_name = " TMDb ID " if library . is_movie else " TVDb ID "
if isinstance ( imdb_id , list ) :
tmdb_id = [ ]
if self . config . Trakt : api_name = " TMDb or Trakt "
new_imdb_id = [ ]
else : api_name = " TMDb "
for imdb in imdb_id :
try :
tmdb_id . append ( self . imdb_to_tmdb ( imdb_id , fail = True ) )
new_imdb_id . append ( imdb )
except Failed :
continue
imdb_id = new_imdb_id
else :
tmdb_id = self . imdb_to_tmdb ( imdb_id )
if not tmdb_id :
raise Failed ( f " Unable to convert IMDb ID: { imdb_id } to TMDb ID " )
if not anidb_id and not tvdb_id and tmdb_id and library . is_show :
tvdb_id = self . tmdb_to_tvdb ( tmdb_id )
if not tvdb_id :
raise Failed ( f " Unable to convert TMDb ID: { tmdb_id } to TVDb ID " )
if tmdb_id and imdb_id : id_name = f " TMDb ID: { tmdb_id } or IMDb ID: { imdb_id } "
def update_cache ( cache_ids , id_type , guid_type ) :
elif imdb_id and tvdb_id : id_name = f " IMDb ID: { imdb_id } or TVDb ID: { tvdb_id } "
if self . config . Cache :
elif tmdb_id : id_name = f " TMDb ID: { tmdb_id } "
cache_ids = util . compile_list ( cache_ids )
elif imdb_id : id_name = f " IMDb ID: { imdb_id } "
util . print_end ( length , f " Cache | { ' ^ ' if expired else ' + ' } | { item . guid : <46 } | { id_type } ID: { cache_ids : <6 } | { item . title } " )
elif tvdb_id : id_name = f " TVDb ID: { tvdb_id } "
self . config . Cache . update_guid ( guid_type , item . guid , cache_ids , expired )
else : id_name = None
if anidb_id and not tmdb_id and not tvdb_id : error_message = f " Unable to convert AniDB ID: { anidb_id } to TMDb ID or TVDb ID "
if tmdb_id and library . is_movie :
elif id_name : error_message = f " Unable to convert { id_name } to { service_name } using { api_name } "
update_cache ( tmdb_id , " TMDb " , " movie " )
else : error_message = f " No ID to convert to { service_name } "
return " movie " , tmdb_id
if self . config . Cache and ( ( tmdb_id and library . is_movie ) or ( ( tvdb_id or ( anidb_id and tmdb_id ) ) and library . is_show ) ) :
elif tvdb_id and library . is_show :
if not isinstance ( tmdb_id , list ) : tmdb_id = [ tmdb_id ]
update_cache ( tvdb_id , " TVDb " , " show " )
if not isinstance ( imdb_id , list ) : imdb_id = [ imdb_id ]
return " show " , tvdb_id
if not isinstance ( tvdb_id , list ) : tvdb_id = [ tvdb_id ]
elif anidb_id and tmdb_id and library . is_show :
try : tvdb_value = tvdb_id [ 0 ]
update_cache ( tmdb_id , " TMDb " , " show_movie " )
except IndexError : tvdb_value = None
return " movie " , tmdb_id
for i in range ( len ( tmdb_id ) ) :
else :
try : imdb_value = imdb_id [ i ]
raise Failed ( f " No ID to convert " )
except IndexError : imdb_value = None
util . print_end ( length , f " Cache | { ' ^ ' if expired is True else ' + ' } | { item . guid : <46 } | { tmdb_id [ i ] if tmdb_id [ i ] else ' None ' : <6 } | { imdb_value if imdb_value else ' None ' : <10 } | { tvdb_value if tvdb_value else ' None ' : <6 } | { anidb_id if anidb_id else ' None ' : <5 } | { item . title } " )
self . config . Cache . update_guid ( " movie " if library . is_movie else " show " , item . guid , tmdb_id [ i ] , imdb_value , tvdb_value , anidb_id , expired )
if tmdb_id and library . is_movie : return " movie " , tmdb_id
except Failed as e :
elif tvdb_id and library . is_show : return " show " , tvdb_id
util . print_end ( length , f " Mapping Error | { item . guid : <46 } | { e } for { item . title } " )
elif anidb_id and tmdb_id : return " movie " , tmdb_id
else :
util . print_end ( length , f " { ' Cache | ! | ' if self . config . Cache else ' Mapping Error: ' } { item . guid : <46 } | { error_message } for { item . title } " )
return None , None
return None , None