|
@ -4,7 +4,7 @@ from couchpotato.core.helpers.encoding import toUnicode |
|
|
from couchpotato.core.helpers.variable import getExt |
|
|
from couchpotato.core.helpers.variable import getExt |
|
|
from couchpotato.core.logger import CPLog |
|
|
from couchpotato.core.logger import CPLog |
|
|
from couchpotato.core.plugins.base import Plugin |
|
|
from couchpotato.core.plugins.base import Plugin |
|
|
from couchpotato.core.settings.model import Library, Movie |
|
|
from couchpotato.core.settings.model import Library |
|
|
import os.path |
|
|
import os.path |
|
|
import re |
|
|
import re |
|
|
import shutil |
|
|
import shutil |
|
@ -15,6 +15,8 @@ log = CPLog(__name__) |
|
|
|
|
|
|
|
|
class Renamer(Plugin): |
|
|
class Renamer(Plugin): |
|
|
|
|
|
|
|
|
|
|
|
renaming_started = False |
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
def __init__(self): |
|
|
|
|
|
|
|
|
addEvent('renamer.scan', self.scan) |
|
|
addEvent('renamer.scan', self.scan) |
|
@ -27,6 +29,10 @@ class Renamer(Plugin): |
|
|
if self.isDisabled(): |
|
|
if self.isDisabled(): |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
if self.renaming_started is True: |
|
|
|
|
|
log.error('Renamer is disabled to avoid infinite looping of the same error.') |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
# 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 self.conf('from') in self.conf('to'): |
|
|
if 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.') |
|
|
log.error('The "to" can\'t be inside of the "from" folder. You\'ll get an infinite loop.') |
|
@ -35,11 +41,12 @@ class Renamer(Plugin): |
|
|
groups = fireEvent('scanner.scan', folder = self.conf('from'), single = True) |
|
|
groups = fireEvent('scanner.scan', folder = self.conf('from'), single = True) |
|
|
if groups is None: return |
|
|
if groups is None: return |
|
|
|
|
|
|
|
|
|
|
|
self.renaming_started = True |
|
|
|
|
|
|
|
|
destination = self.conf('to') |
|
|
destination = self.conf('to') |
|
|
folder_name = self.conf('folder_name') |
|
|
folder_name = self.conf('folder_name') |
|
|
file_name = self.conf('file_name') |
|
|
file_name = self.conf('file_name') |
|
|
trailer_name = self.conf('trailer_name') |
|
|
trailer_name = self.conf('trailer_name') |
|
|
backdrop_name = self.conf('fanart_name') |
|
|
|
|
|
nfo_name = self.conf('nfo_name') |
|
|
nfo_name = self.conf('nfo_name') |
|
|
separator = self.conf('separator') |
|
|
separator = self.conf('separator') |
|
|
|
|
|
|
|
@ -47,6 +54,8 @@ class Renamer(Plugin): |
|
|
|
|
|
|
|
|
group = groups[group_identifier] |
|
|
group = groups[group_identifier] |
|
|
rename_files = {} |
|
|
rename_files = {} |
|
|
|
|
|
remove_files = [] |
|
|
|
|
|
remove_releases = [] |
|
|
|
|
|
|
|
|
# Add _UNKNOWN_ if no library item is connected |
|
|
# Add _UNKNOWN_ if no library item is connected |
|
|
if not group['library']: |
|
|
if not group['library']: |
|
@ -159,6 +168,9 @@ class Renamer(Plugin): |
|
|
|
|
|
|
|
|
# Do rename others |
|
|
# Do rename others |
|
|
else: |
|
|
else: |
|
|
|
|
|
if self.conf('move_leftover') and file_type is 'leftover': |
|
|
|
|
|
rename_files[file] = os.path.join(destination, final_folder_name, os.path.basename(file)) |
|
|
|
|
|
else: |
|
|
rename_files[file] = os.path.join(destination, final_folder_name, final_file_name) |
|
|
rename_files[file] = os.path.join(destination, final_folder_name, final_file_name) |
|
|
|
|
|
|
|
|
# Check for extra subtitle files |
|
|
# Check for extra subtitle files |
|
@ -203,19 +215,21 @@ class Renamer(Plugin): |
|
|
# Go over current movie releases |
|
|
# Go over current movie releases |
|
|
for release in movie.releases: |
|
|
for release in movie.releases: |
|
|
|
|
|
|
|
|
|
|
|
# When a release already exists |
|
|
|
|
|
if release.status_id is done_status.get('id'): |
|
|
|
|
|
|
|
|
# This is where CP removes older, lesser quality releases |
|
|
# This is where CP removes older, lesser quality releases |
|
|
if release.quality.order > group['meta_data']['quality']['order']: |
|
|
if release.quality.order > group['meta_data']['quality']['order']: |
|
|
log.info('Removing older release for %s, with quality %s' % (movie.library.titles[0].title, release.quality.label)) |
|
|
log.info('Removing lesser quality %s for %s.' % (movie.library.titles[0].title, release.quality.label)) |
|
|
|
|
|
|
|
|
for file in release.files: |
|
|
for file in release.files: |
|
|
log.info('Removing (not really) "%s"' % file.path) |
|
|
remove_files.append(file) |
|
|
|
|
|
remove_releases.append(release) |
|
|
# When a release already exists |
|
|
|
|
|
elif release.status_id is done_status.get('id'): |
|
|
|
|
|
|
|
|
|
|
|
# Same quality, but still downloaded, so maybe repack/proper/unrated/directors cut etc |
|
|
# Same quality, but still downloaded, so maybe repack/proper/unrated/directors cut etc |
|
|
if release.quality.order is group['meta_data']['quality']['order']: |
|
|
elif release.quality.order is group['meta_data']['quality']['order']: |
|
|
log.info('Same quality release already exists for %s, with quality %s. Assuming repack.' % (movie.library.titles[0].title, release.quality.label)) |
|
|
log.info('Same quality release already exists for %s, with quality %s. Assuming repack.' % (movie.library.titles[0].title, release.quality.label)) |
|
|
|
|
|
for file in release.files: |
|
|
|
|
|
remove_files.append(file) |
|
|
|
|
|
remove_releases.append(release) |
|
|
|
|
|
|
|
|
# Downloaded a lower quality, rename the newly downloaded files/folder to exclude them from scan |
|
|
# Downloaded a lower quality, rename the newly downloaded files/folder to exclude them from scan |
|
|
else: |
|
|
else: |
|
@ -237,26 +251,39 @@ class Renamer(Plugin): |
|
|
|
|
|
|
|
|
break |
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
# Remove leftover files |
|
|
|
|
|
if self.conf('cleanup') and not self.conf('move_leftover'): |
|
|
|
|
|
log.debug('Removing leftover files') |
|
|
|
|
|
for file in group['files']['leftover']: |
|
|
|
|
|
remove_files.append(file) |
|
|
|
|
|
|
|
|
# Rename all files marked |
|
|
# Rename all files marked |
|
|
for src in rename_files: |
|
|
for src in rename_files: |
|
|
if rename_files[src]: |
|
|
if rename_files[src]: |
|
|
|
|
|
|
|
|
dst = rename_files[src] |
|
|
dst = rename_files[src] |
|
|
|
|
|
|
|
|
log.info('Renaming "%s" to "%s"' % (src, dst)) |
|
|
log.info('Renaming "%s" to "%s"' % (src, dst)) |
|
|
|
|
|
|
|
|
path = os.path.dirname(dst) |
|
|
|
|
|
|
|
|
|
|
|
# Create dir |
|
|
# Create dir |
|
|
self.makeDir(path) |
|
|
self.makeDir(os.path.dirname(dst)) |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
pass |
|
|
self.moveFile(src, dst) |
|
|
#shutil.move(src, 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())) |
|
|
|
|
|
|
|
|
#print rename_me, rename_files[rename_me] |
|
|
# Remove files |
|
|
|
|
|
for src in remove_files: |
|
|
|
|
|
log.info('Removing "%s"' % src) |
|
|
|
|
|
|
|
|
|
|
|
# Remove matching releases |
|
|
|
|
|
for release in remove_releases: |
|
|
|
|
|
log.info('Removing release %s' % release) |
|
|
|
|
|
|
|
|
|
|
|
# Add this release to the library |
|
|
|
|
|
if not group['destination_dir'] is destination: |
|
|
|
|
|
fireEventAsync('scanner.to_library', folder = group['destination_dir']) |
|
|
|
|
|
else: |
|
|
|
|
|
log.error('Single destination folder not fully supported yet.') |
|
|
|
|
|
|
|
|
# Search for trailers etc |
|
|
# Search for trailers etc |
|
|
fireEventAsync('renamer.after', group) |
|
|
fireEventAsync('renamer.after', group) |
|
@ -269,12 +296,14 @@ class Renamer(Plugin): |
|
|
if self.shuttingDown(): |
|
|
if self.shuttingDown(): |
|
|
break |
|
|
break |
|
|
|
|
|
|
|
|
def moveFile(self, old, dest, suppress = True): |
|
|
self.renaming_started = False |
|
|
|
|
|
|
|
|
|
|
|
def moveFile(self, old, dest): |
|
|
try: |
|
|
try: |
|
|
shutil.move(old, dest) |
|
|
shutil.move(old, dest) |
|
|
except: |
|
|
except: |
|
|
log.error("Couldn't move file '%s' to '%s': %s" % (old, dest, traceback.format_exc())) |
|
|
log.error("Couldn't move file '%s' to '%s': %s" % (old, dest, traceback.format_exc())) |
|
|
return False |
|
|
raise Exception |
|
|
|
|
|
|
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
|