@ -3,7 +3,7 @@ from couchpotato.api import addApiView
from couchpotato . core . event import addEvent , fireEvent , fireEventAsync
from couchpotato . core . helpers . encoding import toUnicode , ss
from couchpotato . core . helpers . variable import getExt , mergeDicts , getTitle , \
getImdb , link , symlink , tryInt
getImdb , link , symlink , tryInt , splitString
from couchpotato . core . logger import CPLog
from couchpotato . core . plugins . base import Plugin
from couchpotato . core . settings . model import Library , File , Profile , Release , \
@ -31,8 +31,9 @@ class Renamer(Plugin):
' params ' : {
' async ' : { ' desc ' : ' Optional: Set to 1 if you dont want to fire the renamer.scan asynchronous. ' } ,
' movie_folder ' : { ' desc ' : ' Optional: The folder of the movie to scan. Keep empty for default renamer folder. ' } ,
' downloader ' : { ' desc ' : ' Optional: The downloader this movie has been downloaded with ' } ,
' download_id ' : { ' desc ' : ' Optional: The downloader \' s nzb/torrent ID ' } ,
' downloader ' : { ' desc ' : ' Optional: The downloader the release has been downloaded with. \' download_id \' is required with this option. ' } ,
' download_id ' : { ' desc ' : ' Optional: The nzb/torrent ID of the release in movie_folder. \' downloader \' is required with this option. ' } ,
' status ' : { ' desc ' : ' Optional: The status of the release: \' completed \' (default) or \' seeding \' ' } ,
} ,
} )
@ -65,20 +66,21 @@ class Renamer(Plugin):
movie_folder = kwargs . get ( ' movie_folder ' )
downloader = kwargs . get ( ' downloader ' )
download_id = kwargs . get ( ' download_id ' )
status = kwargs . get ( ' status ' , ' completed ' )
download_info = { ' folder ' : movie_folder } if movie_folder else None
if download_info :
download_info . update ( { ' id ' : download_id , ' downloader ' : downloader } if download_id else { } )
release_ download = { ' folder ' : movie_folder } if movie_folder else None
if release_ download:
release_ download. update ( { ' id ' : download_id , ' downloader ' : downloader , ' status ' : status } if download_id else { } )
fire_handle = fireEvent if not async else fireEventAsync
fire_handle ( ' renamer.scan ' , download_info )
fire_handle ( ' renamer.scan ' , release_ download)
return {
' success ' : True
}
def scan ( self , download_info = None ) :
def scan ( self , release_ download = None ) :
if self . isDisabled ( ) :
return
@ -87,22 +89,41 @@ class Renamer(Plugin):
log . info ( ' Renamer is already running, if you see this often, check the logs above for errors. ' )
return
movie_folder = download_info and download_info . get ( ' folder ' )
# Get movie folder to process
movie_folder = release_download and release_download . get ( ' 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 ' ) ) :
l = log . debug if movie_folder else log . error
l ( ' Both the " To " and " From " have to exist. ' )
return
elif self . conf ( ' from ' ) in self . conf ( ' to ' ) :
log . error ( ' The " to " can \' t be inside of the " from " folder. You \' ll get an infinite loop. ' )
# Get all folders that should not be processed
no_process = [ self . conf ( ' to ' ) ]
cat_list = fireEvent ( ' category.all ' , single = True ) or [ ]
no_process . extend ( [ item [ ' destination ' ] for item in cat_list ] )
try :
if Env . setting ( ' library ' , section = ' manage ' ) . strip ( ) :
no_process . extend ( splitString ( Env . setting ( ' library ' , section = ' manage ' ) , ' :: ' ) )
except :
pass
# Check to see if the no_process folders are inside the "from" folder.
if not os . path . isdir ( self . conf ( ' from ' ) ) or not os . path . isdir ( self . conf ( ' to ' ) ) :
log . error ( ' Both the " To " and " From " have to exist. ' )
return
elif movie_folder and movie_folder in [ self . conf ( ' to ' ) , self . conf ( ' from ' ) ] :
log . error ( ' The " to " and " from " folders can \' t be inside of or the same as the provided movie folder. ' )
else :
for item in no_process :
if self . conf ( ' from ' ) in item :
log . error ( ' To protect your data, the movie libraries can \' t be inside of or the same as the " from " folder. ' )
return
# Check to see if the no_process folders are inside the provided movie_folder
if movie_folder and not os . path . isdir ( movie_folder ) :
log . error ( ' The provided movie folder %s does not exist. ' , movie_folder )
return
elif movie_folder :
for item in no_process :
if movie_folder in item :
log . error ( ' To protect your data, the movie libraries can \' t be inside of or the same as the provided movie folder. ' )
return
# 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 ' )
self . renaming_started = True
@ -115,26 +136,33 @@ class Renamer(Plugin):
movie_folder = movie_folder . rstrip ( os . path . sep )
folder = os . path . dirname ( movie_folder )
# Get all files from the specified folder
try :
for root , folders , names in os . walk ( movie_folder ) :
files . extend ( [ os . path . join ( root , name ) for name in names ] )
except :
log . error ( ' Failed getting files from %s : %s ' , ( movie_folder , traceback . format_exc ( ) ) )
if release_download [ ' files ' ] :
files = release_download [ ' files ' ] . split ( ' | ' )
# If there is only one file in the torrent, the downloader did not create a subfolder
if len ( files ) == 1 :
folder = movie_folder
else :
# Get all files from the specified folder
try :
for root , folders , names in os . walk ( movie_folder ) :
files . extend ( [ os . path . join ( root , name ) for name in names ] )
except :
log . error ( ' Failed getting files from %s : %s ' , ( movie_folder , traceback . format_exc ( ) ) )
db = get_session ( )
# 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
extr_files = None
if self . conf ( ' unrar ' ) :
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 ' ) ,
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 ' )
file_name = self . conf ( ' file_name ' )
@ -142,9 +170,9 @@ class Renamer(Plugin):
nfo_name = self . conf ( ' nfo_name ' )
separator = self . conf ( ' separator ' )
# Statuss es
done_status , active_status , downloaded_status , snatched_status = \
fireEvent ( ' status.get ' , [ ' done ' , ' active ' , ' downloaded ' , ' snatched ' ] , single = True )
# Statuses
done_status , active_status , downloaded_status , snatched_status , seeding_status = \
fireEvent ( ' status.get ' , [ ' done ' , ' active ' , ' downloaded ' , ' snatched ' , ' seeding ' ] , single = True )
for group_identifier in groups :
@ -157,7 +185,7 @@ class Renamer(Plugin):
# Add _UNKNOWN_ if no library item is connected
if not group [ ' library ' ] or not movie_title :
self . tagDir ( group , ' unknown ' )
self . tagRelease ( group = group , tag = ' unknown ' )
continue
# Rename the files using the library data
else :
@ -195,7 +223,7 @@ class Renamer(Plugin):
if extr_files :
group [ ' before_rename ' ] . extend ( extr_files )
# Remove weird chars from moviename
# Remove weird chars from movie name
movie_name = re . sub ( r " [ \ x00 \ / \\ : \ * \ ? \" <> \ |] " , ' ' , movie_title )
# Put 'The' at the end
@ -231,7 +259,7 @@ class Renamer(Plugin):
if file_type is ' nfo ' and not self . conf ( ' rename_nfo ' ) :
log . debug ( ' Skipping, renaming of %s disabled ' , 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 )
continue
@ -391,7 +419,7 @@ class Renamer(Plugin):
log . info ( ' Better quality release already exists for %s , with quality %s ' , ( movie . library . titles [ 0 ] . title , release . quality . label ) )
# Add exists tag to the .ignore file
self . tagDir ( group , ' exists ' )
self . tagRelease ( group = group , tag = ' exists ' )
# Notify on rename fail
download_message = ' Renaming of %s ( %s ) cancelled, exists in %s already. ' % ( movie . library . titles [ 0 ] . title , group [ ' meta_data ' ] [ ' quality ' ] [ ' label ' ] , release . quality . label )
@ -399,10 +427,20 @@ class Renamer(Plugin):
remove_leftovers = False
break
elif release . status_id is snatched_status . get ( ' id ' ) :
if release . quality . id is group [ ' meta_data ' ] [ ' quality ' ] [ ' id ' ] :
# Set the release to downloaded
fireEvent ( ' release.update_status ' , release . id , status = downloaded_status , single = True )
elif release . status_id in [ snatched_status . get ( ' id ' ) , seeding_status . get ( ' id ' ) ] :
if release_download and release_download . get ( ' rls_id ' ) :
if release_download [ ' rls_id ' ] == release . id :
if release_download [ ' status ' ] == ' completed ' :
# Set the release to downloaded
fireEvent ( ' release.update_status ' , release . id , status = downloaded_status , single = True )
elif release_download [ ' status ' ] == ' seeding ' :
# Set the release to seeding
fireEvent ( ' release.update_status ' , release . id , status = seeding_status , single = True )
elif release . quality . id is group [ ' meta_data ' ] [ ' quality ' ] [ ' id ' ] :
# Set the release to downloaded
fireEvent ( ' release.update_status ' , release . id , status = downloaded_status , single = True )
# Remove leftover files
if not remove_leftovers : # Don't remove anything
@ -411,7 +449,7 @@ class Renamer(Plugin):
log . debug ( ' Removing leftover files ' )
for current_file in group [ ' files ' ] [ ' leftover ' ] :
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
@ -437,7 +475,7 @@ class Renamer(Plugin):
except :
log . error ( ' Failed removing %s : %s ' , ( src , traceback . format_exc ( ) ) )
self . tagDir ( group , ' failed_remove ' )
self . tagRelease ( group = group , tag = ' failed_remove ' )
# Delete leftover folder from older releases
for delete_folder in delete_folders :
@ -457,15 +495,15 @@ class Renamer(Plugin):
self . makeDir ( os . path . dirname ( dst ) )
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 )
except :
log . error ( ' Failed moving the file " %s " : %s ' , ( os . path . basename ( src ) , traceback . format_exc ( ) ) )
self . tagDir ( group , ' failed_rename ' )
self . tagRelease ( group = group , tag = ' failed_rename ' )
# 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 ) :
self . tagDir ( group , ' renamed_already ' )
if self . movieInFromFolder ( movie_folder ) and self . downloadIsTorrent ( release_ download) :
self . tagRelease ( group = group , tag = ' renamed_already ' )
# Remove matching releases
for release in remove_releases :
@ -475,7 +513,7 @@ class Renamer(Plugin):
except :
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 :
# Delete the movie folder
group_folder = movie_folder
@ -522,18 +560,9 @@ class Renamer(Plugin):
return rename_files
# This adds a file to ignore / tag a release so it is ignored later
def tagDir ( self , group , tag ) :
ignore_file = None
if isinstance ( group , dict ) :
for movie_file in sorted ( list ( group [ ' files ' ] [ ' movie ' ] ) ) :
ignore_file = ' %s . %s .ignore ' % ( os . path . splitext ( movie_file ) [ 0 ] , tag )
break
else :
if not os . path . isdir ( group ) or not tag :
return
ignore_file = os . path . join ( group , ' %s .ignore ' % tag )
def tagRelease ( self , tag , group = None , release_download = None ) :
if not tag :
return
text = """ This file is from CouchPotato
It has marked this release as " %s "
@ -541,25 +570,88 @@ This file hides the release from the renamer
Remove it if you want it to be renamed ( again , or at least let it try again )
""" % tag
if ignore_file :
self . createFile ( ignore_file , text )
tag_files = [ ]
def untagDir ( self , folder , tag = ' ' ) :
if not os . path . isdir ( folder ) :
# Tag movie files if they are known
if isinstance ( group , dict ) :
tag_files = [ sorted ( list ( group [ ' files ' ] [ ' movie ' ] ) ) [ 0 ] ]
elif isinstance ( release_download , dict ) :
# Tag download_files if they are known
if release_download [ ' files ' ] :
tag_files = release_download [ ' files ' ] . split ( ' | ' )
# Tag all files in release folder
else :
for root , folders , names in os . walk ( release_download [ ' folder ' ] ) :
tag_files . extend ( [ os . path . join ( root , name ) for name in names ] )
for filename in tag_files :
tag_filename = ' %s . %s .ignore ' % ( os . path . splitext ( filename ) [ 0 ] , tag )
if not os . path . isfile ( tag_filename ) :
self . createFile ( tag_filename , text )
def untagRelease ( self , release_download , tag = ' ' ) :
if not release_download :
return
# Remove any .ignore files
tag_files = [ ]
folder = release_download [ ' folder ' ]
if not os . path . isdir ( folder ) :
return False
# Untag download_files if they are known
if release_download [ ' files ' ] :
tag_files = release_download [ ' files ' ] . split ( ' | ' )
# Untag all files in release folder
else :
for root , folders , names in os . walk ( release_download [ ' folder ' ] ) :
tag_files . extend ( [ os . path . join ( root , name ) for name in names if not os . path . splitext ( name ) [ 1 ] == ' .ignore ' ] )
# Find all .ignore files in folder
ignore_files = [ ]
for root , dirnames , filenames in os . walk ( folder ) :
for filename in fnmatch . filter ( filenames , ' * %s .ignore ' % tag ) :
os . remove ( ( os . path . join ( root , filename ) ) )
ignore_files . extend ( fnmatch . filter ( [ os . path . join ( root , filename ) for filename in filenames ] , ' * %s .ignore ' % tag ) )
# Match all found ignore files with the tag_files and delete if found
for tag_file in tag_files :
ignore_file = fnmatch . filter ( ignore_files , ' %s . %s .ignore ' % ( os . path . splitext ( tag_file ) [ 0 ] , tag if tag else ' * ' ) )
for filename in ignore_file :
try :
os . remove ( filename )
except :
log . debug ( ' Unable to remove ignore file: %s . Error: %s . ' % ( filename , traceback . format_exc ( ) ) )
def hastagRelease ( self , release_download , tag = ' ' ) :
if not release_download :
return False
def hastagDir ( self , folder , tag = ' ' ) :
folder = release_download [ ' folder ' ]
if not os . path . isdir ( folder ) :
return False
# Find any .ignore files
tag_files = [ ]
ignore_files = [ ]
# Find tag on download_files if they are known
if release_download [ ' files ' ] :
tag_files = release_download [ ' files ' ] . split ( ' | ' )
# Find tag on all files in release folder
else :
for root , folders , names in os . walk ( release_download [ ' folder ' ] ) :
tag_files . extend ( [ os . path . join ( root , name ) for name in names if not os . path . splitext ( name ) [ 1 ] == ' .ignore ' ] )
# Find all .ignore files in folder
for root , dirnames , filenames in os . walk ( folder ) :
if fnmatch . filter ( filenames , ' * %s .ignore ' % tag ) :
ignore_files . extend ( fnmatch . filter ( [ os . path . join ( root , filename ) for filename in filenames ] , ' * %s .ignore ' % tag ) )
# Match all found ignore files with the tag_files and return True found
for tag_file in tag_files :
ignore_file = fnmatch . filter ( ignore_files , ' %s . %s .ignore ' % ( os . path . splitext ( tag_file ) [ 0 ] , tag if tag else ' * ' ) )
if ignore_file :
return True
return False
@ -578,7 +670,7 @@ Remove it if you want it to be renamed (again, or at least let it try again)
link ( old , dest )
except :
# Try to simlink next
log . debug ( ' Couldn \' t hardlink file " %s " to " %s " . Simlinking instead. Error: %s . ' , ( old , dest , traceback . format_exc ( ) ) )
log . debug ( ' Couldn \' t hardlink file " %s " to " %s " . Simlinking instead. Error: %s . ' , ( old , dest , traceback . format_exc ( ) ) )
shutil . copy ( old , dest )
try :
symlink ( dest , old + ' .link ' )
@ -671,14 +763,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 ' ) ] )
) . all ( )
scan_item s = [ ]
scan_release s = [ ]
scan_required = False
if rels :
log . debug ( ' Checking status snatched releases... ' )
statuse s = fireEvent ( ' download.status ' , merge = True )
if not statuse s:
release_download s = fireEvent ( ' download.status ' , merge = True )
if not release_download s:
log . debug ( ' Download status functionality is not implemented for active downloaders. ' )
scan_required = True
else :
@ -691,85 +783,82 @@ 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 )
found = False
for item in statuse s:
for release_download in release_download s:
found_release = False
if rel_dict [ ' info ' ] . get ( ' download_id ' ) :
if item [ ' id ' ] == rel_dict [ ' info ' ] [ ' download_id ' ] and item [ ' downloader ' ] == rel_dict [ ' info ' ] [ ' download_downloader ' ] :
log . debug ( ' Found release by id: %s ' , item [ ' id ' ] )
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 ' , release_download [ ' id ' ] )
found_release = True
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
if found_release :
timeleft = ' N/A ' if item [ ' timeleft ' ] == - 1 else item [ ' timeleft ' ]
log . debug ( ' Found %s : %s , time to go: %s ' , ( item [ ' name ' ] , item [ ' status ' ] . upper ( ) , timeleft ) )
timeleft = ' N/A ' if release_download [ ' timeleft ' ] == - 1 else release_download [ ' 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
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
if item [ ' folder ' ] and self . conf ( ' from ' ) in item [ ' folder ' ] :
self . tagDir ( item [ ' folder ' ] , ' downloading ' )
elif item [ ' status ' ] == ' seeding ' :
# Set the release to seeding
fireEvent ( ' release.update_status ' , rel . id , status = seeding_status , single = True )
if release_download [ ' folder ' ] and self . conf ( ' from ' ) in release_download [ ' folder ' ] :
self . tagRelease ( release_download = release_download , tag = ' downloading ' )
elif release_download [ ' status ' ] == ' seeding ' :
#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 ) :
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 ' ] ) )
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 . ' , ( release_download [ ' name ' ] , release_download [ ' seed_ratio ' ] ) )
# Remove the downloading tag
self . untagDir ( item [ ' folder ' ] , ' downloading ' )
self . untagRelease ( release_download = release_download , tag = ' downloading ' )
# Scan and set the torrent to paused if required
item . update ( { ' pause ' : True , ' scan ' : True , ' process_complete ' : False } )
scan_item s . append ( item )
release_download . update ( { ' pause ' : True , ' scan ' : True , ' process_complete ' : False } )
scan_release s . append ( release_download )
else :
#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 ' :
# Set the release to seeding
fireEvent ( ' release.update_status ' , rel . id , status = seeding_status , single = True )
elif release_download [ ' status ' ] == ' failed ' :
# Set the release to failed
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 ' ) :
fireEvent ( ' movie.searcher.try_next_release ' , movie_id = rel . movie_id )
elif item [ ' status ' ] == ' completed ' :
log . info ( ' Download of %s completed! ' , item [ ' name ' ] )
if self . statusInfoComplete ( item ) :
elif release_download [ ' status ' ] == ' completed ' :
log . info ( ' Download of %s completed! ' , release_download [ ' name ' ] )
if self . statusInfoComplete ( release_download ) :
# If the release has been seeding, process now the seeding is done
if rel . status_id == seeding_status . get ( ' id ' ) :
if rel . movie . status_id == done_status . get ( ' id ' ) :
if self . conf ( ' file_action ' ) != ' move ' :
# Set the release to done as the movie has already been renamed
fireEvent ( ' release.update_status ' , rel . id , status = downloaded_status , single = True )
# Allow the downloader to clean-up
item . update ( { ' pause ' : False , ' scan ' : False , ' process_complete ' : True } )
scan_item s . append ( item )
release_download . update ( { ' pause ' : False , ' scan ' : False , ' process_complete ' : True } )
scan_release s . append ( release_download )
else :
# 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 )
# Scan and Allow the downloader to clean-up
item . update ( { ' pause ' : False , ' scan ' : True , ' process_complete ' : True } )
scan_item s . append ( item )
release_download . update ( { ' pause ' : False , ' scan ' : True , ' process_complete ' : True } )
scan_releases . append ( release_download )
else :
# Set the release to snatched if it was missing before
fireEvent ( ' release.update_status ' , rel . id , status = snatched_status , single = True )
# Remove the downloading tag
self . untagDir ( item [ ' folder ' ] , ' downloading ' )
self . untagRelease ( release_download = release_download , tag = ' downloading ' )
# Scan and Allow the downloader to clean-up
item . update ( { ' pause ' : False , ' scan ' : True , ' process_complete ' : True } )
scan_item s . append ( item )
release_download . update ( { ' pause ' : False , ' scan ' : True , ' process_complete ' : True } )
scan_release s . append ( release_download )
else :
scan_required = True
@ -791,21 +880,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 ( ) )
# 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
if item [ ' scan ' ] :
if item [ ' pause ' ] and self . conf ( ' file_action ' ) == ' link ' :
fireEvent ( ' download.pause ' , item = item , pause = True , single = True )
fireEvent ( ' renamer.scan ' , download_info = item )
if item [ ' pause ' ] and self . conf ( ' file_action ' ) == ' link ' :
fireEvent ( ' download.pause ' , item = item , pause = False , single = True )
if item [ ' process_complete ' ] :
if release_download [ ' scan ' ] :
if release_download [ ' pause ' ] and self . conf ( ' file_action ' ) == ' link ' :
fireEvent ( ' download.pause ' , release_download = release_download , pause = True , single = True )
fireEvent ( ' renamer.scan ' , release_download = release_download )
if release_download [ ' pause ' ] and self . conf ( ' file_action ' ) == ' link ' :
fireEvent ( ' download.pause ' , release_download = release_download , pause = False , single = True )
if release_download [ ' process_complete ' ] :
#First make sure the files were succesfully processed
if not self . hastagDir ( item [ ' folder ' ] , ' failed_rename ' ) :
if not self . hastagRelease ( release_download = release_download , tag = ' failed_rename ' ) :
# Remove the seeding tag if it exists
self . untagDir ( item [ ' folder ' ] , ' renamed_already ' )
self . untagRelease ( release_download = release_download , tag = ' renamed_already ' )
# 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 :
fireEvent ( ' renamer.scan ' )
@ -814,16 +903,16 @@ Remove it if you want it to be renamed (again, or at least let it try again)
return True
def extendDownloadInfo ( self , download_info ) :
def extendRelease Download ( self , release_ download) :
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 ( )
rlsnfo_dwnlds = db . query ( ReleaseInfo ) . filter_by ( identifier = ' download_downloader ' , value = download_info . get ( ' downloader ' ) ) . all ( )
rlsnfo_ids = db . query ( ReleaseInfo ) . filter_by ( identifier = ' download_id ' , value = download_info . get ( ' id ' ) ) . 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 = release_ download. get ( ' id ' ) ) . all ( )
for rlsnfo_dwnld in rlsnfo_dwnlds :
for rlsnfo_id in rlsnfo_ids :
@ -833,29 +922,30 @@ Remove it if you want it to be renamed (again, or at least let it try again)
if rls : break
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 :
rls_dict = rls . to_dict ( { ' info ' : { } } )
download_info . update ( {
release_ download. update ( {
' imdb_id ' : rls . movie . library . identifier ,
' quality ' : rls . quality . identifier ,
' protocol ' : rls_dict . get ( ' info ' , { } ) . get ( ' protocol ' ) or rls_dict . get ( ' info ' , { } ) . get ( ' type ' ) ,
' rls_id ' : rls . id ,
} )
return download_info
return release_ download
def downloadIsTorrent ( self , download_info ) :
return download_info and download_info . get ( ' protocol ' ) in [ ' torrent ' , ' torrent_magnet ' ]
def downloadIsTorrent ( self , release_ download) :
return release_ download and release_ download. get ( ' protocol ' ) in [ ' torrent ' , ' torrent_magnet ' ]
def fileIsAdded ( self , src , group ) :
if not group or not group . get ( ' before_rename ' ) :
return False
return src in group [ ' before_rename ' ]
def statusInfoComplete ( self , item ) :
return item [ ' id ' ] and item [ ' downloader ' ] and item [ ' folder ' ]
def statusInfoComplete ( self , release_download ) :
return release_download [ ' id ' ] and release_download [ ' downloader ' ] and release_download [ ' folder ' ]
def movieInFromFolder ( self , movie_folder ) :
return movie_folder and self . conf ( ' from ' ) in movie_folder or not movie_folder
@ -886,7 +976,7 @@ Remove it if you want it to be renamed (again, or at least let it try again)
#Extract all found archives
for archive in archives :
# Check if it has already been processed by CPS
if self . hastagDir ( os . path . dirname ( archive [ ' file ' ] ) ) :
if self . hastagRelease ( release_download = { ' folder ' : os . path . dirname ( archive [ ' file ' ] ) , ' files ' : archive [ ' file ' ] } ) :
continue
# Find all related archive files