-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.py
More file actions
360 lines (269 loc) · 10.3 KB
/
extract.py
File metadata and controls
360 lines (269 loc) · 10.3 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Extract playlists from a non-XML iTunes Library file (.itl)
# Copyright (c) 2018 Benno Rice, released under the BSD (2 Clause) Licence.
# Important information on the encryption used in the .itl file found here:
# https://mrexodia.cf/reversing/2014/12/16/iTunes-Library-Format-1
# Highly useful information on the .itl format itself found here:
# https://github.com/josephw/titl/blob/master/titl-core/src/main/java/org/kafsemo/titl/ParseLibrary.java
# originally copied from https://gist.github.com/jeamland/c856e9993008c9611a9910a3b22f9479
import argparse
import collections
import csv
import enum
import io
import struct
import zlib
from urllib.parse import unquote
import unicodedata
from Crypto.Cipher import AES
HEADER_LENGTH = 0x90
CRYPTO_KEY = b'BHUILuilfghuila3'
Hdfm = collections.namedtuple('Hdfm', field_names=[
'file_length',
'version',
])
Hdsm = collections.namedtuple('Hdsm', field_names=[
'block_type',
'block_length',
])
Hghm = collections.namedtuple('Hghm', field_names=[])
Hohm = collections.namedtuple('Hohm', field_names=[
'record_length',
'type',
'data',
])
Halm = collections.namedtuple('Hghm', field_names=[])
Haim = collections.namedtuple('Haim', field_names=[])
Hilm = collections.namedtuple('Hilm', field_names=[])
Hiim = collections.namedtuple('Hiim', field_names=[])
Htlm = collections.namedtuple('Htlm', field_names=[])
Htim = collections.namedtuple('Htim', field_names=[
'record_length',
'sub_blocks',
'song_id',
'block_type',
# 'file_type',
# 'playtime',
# 'track_number',
# 'track_total',
# 'year',
# 'bit_rate',
# 'sample_rate',
# 'volume_adjustment',
# 'start_time',
# 'end_time',
# 'play_count',
# 'compilation',
# 'last_played',
# 'disk_number',
# 'disk_total',
# 'rating',
# 'added',
])
Hqlm = collections.namedtuple('Hqlm', field_names=[])
Hqim = collections.namedtuple('Hqlm', field_names=[])
Hsts = collections.namedtuple('Hsts', field_names=[])
Hplm = collections.namedtuple('Hplm', field_names=[])
Hpim = collections.namedtuple('Hpim', field_names=[
'item_count',
])
Hptm = collections.namedtuple('Hptm', field_names=[
'key',
])
Hslm = collections.namedtuple('Hslm', field_names=[])
Hpsm = collections.namedtuple('Hpsm', field_names=[])
Hrlm = collections.namedtuple('Hrlm', field_names=[])
Hrpm = collections.namedtuple('Hrpm', field_names=[])
class HohmType(enum.IntEnum):
TITLE = 0x02
ALBUM_TITLE = 0x03
ARTIST = 0x04
PLAYLIST_TITLE = 0x64
LOCAL_PATH = 0xb
HOHM_ODD_TYPES = (0x42, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x192, 0x1f7, 0x1f4, 0x202, 0x320)
class ItlIO(io.BytesIO):
def __init__(self, *args, **kwargs):
self.flipped = False
super().__init__(*args, **kwargs)
def skip(self, nbytes):
self.read(nbytes)
def read_ascii(self, nbytes):
return self.read(nbytes).decode('ascii')
def read_byte(self):
return self.read(1)[0]
def read_uint(self):
if self.flipped:
return struct.unpack('<I', self.read(4))[0]
else:
return struct.unpack('>I', self.read(4))[0]
class RecordParser:
def __init__(self, data):
self.data = ItlIO(data)
def parse(self):
while True:
record_type = self.data.read_ascii(4)
original_type = record_type
if not record_type:
return
if self.data.flipped:
record_type = record_type[-1::-1]
method = f'parse_{record_type}'
# print(original_type)
if not hasattr(self, method):
method = f'parse_{record_type[-1::-1]}'
if not hasattr(self, method):
# print(record_type, self.data.getvalue()[self.data.tell():])
raise ValueError(f"unknown record type: {record_type}")
self.data.flipped = True
length = self.data.read_uint() - 8
# print(length)
read_data = self.data.read(length)
data = ItlIO(read_data)
# print(read_data)
if self.data.flipped:
data.flipped = True
yield getattr(self, method)(data)
def parse_hdfm(self, data):
file_length = data.read_uint()
data.skip(4)
version_length = data.read_byte()
version = data.read_ascii(version_length)
return Hdfm(file_length=file_length,
version=version)
def parse_hdsm(self, data):
record_length = data.read_uint()
block_type = data.read_uint()
if block_type in (4, 22):
self.data.skip(record_length - len(data.getvalue()) - 8)
return Hdsm(block_type=block_type, block_length=record_length)
def parse_hghm(self, data):
return Hghm()
def parse_hohm(self, data):
record_length = data.read_uint()
hohm_type = data.read_uint()
hohm_data = self.data.read(record_length - len(data.getvalue()) - 8)
# print(hex(hohm_type), repr(hohm_data))
if hohm_type not in HOHM_ODD_TYPES:
hohm_data = hohm_data[16:]
# What even is character encoding?
# There might be something telling us what the encoding is but this
# is sufficient for current purposes.
if len(hohm_data) > 1 and len(hohm_data) % 2 == 0 and hohm_data[0] == 0:
hohm_data = hohm_data.decode('iso-8859-1')
elif len(hohm_data) > 1 and len(hohm_data) % 2 == 0 and hohm_data[-1] == 0:
hohm_data = hohm_data.decode('utf-16le')
else:
hohm_data = hohm_data.decode('iso-8859-1')
return Hohm(record_length=record_length, type=hohm_type, data=hohm_data)
def parse_halm(self, data):
return Halm()
def parse_haim(self, data):
return Haim()
def parse_hilm(self, data):
return Hilm()
def parse_hiim(self, data):
return Hiim()
def parse_htlm(self, data):
return Htlm()
def parse_htim(self, data):
record_length = data.read_uint()
sub_blocks = data.read_uint()
song_id = data.read_uint()
block_type = data.read_uint()
# data = self.data.read(record_length - len(data.getvalue()) - 8)
# print(repr(data))
return Htim(record_length, sub_blocks, song_id, block_type)
def parse_hqlm(self, data):
return Hqlm()
def parse_hqim(self, data):
return Hqim()
def parse_hsts(self, data):
return Hsts()
def parse_hplm(self, data):
return Hplm()
def parse_hpim(self, data):
data.skip(4 + 4)
item_count = data.read_uint()
return Hpim(item_count)
def parse_hptm(self, data):
data.skip(16)
key = data.read_uint()
return Hptm(key)
def parse_hslm(self, data):
return Hslm()
def parse_hpsm(self, data):
return Hpsm()
def parse_hrlm(self, data):
return Hrlm()
def parse_hrpm(self, data):
return Hrpm()
parser = argparse.ArgumentParser()
parser.add_argument('filename', nargs='?', default='iTunes Library.itl',
help='iTunes Library Filename')
args = parser.parse_args()
# So it appears that the .itl format, in modern versions of iTunes, has a header
# block containing some information, one part of which tells us how much of the
# following data is AES/ECB encrypted with a key that's made it around the
# Internet a bit. To get at the actual data you need to decrypt that bit in place
# then decompress (zlib) the bit after the initial header. After that it's a similar
# format to older iTunes library files.
itl = open(args.filename, 'rb').read()
header = itl[:HEADER_LENGTH]
crypt_length = (len(itl) - HEADER_LENGTH) & ~0xf
max_crypt_length = struct.unpack('>I', header[0x5C:0x60])[0]
crypt_length = min(crypt_length, max_crypt_length)
cipher = AES.new(CRYPTO_KEY, AES.MODE_ECB)
decrypted = cipher.decrypt(itl[HEADER_LENGTH:max_crypt_length + HEADER_LENGTH])
itl = decrypted + itl[max_crypt_length + HEADER_LENGTH:]
itl = header + zlib.decompress(itl)
track = {}
tracks = {}
playlist = {}
playlists = {}
# with open("/Users/kaipi/Desktop/parseditl.txt", "wb") as file:
# file.write(itl)
with open("/Users/kaipi/Desktop/parseditl.txt", "rb") as file:
itl = file.read()
for record in RecordParser(itl).parse():
if type(record) is Htim:
if track:
tracks[track['song_id']] = track
track = {'song_id': record.song_id}
elif type(record) is Hohm:
if record.type == HohmType.TITLE:
track['title'] = record.data
elif record.type == HohmType.ALBUM_TITLE:
track['album'] = record.data
elif record.type == HohmType.ARTIST:
track['artist'] = record.data
elif record.type == HohmType.LOCAL_PATH:
track['path'] = record.data
elif record.type == HohmType.PLAYLIST_TITLE:
playlist['title'] = record.data
elif type(record) is Hpim:
if playlist:
playlists[playlist['title']] = playlist
playlist = {'items': []}
elif type(record) is Hptm:
playlist['items'].append(record.key)
if track:
tracks[track['song_id']] = track
if playlist:
playlists[playlist['title']] = playlist
output = csv.writer(open('playlists.csv', 'w'))
for title, playlist in playlists.items():
# print(title, playlist)
# The playlists I was after had titles of the form 'YYYY-M' or 'YYYY-MM'...
# if len(title) < 5 or title[0] != '2' or title[4] != '-':
# continue
# year, month = title.split('-')
# ... and I wanted to make them consistently 'YYYY-MM'.
# title = f'{year}-{int(month):02d}'
with open(f"/Users/kaipi/Desktop/playlists/{title.replace("- ", " ").replace("\\ ", " ")}.playlist.m3u8", "wb") as playlist_file:
playlist_file.write(b'\xEF\xBB\xBF')
for item in (tracks[x] for x in playlist['items']):
# print(repr(item))
if item.get('path', None) and item['path'].startswith("file://localhost/C:/Users/Patricio%20Tagle/Music"):
path = item['path'].replace("file://localhost/C:/Users/Patricio%20Tagle/Music/", "/storage/external_sd/Music/")
path = unicodedata.normalize("NFD", unquote(path))
playlist_file.write((path + "\n").encode())
output.writerow([title, item['title'], item.get('artist', "n/a"), item.get('album', ''), unquote(path)])