@ -30,7 +30,10 @@ class Overlays:
builder = CollectionBuilder ( self . config , overlay_file , k , v , library = self . library , overlay = True )
builder = CollectionBuilder ( self . config , overlay_file , k , v , library = self . library , overlay = True )
logger . info ( " " )
logger . info ( " " )
logger . separator ( f " Running { k } Overlay " , space = False , border = False )
logger . separator ( f " Gathering Items for { k } Overlay " , space = False , border = False )
if builder . overlay not in overlay_rating_keys :
overlay_rating_keys [ builder . overlay ] = [ ]
if builder . filters or builder . tmdb_filters :
if builder . filters or builder . tmdb_filters :
logger . info ( " " )
logger . info ( " " )
@ -45,13 +48,17 @@ class Overlays:
logger . info ( " " )
logger . info ( " " )
builder . filter_and_save_items ( builder . gather_ids ( method , value ) )
builder . filter_and_save_items ( builder . gather_ids ( method , value ) )
if builder . added_items :
if builder . added_items :
if builder . overlay not in overlay_rating_keys :
overlay_rating_keys [ builder . overlay ] = [ ]
for item in builder . added_items :
for item in builder . added_items :
item_keys [ item . ratingKey ] = item
item_keys [ item . ratingKey ] = item
if item . ratingKey not in overlay_rating_keys [ builder . overlay ] :
if item . ratingKey not in overlay_rating_keys [ builder . overlay ] :
overlay_rating_keys [ builder . overlay ] . append ( item . ratingKey )
overlay_rating_keys [ builder . overlay ] . append ( item . ratingKey )
if builder . remove_overlays :
for rk in overlay_rating_keys [ builder . overlay ] :
for remove_overlay in builder . remove_overlays :
if remove_overlay in overlay_rating_keys and rk in overlay_rating_keys [ remove_overlay ] :
overlay_rating_keys [ remove_overlay ] . remove ( rk )
for overlay_name , over_keys in overlay_rating_keys . items ( ) :
for overlay_name , over_keys in overlay_rating_keys . items ( ) :
clean_name , _ = util . validate_filename ( overlay_name )
clean_name , _ = util . validate_filename ( overlay_name )
image_compare = None
image_compare = None
@ -68,6 +75,20 @@ class Overlays:
if self . config . Cache :
if self . config . Cache :
self . config . Cache . update_image_map ( overlay_name , f " { self . library . image_table_name } _overlays " , overlay_name , overlay_size )
self . config . Cache . update_image_map ( overlay_name , f " { self . library . image_table_name } _overlays " , overlay_name , overlay_size )
def find_poster_url ( plex_item ) :
if self . library . is_movie :
if plex_item . ratingKey in self . library . movie_rating_key_map :
return self . config . TMDb . get_movie ( self . library . movie_rating_key_map [ plex_item . ratingKey ] ) . poster_url
elif self . library . is_show :
check_key = plex_item . ratingKey if isinstance ( plex_item , Show ) else plex_item . show ( ) . ratingKey
tmdb_id = self . config . Convert . tvdb_to_tmdb ( self . library . show_rating_key_map [ check_key ] )
if isinstance ( plex_item , Show ) and plex_item . ratingKey in self . library . show_rating_key_map :
return self . config . TMDb . get_show ( tmdb_id ) . poster_url
elif isinstance ( plex_item , Season ) :
return self . config . TMDb . get_season ( tmdb_id , plex_item . seasonNumber ) . poster_url
elif isinstance ( plex_item , Episode ) :
return self . config . TMDb . get_episode ( tmdb_id , plex_item . seasonNumber , plex_item . episodeNumber ) . still_url
def get_overlay_items ( libtype = None ) :
def get_overlay_items ( libtype = None ) :
return [ o for o in self . library . search ( label = " Overlay " , libtype = libtype ) if o . ratingKey not in item_overlays ]
return [ o for o in self . library . search ( label = " Overlay " , libtype = libtype ) if o . ratingKey not in item_overlays ]
@ -78,6 +99,11 @@ class Overlays:
elif self . library . is_music :
elif self . library . is_music :
remove_overlays . extend ( get_overlay_items ( libtype = " album " ) )
remove_overlays . extend ( get_overlay_items ( libtype = " album " ) )
if remove_overlays :
logger . info ( " " )
logger . separator ( f " Removing Overlays for the { self . library . name } Library " )
logger . info ( " " )
for i , item in enumerate ( remove_overlays , 1 ) :
for i , item in enumerate ( remove_overlays , 1 ) :
logger . ghost ( f " Restoring: { i } / { len ( remove_overlays ) } { item . title } " )
logger . ghost ( f " Restoring: { i } / { len ( remove_overlays ) } { item . title } " )
clean_name , _ = util . validate_filename ( item . title )
clean_name , _ = util . validate_filename ( item . title )
@ -86,29 +112,33 @@ class Overlays:
folder_name = clean_name if self . library . asset_folders else None ,
folder_name = clean_name if self . library . asset_folders else None ,
prefix = f " { item . title } ' s "
prefix = f " { item . title } ' s "
)
)
poster_location = None
is_url = False
is_url = False
original = None
if poster :
if poster :
poster_location = poster . location
poster_location = poster . location
elif os . path . exists ( os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .png " ) ) :
elif os . path . exists ( os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .png " ) ) :
poster_location = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .png " )
original = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .png " )
poster_location = original
elif os . path . exists ( os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .jpg " ) ) :
elif os . path . exists ( os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .jpg " ) ) :
poster_location = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .jpg " )
original = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .jpg " )
poster_location = original
else :
else :
is_url = True
is_url = True
if self . library . is_movie :
poster_location = find_poster_url ( item )
if item . ratingKey in self . library . movie_rating_key_map :
poster_location = self . config . TMDb . get_movie ( self . library . movie_rating_key_map [ item . ratingKey ] ) . poster_url
elif self . library . is_show :
if item . ratingKey in self . library . show_rating_key_map :
poster_location = self . config . TMDb . get_show ( self . config . Convert . tvdb_to_tmdb ( self . library . show_rating_key_map [ item . ratingKey ] ) ) . poster_url
if poster_location :
if poster_location :
self . library . upload_poster ( item , poster_location , url = is_url )
self . library . upload_poster ( item , poster_location , url = is_url )
self . library . edit_tags ( " label " , item , remove_tags = [ " Overlay " ] )
self . library . edit_tags ( " label " , item , remove_tags = [ " Overlay " ] )
if original :
os . remove ( original )
else :
else :
logger . error ( f " No Poster found to restore for { item . title } " )
logger . error ( f " No Poster found to restore for { item . title } " )
logger . exorcise ( )
logger . exorcise ( )
if item_overlays :
logger . info ( " " )
logger . separator ( f " Applying Overlays for the { self . library . name } Library " )
logger . info ( " " )
for i , ( over_key , over_names ) in enumerate ( item_overlays . items ( ) , 1 ) :
for i , ( over_key , over_names ) in enumerate ( item_overlays . items ( ) , 1 ) :
try :
try :
item = item_keys [ over_key ]
item = item_keys [ over_key ]
@ -136,43 +166,38 @@ class Overlays:
folder_name = clean_name if self . library . asset_folders else None ,
folder_name = clean_name if self . library . asset_folders else None ,
prefix = f " { item . title } ' s "
prefix = f " { item . title } ' s "
)
)
has_original = False
has_original = False
changed_image = False
changed_image = False
new_backup = None
if poster :
if poster :
if image_compare and str ( poster . compare ) != str ( image_compare ) :
if image_compare and str ( poster . compare ) != str ( image_compare ) :
changed_image = True
changed_image = True
el se:
el if ha s_ov erlay :
if os . path . exists ( os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .png " ) ) :
if os . path . exists ( os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .png " ) ) :
has_original = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .png " )
has_original = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .png " )
elif os . path . exists ( os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .jpg " ) ) :
elif os . path . exists ( os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .jpg " ) ) :
has_original = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .jpg " )
has_original = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } .jpg " )
else :
else :
changed_image = True
self . library . reload ( item )
self . library . reload ( item )
poster_url = item . posterUrl
new_backup = find_poster_url ( item )
if has_overlay :
if new_backup is None :
if self . library . is_movie :
new_backup = item . posterUrl
if item . ratingKey in self . library . movie_rating_key_map :
else :
poster_url = self . config . TMDb . get_movie ( self . library . movie_rating_key_map [ item . ratingKey ] ) . poster_url
self . library . reload ( item )
elif self . library . is_show :
new_backup = item . posterUrl
check_key = item . ratingKey if isinstance ( item , Show ) else item . show ( ) . ratingKey
if new_backup :
tmdb_id = self . config . Convert . tvdb_to_tmdb ( self . library . show_rating_key_map [ check_key ] )
changed_image = True
if isinstance ( item , Show ) and item . ratingKey in self . library . show_rating_key_map :
image_response = self . config . get ( new_backup )
poster_url = self . config . TMDb . get_show ( tmdb_id ) . poster_url
if image_response . status_code > = 400 :
elif isinstance ( item , Season ) :
raise Failed ( f " Overlay Error: Poster Download Failed for { item . title } " )
poster_url = self . config . TMDb . get_season ( tmdb_id , item . seasonNumber ) . poster_url
i_ext = " jpg " if image_response . headers [ " Content-Type " ] == " image/jpeg " else " png "
elif isinstance ( item , Episode ) :
backup_image_path = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } . { i_ext } " )
poster_url = self . config . TMDb . get_episode ( tmdb_id , item . seasonNumber , item . episodeNumber ) . still_url
with open ( backup_image_path , " wb " ) as handler :
response = self . config . get ( poster_url )
handler . write ( image_response . content )
if response . status_code > = 400 :
while util . is_locked ( backup_image_path ) :
raise Failed ( f " Overlay Error: Poster Download Failed for { item . title } " )
time . sleep ( 1 )
ext = " jpg " if response . headers [ " Content-Type " ] == " image/jpeg " else " png "
has_original = backup_image_path
backup_image = os . path . join ( self . library . overlay_backup , f " { item . ratingKey } . { ext } " )
with open ( backup_image , " wb " ) as handler :
handler . write ( response . content )
while util . is_locked ( backup_image ) :
time . sleep ( 1 )
has_original = backup_image
poster_uploaded = False
poster_uploaded = False
if changed_image or overlay_change :
if changed_image or overlay_change :