aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/postprocessor/classify.py
blob: 34af1d94082aacc9bc43a5c45f502b7d187d52c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-

# Copyright 2018-2021 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

"""Categorize files by file extension"""

from .common import PostProcessor
import os


class ClassifyPP(PostProcessor):

    DEFAULT_MAPPING = {
        "Music" : ("mp3", "aac", "flac", "ogg", "wma", "m4a", "wav"),
        "Video" : ("flv", "ogv", "avi", "mp4", "mpg", "mpeg", "3gp", "mkv",
                   "webm", "vob", "wmv"),
        "Pictures" : ("jpg", "jpeg", "png", "gif", "bmp", "svg", "webp"),
        "Archives" : ("zip", "rar", "7z", "tar", "gz", "bz2"),
    }

    def __init__(self, job, options):
        PostProcessor.__init__(self, job)
        mapping = options.get("mapping", self.DEFAULT_MAPPING)

        self.mapping = {
            ext: directory
            for directory, exts in mapping.items()
            for ext in exts
        }
        job.register_hooks(
            {"prepare": self.prepare, "file": self.move}, options)

    def prepare(self, pathfmt):
        ext = pathfmt.extension
        if ext in self.mapping:
            # set initial paths to enable download skips
            self._build_paths(pathfmt, self.mapping[ext])

    def move(self, pathfmt):
        ext = pathfmt.extension
        if ext in self.mapping:
            # rebuild paths in case the filename extension changed
            path = self._build_paths(pathfmt, self.mapping[ext])
            os.makedirs(path, exist_ok=True)

    @staticmethod
    def _build_paths(pathfmt, extra):
        path = pathfmt.realdirectory + extra
        pathfmt.realpath = path + os.sep + pathfmt.filename
        pathfmt.path = pathfmt.directory + extra + os.sep + pathfmt.filename
        return path


__postprocessor__ = ClassifyPP