organize icon indicating copy to clipboard operation
organize copied to clipboard

Feature request: Filter for common file prefixes/suffixes

Open Paradoxis opened this issue 3 years ago • 4 comments

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

Paradoxis avatar Dec 04 '21 16:12 Paradoxis

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}'

Paradoxis avatar Dec 04 '21 18:12 Paradoxis

Nice, do you mind if I take your code as a starting point for a built-in filter?

tfeldmann avatar Jan 21 '22 09:01 tfeldmann

@tfeldmann yeah of course, go ahead :)

Paradoxis avatar Jan 21 '22 20:01 Paradoxis

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}'

tfeldmann avatar Apr 04 '24 17:04 tfeldmann