aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuis Ávila <[email protected]>2021-09-14 19:07:43 +0100
committerLuis Ávila <[email protected]>2021-09-14 19:07:43 +0100
commite59db766946d7ec98638e3a05eeaa5d15b265381 (patch)
tree20223c2aa049bc4a15bb047805164e7a17320e2d /src
parent1212ffc102641127479f82f522395c46b74255d0 (diff)
downloadmuse-e59db766946d7ec98638e3a05eeaa5d15b265381.tar.xz
muse-e59db766946d7ec98638e3a05eeaa5d15b265381.zip
Fix URL cleaning: youtube IDs are not valid URLs
Diffstat (limited to 'src')
-rw-r--r--src/services/get-songs.ts12
-rw-r--r--src/utils/url.ts18
2 files changed, 20 insertions, 10 deletions
diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts
index 129f167..8a040c0 100644
--- a/src/services/get-songs.ts
+++ b/src/services/get-songs.ts
@@ -9,6 +9,7 @@ import pLimit from 'p-limit';
import shuffle from 'array-shuffle';
import {QueuedSong, QueuedPlaylist} from '../services/player';
import {TYPES} from '../types';
+import {cleanUrl} from '../utils/url';
@injectable()
export default class {
@@ -34,16 +35,7 @@ export default class {
async youtubeVideo(url: string): Promise<QueuedSong|null> {
try {
- // Clean URL
- const u = new URL(url);
-
- for (const [name] of u.searchParams) {
- if (name !== 'v') {
- u.searchParams.delete(name);
- }
- }
-
- const videoDetails = await this.youtube.videos.get(u.toString());
+ const videoDetails = await this.youtube.videos.get(cleanUrl(url));
return {
title: videoDetails.snippet.title,
diff --git a/src/utils/url.ts b/src/utils/url.ts
new file mode 100644
index 0000000..f219929
--- /dev/null
+++ b/src/utils/url.ts
@@ -0,0 +1,18 @@
+import {URL} from 'url';
+
+export const cleanUrl = (url: string) => {
+ try {
+ // Clean URL
+ const u = new URL(url);
+
+ for (const [name] of u.searchParams) {
+ if (name !== 'v') {
+ u.searchParams.delete(name);
+ }
+ }
+ return u.toString();
+ }
+ catch (_: unknown) {
+ return url;
+ }
+} \ No newline at end of file