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

Rename existing / downloaded files #6044

Closed
Anzuch4ng opened this issue Aug 17, 2024 · 7 comments
Closed

Rename existing / downloaded files #6044

Anzuch4ng opened this issue Aug 17, 2024 · 7 comments

Comments

@Anzuch4ng
Copy link

Hi everyone! Is it possible at all to rename already existing files based on their md5 from sqlite archive OR based on {id} field in existing filename from specific website (in this case danbooru)?

Before my config filename was containing only danbooru ID info
"[{id}].{extension}"

and now I have bunch of other tags:
"filename": "[{tags_artist[:2]:J /}] [{tags_copyright[:2]:J /}] [{tags_character[:2]:J /}] [{id}].{extension}"

so the question is upon re-running script - is it possible to detect already existing files in local folder and rename them based on current config filename settings?

My sqlite archive was set to
"archive-format": "{md5}",
"archive": "./db_danbooru.sqlite"

Long time ago I was using "DanbooruDownloader" and it had a feature, where you can scan local folder, and apply new naming scheme based on downloaded info from danbooru/sankaku etc, which obviously not exactly what I need right now, but was the closest thing to solve renaming of existing files.

@Anzuch4ng
Copy link
Author

Anzuch4ng commented Aug 17, 2024

My bad it was not in DanbooruDownloader but in imgbrd-grabber and from what I see this feature is here and still works, but my questions still remains, if it's possible to achieve via gallerydl only.

image

@mikf
Copy link
Owner

mikf commented Aug 17, 2024

It can be done, but it is not a builtin feature (yet), so you'll need to run some external code. Take a look at #5846, which is more or less the same issue.

"filename": "[{tags_artist[:2]:J /}] [{tags_copyright[:2]:J /}] [{tags_character[:2]:J /}] [{id}].{extension}"

OK, you'll need a more complex version of the script from #5846 (comment) to handle these formatting options. Give me a sec.

@mikf
Copy link
Owner

mikf commented Aug 17, 2024

{
    "filename": "[{tags_artist[:2]:J /}] [{tags_copyright[:2]:J /}] [{tags_character[:2]:J /}] [{id}].{extension}",
    "metadata-path": "_path",
    "postprocessors": [
        {
            "name": "python",
            "event": "prepare",
            "function": "/tmp/rename.py:rename"
        }
    ]
}
import os
from gallery_dl import formatter

OLD_FMT = "[{id}].{extension}"
NEW_FMT = "[{tags_artist[:2]:J /}] [{tags_copyright[:2]:J /}] [{tags_character[:2]:J /}] [{id}].{extension}"

fmt_old = formatter.parse(OLD_FMT)
fmt_new = formatter.parse(NEW_FMT)

def rename(metadata):
    pathfmt = metadata["_path"]

    old = fmt_old.format_map(metadata)
    path_old = pathfmt.realdirectory + old

    if os.path.exists(path_old):
        new = fmt_new.format_map(metadata)
        path_new = pathfmt.realdirectory + new
        os.replace(path_old, path_new)

@Anzuch4ng
Copy link
Author

Anzuch4ng commented Aug 17, 2024

Thanks a lot! It does work perfectly.

I was worried that I didn't had any metadata downloaded locally (apart from md5 sqlite archive), but it seems like I don't need them.

The only issue I've encountered so far is that if website filename contains any unsuported ascii characters it gives an error, for example it tries to write new name as "re:zero" where ":" are not replaced with underscores "_" automatically (and probably other unsuported characters as well)

Error from console: [danbooru][error] Unable to download data: OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '[umamusume] [mihono_bourbon_(code:glassage)_(umamusume) mihono_bourbon_(umamusume)] [8003528].png

@mikf
Copy link
Owner

mikf commented Aug 18, 2024

Right, the function needs to apply clean_segment and clean_path to the filenames to replace invalid characters with _ or delete control characters:

import os
from gallery_dl import formatter

OLD_FMT = "[{id}].{extension}"
NEW_FMT = "[{tags_artist[:2]:J /}] [{tags_copyright[:2]:J /}] [{tags_character[:2]:J /}] [{id}].{extension}"

fmt_old = formatter.parse(OLD_FMT)
fmt_new = formatter.parse(NEW_FMT)

def rename(metadata):
    pathfmt = metadata["_path"]

    old = fmt_old.format_map(metadata)
    old = pathfmt.clean_path(pathfmt.clean_segment(old))
    path_old = pathfmt.realdirectory + old

    if os.path.exists(path_old):
        new = fmt_new.format_map(metadata)
        new = pathfmt.clean_path(pathfmt.clean_segment(new))
        path_new = pathfmt.realdirectory + new
        os.replace(path_old, path_new)

@Anzuch4ng
Copy link
Author

And again thanks a lot for your help! Now it works flawlessly :)

mikf added a commit that referenced this issue Aug 31, 2024
renames previously downloaded files to a different filename format
@mikf
Copy link
Owner

mikf commented Sep 3, 2024

I've added a rename post processor that basically uses this script internally. (17f5ba4)

With this, you could have renamed your files with just

gallery-dl --rename "[{id}].{extension}" URL

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