@ -22,7 +22,7 @@ class Convert:
if len ( ids ) > 0 :
try :
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
except ValueError :
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 "
if fail :
raise Failed ( fail_text )
return None
return [ ] if to_id == " imdbid " else None
@retry ( stop_max_attempt_number = 6 , wait_fixed = 10000 )
def _request ( self , ids ) :
@ -67,79 +67,43 @@ class Convert:
converted_ids . append ( anime_ids )
return converted_ids
def anidb_to_ids ( self , anidb_list , language ):
def anidb_to_ids ( self , anidb_list ):
show_ids = [ ]
movie_ids = [ ]
for anidb_id in anidb_list :
try :
for imdb_id in self . anidb_to_imdb ( anidb_id , fail = True ) :
tmdb_id , _ = self . imdb_to_ids ( imdb_id , language )
imdb_ids = self . anidb_to_imdb ( anidb_id )
tmdb_ids = [ ]
if imdb_ids :
for imdb_id in imdb_ids :
tmdb_id = self . imdb_to_tmdb ( imdb_id )
if tmdb_id :
movie_ids . append ( tmdb_id )
except Failed :
tmdb_ids . append ( tmdb_id )
tvdb_id = self . anidb_to_tvdb ( anidb_id )
if tvdb_id :
show_ids . append ( tvdb_id )
else :
if tmdb_ids :
movie_ids . extend ( tmdb_ids )
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
def anilist_to_ids ( self , anilist_ids , language ):
def anilist_to_ids ( self , anilist_ids ):
anidb_ids = [ ]
for id_set in self . _arms_ids ( anilist_ids = anilist_ids ) :
if id_set [ " anidb " ] is not None :
anidb_ids . append ( id_set [ " anidb " ] )
else :
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 = [ ]
for id_set in self . _arms_ids ( mal_ids = mal_ids ) :
if id_set [ " anidb " ] is not None :
anidb_ids . append ( id_set [ " anidb " ] )
else :
logger . error ( f " Convert Error: AniDB ID not found for MyAnimeList ID: { id_set [ ' myanimelist ' ] } " )
return self . anidb_to_ids ( anidb_ids , language )
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
return self . anidb_to_ids ( anidb_ids )
def anidb_to_tvdb ( self , anidb_id , fail = False ) :
return self . _anidb ( anidb_id , " tvdbid " , fail = fail )
@ -161,17 +125,25 @@ class Convert:
return None
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 :
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 :
if self . config . Trakt :
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 :
pass
if fail :
if fail and tmdb_id is None :
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
def tmdb_to_tvdb ( self , tmdb_id , fail = False ) :
@ -215,33 +187,38 @@ class Convert:
return None
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 :
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 :
if self . config . Trakt :
try :
return self . config . Trakt . convert ( imdb_id , " imdb " , " tvdb " , " show " )
tvdb_id = self . config . Trakt . convert ( imdb_id , " imdb " , " tvdb " , " show " )
except Failed :
pass
if fail :
if fail and tvdb_id is None :
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 ) :
expired = None
if self . config . Cache :
cache_id , media_type , expired = self . config . Cache . query_guid_map ( item . guid )
if cache_id and not expired :
media_id_type = " movie " if " movie " in media_type else " show "
return media_id_type , util . get_list ( cache_id , int_list = True )
try :
tmdb_id = None
imdb_id = None
tvdb_id = None
anidb_id = None
mal_id = None
error_message = None
if self . config . Cache :
if library . is_movie : tmdb_id , expired = self . config . Cache . get_tmdb_id ( " movie " , plex_guid = item . guid )
else : tvdb_id , expired = self . config . Cache . get_tvdb_id ( " show " , plex_guid = item . guid )
if not tvdb_id and library . is_show :
tmdb_id , expired = self . config . Cache . get_tmdb_id ( " show " , plex_guid = item . guid )
anidb_id , expired = self . config . Cache . get_anidb_id ( " show " , plex_guid = item . guid )
if expired or ( not tmdb_id and library . is_movie ) or ( not tvdb_id and not tmdb_id and library . is_show ) :
guid = requests . utils . urlparse ( item . guid )
item_type = guid . scheme . split ( " . " ) [ - 1 ]
check_id = guid . netloc
@ -250,17 +227,6 @@ class Convert:
tmdb_id = [ ]
imdb_id = [ ]
tvdb_id = [ ]
if check_id == " movie " :
try :
for guid_tag in library . get_guids ( item ) :
url_parsed = requests . utils . urlparse ( guid_tag . id )
if url_parsed . scheme == " tmdb " : tmdb_id . append ( int ( url_parsed . netloc ) )
elif url_parsed . scheme == " imdb " : imdb_id . append ( 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 check_id == " show " :
try :
for guid_tag in library . get_guids ( item ) :
url_parsed = requests . utils . urlparse ( guid_tag . id )
@ -269,28 +235,28 @@ class Convert:
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
raise Failed ( " No External GUIDs found " )
elif item_type == " imdb " : imdb_id = check_id
elif item_type == " thetvdb " : tvdb_id = int ( check_id )
elif item_type == " themoviedb " : tmdb_id = int ( check_id )
elif item_type == " hama " :
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 )
else : error_message = f " Hama Agent ID: { check_id } not supported "
elif item_type == " myanimelist " : mal_id = check_id
elif item_type == " local " : error_message = " No match in Plex "
else : error_message = f " Agent { item_type } not supported "
else : raise Failed ( f " Hama Agent ID: { check_id } not supported " )
elif item_type == " myanimelist " :
anime_ids = self . _arms_ids ( mal_ids = check_id )
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 mal_id and not anidb_id :
anime_ids = self . _arms_ids ( mal_ids = mal_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 )
if anidb_id and not imdb_id :
if anidb_id :
imdb_id = self . anidb_to_imdb ( anidb_id )
if not imdb_id :
tvdb_id = self . anidb_to_tvdb ( anidb_id )
if not imdb_id and not tvdb_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 = [ ]
@ -304,43 +270,31 @@ class Convert:
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 ) :
service_name = " TMDb ID " if library . is_movie else " TVDb ID "
if self . config . Trakt : api_name = " TMDb or Trakt "
else : api_name = " TMDb "
if tmdb_id and imdb_id : id_name = f " TMDb ID: { tmdb_id } or IMDb ID: { imdb_id } "
elif imdb_id and tvdb_id : id_name = f " IMDb ID: { imdb_id } or TVDb ID: { tvdb_id } "
elif tmdb_id : id_name = f " TMDb ID: { tmdb_id } "
elif imdb_id : id_name = f " IMDb ID: { imdb_id } "
elif tvdb_id : id_name = f " TVDb ID: { tvdb_id } "
else : id_name = None
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 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 "
elif id_name : error_message = f " Unable to convert { id_name } to { service_name } using { api_name } "
else : error_message = f " No ID to convert to { service_name } "
if self . config . Cache and ( ( tmdb_id and library . is_movie ) or ( ( tvdb_id or ( anidb_id and tmdb_id ) ) and library . is_show ) ) :
if not isinstance ( tmdb_id , list ) : tmdb_id = [ tmdb_id ]
if not isinstance ( imdb_id , list ) : imdb_id = [ imdb_id ]
if not isinstance ( tvdb_id , list ) : tvdb_id = [ tvdb_id ]
try : tvdb_value = tvdb_id [ 0 ]
except IndexError : tvdb_value = None
for i in range ( len ( tmdb_id ) ) :
try : imdb_value = imdb_id [ i ]
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 )
def update_cache ( cache_ids , id_type , guid_type ) :
if self . config . Cache :
cache_ids = util . compile_list ( cache_ids )
util . print_end ( length , f " Cache | { ' ^ ' if expired else ' + ' } | { item . guid : <46 } | { id_type } ID: { cache_ids : <6 } | { item . title } " )
self . config . Cache . update_guid ( guid_type , item . guid , cache_ids , expired )
if tmdb_id and library . is_movie : return " movie " , tmdb_id
elif tvdb_id and library . is_show : return " show " , tvdb_id
elif anidb_id and tmdb_id : return " movie " , tmdb_id
if tmdb_id and library . is_movie :
update_cache ( tmdb_id , " TMDb " , " movie " )
return " movie " , tmdb_id
elif tvdb_id and library . is_show :
update_cache ( tvdb_id , " TVDb " , " show " )
return " show " , tvdb_id
elif anidb_id and tmdb_id and library . is_show :
update_cache ( tmdb_id , " TMDb " , " show_movie " )
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 } " )
raise Failed ( f " No ID to convert " )
except Failed as e :
util . print_end ( length , f " Mapping Error | { item . guid : <46 } | { e } for { item . title } " )
return None , None