def convert_multimedia(filename): in_ext = os.path.splitext(filename)[1].lstrip('.') out_ext = config.get(in_ext, 'out_ext') # prepare name and size of the input file names = prepare_names(filename, out_ext) in_size = os.path.getsize(names.in_filename) / 1024 / 1024 # MB # command command = Command(config.get(in_ext, 'command')) stdout = config.getboolean(in_ext, 'stdout') # prepare tmp file with NamedTemporaryFile(delete=False) as f: tmp_filename = f.name # arguments args = parse_cmd_args( config.get(in_ext, 'params'), in_filename=names.in_filename, out_filename=tmp_filename ) kwargs = {} if stdout: kwargs['_out'] = tmp_filename # convert start = time() command(*args, **kwargs) t = time() - start # calculate statistics, etc. out_size = os.path.getsize(names.out_filename) / 1024 / 1024 # MB compression = 100 - (100 * out_size / in_size) # trash the original file, move result if in_ext == out_ext and in_size <= out_size: # converting within the same format and the conversion had no # possitive effect => we can keep the original os.unlink(tmp_filename) else: to_trash(names.in_filename) shutil.move(tmp_filename, names.out_filename) # print summary line = u'{0}: {1:.1f}MB → {2} {3:.1f}MB, {4:.1f}min, {5:.1f}%'.format( names.in_basename, in_size, names.out_basename, out_size, t / 60, compression ) print line if t > 60: # longer than one minute notify("Conversion is complete", line)
def share(directory): new_directory = dropbox_dir(directory) try: shutil.copytree(directory, new_directory) except OSError as e: if e.errno != 17: # already exists raise for filename in list_files(new_directory): basename = os.path.basename(filename) meta = Metadata(filename) caption = meta.get('Headline', meta['Caption-Abstract']) caption = unicode(caption or config.get('dropbox', 'default_caption')) caption = caption.replace('/', '|') # special character in UNIX fs base, ext = os.path.splitext(basename) new_basename = u"{name}:{padding}{caption}{padding}{ext}".format( name=base, caption=caption, ext=ext.lower(), padding=u'\xa0' * 10, ) print u'{0} → {1}'.format(basename, new_basename) os.rename(filename, os.path.join(new_directory, new_basename)) print 'https://www.dropbox.com/home/{}'.format( urllib.quote(dropbox_path(directory).encode('utf-8')) )
def auto(directory): print "Removing rubbish..." rubbish_names = re.split(r'[,\s]+', config.get('general', 'rubbish')) for filename in list_files(directory): basename = os.path.basename(filename) if basename in rubbish_names: print u'RUBBISH: {0}'.format(basename) to_trash(filename) print "Fixing filenames..." names_fix(directory) print "Sorting filenames..." names_sort(directory) print "Fixing captions..." captions_fix(directory) convert(directory)
def dropbox_dir(directory): return os.path.join( os.path.expanduser(config.get('dropbox', 'dir')), os.path.basename(directory) )