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

[Twitter] Replace filenames on already downloaded images? #5846

Closed
snarles888 opened this issue Jul 11, 2024 · 10 comments
Closed

[Twitter] Replace filenames on already downloaded images? #5846

snarles888 opened this issue Jul 11, 2024 · 10 comments

Comments

@snarles888
Copy link

snarles888 commented Jul 11, 2024

Technically this could apply to any website but I specifically need to do it with twitter
Is there a way to get gallery-dl to rename the default filenames of already downloaded files to "{author[name]}-{tweet_id}_{num}.{extension}" instead? Or write something else to do it using gallery-dl code?

@mikf
Copy link
Owner

mikf commented Jul 12, 2024

There is no builtin way to rename files, but it is possible to run a custom Python function that does it.

config.json

{
    "filename": "{author[name]}-{tweet_id}_{num}.{extension}",
    "metadata-path": "_path",
    "postprocessors": [
        {
            "name": "python",
            "event": "prepare",
            "function": "/tmp/rename.py:rename"
        }
    ]
}

rename.py

import os

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

    old = "{tweet_id}_{num}.{extension}".format_map(metadata)
    new = "{author[name]}-{tweet_id}_{num}.{extension}".format_map(metadata)

    path_old = pathfmt.realdirectory + old
    path_new = pathfmt.realdirectory + new

    if os.path.exists(path_old):
        os.replace(path_old, path_new)

@Hrxn
Copy link
Contributor

Hrxn commented Jul 12, 2024

But on already downloaded files? Does this really work with "event": "prepare"?

@mikf
Copy link
Owner

mikf commented Jul 12, 2024

Yes, this works on already downloaded files. At least for me it did when I tested this on a smaller example before posting it here.

"event": "prepare" triggers before any filesystem and archive checks so a file with an old name will get renamed to the new format before being checked and eventually skipped over.

@mikf
Copy link
Owner

mikf commented Jul 12, 2024

I should mention: All this does is adding {author[name]}- before each filename, so writing a shell script that does this on its own is probably better than having gallery-dl go over all Tweets again, considering the current rate limit and account suspension issues.

@snarles888
Copy link
Author

snarles888 commented Jul 12, 2024

Where are you placing the py file? In a new folder within site-packages\gallery-dl? I get "The syntax of the command is incorrect." which makes me think I didn't put it in the right spot

gallery-dl --cookies cookies.txt --exec "rename {}" https://x.com/myaccount/likes

@a84r7a3rga76fg
Copy link

Put the .py file in a folder called tmp. The tmp folder is in the same folder with gallery-dl.exe.

@mikf
Copy link
Owner

mikf commented Jul 13, 2024

You can put the .py file anywhere you want, but you have to update the path in function accordingly. (python.function)

I had mine at /tmp/rename.py, so that's what I set it to in #5846 (comment)


--exec "rename {}"

???

The tmp folder is in the same folder with gallery-dl.exe.

???

@snarles888
Copy link
Author

What command are you running it with? Are you just running it with --exec "rename"? Whatever I try ends up having an error and just redownloading the images normally..

@mikf
Copy link
Owner

mikf commented Jul 14, 2024

Everything you need is in #5846 (comment), nothing else is necessary. Add these config settings to your twitter ones and put the rename function in a .py file somewhere.

@snarles888
Copy link
Author

Ah I had to get the path literal working properly in the config, now it works good

I don't know that there's a way to do this without pestering twitter's server a bunch, but if it's worth anything the folder I'm doing this in has over 30k downloads and I haven't been hit yet.

mikf added a commit that referenced this issue Aug 31, 2024
renames previously downloaded files to a different filename format
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

4 participants