muthera 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
1
+ const { getImageUrl } = require("../functions/fetchImage");
2
+ const escapeRegExp = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3
+
4
+ class Track {
5
+ constructor(data, requester, node) {
6
+ this.track = data.encoded;
7
+ requester = requester;
8
+ this.info = {
9
+ identifier: data.info.identifier,
10
+ seekable: data.info.isSeekable,
11
+ author: data.info.author,
12
+ length: data.info.length,
13
+ stream: data.info.isStream,
14
+ position: data.info.position,
15
+ title: data.info.title,
16
+ uri: data.info.uri,
17
+ requester,
18
+ sourceName: data.info.sourceName,
19
+ };
20
+
21
+ this.info.isrc = data.info.isrc;
22
+
23
+ if (data.info.artworkUrl) {
24
+ this.info.thumbnail = data.info.artworkUrl;
25
+ } else {
26
+ this.info.thumbnail = getImageUrl(this.info);
27
+ }
28
+ }
29
+
30
+ async resolve(muthera) {
31
+ const query = [this.info.author, this.info.title]
32
+ .filter((x) => !!x)
33
+ .join(" - ");
34
+ const result = await muthera.resolve({
35
+ query,
36
+ source: muthera.options.defaultSearchPlatform,
37
+ requester: this.info.requester,
38
+ });
39
+
40
+ if (!result || !result.tracks.length) {
41
+ return;
42
+ }
43
+
44
+ const officialAudio = result.tracks.find((track) => {
45
+ const author = [this.info.author, `${this.info.author} - Topic`];
46
+ return (
47
+ author.some((name) =>
48
+ new RegExp(`^${escapeRegExp(name)}$`, "i").test(track.info.author)
49
+ ) ||
50
+ new RegExp(`^${escapeRegExp(this.info.title)}$`, "i").test(
51
+ track.info.title
52
+ )
53
+ );
54
+ });
55
+
56
+ if (officialAudio) {
57
+ this.info.identifier = officialAudio.info.identifier;
58
+ this.track = officialAudio.track;
59
+ return this;
60
+ }
61
+
62
+ if (this.info.length) {
63
+ const sameDuration = result.tracks.find(
64
+ (track) =>
65
+ track.info.length >=
66
+ (this.info.length ? this.info.length : 0) - 2000 &&
67
+ track.info.length <= (this.info.length ? this.info.length : 0) + 2000
68
+ );
69
+
70
+ if (sameDuration) {
71
+ this.info.identifier = sameDuration.info.identifier;
72
+ this.track = sameDuration.track;
73
+ return this;
74
+ }
75
+
76
+ const sameDurationAndTitle = result.tracks.find(
77
+ (track) =>
78
+ track.info.title === this.info.title &&
79
+ track.info.length >=
80
+ (this.info.length ? this.info.length : 0) - 2000 &&
81
+ track.info.length <= (this.info.length ? this.info.length : 0) + 2000
82
+ );
83
+
84
+ if (sameDurationAndTitle) {
85
+ this.info.identifier = sameDurationAndTitle.info.identifier;
86
+ this.track = sameDurationAndTitle.track;
87
+ return this;
88
+ }
89
+ }
90
+ this.info.identifier = result.tracks[0].info.identifier;
91
+ this.track = result.tracks[0].track;
92
+ return this;
93
+ }
94
+ }
95
+
96
+ module.exports = { Track };