|
@ -1,5 +1,5 @@ |
|
|
from couchpotato import get_session |
|
|
from couchpotato import get_session |
|
|
from couchpotato.core.event import addEvent, fireEvent |
|
|
from couchpotato.core.event import addEvent, fireEvent, fireEventAsync |
|
|
from couchpotato.core.helpers.encoding import toUnicode |
|
|
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 |
|
@ -40,7 +40,7 @@ class Renamer(Plugin): |
|
|
group = groups[group_identifier] |
|
|
group = groups[group_identifier] |
|
|
rename_files = {} |
|
|
rename_files = {} |
|
|
|
|
|
|
|
|
# Add _UNKNOWN_ if no library is connected |
|
|
# Add _UNKNOWN_ if no library item is connected |
|
|
if not group['library']: |
|
|
if not group['library']: |
|
|
if group['dirname']: |
|
|
if group['dirname']: |
|
|
rename_files[group['parentdir']] = group['parentdir'].replace(group['dirname'], '_UNKNOWN_%s' % group['dirname']) |
|
|
rename_files[group['parentdir']] = group['parentdir'].replace(group['dirname'], '_UNKNOWN_%s' % group['dirname']) |
|
@ -53,6 +53,10 @@ class Renamer(Plugin): |
|
|
# Rename the files using the library data |
|
|
# Rename the files using the library data |
|
|
else: |
|
|
else: |
|
|
group['library'] = fireEvent('library.update', identifier = group['library']['identifier'], single = True) |
|
|
group['library'] = fireEvent('library.update', identifier = group['library']['identifier'], single = True) |
|
|
|
|
|
if not group['library']: |
|
|
|
|
|
log.error('Could not rename, no library item to work with: %s' % group_identifier) |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
library = group['library'] |
|
|
library = group['library'] |
|
|
|
|
|
|
|
|
# Find subtitle for renaming |
|
|
# Find subtitle for renaming |
|
@ -85,12 +89,9 @@ class Renamer(Plugin): |
|
|
|
|
|
|
|
|
for file_type in group['files']: |
|
|
for file_type in group['files']: |
|
|
|
|
|
|
|
|
# Move DVD files (no renaming) |
|
|
|
|
|
if group['is_dvd'] and file_type is 'movie': |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
# Move nfo depending on settings |
|
|
# Move nfo depending on settings |
|
|
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) |
|
|
continue |
|
|
continue |
|
|
|
|
|
|
|
|
# Subtitle extra |
|
|
# Subtitle extra |
|
@ -98,7 +99,7 @@ class Renamer(Plugin): |
|
|
continue |
|
|
continue |
|
|
|
|
|
|
|
|
# Move other files |
|
|
# Move other files |
|
|
multiple = len(group['files']['movie']) > 1 |
|
|
multiple = len(group['files']['movie']) > 1 and not group['is_dvd'] |
|
|
cd = 1 if multiple else 0 |
|
|
cd = 1 if multiple else 0 |
|
|
|
|
|
|
|
|
for file in sorted(list(group['files'][file_type])): |
|
|
for file in sorted(list(group['files'][file_type])): |
|
@ -123,7 +124,7 @@ class Renamer(Plugin): |
|
|
if file_type is 'trailer': |
|
|
if file_type is 'trailer': |
|
|
final_file_name = self.doReplace(trailer_name, replacements) |
|
|
final_file_name = self.doReplace(trailer_name, replacements) |
|
|
elif file_type is 'nfo': |
|
|
elif file_type is 'nfo': |
|
|
final_file_name = self.doReplace(nfo_name, replacements) + '-orig' |
|
|
final_file_name = self.doReplace(nfo_name, replacements) |
|
|
elif file_type is 'backdrop': |
|
|
elif file_type is 'backdrop': |
|
|
final_file_name = self.doReplace(backdrop_name, replacements) |
|
|
final_file_name = self.doReplace(backdrop_name, replacements) |
|
|
|
|
|
|
|
@ -131,8 +132,13 @@ class Renamer(Plugin): |
|
|
if separator: |
|
|
if separator: |
|
|
final_file_name = final_file_name.replace(' ', separator) |
|
|
final_file_name = final_file_name.replace(' ', separator) |
|
|
|
|
|
|
|
|
# Main file |
|
|
# Move DVD files (no structure renaming) |
|
|
rename_files[file] = os.path.join(destination, final_folder_name, final_file_name) |
|
|
if group['is_dvd'] and file_type is 'movie': |
|
|
|
|
|
structure_dir = file.split(group['dirname'])[-1].lstrip(os.path.sep) |
|
|
|
|
|
rename_files[file] = os.path.join(destination, final_folder_name, structure_dir) |
|
|
|
|
|
# Do rename others |
|
|
|
|
|
else: |
|
|
|
|
|
rename_files[file] = os.path.join(destination, final_folder_name, final_file_name) |
|
|
|
|
|
|
|
|
# Check for extra subtitle files |
|
|
# Check for extra subtitle files |
|
|
if file_type is 'subtitle': |
|
|
if file_type is 'subtitle': |
|
@ -154,17 +160,13 @@ class Renamer(Plugin): |
|
|
if multiple: |
|
|
if multiple: |
|
|
cd += 1 |
|
|
cd += 1 |
|
|
|
|
|
|
|
|
# Notify on download |
|
|
|
|
|
download_message = 'Download of %s (%s) successful.' % (group['library']['titles'][0]['title'], replacements['quality']) |
|
|
|
|
|
fireEvent('movie.downloaded', message = download_message, data = group) |
|
|
|
|
|
|
|
|
|
|
|
# Before renaming, remove the lower quality files |
|
|
# Before renaming, remove the lower quality files |
|
|
db = get_session() |
|
|
db = get_session() |
|
|
library = db.query(Library).filter_by(identifier = group['library']['identifier']).first() |
|
|
library = db.query(Library).filter_by(identifier = group['library']['identifier']).first() |
|
|
done_status = fireEvent('status.get', 'done', single = True) |
|
|
done_status = fireEvent('status.get', 'done', single = True) |
|
|
for movie in library.movies: |
|
|
for movie in library.movies: |
|
|
for release in movie.releases: |
|
|
for release in movie.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 older release for %s, with quality %s' % (movie.library.titles[0].title, release.quality.label)) |
|
|
elif release.status_id is done_status.get('id'): |
|
|
elif release.status_id is done_status.get('id'): |
|
|
if release.quality.order is group['meta_data']['quality']['order']: |
|
|
if release.quality.order is group['meta_data']['quality']['order']: |
|
@ -200,21 +202,24 @@ class Renamer(Plugin): |
|
|
log.info('Renaming "%s" to "%s"' % (src, dst)) |
|
|
log.info('Renaming "%s" to "%s"' % (src, dst)) |
|
|
|
|
|
|
|
|
path = os.path.dirname(dst) |
|
|
path = os.path.dirname(dst) |
|
|
try: |
|
|
|
|
|
if not os.path.isdir(path): os.makedirs(path) |
|
|
# Create dir |
|
|
except: |
|
|
self.makeDir(path) |
|
|
log.error('Failed creating dir %s: %s' % (path, traceback.format_exc())) |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
shutil.move(src, dst) |
|
|
pass |
|
|
|
|
|
#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] |
|
|
#print rename_me, rename_files[rename_me] |
|
|
|
|
|
|
|
|
# Search for trailers etc |
|
|
# Search for trailers etc |
|
|
fireEvent('renamer.after', group) |
|
|
fireEventAsync('renamer.after', group) |
|
|
|
|
|
|
|
|
|
|
|
# Notify on download |
|
|
|
|
|
download_message = 'Download of %s (%s) successful.' % (group['library']['titles'][0]['title'], replacements['quality']) |
|
|
|
|
|
fireEventAsync('movie.downloaded', message = download_message, data = group) |
|
|
|
|
|
|
|
|
# Break if CP wants to shut down |
|
|
# Break if CP wants to shut down |
|
|
if self.shuttingDown(): |
|
|
if self.shuttingDown(): |
|
|