mirror of
https://github.com/BluemediaGER/muse.git
synced 2024-11-13 21:35:28 +01:00
Fix URL cleaning: youtube IDs are not valid URLs
This commit is contained in:
parent
1212ffc102
commit
e59db76694
|
@ -9,6 +9,7 @@ import pLimit from 'p-limit';
|
||||||
import shuffle from 'array-shuffle';
|
import shuffle from 'array-shuffle';
|
||||||
import {QueuedSong, QueuedPlaylist} from '../services/player';
|
import {QueuedSong, QueuedPlaylist} from '../services/player';
|
||||||
import {TYPES} from '../types';
|
import {TYPES} from '../types';
|
||||||
|
import {cleanUrl} from '../utils/url';
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export default class {
|
export default class {
|
||||||
|
@ -34,16 +35,7 @@ export default class {
|
||||||
|
|
||||||
async youtubeVideo(url: string): Promise<QueuedSong|null> {
|
async youtubeVideo(url: string): Promise<QueuedSong|null> {
|
||||||
try {
|
try {
|
||||||
// Clean URL
|
const videoDetails = await this.youtube.videos.get(cleanUrl(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());
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title: videoDetails.snippet.title,
|
title: videoDetails.snippet.title,
|
||||||
|
|
18
src/utils/url.ts
Normal file
18
src/utils/url.ts
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue