-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_list.ts
299 lines (253 loc) · 10.3 KB
/
function_list.ts
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
import config, { VlcPlaylist, VlcNode, VlcStatus } from "./config.ts";
// Export lastPlayedItemId
export let lastPlayedItemId: string | null = null;
// Export recentlyPlayedIds
export const recentlyPlayedIds: Set<string> = new Set();
// Function to log messages with timestamp
export function log(message: string): void {
const timestamp = new Date().toISOString();
console.log(`[DEBUG] ${timestamp} - ${message}`);
}
// Function to decode URL-encoded string
export function decodeUri(uri: string): string {
return decodeURIComponent(uri.replace(/\+/g, " "));
}
// Function to get the currently playing media from VLC
export async function getCurrentMedia(): Promise<string | null> {
log("Fetching currently playing media from VLC HTTP API...");
const response = await fetch(
`http://localhost:${config.vlcHttpPort}/requests/playlist.json`,
{
headers: {
"Authorization": `Basic ${btoa(`:${config.vlcHttpPassword}`)}`,
},
},
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const playlist: VlcPlaylist = await response.json();
// Check if the playlist has children
if (!playlist.children || playlist.children.length === 0) {
log("No items in the playlist.");
return null; // Return null instead of throwing an error
}
// Find the currently playing media
const currentMedia = playlist.children[0].children?.find((item: VlcNode) =>
item.current === "current"
);
if (!currentMedia || !currentMedia.uri) {
log("No media is currently playing.");
return null; // Return null instead of throwing an error
}
// Store the ID of the current item before we advance
lastPlayedItemId = currentMedia.id;
log(`Current media URI: ${currentMedia.uri}`);
return currentMedia.uri;
}
// Function to move file to good folder
export async function moveFile(filePath: string): Promise<void> {
const path = await import("https://deno.land/[email protected]/path/mod.ts");
// Get directory and filename
log("Extracting directory and filename...");
const directory = path.dirname(filePath);
const filename = path.basename(filePath);
log(`Directory: ${directory}`);
log(`Filename: ${filename}`);
// Create good folder if it doesn't exist
const goodFolderPath = path.join(directory, config.goodFolder);
log(`Creating 'good' folder if it doesn't exist...`);
try {
await Deno.mkdir(goodFolderPath, { recursive: true });
log(`'Good' folder created at: ${goodFolderPath}`);
} catch (error) {
if (!(error instanceof Deno.errors.AlreadyExists)) {
throw error;
}
}
// Move the file
const newPath = path.join(goodFolderPath, filename);
log("Moving file to 'good' folder...");
try {
await Deno.rename(filePath, newPath);
log(`Moved to good folder: ${filename}`);
} catch (error: unknown) {
if (error instanceof Error) {
log(`Error: ${error.message}`);
} else {
log(`Unknown error: ${String(error)}`);
}
}
}
// Function to delete file
export async function deleteFile(filePath: string): Promise<void> {
log(`Deleting file: ${filePath}`);
try {
await Deno.remove(filePath);
log(`File deleted: ${filePath}`);
} catch (error: unknown) {
if (error instanceof Error) {
log(`Error deleting file: ${error.message}`);
} else {
log(`Unknown error deleting file: ${String(error)}`);
}
}
}
// Function to advance to next playlist item
export async function advancePlaylist(): Promise<void> {
log("Advancing to the next item in the playlist...");
// Get the current playlist before advancing
const currentPlaylistResponse = await fetch(
`http://localhost:${config.vlcHttpPort}/requests/playlist.json`,
{
headers: {
"Authorization": `Basic ${btoa(`:${config.vlcHttpPassword}`)}`,
},
},
);
if (!currentPlaylistResponse.ok) {
throw new Error(`HTTP error! status: ${currentPlaylistResponse.status}`);
}
const currentPlaylist: VlcPlaylist = await currentPlaylistResponse.json();
const playlistItems = currentPlaylist.children[0].children || [];
// Find the current item's index
const currentIndex = playlistItems.findIndex((item: VlcNode) => item.current === "current");
if (currentIndex === -1) {
log("Current item not found in the playlist. Playing first item.");
// If no item is marked as 'current', play the first item
if (playlistItems.length > 0) {
const firstItem = playlistItems[0];
const endpoint = `http://localhost:${config.vlcHttpPort}/requests/status.json?command=pl_play&id=${firstItem.id}`;
const advanceResponse = await fetch(endpoint, {
headers: {
"Authorization": `Basic ${btoa(`:${config.vlcHttpPassword}`)}`,
},
});
if (!advanceResponse.ok) {
const errorBody = await advanceResponse.text();
log(`Failed to play first item: ${advanceResponse.status} - ${errorBody}`);
throw new Error(`Failed to play first item: ${advanceResponse.status}`);
}
return;
} else {
log("Playlist is empty, cannot advance.");
return;
}
}
// Check if we're at the end of the playlist
if (currentIndex < playlistItems.length - 1) {
// If we're not at the end of the playlist, play the next item by ID
const nextItem = playlistItems[currentIndex + 1];
const endpoint = `http://localhost:${config.vlcHttpPort}/requests/status.json?command=pl_play&id=${nextItem.id}`;
const advanceResponse = await fetch(endpoint, {
headers: {
"Authorization": `Basic ${btoa(`:${config.vlcHttpPassword}`)}`,
},
});
if (!advanceResponse.ok) {
const errorBody = await advanceResponse.text();
log(`Failed to advance playlist: ${advanceResponse.status} - ${errorBody}`);
throw new Error(`Failed to advance playlist: ${advanceResponse.status}`);
}
} else {
// If we're at the end of the playlist, loop back to the start
log("Reached the end of the playlist. Looping back to the start.");
if (playlistItems.length > 0) {
const firstItem = playlistItems[0];
const endpoint = `http://localhost:${config.vlcHttpPort}/requests/status.json?command=pl_play&id=${firstItem.id}`;
const advanceResponse = await fetch(endpoint, {
headers: {
"Authorization": `Basic ${btoa(`:${config.vlcHttpPassword}`)}`,
},
});
if (!advanceResponse.ok) {
const errorBody = await advanceResponse.text();
log(`Failed to play first item: ${advanceResponse.status} - ${errorBody}`);
throw new Error(`Failed to play first item: ${advanceResponse.status}`);
}
} else {
log("Playlist is empty, cannot loop back to start.");
}
}
log("Advanced to the next item.");
// Check the new current media
const newPlaylistResponse = await fetch(
`http://localhost:${config.vlcHttpPort}/requests/playlist.json`,
{
headers: {
"Authorization": `Basic ${btoa(`:${config.vlcHttpPassword}`)}`,
},
},
);
if (!newPlaylistResponse.ok) {
throw new Error(`HTTP error! status: ${newPlaylistResponse.status}`);
}
const newPlaylist: VlcPlaylist = await newPlaylistResponse.json();
const newCurrentMedia = newPlaylist.children[0].children?.find((item: VlcNode) =>
item.current === "current"
);
if (newCurrentMedia?.id) {
// If the current media ID is the same as the last played ID or has been played recently
if (newCurrentMedia.id === lastPlayedItemId || recentlyPlayedIds.has(newCurrentMedia.id)) {
log("Next item is the same as the previous item or has been played recently.");
// If there's only one item left, don't advance again
if (playlistItems.length <= 1) {
log("Playlist has only one item left. Stopping advance.");
return;
}
log("Advancing again...");
recentlyPlayedIds.add(newCurrentMedia.id);
await advancePlaylist(); // Call advancePlaylist again
} else {
// Store the ID of the *previous* item, not the current one
// lastPlayedItemId = newCurrentMedia.id;
recentlyPlayedIds.add(newCurrentMedia.id);
log(`Added ${newCurrentMedia.id} to recently played items`);
console.log(recentlyPlayedIds);
}
}
}
// Function to remove the most recently played playlist item
export async function removeMostRecentlyPlayedPlaylistItem(): Promise<void> {
log("Removing the most recently played playlist item...");
if (!lastPlayedItemId) {
log("No last played item ID found.");
return;
}
const deleteResponse = await fetch(
`http://localhost:${config.vlcHttpPort}/requests/status.json?command=pl_delete&id=${lastPlayedItemId}`,
{
headers: {
"Authorization": `Basic ${btoa(`:${config.vlcHttpPassword}`)}`,
},
},
);
if (!deleteResponse.ok) {
const errorBody = await deleteResponse.text();
log(`Failed to remove last played item: ${deleteResponse.status} - ${errorBody}`);
throw new Error(`Failed to remove last played item: ${deleteResponse.status}`);
} else {
const json = await deleteResponse.json();
const filename = json.information.category.meta.filename;
log(`filename: ${filename}`);
log("Most recently played playlist item removed.");
}
}
// Add this function after the other utility functions
export async function isShuffleEnabled(): Promise<boolean> {
log("Checking if shuffle is enabled...");
const response = await fetch(
`http://localhost:${config.vlcHttpPort}/requests/status.json`,
{
headers: {
"Authorization": `Basic ${btoa(`:${config.vlcHttpPassword}`)}`,
},
},
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const status: VlcStatus = await response.json();
log(`Shuffle status: ${status.random}`);
return status.random;
}