aboutsummaryrefslogtreecommitdiff
path: root/extractors/job.py
blob: ce44e9aa451569f3db256f028314184373eabe36 (plain)
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
import logging
import re
import importlib
import sys

extrs = [
    'bandcamp'
]


class DlJob():

    def __init__(self, url, output):
        self.extr = self._find(url)
        self.output = output
        self._albums = []
        if not self.extr:
            logging.error(url + " is not supported")
            sys.exit(1)

    def _find(self, url):
        for cls in self._list_extractors():
            match = cls.pattern.match(url)
            if match:
                return cls(match, url)

    def _list_extractors(self):
        for extr in iter(extrs):
            module = importlib.import_module('.'+extr, __package__)
            yield from self._add_module(module)

    def _add_module(self, module):
        classes = self._get_classes(module)
        for cls in classes:
            cls.pattern = re.compile(cls.pattern)
        return classes

    def _get_classes(self, module):
        return [
            cls for cls in module.__dict__.values() if (
                hasattr(cls, "pattern") and cls.__module__ == module.__name__
            )
        ]

    def run(self):
        self.extr.get_albums()
        self.extr.download_albums(self.output)