mikf / gallery-dl

Command-line program to download image galleries and collections from several image hosting sites
GNU General Public License v2.0
11.49k stars 942 forks source link

How to derive the extractor directory from the filename? #5518

Open cenodis opened 5 months ago

cenodis commented 5 months ago

I am trying to download media from websites that allow reposting/reblogging of content, thereby duplicating that media across multiple accounts (think twitter, mastodon, etc). To better organize this I'm trying to consolidate all the media files into a directory independent of accounts. Because said directory fills up fairly quickly I would like to further subdivide it based on the filename.

For example the file: example_file.png

would be stored like this: .../ex/am/example_file.png

Ideally I would like to have a configuration like this:

{
  "extractor": {
    "directory": ["{media}[id][0:2]}", "{media}[id][2:4]}"],
    "filename": "{media[id].{extension}"
  }
}

This doesn't work however. It seems that the directory is being created before the file is actually known and the "media" field is completely absent. Is there any way to make the directory config work on a per-file basis?

taskhawk commented 5 months ago

The syntax is wrong, it should be like this to work:

{
  "extractor": {
    "directory": ["{media[id][0:2]}", "{media[id][2:4]}"],
    "filename": "{media[id]}.{extension}"
  }
}

Give it a try.

cenodis commented 5 months ago

Sorry, the brackets seem to have gotten lost in a copy paste somewhere. My config file has proper bracketing but still doesn't work and the result path looks like this: .../None/None/01876e39-c51c-16ec-6677-94bdaf5c247d.png

taskhawk commented 5 months ago

When you check the keywords with gallery-dl -K "URL", does media['id'] appears in the section "Keywords for directory names:"? If so, it should work. Care to share an example to test?

cenodis commented 5 months ago

It doesnt. You can try with any mastodon account like so: gallery-dl -K 'https://mastodon.social/@Mastodon'

The problem isn't the metadata, its that gallery-dl seems to create directories independent of the files (and therefore metadata about files is not available during directory creation). Hence my question:

Is there any way to make the directory config work on a per-file basis?

taskhawk commented 5 months ago

Then I think the only way would be to use a post-processor to create the folders and move the just-downloaded file there, tbh.

{
  "extractor": {
    "directory": ["temp-dir"],
    "filename": "{media[id]}.{extension}",
    "postprocessor": ["move-files"]
  },

  "postprocessor": {

    "move-files": {
      "name": "exec",
      "event": "after",
      "command": ["script.sh", "{_path}", "{media[id][0:2]}/{media[id][2:4]}/"]
    },
  }
}

And the script should be something like this (making example in Bash):

#!/bin/bash

filepath="$1"
target="$2"

mkdir --parents "$target"
mv --force "$filepath" "$target"

Probably will require some adjustments but that's the gist of it.