resources.py 5.43 KB
Newer Older
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2017-2020 - The Aura Engine Team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from enum import Enum



class ResourceType(Enum):
    """
    Media content types.
    """    
    FILE = "file:"
    STREAM_HTTP = "http:"
    STREAM_HTTPS = "https:"
    LINE = "line:"
    PLAYLIST = "playlist:"
    POOL = "pool:"


class ResourceClass(Enum):
    """
    Media content classes.
    """
    FILE = {
        "id": "fs",
        "numeric": 0,
        "types": [ResourceType.FILE]
    }
    STREAM = {
        "id": "fs",
        "numeric": 0,
        "types": [ResourceType.STREAM_HTTP, ResourceType.STREAM_HTTPS]
    }
    LIVE = {
        "id": "http",
        "numeric": 1,
        "types": [ResourceType.LINE]
    }
    PLAYLIST = {
        "id": "https",
        "numeric": 2,
        "types": [ResourceType.PLAYLIST, ResourceType.POOL]
    }

    @property
    def types(self):
        return self.value["types"]

    @property
    def numeric(self):
        return self.value["numeric"]

    def __str__(self):
        return str(self.value["id"])



class ResourceUtil(Enum):
    """
    Utilities for different resource types.
    """


    @staticmethod
    def get_content_type(uri):
        """
        Returns the content type identified by the passed URI.

        Args:
            uri (String):   The URI of the source

        Returns:
            (ResourceType)
        """
        if uri.startswith(ResourceType.STREAM_HTTPS.value):
            return ResourceType.STREAM_HTTPS
        if uri.startswith(ResourceType.STREAM_HTTP.value):
            return ResourceType.STREAM_HTTP
        if uri.startswith(ResourceType.POOL.value):
            return ResourceType.POOL
        if uri.startswith(ResourceType.FILE.value):
            return ResourceType.FILE
        if uri.startswith(ResourceType.LINE.value):
            return ResourceType.LINE


    @staticmethod
    def get_content_class(content_type):
        """
        Returns the content class identified by the passed type.

        Args:
            content_type (ContentType):   The content type

        Returns:
            (ResourceType)
        """
        if content_type in ResourceClass.FILE.types:
            return ResourceClass.FILE
        if content_type in ResourceClass.STREAM.types:
            return ResourceClass.STREAM
        if content_type in ResourceClass.LIVE.types:
            return ResourceClass.LIVE
        if content_type in ResourceClass.PLAYLIST.types:
            return ResourceClass.PLAYLIST


    @staticmethod
    def generate_m3u_file(target_file, audio_store_path, entries, entry_extension):
        """
        Writes a M3U file based on the given playlist object.

        Args:
            target_file (File):         The M3U playlist to write
            audio_store_path (String):  Folder containing the source files
            entries (PlaylistEntry):    Entries of the playlist
            entry_extension (String):   The file extension of the playlist entries
        """
        file = open(target_file, "w")
        fb = [ "#EXTM3U" ]

        for entry in entries:     
            if ResourceUtil.get_content_type(entry.source) == ResourceType.FILE:
                path = ResourceUtil.uri_to_filepath(audio_store_path, entry.source, entry_extension)
                fb.append(f"#EXTINF:{entry.duration},{entry.meta_data.artist} - {entry.meta_data.title}")
                fb.append(path)

        file.writelines(fb)
        file.close()


    @staticmethod
    def uri_to_filepath(base_dir, uri, source_extension):
        """
        Converts a file-system URI to an actual, absolute path to the file.

        Args:
            basi_dir (String):          The location of the audio store.
            uri (String):               The URI of the file
            source_extension (String):  The file extension of audio sources

        Returns:
            path (String):  Absolute file path
        """
        return base_dir + "/" + uri[7:] + source_extension


    @staticmethod
    def get_entries_string(entries):
        """
        Returns a list of entries as String for logging purposes.
        """
        s = ""
        if isinstance(entries, list):
            for entry in entries:
                s += str(entry)
                if entry != entries[-1]: s += ", "
        else:
            s = str(entries)
        return s


    @staticmethod
    def lqs_annotate_cuein(uri, cue_in):
        """
        Wraps the given URI with a Liquidsoap Cue In annotation.

        Args:
            uri (String):   The path to the audio source
            cue_in (Float): The value in seconds wher the cue in should start
        
        Returns:
            (String):       The annotated URI
        """
        if cue_in > 0.0:
            uri = "annotate:liq_cue_in=\"%s\":%s" % (str(cue_in), uri)
        return uri