-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
SearchResult.ts
123 lines (118 loc) · 2.65 KB
/
SearchResult.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { DisTubeError, SearchResultType, formatDuration, toSecond } from "..";
import type { Playlist, Video } from "@distube/ytsr";
/**
* A abstract class representing a search result.
*
* @virtual
*/
abstract class ISearchResult {
source: "youtube";
abstract type: SearchResultType;
id: string;
name: string;
url: string;
uploader: {
name?: string;
url?: string;
};
/**
* Create a search result
*
* @param info - ytsr result
*/
constructor(info: Video | Playlist) {
/**
* The source of the search result
*/
this.source = "youtube";
/**
* YouTube video or playlist id
*/
this.id = info.id;
/**
* Video or playlist title.
*/
this.name = info.name;
/**
* Video or playlist URL.
*/
this.url = info.url;
/**
* Video or playlist uploader
*/
this.uploader = {
name: undefined,
url: undefined,
};
}
}
/**
* A class representing a video search result.
*/
export class SearchResultVideo extends ISearchResult {
type: SearchResultType.VIDEO;
views: number;
isLive: boolean;
duration: number;
formattedDuration: string;
thumbnail: string;
constructor(info: Video) {
super(info);
if (info.type !== "video") throw new DisTubeError("INVALID_TYPE", "video", info.type, "type");
/**
* Type of SearchResult
*/
this.type = SearchResultType.VIDEO;
/**
* Video views count
*/
this.views = info.views;
/**
* Indicates if the video is an active live.
*/
this.isLive = info.isLive;
/**
* Video duration.
*/
this.duration = this.isLive ? 0 : toSecond(info.duration);
/**
* Formatted duration string `hh:mm:ss` or `mm:ss`.
*/
this.formattedDuration = this.isLive ? "Live" : formatDuration(this.duration);
/**
* Video thumbnail.
*/
this.thumbnail = info.thumbnail;
this.uploader = {
name: info.author?.name,
url: info.author?.url,
};
}
}
/**
* A video or playlist search result
*/
export type SearchResult = SearchResultVideo | SearchResultPlaylist;
/**
* A class representing a playlist search result.
*/
export class SearchResultPlaylist extends ISearchResult {
type: SearchResultType.PLAYLIST;
length: number;
constructor(info: Playlist) {
super(info);
if (info.type !== "playlist") throw new DisTubeError("INVALID_TYPE", "playlist", info.type, "type");
/**
* Type of SearchResult
*/
this.type = SearchResultType.PLAYLIST;
/**
* Length of the playlist
*/
this.length = info.length;
this.uploader = {
name: info.owner?.name,
url: info.owner?.url,
};
}
}