-
-
Notifications
You must be signed in to change notification settings - Fork 975
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
Comments
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.
OK, you'll need a more complex version of the script from #5846 (comment) to handle these formatting options. Give me a sec. |
{
"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) |
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: |
Right, the function needs to apply 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) |
And again thanks a lot for your help! Now it works flawlessly :) |
I've added a With this, you could have renamed your files with just
|
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.
The text was updated successfully, but these errors were encountered: