Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Filter for common file prefixes/suffixes #153

Open
Paradoxis opened this issue Dec 4, 2021 · 4 comments
Open

Feature request: Filter for common file prefixes/suffixes #153

Paradoxis opened this issue Dec 4, 2021 · 4 comments

Comments

@Paradoxis
Copy link

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
Copy link
Author

Paradoxis commented Dec 4, 2021

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

@tfeldmann
Copy link
Owner

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

@Paradoxis
Copy link
Author

@tfeldmann yeah of course, go ahead :)

@tfeldmann
Copy link
Owner

tfeldmann commented Apr 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants