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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! # Input validation
//!
//! The extraction functions of RustyPipe will produce errors when fed with invalid input data
//! (e.g. YouTube ID's with invalid format). Therefore you will need to validate all untrusted
//! input data beforehand. The library offers two options for this:
//!
//! - The [URL resolver](crate::client::RustyPipeQuery::resolve_url) or
//! [string resolver](crate::client::RustyPipeQuery::resolve_string) is great for handling
//! arbitrary input and returns a [`UrlTarget`](crate::model::UrlTarget) enum that tells you
//! whether the given URL points to a video, channel, playlist, etc.
//! - The validation functions of this module are meant vor validating specific data (video IDs,
//! channel IDs, playlist IDs) and return [`true`] if the given input is valid
use crate::;
use Lazy;
use Regex;
/// Validate the given video ID
///
/// YouTube video IDs are exactly 11 characters long and consist of the charactes `A-Za-z0-9_-`.
///
/// # Examples
/// ```
/// # use rustypipe::validate;
/// assert!(validate::video_id("dQw4w9WgXcQ").is_ok());
/// assert!(validate::video_id("Abcd").is_err());
/// assert!(validate::video_id("dQw4w9WgXc@").is_err());
/// ```
/// Validate the given channel ID
///
/// YouTube channel IDs are exactly 24 characters long, start with the characters `UC`,
/// followed by 22 of these characters: `A-Za-z0-9_-`.
///
/// # Examples
/// ```
/// # use rustypipe::validate;
/// assert!(validate::channel_id("UC2DjFE7Xf11URZqWBigcVOQ").is_ok());
/// assert!(validate::channel_id("Abcd").is_err());
/// assert!(validate::channel_id("XY2DjFE7Xf11URZqWBigcVOQ").is_err());
/// ```
/// Validate the given playlist ID
///
/// YouTube playlist IDs start with the characters `PL` (user-created playlist),
/// `RDCLAK` (YouTube Music-curated playlist) or `OLAK` (YouTube Music album),
/// followed by at least 30 of these characters: `A-Za-z0-9_-`.
///
/// # Examples
/// ```
/// # use rustypipe::validate;
/// assert!(validate::playlist_id("PL4lEESSgxM_5O81EvKCmBIm_JT5Q7JeaI").is_ok());
/// assert!(validate::playlist_id("RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk").is_ok());
/// assert!(validate::playlist_id("OLAK5uy_k0yFrZlFRgCf3rLPza-lkRmCrtLPbK9pE").is_ok());
///
/// assert!(validate::playlist_id("Abcd").is_err());
/// ```
/// Validate the given album ID
///
/// YouTube Music album IDs are exactly 17 characters long, start with the characters `MPREB_`,
/// followed by 11 of these characters: `A-Za-z0-9_-`.
///
/// # Examples
/// ```
/// # use rustypipe::validate;
/// assert!(validate::album_id("MPREb_GyH43gCvdM5").is_ok());
/// assert!(validate::album_id("Abcd_GyH43gCvdM5").is_err());
/// ```
///
/// # Note
///
/// Albums on YouTube Music have an album ID (`MPREB_...`) and a playlist ID
/// (`OLAK...`). If you open an album on the YouTube Music website, the address bar shows
/// the playlist ID, not the album ID.
///
/// If you have the playlist ID of an album and need the album ID, you can use the
/// [string resolver](crate::client::RustyPipeQuery::resolve_string) with the `resolve_albums`
/// option enabled.
/// Validate the given radio ID
///
/// YouTube radio IDs start with the characters `RD`,
/// followed by at least 22 of these characters: `A-Za-z0-9_-`.
///
/// # Radio types
///
/// - Artist radio: `RDEMSuoM_jxfse1_g8uCO7MCtg`
/// - Genre radio: `RDQM1xqCV6EdPUw`
/// - Shuffle radio: `RDAOVeZA-2uzuUKdoB81Ha3srw`
/// - Playlist radio (`RDAMPL` + playlist ID): `RDAMPLPL4lEESSgxM_5O81EvKCmBIm_JT5Q7JeaI`
/// - Track radio (`RDAMVM` + video ID): `RDAMVMZeerrnuLi5E`
///
/// # Examples
///
/// ```
/// # use rustypipe::validate;
/// assert!(validate::radio_id("RDEMSuoM_jxfse1_g8uCO7MCtg").is_ok());
/// assert!(validate::radio_id("Abcd").is_err());
/// assert!(validate::radio_id("XYEMSuoM_jxfse1_g8uCO7MCtg").is_err());
/// ```
/// Validate the given genre ID
///
/// YouTube genre IDs are exactly 24 characters long, start with the characters `ggMPO`,
/// followed by 19 of these characters: `A-Za-z0-9_-`.
///
/// # Examples
///
/// ```
/// # use rustypipe::validate;
/// assert!(validate::genre_id("ggMPOg1uX1JOQWZFeDByc2Jm").is_ok());
/// assert!(validate::genre_id("Abcd").is_err());
/// assert!(validate::genre_id("ggAbcg1uX1JOQWZFeDByc2Jm").is_err());
/// ```
/// Validate the given related tracks ID
///
/// YouTube related IDs are exactly 17 characters long, start with the characters `MPTRt_`,
/// followed by 11 of these characters: `A-Za-z0-9_-`.
///
/// # Examples
///
/// ```
/// # use rustypipe::validate;
/// assert!(validate::track_related_id("MPTRt_wrKjTn9hmry").is_ok());
/// assert!(validate::track_related_id("Abcd").is_err());
/// assert!(validate::track_related_id("Abcdt_wrKjTn9hmry").is_err());
/// ```
/// Validate the given lyrics ID
///
/// YouTube lyrics IDs are exactly 17 characters long, start with the characters `MPLYt_`,
/// followed by 11 of these characters: `A-Za-z0-9_-`.
///
/// # Examples
///
/// ```
/// # use rustypipe::validate;
/// assert!(validate::track_lyrics_id("MPLYt_wrKjTn9hmry").is_ok());
/// assert!(validate::track_lyrics_id("Abcd").is_err());
/// assert!(validate::track_lyrics_id("Abcdt_wrKjTn9hmry").is_err());
/// ```
/// Validate the given channel handle
///
/// YouTube channel handles can be up to 30 characters long and start with an `@`.
/// Allowed characters are letters and numbers (Unicode), underscores (`_`), hyphens (`-`),
/// full stops (`.`) and middle dots (`· U+00B7`)
///
/// There are more fine-grained rules for specific scripts. Verifying these is not implemented.
///
/// Reference: <https://support.google.com/youtube/answer/11585688>
///
/// ```
/// # use rustypipe::validate;
/// assert!(validate::channel_handle("@EEVBlog").is_ok());
/// assert!(validate::channel_handle("@Āll·._-").is_ok());
/// assert!(validate::channel_handle("@한국").is_ok());
///
/// assert!(validate::channel_handle("noat").is_err());
/// assert!(validate::channel_handle("@no space").is_err());
/// ```