-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.js
301 lines (252 loc) · 9.02 KB
/
utils.js
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
const axios = require("axios");
const logger = require("./logger");
var config = require("./config");
const ABS_TOKEN = config.ABS_TOKEN;
const ABS_URI = config.ABS_URI;
const ABS_LIBRARY_ID = config.ABS_LIBRARY_ID;
// Network Requests
async function getLibraryItems() {
try {
const config = {
headers: { Authorization: `Bearer ${ABS_TOKEN}` },
};
let path = `${ABS_URI}/api/libraries/${ABS_LIBRARY_ID}/items`;
logger.info("Fetching library item at:", path)
const { data } = await axios.get(path, config);
return data;
} catch (error) {
logger.error("Error fetching library items", error);
}
}
async function getLibraryItem(libraryItemId) {
try {
const config = {
headers: { Authorization: `Bearer ${ABS_TOKEN}` },
};
let path = `${ABS_URI}/api/items/${libraryItemId}`;
logger.info("Fetching library item at:", path)
const { data } = await axios.get(path, config);
return data;
} catch (error) {
logger.error("Error fetching library item", error);
}
}
async function getABSProgress(libraryItemId) {
try {
const config = {
headers: { Authorization: `Bearer ${ABS_TOKEN}` },
};
let path = `${ABS_URI}/api/me/progress/${libraryItemId}`;
logger.info("Fetching library item progress at:", path)
const { data } = await axios.get(path, config);
return data;
} catch (error) {
logger.error("Error fetching library item progress", error);
}
}
async function setProgress(updateObject, progress) {
try {
let libraryItemId = updateObject.libraryItemId;
const config = {
headers: { Authorization: `Bearer ${ABS_TOKEN}` },
};
let path = `${ABS_URI}/api/me/progress/${libraryItemId}`;
logger.info("Setting library item progress at:", path)
const updateData = {
duration: progress.bookDuration,
currentTime: progress.progress,
progress: progress.progress / progress.bookDuration,
};
logger.debug('updateData', updateData)
const { data } = await axios.patch(path, updateData, config);
} catch (error) {
console.error("Error setting library item progress", error);
}
}
// Build the Objects
async function buildMediaURI(id) {
let path = `${ABS_URI}/api/items/${id}?token=${ABS_TOKEN}`
logger.info('Building Media URI to fetch...', path)
return {
getMediaURIResult: path,
};
}
async function buildLibraryMetadataResult(res) {
let libraryItems = res.results;
let count = res.total;
let total = count;
let mediaMetadata = [];
for (const libraryItem of libraryItems) {
// https://developer.sonos.com/build/content-service-add-features/save-resume-playback/
var mediaMetadataEntry = {
itemType: "audiobook",
id: libraryItem.id,
//mimeType: libraryItem.media.audioFiles[0].mimeType,
canPlay: true,
canResume: true,
title: libraryItem.media.metadata.title,
summary: libraryItem.media.metadata.description,
//authorId: libraryItem.media.metadata.authors[0].id,
//author: libraryItem.media.metadata.authors[0].name,
//narratorId: libraryItem.media.metadata.narrators[0].id,
//narrator: libraryItem.media.metadata.narrators[0].name,
//albumArtURI: `${ABS_URI}${libraryItem.media.coverPath}?token=${ABS_TOKEN}`,
};
logger.debug("libraryItem for mediaMetadataEntry:", libraryItem)
mediaMetadata.push(mediaMetadataEntry);
}
// count and total HAVE to be correct, otherwise the sonos app falls over silently
return {
getMetadataResult: {
count: count,
total: total,
index: 0,
mediaCollection: mediaMetadata,
},
};
}
async function buildAudiobookTrackList(libraryItem, progressData) {
let tracks = libraryItem.media.audioFiles;
let icount = tracks.length;
let itotal = tracks.length;
let imediaMetadata = [];
for (const track of tracks) {
var mediaMetadataEntry = {
id: `${libraryItem.media.libraryItemId}/file/${track.ino}`,
itemType: "track",
title: track.metadata.filename,
mimeType: track.mimeType,
trackMetadata: {
authorId: libraryItem.media.metadata.authors[0].id,
author: libraryItem.media.metadata.authors[0].name,
// narratorId: libraryItem.media.metadata.narrators[0].id,
// narrator: libraryItem.media.metadata.narrators[0].name,
duration: track.duration,
book: libraryItem.media.metadata.title,
albumArtURI: `${ABS_URI}${libraryItem.media.coverPath}?token=${ABS_TOKEN}`,
canPlay: true,
canAddToFavorites: false,
},
};
imediaMetadata.push(mediaMetadataEntry);
}
let positionInformation = {};
if (progressData) {
logger.info(`Progress data found from ABS for ${libraryItem.id}`)
try {
positionInformation = {
id: `${libraryItem.id}/file/${progressData.partName}`, // UUID-ITEM-ID/12345
index: 0,
// 1) Sonos gets upset if there are too many decimals
// 2) ABS returns everything in seconds, so multiple by 1000 for milliseconds for sonos
offsetMillis: Math.round(progressData.relativeTimeForPart * 1000),
};
logger.debug("positionInformation for library item", positionInformation)
} catch (error) {
logger.derror("Error trying to get progressData", error.message)
}
}
return {
getMetadataResult: {
count: icount,
total: itotal,
index: 0,
positionInformation: positionInformation,
mediaMetadata: imediaMetadata,
},
};
}
function partNameAndRelativeProgress(currentProgress, libraryItem) {
// create an array of each parts "running sum" (the current part + all previous parts durations)
// find which part the currentProgress exists in, and return that part name and how far along it we are
let audioFiles = libraryItem.media.audioFiles;
let durations = audioFiles.map((x) => x.duration);
let currentTime = currentProgress.currentTime;
let newDurationSums = [];
let running = 0;
let res = {
partName: "",
relativeTimeForPart: 0,
};
for (const duration of durations) {
running += duration;
newDurationSums.push(running);
}
let inThisPart;
for (const duration of newDurationSums) {
if (duration > currentTime) {
inThisPart = duration;
break;
}
}
let closestIndex = newDurationSums.indexOf(inThisPart);
logger.debug("newDurationSums", newDurationSums)
logger.debug("closestIndex", closestIndex)
logger.debug("newDuration[closestIndex]", newDurationSums[closestIndex])
logger.debug("Maths", Math.abs(currentTime - newDurationSums[closestIndex - 1]))
res.partName = audioFiles[closestIndex].ino;
res.relativeTimeForPart =
durations[closestIndex] == 0
? currentTime
: Math.abs(currentTime - newDurationSums[closestIndex - 1]);
return res;
}
async function buildProgress(libraryItem, updateObject) {
//let partId = updateObject.libraryItemIdAndFileName.split("/")[1]; // li_{string}/Part##.mp3
let partId = updateObject.libraryItemIdAndFileName.split("/")[2]; // ITEM-UUID/file/12345 -> 12345
logger.debug("partId", partId)
let audioFiles = libraryItem.media.audioFiles;
logger.debug("audioFiles", audioFiles)
let res = {
progress: updateObject.positionMillis / 1000, // abs tracks progress in seconds
bookDuration: audioFiles
.map((audioFile) => audioFile.duration)
.reduce((result, item) => result + item),
};
logger.debug("res", res)
for (const audioFile of audioFiles) {
let filename = audioFile.ino;
if (filename == partId) {
// only grab as much duration as up to the part we are currently at
break;
}
res.progress += audioFile.duration;
}
return res;
}
// Methods to invoke
async function getMediaURI(id) {
logger.info("called with id", id)
return await buildMediaURI(id);
}
async function getMetadataResult(libraryItemId) {
if (libraryItemId == "root") {
let libraryItems = await getLibraryItems();
return await buildLibraryMetadataResult(libraryItems);
} else {
let libraryItem = await getLibraryItem(libraryItemId);
// if there is existing progress, figure it out here and send it along
let absProgress = await getABSProgress(libraryItemId);
let progressData;
if (absProgress) {
logger.info("absProgress found! absProgress", absProgress)
progressData = partNameAndRelativeProgress(absProgress, libraryItem);
logger.debug("progressData from partNameAndRelativeProgress", progressData)
}
return await buildAudiobookTrackList(libraryItem, progressData);
}
}
async function updateAudioBookshelfProgress(updateObject) {
// 1. grab the library item
// 2. sum up all parts prior to current from updateObject (grabing durations from step 1)
// 3. add updateObject.positionMillis to sum from step 2
logger.info("Updating audiobook progress...")
let libraryItem = await getLibraryItem(updateObject.libraryItemId); // lets us get durations per part
let progress = await buildProgress(libraryItem, updateObject);
return await setProgress(updateObject, progress);
}
module.exports = {
getMetadataResult,
getMediaURI,
updateAudioBookshelfProgress,
};