organize
organize copied to clipboard
Feature request: Filter for common file prefixes/suffixes
I have a flat directory with various different files which all start with common prefixes, eg:
My Podcast Ep 1.mp3
My Podcast Ep 2.mp3
My Podcast Ep 3.mp3
Your Podcast Ep 1.mp3
Your Podcast Ep 2.mp3
Your Podcast Ep 3.mp3
Would it be possible to create a filter which matches common prefixes in files, allowing you to group these files into related folders? Eg:
rules:
- enabled: false
folders:
- ~/Downloads/
subfolders: true
filters:
- extension: mp3
- filename:
common_prefix: true
actions:
- move: '{prefix}\{path}'
Output:
My Podcast\
Ep 1.mp3
Ep 2.mp3
Ep 3.mp3
Your Podcast\
Ep 1.mp3
Ep 2.mp3
Ep 3.mp3
Little dirty, but this is how I've implemented it now:
rules:
- enabled: true
folders:
- ~/Downloads/
filters:
- python: |
import os, os.path
from collections import Counter
files = os.listdir(basedir)
parts = path.name.split(' ')
counter = Counter()
for i in range(len(parts) + 1):
prefix = ' '.join(parts[0:i])
counter[prefix] = len([i for i in files if i.startswith(prefix)])
del counter['']
prefix = ''
count = 1
for (p, c) in counter.most_common(2):
if len(p) > len(prefix):
prefix = p
count = c
if count == 1 or len(prefix) <= 3:
return False
return {
'prefix': prefix.strip(),
'count': count,
'suffix': path.name.replace(prefix, '', 1).strip()
}
actions:
- move: '{basedir}/{python.prefix}/{python.suffix}'
Nice, do you mind if I take your code as a starting point for a built-in filter?
@tfeldmann yeah of course, go ahead :)
I randomly thought about this. I guess this could have been a regex:
rules:
- locations: .
filters:
- regex: '^(?P<podcast>.+?) (?P<episode>Ep \d+\.mp3)'
actions:
- move: './{regex.podcast}/{regex.episode}'