Debian Codegraph @

xl/metadata/_id3.py

raw
1 # Copyright (C) 2008-2010 Adam Olsen
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
6 # any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 #
18 # The developers of the Exaile media player hereby grant permission
19 # for non-GPL compatible GStreamer and Exaile plugins to be used and
20 # distributed together with GStreamer and Exaile. This permission is
21 # above and beyond the permissions granted by the GPL license by which
22 # Exaile is covered. If you modify this code, you may extend this
23 # exception to your version of the code, but you are not obligated to
24 # do so. If you do not wish to do so, delete this exception statement
25 # from your version.
26
27 from xl.metadata._base import BaseFormat, CoverImage
28 from xl import settings
29 from mutagen import id3
30
31
32 class ID3Format(BaseFormat):
33 MutagenType = id3.ID3
34 tag_mapping = {
35 "originalalbum": "TOAL",
36 "lyricist": "TEXT",
37 "part": "TSST",
38 "website": "WOAR",
39 "cover": "APIC",
40 "originalartist": "TOPE",
41 "author": "TOLY",
42 "originaldate": "TDOR",
43 "date": "TDRC",
44 "arranger": "TPE4",
45 "conductor": "TPE3",
46 "albumartist": "TPE2",
47 "artist": "TPE1",
48 "album": "TALB",
49 "copyright": "TCOP",
50 "lyrics": "USLT",
51 "tracknumber": "TRCK",
52 "version": "TIT3",
53 "title": "TIT2",
54 "isrc": "TSRC",
55 "genre": "TCON",
56 "composer": "TCOM",
57 "encodedby": "TENC",
58 "organization": "TPUB",
59 "discnumber": "TPOS",
60 "bpm": "TBPM",
61 "grouping": "TIT1",
62 "comment": "COMM",
63 'language': "TLAN",
64 '__rating': "POPM",
65 }
66 writable = True
67 others = False # make this true once custom tag support actually works
68
69 def get_keys_disk(self):
70 keys = []
71 vals = self._get_raw().values()
72 for v in self._get_raw().values():
73 # Have to use this because some ID3 tags have a colon
74 # in them, and so self._get_raw().keys() doesn't return
75 # the expected value
76 k = v.FrameID
77 if k in self._reverse_mapping:
78 keys.append(self._reverse_mapping[k])
79 else:
80 keys.append(k)
81 return keys
82
83 def _get_tag(self, raw, t):
84 if not raw.tags:
85 return []
86 if t not in self.tag_mapping.values():
87 t = "TXXX:" + t
88 field = raw.tags.getall(t)
89 if len(field) <= 0:
90 return []
91 ret = []
92 if t in ('TDRC', 'TDOR'): # values are ID3TimeStamps, need str conversion
93 for value in field:
94 ret.extend([str(x) for x in value.text])
95 elif t == 'USLT': # Lyrics are stored in a single str object
96 for value in field:
97 ret.append(value.text)
98 elif (
99 t == 'WOAR'
100 ): # URLs are stored in url field instead of text field (as a single str object)
101 for value in field:
102 ret.append(value.url.replace('\n', '').replace('\r', ''))
103 elif t == 'APIC':
104 ret = [
105 CoverImage(type=f.type, desc=f.desc, mime=f.mime, data=f.data)
106 for f in field
107 ]
108 elif t == 'COMM': # Newlines within comments are allowed, keep them
109 for item in field:
110 ret.extend([value for value in item.text])
111 elif t == 'POPM':
112 # Rating Stars
113 data = field[0].rating
114 rating = str(self._rating_to_stars(data))
115 ret.append(rating)
116 else:
117 for value in field:
118 try:
119 ret.extend(
120 [x.replace('\n', '').replace('\r', '') for x in value.text]
121 )
122 except Exception:
123 pass
124 return ret
125
126 def _set_tag(self, raw, tag, data):
127 if tag not in self.tag_mapping.values():
128 tag = "TXXX:" + tag
129
130 if raw.tags is not None:
131 raw.tags.delall(tag)
132
133 # FIXME: Properly set and retrieve multiple values
134 if tag == 'USLT':
135 data = data[0]
136
137 if tag == 'APIC':
138 frames = [
139 id3.Frames[tag](
140 encoding=3,
141 mime=info.mime,
142 type=info.type,
143 desc=info.desc,
144 data=info.data,
145 )
146 for info in data
147 ]
148 elif tag == 'COMM':
149 frames = [
150 id3.COMM(encoding=3, text=d, desc='', lang='\x00\x00\x00') for d in data
151 ]
152 elif tag == 'WOAR':
153 frames = [id3.WOAR(encoding=3, url=d) for d in data]
154 elif tag == 'POPM':
155 # Rating Stars
156 data = int(data[0])
157 rating = self._stars_to_rating(data)
158 email = settings.get_option(
159 'collection/write_rating_to_audio_file_metadata_popm_mail'
160 )
161 frames = [id3.POPM(email=email, rating=rating)]
162 else:
163 frames = [id3.Frames[tag](encoding=3, text=data)]
164
165 if raw.tags is not None:
166 for frame in frames:
167 raw.tags.add(frame)
168
169 def _del_tag(self, raw, tag):
170 if tag not in self.tag_mapping.values():
171 tag = "TXXX:" + tag
172 if raw.tags is not None:
173 raw.tags.delall(tag)
174
175 def _stars_to_rating(self, data: int) -> int:
176 stars_to_rating = {
177 0: 0,
178 20: 1,
179 40: 64,
180 60: 128,
181 80: 196,
182 100: 255,
183 }
184 return stars_to_rating[data]
185
186 def _rating_to_stars(self, data: int) -> int:
187 # mapping according to kid3
188 # 0: 0
189 # 1: 1 - 31
190 # 2: 32 - 95
191 # 3: 96 - 159
192 # 4: 160 - 223
193 # 5: 224 -
194 if data <= 0:
195 # 0
196 rating = 0
197 elif data <= 31:
198 # 1
199 rating = 20
200 elif data <= 95:
201 # 2
202 rating = 40
203 elif data <= 159:
204 # 3
205 rating = 60
206 elif data <= 223:
207 # 4
208 rating = 80
209 else:
210 # 5
211 rating = 100
212 return rating
213
214
215 # vim: et sts=4 sw=4