summaryrefslogtreecommitdiffstats
path: root/.bin/music
blob: 0c280add44f4ac80d240b2777f69adc82be07008 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python3

import logging
import os
import sys

import yt_dlp  # type: ignore[import]

from dataclasses import dataclass

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def get_ytdlp_options(output_dir: str) -> dict:
    """yt_dlp download and convertion options."""

    def match_filter(info: dict, *, incomplete) -> str | None:
        duration = info.get("duration")
        duration_min = 60

        if duration is not None and int(duration) < duration_min:
            return "Duration too short: < _duration_min"

        return None

    return {
        "format": "bestaudio/best",
        "match_filter": match_filter,
        "postprocessors": [
            {
                "key": "FFmpegExtractAudio",
                # "preferredcodec": "m4a",
            },
            {
                "key": "FFmpegMetadata",
                "add_metadata": True,
            },
            {
                "key": "EmbedThumbnail",
                "already_have_thumbnail": False,
            },
        ],
        "outtmpl": f"{output_dir}/%(title)s.%(ext)s",
        "restrictfilenames": True,
        "ignoreerrors": True,
        "writethumbnail": True,
    }


def parse_raw_lines(lines: list[str]) -> list[list[str]]:
    """Parse collections of name + link(s)

    (Usually stored in a text file).
    """
    entries: list[list[str]] = list()
    entry: list[str] = list()

    for index, line in enumerate(lines):

        # entries are separated by an empty line.
        if line == "":
            entries.append(entry)
            entry = list()
            continue

        entry.append(line)

        # handle the last entry when reaching the end of the file.
        if index + 1 == len(lines):
            entries.append(entry)
            entry = list()

    return entries


@dataclass(frozen=True)
class Link:
    """A music link."""

    url: str
    is_enabled: bool


@dataclass(frozen=True)
class Collection:
    """A music collection."""

    name: str
    links: tuple[Link, ...]
    is_enabled: bool

    def __eq__(self, other) -> bool:
        if isinstance(other, Collection):
            return self.name == other.name

        raise NotImplementedError


def sanitize_entry_informations(
    entry: str, indicator: str = "#"
) -> tuple[str, bool]:

    is_comment = entry.startswith(indicator)

    if is_comment:
        entry = entry.split(indicator, 1)[1].lstrip()

    return entry, not is_comment


def create_link(entry: str) -> Link:
    url, is_enabled = sanitize_entry_informations(entry)
    return Link(url=url, is_enabled=is_enabled)


def create_collection(entry: list[str]) -> Collection:
    """Create a collection from a raw entry."""
    name, is_enabled = sanitize_entry_informations(entry[0])
    links = [create_link(_link) for _link in entry[1:]]

    return Collection(
        name=name,
        links=tuple(links),
        is_enabled=is_enabled
    )


def get_collection_dir(collection: Collection, parent_dir: str) -> str:
    return os.path.join(parent_dir, collection.name)


def download_collection(collection: Collection, directory: str) -> None:
    """Download a music collection to the local filesystem."""

    # create directory and download/convert with opinionated settings.
    os.makedirs(directory, exist_ok=True)

    with yt_dlp.YoutubeDL(get_ytdlp_options(directory)) as downloader:
        for link in collection.links:
            if not link.is_enabled:
                logger.info(f"Skipping {collection.name}, {link}")
                continue

            logger.info(f"Downloading {collection.name}, {link}")
            downloader.download(link.url)


def main() -> int:
    """Main entrypoint."""

    # argument handling
    if len(sys.argv) != 2:
        return 1

    with open(sys.argv[1], "r") as file:
        filedata = file.read().splitlines()

    for entry in parse_raw_lines(filedata):
        collection = create_collection(entry)
        output_dir = get_collection_dir(collection, os.getcwd())

        if os.path.isdir(output_dir) or not collection.is_enabled:
            logger.info(f"Skipping {collection.name}")
            continue

        logger.info(f"Handling {collection.name}")
        download_collection(collection, output_dir)

    return 0


if __name__ == "__main__":
    exit(main())
remember that computers suck.