def _jsonfix( self, show, jsonpath ): video_files = [] nfo_files = [] ext_dict = {} try: items = os.listdir( self.FOLDERPATH ) except OSError: err_str = 'directory %s not found' % self.FOLDERPATH lw.log( [err_str, 'script stopped'] ) sys.exit( err_str ) jsondata = readFile( jsonpath ) lw.log( ['-----JSON data-----', jsondata[1]] ) episodes = _json.loads( jsondata[1] ) for item in items: fileroot, ext = os.path.splitext( item ) processfilepath = os.path.join( self.FOLDERPATH, item ) last_mod = time.strftime( '%Y-%m-%d', time.localtime( os.path.getmtime( processfilepath ) ) ) if ext in settings.video_exts : for key in episodes: episode = episodes[key] lw.log( ['comparing file last mod of %s with json record date of %s' % (last_mod, episode['record-date'])] ) if last_mod == episode['record-date']: newfilename = '%s.S%sE%s.%s%s' % (show, episode['season'], episode['episode'], episode['title'], ext) newfilepath = os.path.join( self.FOLDERPATH, newfilename ) exists, loglines = checkPath( newfilepath, create=False ) lw.log( loglines ) if not exists: success, loglines = renameFile( processfilepath, newfilepath ) lw.log( loglines ) if success: self._update_db( newfilepath ) else: lw.log( ['%s already has the correct file name' % processfilepath] ) break
def _specialseason( self, show, nfotemplate, ep_info ): newfileroot = '%s.S00E%s.%s' % (show, ep_info['episode'].zfill( 2 ), ep_info['title']) newfilename = newfileroot + '.' + self.FILEPATH.split( '.' )[-1] newfilepath = os.path.join( self.FOLDERPATH, newfilename ) newnfoname = newfileroot + '.nfo' self._write_nfofile( nfotemplate, ep_info, newnfoname ) success, loglines = renameFile( self.FILEPATH, newfilepath ) lw.log( loglines ) self._update_db( newfilepath )
def _transform_files( self, files, destconfig ): transforms_list = destconfig.get( 'transforms' ) tfiles = [] if not transforms_list: lw.log( ['no transformations listed for destination ' + destconfig.get( 'name', '')], 'info' ) return files transforms = _parse_items( transforms_list ).get( self.ARGS.source ) if not transforms: lw.log( ['no transformations needed for %s when sending to %s' % (self.ARGS.source, destconfig.get( 'name', ''))], 'info' ) return files for transform in _parse_items( transforms, itemdelim=';', subitemdelim=None ): if tfiles: usefiles = tfiles tfiles = [] else: usefiles = files lw.log( ['transforming files using %s transform' % transform], 'info' ) for file in usefiles: destfile = os.path.join( self.DATAROOT, 'downloads', file ) file_exists = True i = 1 while file_exists: filepieces = os.path.splitext( file ) orgfilename = '%s-org%s%s' % (filepieces[0], str( i ), filepieces[1]) orgfile = os.path.join( self.DATAROOT, 'downloads', orgfilename ) file_exists = os.path.isfile( orgfile ) i = i + 1 success, loglines = renameFile( destfile, orgfile ) lw.log( loglines ) if not success: lw.log( ['error renaming %s to %s' % (destfile, orgfile)], 'info' ) return False try: tfile, loglines = transform_modules[transform].Transform().Run( orgfile, destfile, destconfig.get( '%s_%s_config' % (self.ARGS.source, transform), {} ), config.Get( 'debug' ) ) except KeyError as e: lw.log( ['no transform module named %s found' % transform], 'info' ) return False lw.log( loglines, 'info' ) if tfile: tpath = pathlib.PurePath( tfile ) tfiles.append( tpath.name ) else: lw.log( ['error transforming file ' + file], 'info' ) return tfiles
def _nfofix( self, show, nfotemplate ): video_files = [] other_files = [] try: items = os.listdir( self.FOLDERPATH ) except OSError: err_str = 'directory %s not found' % self.FOLDERPATH lw.log( [err_str, 'script stopped'] ) sys.exit( err_str ) for item in items: fileroot, ext = os.path.splitext( item ) if ext in settings.thumb_exts: for rename_end in settings.rename_ends: if fileroot.endswith( rename_end ): old_thumb = os.path.join( self.FOLDERPATH, item ) item = fileroot[:-len( rename_end )] + settings.thumb_end + ext new_thumb = os.path.join( self.FOLDERPATH, item ) success, loglines = renameFile( old_thumb, new_thumb ) lw.log( loglines ) if item in settings.protected_files: pass elif ext in settings.video_exts : video_files.append( item ) else: other_files.append( item ) lw.log( ['checking files to see if they need to be deleted'] ) lw.log( ['other files:', other_files, 'video files:', video_files] ) for one_file in other_files: match = False other_fileroot, throwaway = os.path.splitext( one_file ) for one_video in video_files: video_root, throwaway = os.path.splitext( one_video ) if (other_fileroot == video_root) or (other_fileroot == video_root + settings.thumb_end): match = True if not match: success, loglines = deleteFile( os.path.join( self.FOLDERPATH, one_file ) ) lw.log( loglines ) ep_info = {} ep_info['airdate'] = time.strftime( '%Y-%m-%d', time.localtime( os.path.getmtime( self.FILEPATH ) ) ) try: ep_info['season'] = self.EVENT_DETAILS["Event"]["Season"] ep_info['episode'] = self.EVENT_DETAILS["Event"]["Episode"] except KeyError: ep_info['season'] = '0' ep_info['episode'] = self._special_epnumber( show, video_files ) try: ep_info['title'] = self.EVENT_DETAILS["Event"]["SubTitle"] except KeyError: ep_info['title'] = ep_info['airdate'] if ep_info['title'] == None: ep_info['title'] = ep_info['airdate'] try: ep_info['description'] = self.EVENT_DETAILS["Event"]["Description"] except KeyError: ep_info['description'] = '' if ep_info['description'] == None: ep_info['description'] = '' lw.log( [ep_info] ) if ep_info['season'] == '0': self._specialseason( show, nfotemplate, ep_info ) else: self._write_nfofile( nfotemplate, ep_info, os.path.splitext( self.FILEPATH )[0] + '.nfo' )