forked from mikez/spotify-folders
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfolders.py
196 lines (170 loc) · 6.74 KB
/
folders.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Get your Spotify folder hierarchy with playlists into JSON.
:license: MIT, see LICENSE for more details.
"""
from __future__ import print_function
import argparse
import json
import os
import re
import subprocess
import sys
try:
from urllib import unquote_plus # Python 2
except ImportError:
from urllib.parse import unquote_plus # Python 3
# Change if different on your machine.
MAC_PERSISTENT_CACHE_PATH = (
'~/Library/Application Support/Spotify/PersistentCache/Storage')
LINUX_PERSISTENT_CACHE_PATH = os.path.join(
os.getenv('XDG_CACHE_HOME', '~/.cache'), 'spotify/Storage')
PERSISTENT_CACHE_PATH = (
MAC_PERSISTENT_CACHE_PATH if sys.platform == 'darwin'
else LINUX_PERSISTENT_CACHE_PATH)
def parse(file_name, user_id):
"""
Parse a Spotify PersistentStorage file with folder structure at start.
`file_name`
Location of a PersistentStorage file.
`user_id`
Specify a user id to use for folder URIs. Can also be a
placeholder value like 'unknown'. (Background: this information
doesn't seem to be provided in the source file.)
"""
with open(file_name, 'rb') as data_file:
data = data_file.read()
# spotify:playlist, spotify:start-group, spotify:end-group
rows = re.split(b'spotify:[pse]', data)
folder = {'type': 'folder', 'children': []}
stack = []
for row in rows:
# Note: '\r' marks end of repeated block. This might break in
# future versions of Spotify. An alternative solution is to read
# the number of repeats coded into the protobuf file.
chunks = row.split(b'\r', 1)
row = chunks[0]
if row.startswith(b'laylist:'):
folder['children'].append({
'type': 'playlist',
'uri': 'spotify:p' + row[:-1].decode('utf-8')
})
elif row.startswith(b'tart-group:'):
stack.append(folder)
tags = row.split(b':')
folder = dict(
# Assuming folder names < 128 characters.
# Alternatively, do a protobuf varint parser to get length.
name=unquote_plus(tags[-1][:-1].decode('utf-8')),
type='folder',
uri=(
'spotify:user:%s:folder:' % user_id
+ tags[-2].decode('utf-8')
),
children=[]
)
elif row.startswith(b'nd-group:'):
parent = stack.pop()
parent['children'].append(folder)
folder = parent
if folder.get('children') and len(chunks) > 1:
break
# close any remaining groups -- sometimes cache files contain errors.
while len(stack) > 0:
parent = stack.pop()
parent['children'].append(folder)
folder = parent
return folder
def get_folder(folder_id, data):
"""Get data for a particular folder in parsed data."""
data_type = data.get('type')
if data_type == 'folder':
if data.get('uri', '').endswith(folder_id):
return data
for child in data.get('children', []):
folder = get_folder(folder_id, child)
if folder:
return folder
def get_all_persistent_cache_files(path):
"""Get all files in PersistentCache storage with "start-group" marker."""
path = os.path.expanduser(path)
shell_command = (
'grep -rls "start-group" "{path}" --null | xargs -0 ls -t'
).format(path=path)
output = subprocess.check_output(shell_command, shell=True).strip()
if output:
return output.split(b'\n')
else:
return []
def print_info_text(number):
"""Prints info text for `number` of PersistentCache storage files."""
suffix = 'y' if number == 1 else 'ies'
message = 'Found {number} folder hierarch{suffix} on this machine.'.format(
number=number, suffix=suffix)
if number > 1:
message += (
'\n\n'
'To see the second one, run'
'\n\n'
' spotifyfolders --account 2\n')
print(message)
def _process(file_name, args, user_id='unknown'):
# preprocessing
if args.folder:
uri = args.folder
if '/' not in uri and ':' not in uri:
print('Specify folder as a URL or Spotify URI. See `--help`.')
return
separator = '/' if uri.find('/') > 0 else ':'
user_id = uri.split(separator)[-3]
folder_id = uri.split(separator)[-1]
data = parse(file_name, user_id=user_id)
# postprocessing
if args.folder:
data = get_folder(folder_id, data)
if not data:
print('Folder not found :(')
return
return json.dumps(data)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=(
'Get your Spotify folder hierarchy with playlists into JSON.'),
add_help=False)
parser.add_argument(
'folder', default=None, metavar='folder', nargs='?',
help=('Get only a specific Spotify folder. If omitted, returns entire '
'hierarchy. A folder is specified by its URL or URI. '
'Obtain this by dragging a folder into a Terminal window. '
'Alternatively, click on a folder in Spotify and do Cmd+C.'))
parser.add_argument(
'-i', '--info', action='store_const', const=True, default=False,
help='Information about Spotify folders on this machine.')
parser.add_argument(
'-a', '--account', dest='account', default='1',
help=('Sometimes a machine has multiple Spotify accounts. This gets a '
'Spotify folder hierachy of a specific account. 1 is the most '
'recently updated account, 2 is the second most recently '
'updated account, etc.'))
parser.add_argument(
'--cache', dest='cache_dir', default=PERSISTENT_CACHE_PATH,
help='Specify a custom PersistentCache directory to look for data in.')
parser.add_argument(
'-h', '--help', action='help', default=argparse.SUPPRESS,
help='Show this help message and exit.')
args = parser.parse_args()
cache_files = get_all_persistent_cache_files(args.cache_dir)
if args.info:
print_info_text(len(cache_files))
else:
if not args.account.isdigit() or int(args.account) == 0:
print('Specify account as a positive number. See `--help`.')
exit(0)
cache_file_index = int(args.account) - 1
if cache_file_index >= len(cache_files):
print('No data found in Spotify cache. If you have a custom cache '
'directory set, specify its path with the `--cache` flag.')
exit(0)
cache_file_name = cache_files[cache_file_index]
print(_process(cache_file_name, args))