Skip to content

Commit 0538a80

Browse files
committed
Replace youtube-ext with @distube/ytsr
1 parent 79d57c9 commit 0538a80

6 files changed

Lines changed: 71 additions & 83 deletions

File tree

package-lock.json

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/app/webpack.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ module.exports = (env) => {
116116
EventEmitter: ['events', 'EventEmitter']
117117
}),
118118
new webpack.ContextReplacementPlugin(
119-
/\/(@distube\/ytpl|@distube\/ytsr|youtube-ext|bandcamp-scraper|http-cookie-agent|deasync)\//,
119+
/\/(@distube\/ytpl|@distube\/ytsr|bandcamp-scraper|http-cookie-agent|deasync)\//,
120120
false
121121
),
122122
new webpack.ContextReplacementPlugin(

packages/core/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949
"search-azlyrics": "0.0.3",
5050
"tough-cookie": "^4.1.4",
5151
"ts-jest": "^27.1.3",
52-
"uuid": "^9.0.0",
53-
"youtube-ext": "1.1.25"
52+
"uuid": "^9.0.0"
5453
},
5554
"devDependencies": {
5655
"@types/fast-levenshtein": "^0.0.2",
@@ -63,4 +62,4 @@
6362
"ts-node": "^10.7.0",
6463
"typescript": "^4.2.4"
6564
}
66-
}
65+
}

packages/core/src/rest/Youtube.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { logger } from '../../';
22
import ytdl from '@distube/ytdl-core';
33
import ytpl from '@distube/ytpl';
4-
import {search, SearchVideo} from 'youtube-ext';
54

65
import { StreamData, StreamQuery } from '../plugins/plugins.types';
76
import * as SponsorBlock from './SponsorBlock';
87
import { YoutubeHeuristics } from './heuristics';
9-
import ytsr from '@distube/ytsr';
8+
import ytsr, { Video } from '@distube/ytsr';
109

1110
export type YoutubeResult = {
1211
streams: { source: string, id: string }[]
@@ -132,18 +131,18 @@ export async function trackSearch(query: StreamQuery, sourceName?: string) {
132131
export async function trackSearchByString(query: StreamQuery, sourceName?: string, useSponsorBlock = true): Promise<StreamData[]> {
133132
const terms = query.artist + ' ' + query.track;
134133

135-
const results = await search(terms, { filterType: 'video' });
134+
const tracks = (await ytsr(terms, { safeSearch: false, type: 'video', limit: 10 })).items.filter((item) => item.isLive === false);
136135

137136
const heuristics = new YoutubeHeuristics();
138137

139138
const orderedTracks = heuristics.orderTracks({
140-
tracks: results.videos,
139+
tracks,
141140
artist: query.artist,
142141
title: query.track
143142
});
144143

145144
return orderedTracks
146-
.map((track) => videoToStreamData(track as SearchVideo, sourceName));
145+
.map((track) => videoToStreamData(track as Video, sourceName));
147146
}
148147

149148
export const getStreamForId = async (id: string, sourceName: string, useSponsorBlock = true): Promise<StreamData> => {
@@ -176,17 +175,17 @@ export const getStreamForId = async (id: string, sourceName: string, useSponsorB
176175
}
177176
};
178177

179-
function videoToStreamData(video: SearchVideo, source: string): StreamData {
178+
function videoToStreamData(video: Video, source: string): StreamData {
180179
return {
181180
source,
182181
id: video.id,
183182
stream: undefined,
184-
duration: parseInt(video.duration.text),
185-
title: video.title,
183+
duration: parseInt(video.duration),
184+
title: video.name,
186185
thumbnail: getLargestThumbnail(video.thumbnails),
187186
originalUrl: video.url,
188187
author: {
189-
name: video.channel.name,
188+
name: video.author.name,
190189
thumbnail: getLargestThumbnail(video.thumbnails)
191190
}
192191
};

packages/core/src/rest/heuristics.test.ts

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import { SearchVideo } from 'youtube-ext';
1+
import { Video } from '@distube/ytsr';
22
import { YoutubeHeuristics } from './heuristics';
33

4-
export const ytTrack = (overrides?: Partial<SearchVideo>): Partial<SearchVideo> => ({
5-
title: 'Black Sabbath - Paranoid',
6-
channel: {
4+
export const ytTrack = (overrides?: Partial<Video>): Partial<Video> => ({
5+
name: 'Black Sabbath - Paranoid',
6+
author: {
77
name: 'Black Sabbath'
8-
} as SearchVideo['channel'],
9-
duration: { text: '172', pretty: '2:12' },
8+
} as Video['author'],
9+
duration: '172',
1010
...overrides
1111
});
1212

1313
describe('Search heuristics', () => {
1414
it('finds a verbatim match', () => {
1515
const tracks = [
1616
ytTrack({
17-
title: 'Ozzy Osbourne - Crazy Train'
17+
name: 'Ozzy Osbourne - Crazy Train'
1818
}),
1919
ytTrack({
20-
title: 'Frank Zappa - Muffin Man'
20+
name: 'Frank Zappa - Muffin Man'
2121
}),
2222
ytTrack({
23-
title: 'Black Sabbath - Paranoid'
23+
name: 'Black Sabbath - Paranoid'
2424
}),
2525
ytTrack({
26-
title: 'The White Stripes - Seven Nation Army'
26+
name: 'The White Stripes - Seven Nation Army'
2727
})
2828
];
2929

@@ -40,13 +40,13 @@ describe('Search heuristics', () => {
4040
it('prefers titles that contain the artist and title verbatim', () => {
4141
const tracks = [
4242
ytTrack({
43-
title: 'Rammstein - Zick Zack (Official Video)'
43+
name: 'Rammstein - Zick Zack (Official Video)'
4444
}),
4545
ytTrack({
46-
title: 'Rammstein - Zeit (Official Video)'
46+
name: 'Rammstein - Zeit (Official Video)'
4747
}),
4848
ytTrack({
49-
title: 'Rammstein - Zeig Dich (Official Lyric Video)'
49+
name: 'Rammstein - Zeig Dich (Official Lyric Video)'
5050
})
5151
];
5252

@@ -64,10 +64,10 @@ describe('Search heuristics', () => {
6464
it('prefers a closer match in case of a partial match', () => {
6565
const tracks = [
6666
ytTrack({
67-
title: 'Johnny Cash - The Mercy Seat'
67+
name: 'Johnny Cash - The Mercy Seat'
6868
}),
6969
ytTrack({
70-
title: 'Nick Cave - The Mercy Seat'
70+
name: 'Nick Cave - The Mercy Seat'
7171
})
7272
];
7373

@@ -83,13 +83,13 @@ describe('Search heuristics', () => {
8383
it('tolerates typos', () => {
8484
const tracks = [
8585
ytTrack({
86-
title: 'The Whte Stipes - Sevn Natn Ary'
86+
name: 'The Whte Stipes - Sevn Natn Ary'
8787
}),
8888
ytTrack({
89-
title: 'The Whte Stripes - Sevn Nation Army'
89+
name: 'The Whte Stripes - Sevn Nation Army'
9090
}),
9191
ytTrack({
92-
title: 'Arctic Monkeys - Do I Wanna Know'
92+
name: 'Arctic Monkeys - Do I Wanna Know'
9393
})
9494
];
9595

@@ -110,10 +110,10 @@ describe('Search heuristics', () => {
110110
it('prefers an official video', () => {
111111
const tracks = [
112112
ytTrack({
113-
title: 'Ozzy Osbourne - Crazy Train (crappy bootleg)'
113+
name: 'Ozzy Osbourne - Crazy Train (crappy bootleg)'
114114
}),
115115
ytTrack({
116-
title: 'Ozzy Osbourne - Crazy Train - Official Video'
116+
name: 'Ozzy Osbourne - Crazy Train - Official Video'
117117
})
118118
];
119119

@@ -130,13 +130,13 @@ describe('Search heuristics', () => {
130130
it('prefers high quality videos', () => {
131131
const tracks = [
132132
ytTrack({
133-
title: 'Ozzy Osbourne - Crazy Train (crappy bootleg)'
133+
name: 'Ozzy Osbourne - Crazy Train (crappy bootleg)'
134134
}),
135135
ytTrack({
136-
title: 'Ozzy Osbourne - Crazy Train (HQ)'
136+
name: 'Ozzy Osbourne - Crazy Train (HQ)'
137137
}),
138138
ytTrack({
139-
title: 'Ozzy Osbourne - Crazy Train - High Quality'
139+
name: 'Ozzy Osbourne - Crazy Train - High Quality'
140140
})
141141
];
142142

@@ -157,20 +157,20 @@ describe('Search heuristics', () => {
157157
it('prefers videos with closest duration', () => {
158158
const tracks = [
159159
ytTrack({
160-
title: 'Frank Zappa - Peaches en Regalia',
161-
duration: { text: '180', pretty: '3:00' }
160+
name: 'Frank Zappa - Peaches en Regalia',
161+
duration: '180'
162162
}),
163163
ytTrack({
164-
title: 'Frank Zappa - Peaches en Regalia',
165-
duration: { text: '219', pretty: '3:39' }
164+
name: 'Frank Zappa - Peaches en Regalia',
165+
duration: '219'
166166
}),
167167
ytTrack({
168-
title: 'Frank Zappa - Peaches en Regalia',
169-
duration: { text: '300', pretty: '5:00' }
168+
name: 'Frank Zappa - Peaches en Regalia',
169+
duration: '300'
170170
}),
171171
ytTrack({
172-
title: 'Frank Zappa - Peaches en Regalia',
173-
duration: { text: '360', pretty: '6:00' }
172+
name: 'Frank Zappa - Peaches en Regalia',
173+
duration: '360'
174174
})
175175
];
176176

@@ -193,10 +193,10 @@ describe('Search heuristics', () => {
193193
it('ranks videos with full albums lower', () => {
194194
const tracks = [
195195
ytTrack({
196-
title: 'Black Sabbath - Paranoid (Full album)'
196+
name: 'Black Sabbath - Paranoid (Full album)'
197197
}),
198198
ytTrack({
199-
title: 'Black Sabbath - Paranoid (random addition to the title)'
199+
name: 'Black Sabbath - Paranoid (random addition to the title)'
200200
})
201201
];
202202

@@ -216,10 +216,10 @@ describe('Search heuristics', () => {
216216
it('ranks live videos lower', () => {
217217
const tracks = [
218218
ytTrack({
219-
title: 'Black Sabbath - Paranoid (Live)'
219+
name: 'Black Sabbath - Paranoid (Live)'
220220
}),
221221
ytTrack({
222-
title: 'Black Sabbath - Paranoid (Something else)'
222+
name: 'Black Sabbath - Paranoid (Something else)'
223223
})
224224
];
225225

@@ -238,13 +238,13 @@ describe('Search heuristics', () => {
238238
it('ranks live videos normally if they also appear in the search query title', () => {
239239
const tracks = [
240240
ytTrack({
241-
title: 'Black Sabbath - Paranoid'
241+
name: 'Black Sabbath - Paranoid'
242242
}),
243243
ytTrack({
244-
title: 'Black Sabbath - Paranoid (Live)'
244+
name: 'Black Sabbath - Paranoid (Live)'
245245
}),
246246
ytTrack({
247-
title: 'Black Sabbath - Paranoid (Random addition to the title)'
247+
name: 'Black Sabbath - Paranoid (Random addition to the title)'
248248
})
249249
];
250250

@@ -265,10 +265,10 @@ describe('Search heuristics', () => {
265265
it('ranks remix videos lower', () => {
266266
const tracks = [
267267
ytTrack({
268-
title: 'Black Sabbath - Paranoid (Remix)'
268+
name: 'Black Sabbath - Paranoid (Remix)'
269269
}),
270270
ytTrack({
271-
title: 'Black Sabbath - Paranoid (Random addition to the title)'
271+
name: 'Black Sabbath - Paranoid (Random addition to the title)'
272272
})
273273
];
274274

@@ -289,16 +289,16 @@ describe('Search heuristics', () => {
289289
it('ranks videos whose channel names include the artist name higher', () => {
290290
const tracks = [
291291
ytTrack({
292-
title: 'Savant - Valkyrie',
293-
channel: {
292+
name: 'Savant - Valkyrie',
293+
author: {
294294
name: 'EssentialEDMusic'
295-
} as SearchVideo['channel']
295+
} as Video['author']
296296
}),
297297
ytTrack({
298-
title: 'Savant - Orakel - Valkyrie',
299-
channel: {
298+
name: 'Savant - Orakel - Valkyrie',
299+
author: {
300300
name: 'SAVANT'
301-
} as SearchVideo['channel']
301+
} as Video['author']
302302
})
303303
];
304304

@@ -318,16 +318,16 @@ describe('Search heuristics', () => {
318318
it('ranks videos whose names match the track title exactly, and channel names match artist name exactly higher', () => {
319319
const tracks = [
320320
ytTrack({
321-
title: 'Savant - Wildstyle',
322-
channel: {
321+
name: 'Savant - Wildstyle',
322+
author: {
323323
name: 'Tasty'
324-
} as SearchVideo['channel']
324+
} as Video['author']
325325
}),
326326
ytTrack({
327-
title: 'Firestarter',
328-
channel: {
327+
name: 'Firestarter',
328+
author: {
329329
name: 'SAVANT'
330-
} as SearchVideo['channel']
330+
} as Video['author']
331331
})
332332
];
333333

0 commit comments

Comments
 (0)