Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement clean title property for youtube videos #1908

Merged
merged 7 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/website/src/data/showcase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const ShowcaseResource: IShowcase = {
extractors: PromotedList.extractors.concat([
{
name: 'discord-player-deezer',
description: 'An unofficial extractor for discord-player to add support for deezer source.',
description: 'An official extractor for discord-player to add support for deezer source.',
url: 'https://npm.im/discord-player-deezer'
},
{
Expand Down
1 change: 1 addition & 0 deletions packages/discord-player/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@types/ip": "^1.1.0",
"@types/node": "^18.6.3",
"@types/ws": "^8.5.3",
"@web-scrobbler/metadata-filter": "^3.1.0",
"discord-api-types": "^0.37.0",
"discord.js": "^14.1.2",
"opusscript": "^0.0.8",
Expand Down
5 changes: 4 additions & 1 deletion packages/discord-player/src/fabric/Track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { BaseExtractor } from '../extractors/BaseExtractor';
import { Collection } from '@discord-player/utils';
import { TypeUtil } from '../utils/TypeUtil';
import { SerializedType, tryIntoThumbnailString } from '../utils/serde';
import { Exceptions } from '../errors';
import { Exceptions } from '../errors';
import { Util } from '../utils/Util';

export type TrackResolvable = Track | string | number;

Expand Down Expand Up @@ -36,6 +37,7 @@ export class Track<T = unknown> {
public readonly id = SnowflakeUtil.generate().toString();
private __metadata: T | null = null;
private __reqMetadataFn: () => Promise<T | null>;
public cleanTitle: string;

/**
* Track constructor
Expand All @@ -56,6 +58,7 @@ export class Track<T = unknown> {
this.raw = Object.assign({}, { source: data.raw?.source ?? data.source }, data.raw ?? data);
this.__metadata = data.metadata ?? null;
this.__reqMetadataFn = data.requestMetadata || (() => Promise.resolve<T | null>(null));
this.cleanTitle = data.cleanTitle ?? Util.cleanTitle(this.title, this.source);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/discord-player/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export interface RawTrackData {
* The query type
*/
queryType?: SearchQueryType;
/**
* The seralised title
*/
cleanTitle?: string
}

export interface TimeData {
Expand Down
24 changes: 24 additions & 0 deletions packages/discord-player/src/utils/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { GuildQueue } from '../queue';
import { Playlist, Track } from '../fabric';
import { Exceptions } from '../errors';
import { randomInt } from 'node:crypto';
import { createFilter, createSpotifyFilter, fixTrackSuffix, removeLive, removeRemastered, youtube } from '@web-scrobbler/metadata-filter';
import { TrackSource } from '../../dist';

export type RuntimeType = 'node' | 'deno' | 'bun' | 'unknown';

Expand Down Expand Up @@ -102,6 +104,28 @@ class Util {
return channel && channel.members.filter((member) => !member.user.bot).size === 0;
}

static cleanTitle(title: string, source: TrackSource) {
const filterOpts = {
track: [
removeRemastered,
removeLive,
fixTrackSuffix
]
};
const spotifyFilter = createFilter(filterOpts);
spotifyFilter.extend(createSpotifyFilter());
const defaultFilter = createFilter(filterOpts);

switch(source) {
case "youtube":
return youtube(title);
case "spotify":
return spotifyFilter.filterField("track", title);
default:
return defaultFilter.filterField("track", title);
}
}

/**
* Safer require
* @param {string} id Node require id
Expand Down
2 changes: 1 addition & 1 deletion packages/extractor/src/extractors/AppleMusicExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class AppleMusicExtractor extends BridgedExtractor<AppleMusicExtractorIni
source: info,
bridge: this.options.bridgeProvider ? (await this.options.bridgeProvider.resolve(this, track)).data : await pullYTMetadata(this, track)
};
}
},
});

track.extractor = this;
Expand Down
9 changes: 6 additions & 3 deletions packages/extractor/src/extractors/SoundCloudExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export class SoundCloudExtractor extends BaseExtractor<SoundCloudExtractorInit>
metadata: trackInfo,
requestMetadata: async () => {
return trackInfo;
}
},
cleanTitle: trackInfo.title
});

newTrack.extractor = this;
Expand Down Expand Up @@ -112,7 +113,8 @@ export class SoundCloudExtractor extends BaseExtractor<SoundCloudExtractorInit>
metadata: trackInfo,
requestMetadata: async () => {
return trackInfo;
}
},
cleanTitle: trackInfo.title
});

track.extractor = this;
Expand Down Expand Up @@ -156,7 +158,8 @@ export class SoundCloudExtractor extends BaseExtractor<SoundCloudExtractorInit>
metadata: song,
requestMetadata: async () => {
return song;
}
},
cleanTitle: song.title
});
track.extractor = this;
track.playlist = res;
Expand Down
2 changes: 1 addition & 1 deletion packages/extractor/src/extractors/common/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export async function loadYtdl(options?: any, force = false) {

const cookie = options?.requestOptions?.headers?.cookie;

if(cookie && typeof cookie === "string") dl.cookie = cookie
if(cookie && typeof cookie === "string") dl.cookie = cookie;

// @ts-ignore Default lib did not provide types for this function
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3651,6 +3651,13 @@ __metadata:
languageName: node
linkType: hard

"@web-scrobbler/metadata-filter@npm:^3.1.0":
version: 3.1.0
resolution: "@web-scrobbler/metadata-filter@npm:3.1.0"
checksum: 71e0ff278efc0c6d09bc60cf1cde67e67369d6fb13aee7dcb946a18f02c5387bfb8a391e65fe945f7a4c4cd3fdaad02539a197ff5e14961719eb1349fc42d393
languageName: node
linkType: hard

"abbrev@npm:1, abbrev@npm:^1.0.0":
version: 1.1.1
resolution: "abbrev@npm:1.1.1"
Expand Down Expand Up @@ -4893,6 +4900,7 @@ __metadata:
"@types/ip": "npm:^1.1.0"
"@types/node": "npm:^18.6.3"
"@types/ws": "npm:^8.5.3"
"@web-scrobbler/metadata-filter": "npm:^3.1.0"
discord-api-types: "npm:^0.37.0"
discord-voip: "npm:^0.1.3"
discord.js: "npm:^14.1.2"
Expand Down
Loading