@ -66,19 +66,19 @@ class Renamer(Plugin):
downloader = kwargs . get ( ' downloader ' )
downloader = kwargs . get ( ' downloader ' )
download_id = kwargs . get ( ' download_id ' )
download_id = kwargs . get ( ' download_id ' )
download_info = { ' folder ' : movie_folder } if movie_folder else None
release_ download = { ' folder ' : movie_folder } if movie_folder else None
if download_info :
if release_ download:
download_info . update ( { ' id ' : download_id , ' downloader ' : downloader } if download_id else { } )
release_ download. update ( { ' id ' : download_id , ' downloader ' : downloader } if download_id else { } )
fire_handle = fireEvent if not async else fireEventAsync
fire_handle = fireEvent if not async else fireEventAsync
fire_handle ( ' renamer.scan ' , download_info )
fire_handle ( ' renamer.scan ' , release_ download)
return {
return {
' success ' : True
' success ' : True
}
}
def scan ( self , download_info = None ) :
def scan ( self , release_ download = None ) :
if self . isDisabled ( ) :
if self . isDisabled ( ) :
return
return
@ -87,7 +87,7 @@ class Renamer(Plugin):
log . info ( ' Renamer is already running, if you see this often, check the logs above for errors. ' )
log . info ( ' Renamer is already running, if you see this often, check the logs above for errors. ' )
return
return
movie_folder = download_info and download_info . get ( ' folder ' )
movie_folder = release_ download and release_ download. get ( ' folder ' )
# Check to see if the "to" folder is inside the "from" folder.
# Check to see if the "to" folder is inside the "from" folder.
if movie_folder and not os . path . isdir ( movie_folder ) or not os . path . isdir ( self . conf ( ' from ' ) ) or not os . path . isdir ( self . conf ( ' to ' ) ) :
if movie_folder and not os . path . isdir ( movie_folder ) or not os . path . isdir ( self . conf ( ' from ' ) ) or not os . path . isdir ( self . conf ( ' to ' ) ) :
@ -102,7 +102,7 @@ class Renamer(Plugin):
return
return
# Make sure a checkSnatched marked all downloads/seeds as such
# Make sure a checkSnatched marked all downloads/seeds as such
if not download_info and self . conf ( ' run_every ' ) > 0 :
if not release_ download and self . conf ( ' run_every ' ) > 0 :
fireEvent ( ' renamer.check_snatched ' )
fireEvent ( ' renamer.check_snatched ' )
self . renaming_started = True
self . renaming_started = True
@ -115,8 +115,8 @@ class Renamer(Plugin):
movie_folder = movie_folder . rstrip ( os . path . sep )
movie_folder = movie_folder . rstrip ( os . path . sep )
folder = os . path . dirname ( movie_folder )
folder = os . path . dirname ( movie_folder )
if download_info [ ' files ' ] :
if release_ download[ ' files ' ] :
files = download_info [ ' files ' ] . split ( ' | ' )
files = release_ download[ ' files ' ] . split ( ' | ' )
# If there is only one file in the torrent, the downloader did not create a subfolder
# If there is only one file in the torrent, the downloader did not create a subfolder
if len ( files ) == 1 :
if len ( files ) == 1 :
@ -132,16 +132,16 @@ class Renamer(Plugin):
db = get_session ( )
db = get_session ( )
# Extend the download info with info stored in the downloaded release
# Extend the download info with info stored in the downloaded release
download_info = self . extendDownloadInfo ( download_info )
release_ download = self . extendRelease Download ( release_ download)
# Unpack any archives
# Unpack any archives
extr_files = None
extr_files = None
if self . conf ( ' unrar ' ) :
if self . conf ( ' unrar ' ) :
folder , movie_folder , files , extr_files = self . extractFiles ( folder = folder , movie_folder = movie_folder , files = files ,
folder , movie_folder , files , extr_files = self . extractFiles ( folder = folder , movie_folder = movie_folder , files = files ,
cleanup = self . conf ( ' cleanup ' ) and not self . downloadIsTorrent ( download_info ) )
cleanup = self . conf ( ' cleanup ' ) and not self . downloadIsTorrent ( release_ download) )
groups = fireEvent ( ' scanner.scan ' , folder = folder if folder else self . conf ( ' from ' ) ,
groups = fireEvent ( ' scanner.scan ' , folder = folder if folder else self . conf ( ' from ' ) ,
files = files , download_info = download_info , return_ignored = False , single = True ) or [ ]
files = files , release_ download = release_ download, return_ignored = False , single = True ) or [ ]
folder_name = self . conf ( ' folder_name ' )
folder_name = self . conf ( ' folder_name ' )
file_name = self . conf ( ' file_name ' )
file_name = self . conf ( ' file_name ' )
@ -238,7 +238,7 @@ class Renamer(Plugin):
if file_type is ' nfo ' and not self . conf ( ' rename_nfo ' ) :
if file_type is ' nfo ' and not self . conf ( ' rename_nfo ' ) :
log . debug ( ' Skipping, renaming of %s disabled ' , file_type )
log . debug ( ' Skipping, renaming of %s disabled ' , file_type )
for current_file in group [ ' files ' ] [ file_type ] :
for current_file in group [ ' files ' ] [ file_type ] :
if self . conf ( ' cleanup ' ) and ( not self . downloadIsTorrent ( download_info ) or self . fileIsAdded ( current_file , group ) ) :
if self . conf ( ' cleanup ' ) and ( not self . downloadIsTorrent ( release_ download) or self . fileIsAdded ( current_file , group ) ) :
remove_files . append ( current_file )
remove_files . append ( current_file )
continue
continue
@ -418,7 +418,7 @@ class Renamer(Plugin):
log . debug ( ' Removing leftover files ' )
log . debug ( ' Removing leftover files ' )
for current_file in group [ ' files ' ] [ ' leftover ' ] :
for current_file in group [ ' files ' ] [ ' leftover ' ] :
if self . conf ( ' cleanup ' ) and not self . conf ( ' move_leftover ' ) and \
if self . conf ( ' cleanup ' ) and not self . conf ( ' move_leftover ' ) and \
( not self . downloadIsTorrent ( download_info ) or self . fileIsAdded ( current_file , group ) ) :
( not self . downloadIsTorrent ( release_ download) or self . fileIsAdded ( current_file , group ) ) :
remove_files . append ( current_file )
remove_files . append ( current_file )
# Remove files
# Remove files
@ -464,14 +464,14 @@ class Renamer(Plugin):
self . makeDir ( os . path . dirname ( dst ) )
self . makeDir ( os . path . dirname ( dst ) )
try :
try :
self . moveFile ( src , dst , forcemove = not self . downloadIsTorrent ( download_info ) or self . fileIsAdded ( src , group ) )
self . moveFile ( src , dst , forcemove = not self . downloadIsTorrent ( release_ download) or self . fileIsAdded ( src , group ) )
group [ ' renamed_files ' ] . append ( dst )
group [ ' renamed_files ' ] . append ( dst )
except :
except :
log . error ( ' Failed moving the file " %s " : %s ' , ( os . path . basename ( src ) , traceback . format_exc ( ) ) )
log . error ( ' Failed moving the file " %s " : %s ' , ( os . path . basename ( src ) , traceback . format_exc ( ) ) )
self . tagDir ( group , ' failed_rename ' )
self . tagDir ( group , ' failed_rename ' )
# Tag folder if it is in the 'from' folder and it will not be removed because it is a torrent
# Tag folder if it is in the 'from' folder and it will not be removed because it is a torrent
if self . movieInFromFolder ( movie_folder ) and self . downloadIsTorrent ( download_info ) :
if self . movieInFromFolder ( movie_folder ) and self . downloadIsTorrent ( release_ download) :
self . tagDir ( group , ' renamed_already ' )
self . tagDir ( group , ' renamed_already ' )
# Remove matching releases
# Remove matching releases
@ -482,7 +482,7 @@ class Renamer(Plugin):
except :
except :
log . error ( ' Failed removing %s : %s ' , ( release . identifier , traceback . format_exc ( ) ) )
log . error ( ' Failed removing %s : %s ' , ( release . identifier , traceback . format_exc ( ) ) )
if group [ ' dirname ' ] and group [ ' parentdir ' ] and not self . downloadIsTorrent ( download_info ) :
if group [ ' dirname ' ] and group [ ' parentdir ' ] and not self . downloadIsTorrent ( release_ download) :
if movie_folder :
if movie_folder :
# Delete the movie folder
# Delete the movie folder
group_folder = movie_folder
group_folder = movie_folder
@ -678,14 +678,14 @@ Remove it if you want it to be renamed (again, or at least let it try again)
Release . status_id . in_ ( [ snatched_status . get ( ' id ' ) , seeding_status . get ( ' id ' ) , missing_status . get ( ' id ' ) ] )
Release . status_id . in_ ( [ snatched_status . get ( ' id ' ) , seeding_status . get ( ' id ' ) , missing_status . get ( ' id ' ) ] )
) . all ( )
) . all ( )
scan_item s = [ ]
scan_release s = [ ]
scan_required = False
scan_required = False
if rels :
if rels :
log . debug ( ' Checking status snatched releases... ' )
log . debug ( ' Checking status snatched releases... ' )
statuse s = fireEvent ( ' download.status ' , merge = True )
release_download s = fireEvent ( ' download.status ' , merge = True )
if not statuse s:
if not release_download s:
log . debug ( ' Download status functionality is not implemented for active downloaders. ' )
log . debug ( ' Download status functionality is not implemented for active downloaders. ' )
scan_required = True
scan_required = True
else :
else :
@ -698,57 +698,57 @@ Remove it if you want it to be renamed (again, or at least let it try again)
nzbname = self . createNzbName ( rel_dict [ ' info ' ] , movie_dict )
nzbname = self . createNzbName ( rel_dict [ ' info ' ] , movie_dict )
found = False
found = False
for item in statuse s:
for release_download in release_download s:
found_release = False
found_release = False
if rel_dict [ ' info ' ] . get ( ' download_id ' ) :
if rel_dict [ ' info ' ] . get ( ' download_id ' ) :
if item [ ' id ' ] == rel_dict [ ' info ' ] [ ' download_id ' ] and item [ ' downloader ' ] == rel_dict [ ' info ' ] [ ' download_downloader ' ] :
if release_download [ ' id ' ] == rel_dict [ ' info ' ] [ ' download_id ' ] and release_download [ ' downloader ' ] == rel_dict [ ' info ' ] [ ' download_downloader ' ] :
log . debug ( ' Found release by id: %s ' , item [ ' id ' ] )
log . debug ( ' Found release by id: %s ' , release_download [ ' id ' ] )
found_release = True
found_release = True
else :
else :
if item [ ' name ' ] == nzbname or rel_dict [ ' info ' ] [ ' name ' ] in item [ ' name ' ] or getImdb ( item [ ' name ' ] ) == movie_dict [ ' library ' ] [ ' identifier ' ] :
if release_download [ ' name ' ] == nzbname or rel_dict [ ' info ' ] [ ' name ' ] in release_download [ ' name ' ] or getImdb ( release_download [ ' name ' ] ) == movie_dict [ ' library ' ] [ ' identifier ' ] :
found_release = True
found_release = True
if found_release :
if found_release :
timeleft = ' N/A ' if item [ ' timeleft ' ] == - 1 else item [ ' timeleft ' ]
timeleft = ' N/A ' if release_download [ ' timeleft ' ] == - 1 else release_download [ ' timeleft ' ]
log . debug ( ' Found %s : %s , time to go: %s ' , ( item [ ' name ' ] , item [ ' status ' ] . upper ( ) , timeleft ) )
log . debug ( ' Found %s : %s , time to go: %s ' , ( release_download [ ' name ' ] , release_download [ ' status ' ] . upper ( ) , timeleft ) )
if item [ ' status ' ] == ' busy ' :
if release_download [ ' status ' ] == ' busy ' :
# Set the release to snatched if it was missing before
# Set the release to snatched if it was missing before
fireEvent ( ' release.update_status ' , rel . id , status = snatched_status , single = True )
fireEvent ( ' release.update_status ' , rel . id , status = snatched_status , single = True )
# Tag folder if it is in the 'from' folder and it will not be processed because it is still downloading
# Tag folder if it is in the 'from' folder and it will not be processed because it is still downloading
if item [ ' folder ' ] and self . conf ( ' from ' ) in item [ ' folder ' ] :
if release_download [ ' folder ' ] and self . conf ( ' from ' ) in release_download [ ' folder ' ] :
self . tagDir ( item [ ' folder ' ] , ' downloading ' )
self . tagDir ( release_download [ ' folder ' ] , ' downloading ' )
elif item [ ' status ' ] == ' seeding ' :
elif release_download [ ' status ' ] == ' seeding ' :
# Set the release to seeding
# Set the release to seeding
fireEvent ( ' release.update_status ' , rel . id , status = seeding_status , single = True )
fireEvent ( ' release.update_status ' , rel . id , status = seeding_status , single = True )
#If linking setting is enabled, process release
#If linking setting is enabled, process release
if self . conf ( ' file_action ' ) != ' move ' and not rel . status_id == seeding_status . get ( ' id ' ) and self . statusInfoComplete ( item ) :
if self . conf ( ' file_action ' ) != ' move ' and not rel . status_id == seeding_status . get ( ' id ' ) and self . statusInfoComplete ( release_download ) :
log . info ( ' Download of %s completed! It is now being processed while leaving the original files alone for seeding. Current ratio: %s . ' , ( item [ ' name ' ] , item [ ' seed_ratio ' ] ) )
log . info ( ' Download of %s completed! It is now being processed while leaving the original files alone for seeding. Current ratio: %s . ' , ( release_download [ ' name ' ] , release_download [ ' seed_ratio ' ] ) )
# Remove the downloading tag
# Remove the downloading tag
self . untagDir ( item [ ' folder ' ] , ' downloading ' )
self . untagDir ( release_download [ ' folder ' ] , ' downloading ' )
# Scan and set the torrent to paused if required
# Scan and set the torrent to paused if required
item . update ( { ' pause ' : True , ' scan ' : True , ' process_complete ' : False } )
release_download . update ( { ' pause ' : True , ' scan ' : True , ' process_complete ' : False } )
scan_item s . append ( item )
scan_release s . append ( release_download )
else :
else :
#let it seed
#let it seed
log . debug ( ' %s is seeding with ratio: %s ' , ( item [ ' name ' ] , item [ ' seed_ratio ' ] ) )
log . debug ( ' %s is seeding with ratio: %s ' , ( release_download [ ' name ' ] , release_download [ ' seed_ratio ' ] ) )
elif item [ ' status ' ] == ' failed ' :
elif release_download [ ' status ' ] == ' failed ' :
# Set the release to failed
# Set the release to failed
fireEvent ( ' release.update_status ' , rel . id , status = failed_status , single = True )
fireEvent ( ' release.update_status ' , rel . id , status = failed_status , single = True )
fireEvent ( ' download.remove_failed ' , item , single = True )
fireEvent ( ' download.remove_failed ' , release_download , single = True )
if self . conf ( ' next_on_failed ' ) :
if self . conf ( ' next_on_failed ' ) :
fireEvent ( ' movie.searcher.try_next_release ' , movie_id = rel . movie_id )
fireEvent ( ' movie.searcher.try_next_release ' , movie_id = rel . movie_id )
elif item [ ' status ' ] == ' completed ' :
elif release_download [ ' status ' ] == ' completed ' :
log . info ( ' Download of %s completed! ' , item [ ' name ' ] )
log . info ( ' Download of %s completed! ' , release_download [ ' name ' ] )
if self . statusInfoComplete ( item ) :
if self . statusInfoComplete ( release_download ) :
# If the release has been seeding, process now the seeding is done
# If the release has been seeding, process now the seeding is done
if rel . status_id == seeding_status . get ( ' id ' ) :
if rel . status_id == seeding_status . get ( ' id ' ) :
@ -757,26 +757,26 @@ Remove it if you want it to be renamed (again, or at least let it try again)
fireEvent ( ' release.update_status ' , rel . id , status = downloaded_status , single = True )
fireEvent ( ' release.update_status ' , rel . id , status = downloaded_status , single = True )
# Allow the downloader to clean-up
# Allow the downloader to clean-up
item . update ( { ' pause ' : False , ' scan ' : False , ' process_complete ' : True } )
release_download . update ( { ' pause ' : False , ' scan ' : False , ' process_complete ' : True } )
scan_item s . append ( item )
scan_release s . append ( release_download )
else :
else :
# Set the release to snatched so that the renamer can process the release as if it was never seeding
# Set the release to snatched so that the renamer can process the release as if it was never seeding
fireEvent ( ' release.update_status ' , rel . id , status = snatched_status , single = True )
fireEvent ( ' release.update_status ' , rel . id , status = snatched_status , single = True )
# Scan and Allow the downloader to clean-up
# Scan and Allow the downloader to clean-up
item . update ( { ' pause ' : False , ' scan ' : True , ' process_complete ' : True } )
release_download . update ( { ' pause ' : False , ' scan ' : True , ' process_complete ' : True } )
scan_item s . append ( item )
scan_release s . append ( release_download )
else :
else :
# Set the release to snatched if it was missing before
# Set the release to snatched if it was missing before
fireEvent ( ' release.update_status ' , rel . id , status = snatched_status , single = True )
fireEvent ( ' release.update_status ' , rel . id , status = snatched_status , single = True )
# Remove the downloading tag
# Remove the downloading tag
self . untagDir ( item [ ' folder ' ] , ' downloading ' )
self . untagDir ( release_download [ ' folder ' ] , ' downloading ' )
# Scan and Allow the downloader to clean-up
# Scan and Allow the downloader to clean-up
item . update ( { ' pause ' : False , ' scan ' : True , ' process_complete ' : True } )
release_download . update ( { ' pause ' : False , ' scan ' : True , ' process_complete ' : True } )
scan_item s . append ( item )
scan_release s . append ( release_download )
else :
else :
scan_required = True
scan_required = True
@ -798,21 +798,21 @@ Remove it if you want it to be renamed (again, or at least let it try again)
log . error ( ' Failed checking for release in downloader: %s ' , traceback . format_exc ( ) )
log . error ( ' Failed checking for release in downloader: %s ' , traceback . format_exc ( ) )
# The following can either be done here, or inside the scanner if we pass it scan_items in one go
# The following can either be done here, or inside the scanner if we pass it scan_items in one go
for item in scan_item s:
for release_download in scan_release s:
# Ask the renamer to scan the item
# Ask the renamer to scan the item
if item [ ' scan ' ] :
if release_download [ ' scan ' ] :
if item [ ' pause ' ] and self . conf ( ' file_action ' ) == ' link ' :
if release_download [ ' pause ' ] and self . conf ( ' file_action ' ) == ' link ' :
fireEvent ( ' download.pause ' , item = item , pause = True , single = True )
fireEvent ( ' download.pause ' , release_download = release_download , pause = True , single = True )
fireEvent ( ' renamer.scan ' , download_info = item )
fireEvent ( ' renamer.scan ' , release_download = release_download )
if item [ ' pause ' ] and self . conf ( ' file_action ' ) == ' link ' :
if release_download [ ' pause ' ] and self . conf ( ' file_action ' ) == ' link ' :
fireEvent ( ' download.pause ' , item = item , pause = False , single = True )
fireEvent ( ' download.pause ' , release_download = release_download , pause = False , single = True )
if item [ ' process_complete ' ] :
if release_download [ ' process_complete ' ] :
#First make sure the files were succesfully processed
#First make sure the files were succesfully processed
if not self . hastagDir ( item [ ' folder ' ] , ' failed_rename ' ) :
if not self . hastagDir ( release_download [ ' folder ' ] , ' failed_rename ' ) :
# Remove the seeding tag if it exists
# Remove the seeding tag if it exists
self . untagDir ( item [ ' folder ' ] , ' renamed_already ' )
self . untagDir ( release_download [ ' folder ' ] , ' renamed_already ' )
# Ask the downloader to process the item
# Ask the downloader to process the item
fireEvent ( ' download.process_complete ' , item = item , single = True )
fireEvent ( ' download.process_complete ' , release_download = release_download , single = True )
if scan_required :
if scan_required :
fireEvent ( ' renamer.scan ' )
fireEvent ( ' renamer.scan ' )
@ -821,16 +821,16 @@ Remove it if you want it to be renamed (again, or at least let it try again)
return True
return True
def extendDownloadInfo ( self , download_info ) :
def extendRelease Download ( self , release_ download) :
rls = None
rls = None
if download_info and download_info . get ( ' id ' ) and download_info . get ( ' downloader ' ) :
if release_ download and release_ download. get ( ' id ' ) and release_ download. get ( ' downloader ' ) :
db = get_session ( )
db = get_session ( )
rlsnfo_dwnlds = db . query ( ReleaseInfo ) . filter_by ( identifier = ' download_downloader ' , value = download_info . get ( ' downloader ' ) ) . all ( )
rlsnfo_dwnlds = db . query ( ReleaseInfo ) . filter_by ( identifier = ' download_downloader ' , value = release_ download. get ( ' downloader ' ) ) . all ( )
rlsnfo_ids = db . query ( ReleaseInfo ) . filter_by ( identifier = ' download_id ' , value = download_info . get ( ' id ' ) ) . all ( )
rlsnfo_ids = db . query ( ReleaseInfo ) . filter_by ( identifier = ' download_id ' , value = release_ download. get ( ' id ' ) ) . all ( )
for rlsnfo_dwnld in rlsnfo_dwnlds :
for rlsnfo_dwnld in rlsnfo_dwnlds :
for rlsnfo_id in rlsnfo_ids :
for rlsnfo_id in rlsnfo_ids :
@ -840,29 +840,29 @@ Remove it if you want it to be renamed (again, or at least let it try again)
if rls : break
if rls : break
if not rls :
if not rls :
log . error ( ' Download ID %s from downloader %s not found in releases ' , ( download_info . get ( ' id ' ) , download_info . get ( ' downloader ' ) ) )
log . error ( ' Download ID %s from downloader %s not found in releases ' , ( release_ download. get ( ' id ' ) , release_ download. get ( ' downloader ' ) ) )
if rls :
if rls :
rls_dict = rls . to_dict ( { ' info ' : { } } )
rls_dict = rls . to_dict ( { ' info ' : { } } )
download_info . update ( {
release_ download. update ( {
' imdb_id ' : rls . movie . library . identifier ,
' imdb_id ' : rls . movie . library . identifier ,
' quality ' : rls . quality . identifier ,
' quality ' : rls . quality . identifier ,
' protocol ' : rls_dict . get ( ' info ' , { } ) . get ( ' protocol ' ) or rls_dict . get ( ' info ' , { } ) . get ( ' type ' ) ,
' protocol ' : rls_dict . get ( ' info ' , { } ) . get ( ' protocol ' ) or rls_dict . get ( ' info ' , { } ) . get ( ' type ' ) ,
} )
} )
return download_info
return release_ download
def downloadIsTorrent ( self , download_info ) :
def downloadIsTorrent ( self , release_ download) :
return download_info and download_info . get ( ' protocol ' ) in [ ' torrent ' , ' torrent_magnet ' ]
return release_ download and release_ download. get ( ' protocol ' ) in [ ' torrent ' , ' torrent_magnet ' ]
def fileIsAdded ( self , src , group ) :
def fileIsAdded ( self , src , group ) :
if not group or not group . get ( ' before_rename ' ) :
if not group or not group . get ( ' before_rename ' ) :
return False
return False
return src in group [ ' before_rename ' ]
return src in group [ ' before_rename ' ]
def statusInfoComplete ( self , item ) :
def statusInfoComplete ( self , release_download ) :
return item [ ' id ' ] and item [ ' downloader ' ] and item [ ' folder ' ]
return release_download [ ' id ' ] and release_download [ ' downloader ' ] and release_download [ ' folder ' ]
def movieInFromFolder ( self , movie_folder ) :
def movieInFromFolder ( self , movie_folder ) :
return movie_folder and self . conf ( ' from ' ) in movie_folder or not movie_folder
return movie_folder and self . conf ( ' from ' ) in movie_folder or not movie_folder