1#![allow(dead_code)]
3
4extern crate alloc;
5use alloc::format;
6use alloc::string::{String, ToString};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum YouTubeUrlType {
11 Watch(String),
13 Shorts(String),
15 Embed(String),
17 Search(String),
19 Home,
21}
22
23pub fn is_youtube_host(host: &str) -> bool {
26 let h = host.to_ascii_lowercase();
27 let clean_h = h.split(':').next().unwrap_or(&h);
28 clean_h == "youtube.com"
29 || clean_h == "www.youtube.com"
30 || clean_h == "m.youtube.com"
31 || clean_h == "youtu.be"
32 || clean_h == "music.youtube.com"
33}
34
35pub fn extract_youtube_video_id(url: &str) -> Option<String> {
47 let clean = url.trim();
48
49 if !clean.contains("youtube.com") && !clean.contains("youtu.be") {
51 return None;
52 }
53
54 if let Some(pos) = clean.find("youtu.be/") {
56 let after = clean.get(pos + 9..).unwrap_or("");
57 let id_part = after.split(&['?', '&', '/', '#'][..]).next().unwrap_or("");
58 if is_valid_youtube_id(id_part) {
59 return Some(id_part.to_string());
60 }
61 }
62
63 if let Some(pos) = clean.find("/embed/") {
65 let after = clean.get(pos + 7..).unwrap_or("");
66 let id_part = after.split(&['?', '&', '/', '#'][..]).next().unwrap_or("");
67 if is_valid_youtube_id(id_part) {
68 return Some(id_part.to_string());
69 }
70 }
71
72 if let Some(pos) = clean.find("/shorts/") {
74 let after = clean.get(pos + 8..).unwrap_or("");
75 let id_part = after.split(&['?', '&', '/', '#'][..]).next().unwrap_or("");
76 if is_valid_youtube_id(id_part) {
77 return Some(id_part.to_string());
78 }
79 }
80
81 if let Some(pos) = clean.find("/v/") {
83 let after = clean.get(pos + 3..).unwrap_or("");
84 let id_part = after.split(&['?', '&', '/', '#'][..]).next().unwrap_or("");
85 if is_valid_youtube_id(id_part) {
86 return Some(id_part.to_string());
87 }
88 }
89
90 if clean.contains("/watch") || clean.contains("youtube.com") {
92 if let Some(q_idx) = clean.find('?') {
93 let query = clean.get(q_idx + 1..).unwrap_or("");
94 for pair in query.split('&') {
95 if let Some(val) = pair.strip_prefix("v=") {
96 let id_part = val.split('#').next().unwrap_or("");
97 if is_valid_youtube_id(id_part) {
98 return Some(id_part.to_string());
99 }
100 }
101 }
102 }
103 }
104
105 None
106}
107
108fn is_valid_youtube_id(id: &str) -> bool {
110 if id.len() != 11 {
111 return false;
112 }
113 id.chars()
114 .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
115}
116
117pub fn parse_youtube_url(url: &str) -> Option<YouTubeUrlType> {
120 let clean = url.trim();
121
122 if let Some(id) = extract_youtube_video_id(clean) {
124 if clean.contains("/shorts/") {
125 return Some(YouTubeUrlType::Shorts(id));
126 }
127 if clean.contains("/embed/") {
128 return Some(YouTubeUrlType::Embed(id));
129 }
130 return Some(YouTubeUrlType::Watch(id));
131 }
132
133 if clean.contains("/results") {
135 if let Some(q_idx) = clean.find('?') {
136 let query_str = clean.get(q_idx + 1..).unwrap_or("");
137 for pair in query_str.split('&') {
138 if let Some(val) = pair.strip_prefix("search_query=").or_else(|| pair.strip_prefix("q=")) {
139 let decoded = super::duckduckgo::url_decode(val);
140 if !decoded.is_empty() {
141 return Some(YouTubeUrlType::Search(decoded));
142 }
143 }
144 }
145 }
146 }
147
148 let stripped = clean
151 .strip_prefix("https://")
152 .or_else(|| clean.strip_prefix("http://"))
153 .unwrap_or(clean);
154 let (host, path) = stripped.split_once('/').unwrap_or((stripped, ""));
155 if is_youtube_host(host)
156 && (path.is_empty() || path == "/" || path.starts_with('?')) {
157 return Some(YouTubeUrlType::Home);
158 }
159
160 if clean == "https://www.youtube.com"
161 || clean == "https://www.youtube.com/"
162 || clean == "http://www.youtube.com"
163 || clean == "http://www.youtube.com/"
164 || clean == "https://youtube.com"
165 || clean == "https://youtube.com/"
166 || clean == "https://m.youtube.com"
167 || clean == "https://m.youtube.com/"
168 || clean.starts_with("https://www.youtube.com/?")
169 || clean.starts_with("https://youtube.com/?")
170 {
171 return Some(YouTubeUrlType::Home);
172 }
173
174 None
175}
176
177pub fn get_youtube_thumbnail_url(video_id: &str) -> String {
180 format!("https://img.youtube.com/vi/{}/hqdefault.jpg", video_id)
181}
182
183pub fn get_youtube_stream_url(video_id: &str) -> String {
186 match video_id {
187 "aqz-KE-bpKQ" => String::from("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"),
188 "dQw4w9WgXcQ" => String::from("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"),
189 "jNQXAC9IVRw" => String::from("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4"),
190 "kJQP7kiw5Fk" => String::from("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4"),
191 "M7lc1UVf-VE" => String::from("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyBlazes.mp4"),
192 "9bZkp7q19f0" => String::from("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerMeltdowns.mp4"),
193 "OPf0YbXqDm0" => String::from("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/Sintel.mp4"),
194 "C0DPdy98e4c" => String::from("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/SubaruOutbackSeeTheWorld.mp4"),
195 _ => String::from("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"),
196 }
197}
198
199#[derive(Debug, Clone, PartialEq, Eq)]
201pub struct YouTubeVideoInfo {
202 pub video_id: String,
203 pub title: String,
204 pub channel_name: String,
205 pub duration: String,
206 pub views: String,
207 pub time_ago: String,
208 pub description: String,
209}
210
211pub fn get_featured_videos() -> alloc::vec::Vec<YouTubeVideoInfo> {
214 alloc::vec![
215 YouTubeVideoInfo {
216 video_id: String::from("dQw4w9WgXcQ"),
217 title: String::from("AtmOS ベアメタル H.264 デコーダ再生デモ (Never Gonna Give You Up)"),
218 channel_name: String::from("Sugimura Laboratory Official"),
219 duration: String::from("3:33"),
220 views: String::from("15億回"),
221 time_ago: String::from("15年前"),
222 description: String::from("AtmOS 自前実装の H.264 Baseline ソフトウェアデコーダおよび AAC/PWM 同期音声出力による、フルスクラッチベアメタル動画再生デモです。Rust言語によるゼロコピー実装。"),
223 },
224 YouTubeVideoInfo {
225 video_id: String::from("jNQXAC9IVRw"),
226 title: String::from("Me at the zoo (2005) - YouTube 史上最初の動画アーカイブ"),
227 channel_name: String::from("jawed"),
228 duration: String::from("0:19"),
229 views: String::from("3.2億回"),
230 time_ago: String::from("2005/04/24"),
231 description: String::from("The very first video uploaded to YouTube. Recorded by Yakov Lapitsky at the San Diego Zoo."),
232 },
233 YouTubeVideoInfo {
234 video_id: String::from("aqz-KE-bpKQ"),
235 title: String::from("Big Buck Bunny - 4K オープンソースアニメーション(H.264 Baseline)"),
236 channel_name: String::from("Blender Foundation"),
237 duration: String::from("9:56"),
238 views: String::from("2,400万回"),
239 time_ago: String::from("2008年"),
240 description: String::from("A large and lovable rabbit deals with bullying forest creatures in this classic Blender open movie test clip. アニメーション映画。"),
241 },
242 YouTubeVideoInfo {
243 video_id: String::from("kJQP7kiw5Fk"),
244 title: String::from("Luis Fonsi - Despacito ft. Daddy Yankee"),
245 channel_name: String::from("Luis Fonsi"),
246 duration: String::from("4:41"),
247 views: String::from("85億回"),
248 time_ago: String::from("7年前"),
249 description: String::from("Official Music Video for Despacito performed by Luis Fonsi featuring Daddy Yankee. ラテンポップ音楽。"),
250 },
251 YouTubeVideoInfo {
252 video_id: String::from("M7lc1UVf-VE"),
253 title: String::from("YouTube API & Web Components プログラミング入門"),
254 channel_name: String::from("Google Developers"),
255 duration: String::from("8:15"),
256 views: String::from("180万回"),
257 time_ago: String::from("2年前"),
258 description: String::from("HTML5 Custom Elements, Shadow DOM, Polymer によるモダン Web フロントエンドの構築と、YouTube Player API の基礎解説。プログラミング技術講座。"),
259 },
260 YouTubeVideoInfo {
261 video_id: String::from("9bZkp7q19f0"),
262 title: String::from("PSY - GANGNAM STYLE(강남스타일) M/V"),
263 channel_name: String::from("officialpsy"),
264 duration: String::from("4:12"),
265 views: String::from("51億回"),
266 time_ago: String::from("11年前"),
267 description: String::from("PSY - ‘I LUV IT’ M/V @ https://youtu.be/Xvjnoagk6GU. YouTube史上初めて20億回再生を突破した歴史的音楽MV。"),
268 },
269 YouTubeVideoInfo {
270 video_id: String::from("OPf0YbXqDm0"),
271 title: String::from("Mark Ronson - Uptown Funk (Official Video) ft. Bruno Mars"),
272 channel_name: String::from("MarkRonsonVEVO"),
273 duration: String::from("4:30"),
274 views: String::from("49億回"),
275 time_ago: String::from("8年前"),
276 description: String::from("Official Video for Uptown Funk by Mark Ronson ft. Bruno Mars. 軽快なファンクサウンドの代表的ヒット曲。"),
277 },
278 YouTubeVideoInfo {
279 video_id: String::from("C0DPdy98e4c"),
280 title: String::from("AtmOS ゲームエンジン & 60FPS リアルタイム描画ベンチマーク"),
281 channel_name: String::from("AtmOS Tech Channel"),
282 duration: String::from("5:20"),
283 views: String::from("45万回"),
284 time_ago: String::from("1年前"),
285 description: String::from("ベアメタル ARM64 上で動作する AtmOS の高効率グラフィックスパイプライン、動画デコーダ、自作GUIエンジンの性能検証。ゲーム・アニメーション・生放送。"),
286 },
287 ]
288}
289
290pub fn get_video_info_by_id(video_id: &str) -> YouTubeVideoInfo {
293 for v in get_featured_videos() {
294 if v.video_id == video_id {
295 return v;
296 }
297 }
298 YouTubeVideoInfo {
299 video_id: String::from(video_id),
300 title: format!("YouTube 動画 - {}", video_id),
301 channel_name: String::from("YouTube チャンネル"),
302 duration: String::from("4:30"),
303 views: String::from("10万回"),
304 time_ago: String::from("最近"),
305 description: format!("YouTube 動画ID: {}。AtmOS 自前のベアメタル H.264 デコーダで再生します。", video_id),
306 }
307}
308
309pub fn search_videos(query: &str) -> alloc::vec::Vec<YouTubeVideoInfo> {
312 let q_lower = query.trim().to_ascii_lowercase();
313 let all = get_featured_videos();
314 let mut matches = alloc::vec::Vec::new();
315 for v in all {
316 if v.title.to_ascii_lowercase().contains(&q_lower)
317 || v.channel_name.to_ascii_lowercase().contains(&q_lower)
318 || v.description.to_ascii_lowercase().contains(&q_lower)
319 {
320 matches.push(v);
321 }
322 }
323 if matches.is_empty() {
324 get_featured_videos()
325 } else {
326 matches
327 }
328}
329
330pub fn generate_rich_thumbnail_svg(v: &YouTubeVideoInfo) -> String {
333 let hash = v.video_id.bytes().fold(0u32, |acc, b| acc.wrapping_mul(31).wrapping_add(b as u32));
334 let (c1, c2) = match hash % 6 {
335 0 => ("#1a102f", "#3b1e54"),
336 1 => ("#0b192c", "#1e3e62"),
337 2 => ("#2c001e", "#5c0032"),
338 3 => ("#0d2b2c", "#164e63"),
339 4 => ("#261c14", "#4a3525"),
340 _ => ("#1f1f1f", "#2d2d2d"),
341 };
342 let title_short: String = v.title.chars().take(22).collect();
343 let esc_title = title_short.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """);
344 let esc_id = &v.video_id;
345
346 format!(
347 r##"<svg viewBox="0 0 320 180" preserveAspectRatio="xMidYMid slice" class="rich-thumb-svg" style="width:100%;height:100%;display:block;border-radius:12px;">
348 <defs>
349 <linearGradient id="grad_{id}" x1="0%" y1="0%" x2="100%" y2="100%">
350 <stop offset="0%" stop-color="{c1}"/>
351 <stop offset="100%" stop-color="{c2}"/>
352 </linearGradient>
353 </defs>
354 <rect width="320" height="180" fill="url(#grad_{id})"/>
355 <circle cx="160" cy="90" r="28" fill="rgba(0,0,0,0.65)"/>
356 <path d="M153 78 L173 90 L153 102 Z" fill="#FF0000"/>
357 <rect x="12" y="12" width="54" height="20" rx="4" fill="rgba(255,0,0,0.85)"/>
358 <text x="39" y="26" fill="#FFFFFF" font-family="Arial, sans-serif" font-size="11" font-weight="bold" text-anchor="middle">YouTube</text>
359 <text x="160" y="145" fill="#EEEEEE" font-family="Arial, sans-serif" font-size="12" font-weight="500" text-anchor="middle">{title}</text>
360</svg>"##,
361 id = esc_id,
362 c1 = c1,
363 c2 = c2,
364 title = esc_title,
365 )
366}
367
368pub fn yt_svg_logo() -> &'static str {
375 r##"<svg viewBox="0 0 90 20" preserveAspectRatio="xMidYMid meet" style="width:90px;height:20px;display:block;"><g><path d="M27.97 3.12C27.65 1.93 26.71 1 25.52.68 23.36.1 14.3.1 14.3.1s-9.06 0-11.22.58C1.89 1 .95 1.93.63 3.12 0 5.28 0 10 0 10s0 4.72.63 6.88c.32 1.19 1.26 2.12 2.45 2.44C5.24 20 14.3 20 14.3 20s9.06 0 11.22-.68c1.19-.32 2.13-1.25 2.45-2.44.63-2.16.63-6.88.63-6.88s0-4.72-.63-6.88z" fill="#FF0000"></path><polygon points="11.45,14.28 18.89,10 11.45,5.72" fill="#FFFFFF"></polygon></g><text x="32" y="16" fill="#FFFFFF" font-family="Roboto, Arial, sans-serif" font-size="16" font-weight="bold" letter-spacing="-0.5px">YouTube</text><text x="82" y="9" fill="#AAAAAA" font-family="Roboto, Arial, sans-serif" font-size="8">JP</text></svg>"##
376}
377
378pub fn yt_svg_guide() -> &'static str {
381 r#"<svg viewBox="0 0 24 24" style="width:24px;height:24px;fill:currentColor;"><path d="M21 6H3V5h18v1zm0 5H3v1h18v-1zm0 6H3v1h18v-1z"></path></svg>"#
382}
383
384pub fn yt_svg_search() -> &'static str {
387 r#"<svg viewBox="0 0 24 24" style="width:20px;height:20px;fill:currentColor;"><path d="M20.87 20.13l-5.46-5.46a7.5 7.5 0 1 0-.74.74l5.46 5.46a.52.52 0 0 0 .74 0 .52.52 0 0 0 0-.74zM4.1 10.5a6.4 6.4 0 1 1 6.4 6.4 6.41 6.41 0 0 1-6.4-6.4z"></path></svg>"#
388}
389
390pub fn yt_svg_mic() -> &'static str {
393 r#"<svg viewBox="0 0 24 24" style="width:20px;height:20px;fill:currentColor;"><path d="M12 14c1.66 0 3-1.34 3-3V5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3zm5-3c0 2.76-2.24 5-5 5s-5-2.24-5-5H5c0 3.53 2.61 6.43 6 6.92V21h2v-3.08c3.39-.49 6-3.39 6-6.92h-2z"></path></svg>"#
394}
395
396pub fn yt_svg_create() -> &'static str {
399 r#"<svg viewBox="0 0 24 24" style="width:22px;height:22px;fill:currentColor;"><path d="M14 13h-3v3H9v-3H6v-2h3V8h2v3h3v2zm3-7H3v12h14v-4.5l4 4v-11l-4 4V6zm1 11.5l-3.5-3.5V17H4V7h10.5v3l3.5-3.5v11z"></path></svg>"#
400}
401
402pub fn yt_svg_bell() -> &'static str {
405 r#"<svg viewBox="0 0 24 24" style="width:22px;height:22px;fill:currentColor;"><path d="M10 20h4c0 1.1-.9 2-2 2s-2-.9-2-2zm10-4v-5c0-3.07-1.63-5.64-4.5-6.32V4c0-.83-.67-1.5-1.5-1.5s-1.5.67-1.5 1.5v.68C9.63 5.36 8 7.92 8 11v5l-2 2v1h16v-1l-2-2z"></path></svg>"#
406}
407
408pub fn yt_svg_signin() -> &'static str {
411 r##"<svg viewBox="0 0 24 24" style="width:22px;height:22px;fill:#3ea6ff;"><path fill="#3ea6ff" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 3c1.66 0 3 1.34 3 3s-1.34 3-3 3-3-1.34-3-3 1.34-3 3-3zm0 14.2c-2.5 0-4.71-1.28-6-3.22.03-1.99 4-3.08 6-3.08 1.99 0 5.97 1.09 6 3.08-1.29 1.94-3.5 3.22-6 3.22z"></path></svg>"##
412}
413
414pub fn yt_svg_home() -> &'static str {
417 r#"<svg viewBox="0 0 24 24" style="width:22px;height:22px;fill:currentColor;"><path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"></path></svg>"#
418}
419
420pub fn yt_svg_shorts() -> &'static str {
423 r#"<svg viewBox="0 0 24 24" style="width:22px;height:22px;fill:currentColor;"><path d="M17.77 10.32l-1.2-.5-1.1-.46 1.05-.62c1.65-.97 2.21-3.08 1.25-4.73-.97-1.65-3.08-2.21-4.73-1.25L5.7 7.15c-1.64.96-2.2 3.06-1.25 4.71.5.86 1.33 1.43 2.26 1.62l1.2.49 1.1.46-1.05.62c-1.65.97-2.21 3.08-1.25 4.73.97 1.65 3.08 2.21 4.73 1.25l7.34-4.39c1.64-.96 2.2-3.06 1.25-4.71-.5-.86-1.33-1.43-2.26-1.62zM10 14.5v-5l4.5 2.5-4.5 2.5z"></path></svg>"#
424}
425
426pub fn yt_svg_subscriptions() -> &'static str {
429 r#"<svg viewBox="0 0 24 24" style="width:22px;height:22px;fill:currentColor;"><path d="M18.7 8.7H5.3V7h13.4v1.7zm1.8-3.4H3.5V3.6h17v1.7zm-3.5 6.8H7V10.4h10v1.7zm5.2 3.4H1.8V20.4h20.4V15.5zm-1.8 3.2H3.6v-1.5h16.8v1.5z"></path></svg>"#
430}
431
432pub fn yt_svg_library() -> &'static str {
435 r#"<svg viewBox="0 0 24 24" style="width:22px;height:22px;fill:currentColor;"><path d="M4 6H2v14c0 1.1.9 2 2 2h14v-2H4V6zm16-4H8c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm-8 12.5v-9l6 4.5-6 4.5z"></path></svg>"#
436}
437
438pub fn yt_svg_history() -> &'static str {
441 r#"<svg viewBox="0 0 24 24" style="width:22px;height:22px;fill:currentColor;"><path d="M13 3c-4.97 0-9 4.03-9 9H1l3.89 3.89.07.14L9 12H6c0-3.87 3.13-7 7-7s7 3.13 7 7-3.13 7-7 7c-1.93 0-3.68-.79-4.94-2.06l-1.42 1.42C8.27 19.99 10.51 21 13 21c4.97 0 9-4.03 9-9s-4.03-9-9-9zm-1 5v5l4.28 2.54.72-1.21-3.5-2.08V8H12z"></path></svg>"#
442}
443
444pub fn yt_svg_like() -> &'static str {
447 r#"<svg viewBox="0 0 24 24" style="width:18px;height:18px;fill:currentColor;"><path d="M1 21h4V9H1v12zm22-11c0-1.1-.9-2-2-2h-6.31l.95-4.57.03-.32c0-.41-.17-.79-.44-1.06L14.17 1 7.59 7.59C7.22 7.95 7 8.45 7 9v10c0 1.1.9 2 2 2h9c.83 0 1.54-.5 1.84-1.22l3.02-7.05c.09-.23.14-.47.14-.73v-2z"></path></svg>"#
448}
449
450pub fn yt_svg_dislike() -> &'static str {
453 r#"<svg viewBox="0 0 24 24" style="width:18px;height:18px;fill:currentColor;"><path d="M15 3H6c-.83 0-1.54.5-1.84 1.22l-3.02 7.05c-.09.23-.14.47-.14.73v2c0 1.1.9 2 2 2h6.31l-.95 4.57-.03.32c0 .41.17.79.44 1.06L9.83 23l6.59-6.59c.36-.36.58-.86.58-1.41V5c0-1.1-.9-2-2-2zm4 0v12h4V3h-4z"></path></svg>"#
454}
455
456pub fn yt_svg_share() -> &'static str {
459 r#"<svg viewBox="0 0 24 24" style="width:18px;height:18px;fill:currentColor;"><path d="M14 9V5l7 7-7 7v-4.1c-5 0-8.5 1.6-11 5.1 1-5 4-10 11-11z"></path></svg>"#
460}
461
462pub fn yt_svg_download() -> &'static str {
465 r#"<svg viewBox="0 0 24 24" style="width:18px;height:18px;fill:currentColor;"><path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"></path></svg>"#
466}
467
468pub fn yt_svg_clip() -> &'static str {
471 r#"<svg viewBox="0 0 24 24" style="width:18px;height:18px;fill:currentColor;"><path d="M10 6.34L8.74 5.08 7.33 6.49l1.26 1.26L10 6.34zm6.41.73l-1.26-1.26-1.41 1.41 1.26 1.26 1.41-1.41zM2.41 2.41L1 3.82l6.21 6.21c-.74.6-1.21 1.51-1.21 2.53 0 1.84 1.5 3.34 3.34 3.34.87 0 1.66-.34 2.26-.89l3.58 3.58c-.55.6-.89 1.39-.89 2.26 0 1.84 1.5 3.34 3.34 3.34s3.34-1.5 3.34-3.34c0-1.84-1.5-3.34-3.34-3.34-.87 0-1.66.34-2.26.89L11.82 14.6l-1.6-1.6 3.63-3.63 7.73 7.73 1.41-1.41L2.41 2.41z"></path></svg>"#
472}
473
474pub fn yt_svg_save() -> &'static str {
477 r#"<svg viewBox="0 0 24 24" style="width:18px;height:18px;fill:currentColor;"><path d="M14 10H2v2h12v-2zm0-4H2v2h12V6zm4 8v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zM2 16h8v-2H2v2z"></path></svg>"#
478}
479
480pub fn generate_masthead_html(search_query: &str) -> String {
483 format!(
484 r#"<div id="masthead-container">
485 <ytd-masthead id="masthead">
486 <div id="start">
487 <button id="guide-button" aria-label="ガイド">
488 {guide_icon}
489 </button>
490 <a id="logo" href="https://www.youtube.com/" title="YouTube ホーム" style="text-decoration:none;border:none;outline:none;">
491 {logo_svg}
492 </a>
493 </div>
494 <div id="center">
495 <form id="search-form" action="https://www.youtube.com/results" method="get">
496 <div id="container" class="ytd-searchbox">
497 <input id="search-input" type="text" name="search_query" placeholder="検索" value="{q}" autocomplete="off">
498 </div>
499 <button id="search-icon-legacy" type="submit" aria-label="検索">
500 {search_icon}
501 </button>
502 </form>
503 <button id="voice-search-button" aria-label="音声で検索">
504 {mic_icon}
505 </button>
506 </div>
507 <div id="end">
508 <button class="masthead-btn" aria-label="作成">
509 {create_icon}
510 </button>
511 <button class="masthead-btn" aria-label="通知">
512 {bell_icon}
513 </button>
514 <a class="signin-btn" href="https://accounts.google.com/" style="text-decoration:none;">
515 {signin_icon}
516 <span>ログイン</span>
517 </a>
518 </div>
519 </ytd-masthead>
520</div>"#,
521 guide_icon = yt_svg_guide(),
522 logo_svg = yt_svg_logo(),
523 q = search_query,
524 search_icon = yt_svg_search(),
525 mic_icon = yt_svg_mic(),
526 create_icon = yt_svg_create(),
527 bell_icon = yt_svg_bell(),
528 signin_icon = yt_svg_signin(),
529 )
530}
531
532pub fn generate_guide_html(active_item: &str) -> String {
535 let is_home = active_item == "home";
536 let is_shorts = active_item == "shorts";
537 let is_subs = active_item == "subscriptions";
538
539 format!(
540 r#"<div id="guide" class="ytd-guide-renderer">
541 <a href="https://www.youtube.com/" class="guide-entry{home_act}">
542 {home_icon}
543 <span>ホーム</span>
544 </a>
545 <a href="https://www.youtube.com/shorts" class="guide-entry{shorts_act}">
546 {shorts_icon}
547 <span>Shorts</span>
548 </a>
549 <a href="https://www.youtube.com/feed/subscriptions" class="guide-entry{subs_act}">
550 {subs_icon}
551 <span>登録チャンネル</span>
552 </a>
553 <div class="guide-section-divider"></div>
554 <div class="guide-section-title">ライブラリ</div>
555 <a href="https://www.youtube.com/feed/library" class="guide-entry">
556 {lib_icon}
557 <span>ライブラリ</span>
558 </a>
559 <a href="https://www.youtube.com/feed/history" class="guide-entry">
560 {hist_icon}
561 <span>履歴</span>
562 </a>
563</div>"#,
564 home_act = if is_home { " active" } else { "" },
565 shorts_act = if is_shorts { " active" } else { "" },
566 subs_act = if is_subs { " active" } else { "" },
567 home_icon = yt_svg_home(),
568 shorts_icon = yt_svg_shorts(),
569 subs_icon = yt_svg_subscriptions(),
570 lib_icon = yt_svg_library(),
571 hist_icon = yt_svg_history(),
572 )
573}
574
575pub fn generate_youtube_watch_html(video_id: &str, title: &str, desc: &str) -> String {
578 generate_youtube_watch_html_with_stream(video_id, title, desc, None)
579}
580
581pub fn generate_youtube_watch_html_with_stream(
584 video_id: &str,
585 title: &str,
586 desc: &str,
587 stream_url_opt: Option<&str>,
588) -> String {
589 let thumb = get_youtube_thumbnail_url(video_id);
590 let stream_url = stream_url_opt
591 .map(String::from)
592 .unwrap_or_else(|| get_youtube_stream_url(video_id));
593 let info = get_video_info_by_id(video_id);
594 let final_title = if !title.is_empty() && title != "YouTube Video" {
595 title
596 } else {
597 &info.title
598 };
599 let final_desc = if !desc.is_empty() { desc } else { &info.description };
600 let masthead_html = generate_masthead_html("");
601
602 let mut related_html = String::new();
603 let mut videos_js_obj = String::from("{\n");
604 for rel in get_featured_videos() {
605 let svg_bg = generate_rich_thumbnail_svg(&rel);
606 let st_url = get_youtube_stream_url(&rel.video_id);
607 let escaped_title = rel.title.replace('"', "\\\"").replace('\n', " ");
608 let escaped_channel = rel.channel_name.replace('"', "\\\"").replace('\n', " ");
609 let escaped_views = rel.views.replace('"', "\\\"").replace('\n', " ");
610 let escaped_time = rel.time_ago.replace('"', "\\\"").replace('\n', " ");
611 let escaped_desc = rel.description.replace('"', "\\\"").replace('\n', " ");
612 let rel_initial = rel.channel_name.chars().next().unwrap_or('Y');
613 videos_js_obj.push_str(&format!(
614 r#" "{id}": {{ id: "{id}", title: "{title}", channel: "{channel}", initial: "{initial}", views: "{views}", time: "{time}", desc: "{desc}", streamUrl: "{stream_url}" }},
615"#,
616 id = rel.video_id,
617 title = escaped_title,
618 channel = escaped_channel,
619 initial = rel_initial,
620 views = escaped_views,
621 time = escaped_time,
622 desc = escaped_desc,
623 stream_url = st_url,
624 ));
625
626 if rel.video_id != video_id {
627 related_html.push_str(&format!(
628 r#"<ytd-compact-video-renderer class="compact-video-item">
629 <a href="https://www.youtube.com/watch?v={id}" class="compact-media-link" onclick="playRelatedVideo('{id}'); return false;">
630 <div class="compact-thumb-wrap">
631 {svg_bg}
632 <img src="https://img.youtube.com/vi/{id}/hqdefault.jpg" class="compact-thumb" style="position:absolute;top:0;left:0;width:100%;height:100%;object-fit:cover;border-radius:8px;" onerror="this.style.display='none'" alt="Thumb">
633 <span class="duration-badge">{duration}</span>
634 </div>
635 <div class="compact-details">
636 <div class="compact-title">{title}</div>
637 <div class="compact-channel">{channel}</div>
638 <div class="compact-meta">{views} • {time}</div>
639 </div>
640 </a>
641</ytd-compact-video-renderer>"#,
642 id = rel.video_id,
643 duration = rel.duration,
644 title = rel.title,
645 channel = rel.channel_name,
646 views = rel.views,
647 time = rel.time_ago,
648 svg_bg = svg_bg,
649 ));
650 }
651 }
652 videos_js_obj.push('}');
653
654 let ch_initial = info.channel_name.chars().next().unwrap_or('Y');
655
656 format!(
657 r##"<!DOCTYPE html>
658<html>
659<head>
660<meta charset="utf-8">
661<title>{title} - YouTube</title>
662<style>
663* {{ box-sizing: border-box; }}
664body {{
665 background-color: #0f0f0f;
666 color: #f1f1f1;
667 font-family: Roboto, Arial, sans-serif;
668 margin: 0;
669 padding: 0;
670 overflow-x: hidden;
671}}
672ytd-app {{
673 display: flex;
674 flex-direction: column;
675 min-height: 100vh;
676 background-color: #0f0f0f;
677}}
678#masthead-container {{
679 position: sticky;
680 top: 0;
681 z-index: 200;
682 background-color: #0f0f0f;
683 border-bottom: 1px solid #222222;
684}}
685ytd-masthead#masthead {{
686 display: flex;
687 align-items: center;
688 justify-content: space-between;
689 height: 56px;
690 padding: 0 16px;
691}}
692#start {{
693 display: flex;
694 align-items: center;
695 gap: 16px;
696}}
697#guide-button {{
698 background: none;
699 border: none;
700 color: #f1f1f1;
701 cursor: pointer;
702 padding: 8px;
703 display: flex;
704 align-items: center;
705}}
706#logo {{
707 display: flex;
708 align-items: center;
709 text-decoration: none;
710 border: none;
711 outline: none;
712}}
713#logo:hover, #logo:focus, #logo:visited {{
714 text-decoration: none;
715 border: none;
716}}
717#center {{
718 display: flex;
719 align-items: center;
720 flex: 0 1 732px;
721 justify-content: center;
722 margin: 0 20px;
723}}
724#search-form {{
725 display: flex;
726 width: 100%;
727 max-width: 600px;
728}}
729#container.ytd-searchbox {{
730 display: flex;
731 flex: 1;
732 border: 1px solid #303030;
733 border-radius: 20px 0 0 20px;
734 background-color: #121212;
735 overflow: hidden;
736}}
737#search-input {{
738 width: 100%;
739 background: transparent;
740 border: none;
741 padding: 0 16px;
742 height: 38px;
743 color: #ffffff;
744 font-size: 16px;
745 outline: none;
746}}
747#search-icon-legacy {{
748 width: 64px;
749 height: 40px;
750 background-color: #222222;
751 border: 1px solid #303030;
752 border-left: none;
753 border-radius: 0 20px 20px 0;
754 color: #ffffff;
755 cursor: pointer;
756 display: flex;
757 align-items: center;
758 justify-content: center;
759}}
760#voice-search-button {{
761 width: 40px;
762 height: 40px;
763 border-radius: 50%;
764 background-color: #222222;
765 border: none;
766 color: #ffffff;
767 margin-left: 12px;
768 cursor: pointer;
769 display: flex;
770 align-items: center;
771 justify-content: center;
772}}
773#end {{
774 display: flex;
775 align-items: center;
776 gap: 12px;
777}}
778.masthead-btn {{
779 background: none;
780 border: none;
781 color: #f1f1f1;
782 cursor: pointer;
783 padding: 8px;
784 display: flex;
785 align-items: center;
786 border-radius: 50%;
787}}
788.signin-btn {{
789 display: inline-flex;
790 align-items: center;
791 justify-content: center;
792 gap: 6px;
793 background-color: transparent;
794 color: #3ea6ff;
795 border: 1px solid rgba(255,255,255,0.2);
796 border-radius: 18px;
797 padding: 6px 14px;
798 font-size: 14px;
799 font-weight: 500;
800 line-height: 20px;
801 cursor: pointer;
802 text-decoration: none;
803}}
804.signin-btn span {{
805 line-height: 20px;
806 vertical-align: middle;
807}}
808#columns.ytd-watch-flexy {{
809 display: flex;
810 max-width: 1700px;
811 margin: 0 auto;
812 padding: 24px 24px 48px;
813 gap: 24px;
814}}
815#primary.ytd-watch-flexy {{
816 flex: 1;
817 min-width: 0;
818}}
819#secondary.ytd-watch-flexy {{
820 width: 400px;
821 flex-shrink: 0;
822}}
823#player.ytd-watch-flexy {{
824 background-color: #000000;
825 border-radius: 12px;
826 overflow: hidden;
827 margin-bottom: 16px;
828 position: relative;
829 aspect-ratio: 16 / 9;
830}}
831.video-stream {{
832 width: 100%;
833 height: 100%;
834 display: block;
835 object-fit: contain;
836}}
837.watch-title {{
838 font-size: 20px;
839 font-weight: 700;
840 line-height: 1.4;
841 color: #f1f1f1;
842 margin: 0 0 12px;
843}}
844#owner {{
845 display: flex;
846 align-items: center;
847 gap: 12px;
848 min-width: 0;
849}}
850.owner-avatar {{
851 width: 40px;
852 height: 40px;
853 border-radius: 50%;
854 background-color: #ff0000;
855 color: #ffffff;
856 font-weight: bold;
857 text-align: center;
858 line-height: 40px;
859 font-size: 16px;
860 flex-shrink: 0;
861}}
862.owner-info {{
863 display: flex;
864 flex-direction: column;
865 white-space: nowrap;
866 min-width: 0;
867 overflow: hidden;
868}}
869#channel-name {{
870 font-size: 16px;
871 font-weight: 500;
872 color: #f1f1f1;
873 line-height: 1.3;
874 white-space: nowrap;
875 overflow: hidden;
876 text-overflow: ellipsis;
877}}
878#owner-sub-count {{
879 font-size: 12px;
880 color: #aaaaaa;
881 white-space: nowrap;
882}}
883#subscribe-button {{
884 background-color: #f1f1f1;
885 color: #0f0f0f;
886 border: none;
887 padding: 0 16px;
888 height: 36px;
889 border-radius: 18px;
890 font-weight: 500;
891 font-size: 14px;
892 cursor: pointer;
893 margin-left: 16px;
894 flex-shrink: 0;
895 white-space: nowrap;
896}}
897.action-row {{
898 display: flex;
899 align-items: center;
900 justify-content: space-between;
901 margin-bottom: 16px;
902 gap: 12px;
903}}
904.action-buttons {{
905 display: flex;
906 align-items: center;
907 gap: 8px;
908 flex-shrink: 0;
909}}
910.yt-button-group {{
911 display: flex;
912 align-items: center;
913 background-color: #272727;
914 border-radius: 18px;
915 overflow: hidden;
916}}
917.yt-pill-btn {{
918 display: flex;
919 align-items: center;
920 gap: 6px;
921 background-color: #272727;
922 border: none;
923 color: #f1f1f1;
924 height: 36px;
925 padding: 0 14px;
926 border-radius: 18px;
927 font-size: 14px;
928 font-weight: 500;
929 cursor: pointer;
930}}
931.yt-pill-btn:hover {{
932 background-color: #383838;
933}}
934.yt-button-group .yt-pill-btn {{
935 border-radius: 0;
936 background: transparent;
937}}
938.yt-button-group .yt-pill-btn:first-child {{
939 border-right: 1px solid #383838;
940}}
941#description.ytd-watch-metadata {{
942 background-color: #272727;
943 border-radius: 12px;
944 padding: 12px 16px;
945 font-size: 14px;
946 line-height: 1.5;
947 color: #f1f1f1;
948 margin-bottom: 24px;
949 cursor: pointer;
950 transition: background-color 0.2s;
951}}
952#description.ytd-watch-metadata:hover {{
953 background-color: #383838;
954}}
955.desc-meta-header {{
956 font-size: 14px;
957 font-weight: 500;
958 color: #f1f1f1;
959 margin-bottom: 8px;
960}}
961.desc-text {{
962 color: #e0e0e0;
963 white-space: pre-wrap;
964}}
965.desc-text.collapsed {{
966 max-height: 64px;
967 overflow: hidden;
968 display: -webkit-box;
969 -webkit-line-clamp: 3;
970 -webkit-box-orient: vertical;
971}}
972.desc-toggle-btn {{
973 margin-top: 8px;
974 font-size: 13px;
975 font-weight: 500;
976 color: #aaaaaa;
977 cursor: pointer;
978 user-select: none;
979}}
980.desc-toggle-btn:hover {{
981 color: #f1f1f1;
982}}
983#comments.ytd-comments {{
984 margin-top: 24px;
985}}
986.comments-header {{
987 font-size: 18px;
988 font-weight: 700;
989 margin-bottom: 16px;
990}}
991.comment-item {{
992 display: flex;
993 gap: 16px;
994 margin-bottom: 20px;
995}}
996.comment-avatar {{
997 width: 40px;
998 height: 40px;
999 border-radius: 50%;
1000 background-color: #333333;
1001 color: #ffffff;
1002 display: flex;
1003 align-items: center;
1004 justify-content: center;
1005 font-weight: 500;
1006}}
1007.comment-content {{
1008 flex: 1;
1009}}
1010.comment-author {{
1011 font-size: 13px;
1012 font-weight: 500;
1013 color: #f1f1f1;
1014 margin-bottom: 4px;
1015}}
1016.comment-author span {{
1017 font-size: 12px;
1018 color: #aaaaaa;
1019 font-weight: normal;
1020 margin-left: 4px;
1021}}
1022.comment-body-text {{
1023 font-size: 14px;
1024 color: #f1f1f1;
1025 line-height: 1.4;
1026}}
1027.compact-video-item {{
1028 display: block;
1029 margin-bottom: 12px;
1030}}
1031.compact-media-link {{
1032 display: flex;
1033 gap: 8px;
1034 text-decoration: none;
1035 color: inherit;
1036}}
1037.compact-thumb-wrap {{
1038 position: relative;
1039 width: 168px;
1040 aspect-ratio: 16 / 9;
1041 flex-shrink: 0;
1042 border-radius: 8px;
1043 overflow: hidden;
1044 background-color: #222222;
1045}}
1046.compact-thumb {{
1047 width: 100%;
1048 height: 100%;
1049 object-fit: cover;
1050}}
1051.compact-details {{
1052 flex: 1;
1053 min-width: 0;
1054}}
1055.compact-title {{
1056 font-size: 14px;
1057 font-weight: 500;
1058 line-height: 1.3;
1059 color: #f1f1f1;
1060 margin-bottom: 4px;
1061 overflow: hidden;
1062 display: -webkit-box;
1063 -webkit-line-clamp: 2;
1064 -webkit-box-orient: vertical;
1065}}
1066.compact-channel {{
1067 font-size: 12px;
1068 color: #aaaaaa;
1069 margin-bottom: 2px;
1070}}
1071.compact-meta {{
1072 font-size: 12px;
1073 color: #aaaaaa;
1074}}
1075.duration-badge {{
1076 position: absolute;
1077 bottom: 4px;
1078 right: 4px;
1079 background-color: rgba(0, 0, 0, 0.8);
1080 color: #ffffff;
1081 font-size: 12px;
1082 font-weight: 500;
1083 padding: 1px 4px;
1084 border-radius: 4px;
1085}}
1086</style>
1087</head>
1088<body>
1089<ytd-app>
1090 {masthead}
1091 <div id="columns" class="ytd-watch-flexy">
1092 <div id="primary" class="ytd-watch-flexy">
1093 <div id="player" class="ytd-watch-flexy">
1094 <video id="main-video-player" src="{stream_url}" poster="{thumb}" class="video-stream" controls autoplay playsinline>
1095 <track kind="subtitles" srclang="ja" label="日本語" default>
1096 <track kind="subtitles" srclang="en" label="English">
1097 </video>
1098 </div>
1099 <div id="below">
1100 <h1 class="watch-title" id="video-title">{final_title}</h1>
1101 <div class="action-row">
1102 <div id="owner">
1103 <div class="owner-avatar" id="owner-avatar">{ch_initial}</div>
1104 <div class="owner-info">
1105 <span id="channel-name">{channel_name}</span>
1106 <span id="owner-sub-count">登録者数 12.8万人</span>
1107 </div>
1108 <button id="subscribe-button" onclick="toggleSub(this)">チャンネル登録</button>
1109 </div>
1110 <div class="action-buttons">
1111 <div class="yt-button-group">
1112 <button class="yt-pill-btn" onclick="toggleLike(this)">
1113 {like_svg}
1114 <span>1.2万</span>
1115 </button>
1116 <button class="yt-pill-btn" aria-label="低評価">
1117 {dislike_svg}
1118 </button>
1119 </div>
1120 <button class="yt-pill-btn">
1121 {share_svg}
1122 <span>共有</span>
1123 </button>
1124 <button class="yt-pill-btn">
1125 {download_svg}
1126 <span>オフライン</span>
1127 </button>
1128 <button class="yt-pill-btn">
1129 {clip_svg}
1130 <span>クリップ</span>
1131 </button>
1132 <button class="yt-pill-btn">
1133 {save_svg}
1134 <span>保存</span>
1135 </button>
1136 </div>
1137 </div>
1138
1139 <div id="description" class="ytd-watch-metadata" onclick="toggleDescription()">
1140 <div class="desc-meta-header" id="desc-meta">{views} • {time}</div>
1141 <div id="desc-text" class="desc-text collapsed">{final_desc}</div>
1142 <div id="desc-toggle" class="desc-toggle-btn">もっと見る</div>
1143 </div>
1144
1145 <div id="comments" class="ytd-comments">
1146 <div class="comments-header">コメント 428件</div>
1147 <div class="comment-item">
1148 <div class="comment-avatar">Y</div>
1149 <div class="comment-content">
1150 <div class="comment-author">YouTube Viewer<span>3日前</span></div>
1151 <div class="comment-body-text">高画質・高音質でのスムーズな再生をありがとうございます!</div>
1152 </div>
1153 </div>
1154 <div class="comment-item">
1155 <div class="comment-avatar">R</div>
1156 <div class="comment-content">
1157 <div class="comment-author">Rust & Systems Engineer<span>1週間前</span></div>
1158 <div class="comment-body-text">Polymerカスタム要素とShadow DOMが完璧に連携していて素晴らしいです。</div>
1159 </div>
1160 </div>
1161 </div>
1162 </div>
1163 </div>
1164
1165 <div id="secondary" class="ytd-watch-flexy">
1166 <div id="related" class="ytd-watch-next-feed">
1167 {related_html}
1168 </div>
1169 </div>
1170 </div>
1171</ytd-app>
1172<script>
1173var ytInitialPlayerResponse = {{
1174 videoDetails: {{
1175 videoId: "{video_id}",
1176 title: "{final_title}",
1177 author: "{channel_name}",
1178 lengthSeconds: "270",
1179 viewCount: "{views}"
1180 }}
1181}};
1182var ytInitialData = {{
1183 currentVideoEndpoint: {{
1184 watchEndpoint: {{
1185 videoId: "{video_id}"
1186 }}
1187 }}
1188}};
1189var ALL_VIDEOS = {videos_js};
1190function playRelatedVideo(id) {{
1191 var vData = ALL_VIDEOS[id];
1192 if (!vData) return;
1193 try {{
1194 history.pushState({{ videoId: id }}, '', '/watch?v=' + id);
1195 }} catch(e) {{}}
1196 document.title = vData.title + ' - YouTube';
1197 var titleEl = document.getElementById('video-title');
1198 if (titleEl) titleEl.innerText = vData.title;
1199 var chEl = document.getElementById('channel-name');
1200 if (chEl) chEl.innerText = vData.channel;
1201 var avEl = document.getElementById('owner-avatar');
1202 if (avEl) avEl.innerText = vData.initial;
1203 var metaEl = document.getElementById('desc-meta');
1204 if (metaEl) metaEl.innerText = vData.views + ' • ' + vData.time;
1205 var descEl = document.getElementById('desc-text');
1206 if (descEl) descEl.innerText = vData.desc;
1207 var player = document.getElementById('main-video-player');
1208 if (player) {{
1209 player.src = vData.streamUrl;
1210 player.poster = 'https://img.youtube.com/vi/' + id + '/hqdefault.jpg';
1211 if (typeof player.load === 'function') player.load();
1212 if (typeof player.play === 'function') player.play();
1213 }}
1214}}
1215window.addEventListener('popstate', function(e) {{
1216 if (e && e.state && e.state.videoId) {{
1217 playRelatedVideo(e.state.videoId);
1218 }}
1219}});
1220var v = document.getElementById('main-video-player');
1221function toggleSub(btn) {{
1222 if (btn.innerText === 'チャンネル登録') {{
1223 btn.innerText = '登録済み ✓';
1224 btn.style.backgroundColor = '#272727';
1225 btn.style.color = '#f1f1f1';
1226 }} else {{
1227 btn.innerText = 'チャンネル登録';
1228 btn.style.backgroundColor = '#f1f1f1';
1229 btn.style.color = '#0f0f0f';
1230 }}
1231}}
1232function toggleDescription() {{
1233 var d = document.getElementById('desc-text');
1234 var b = document.getElementById('desc-toggle');
1235 if (!d) return;
1236 if (d.classList.contains('collapsed')) {{
1237 d.classList.remove('collapsed');
1238 if (b) b.innerText = '一部を表示';
1239 }} else {{
1240 d.classList.add('collapsed');
1241 if (b) b.innerText = 'もっと見る';
1242 }}
1243}}
1244function toggleLike(btn) {{
1245 if (btn.classList.contains('active')) {{
1246 btn.classList.remove('active');
1247 btn.style.color = '#f1f1f1';
1248 }} else {{
1249 btn.classList.add('active');
1250 btn.style.color = '#3ea6ff';
1251 }}
1252}}
1253window.addEventListener('keydown', function(e) {{
1254 if (!v) return;
1255 if (e.key === ' ' || e.key === 'k') {{
1256 if (v.paused) {{ v.play(); }} else {{ v.pause(); }}
1257 }} else if (e.key === 'm') {{
1258 v.muted = !v.muted;
1259 }} else if (e.key === 'ArrowLeft' || e.key === 'j') {{
1260 v.currentTime = Math.max(0, v.currentTime - 5);
1261 }} else if (e.key === 'ArrowRight' || e.key === 'l') {{
1262 v.currentTime = Math.min(v.duration, v.currentTime + 5);
1263 }}
1264}});
1265</script>
1266</body>
1267</html>"##,
1268 title = final_title,
1269 masthead = masthead_html,
1270 final_title = final_title,
1271 ch_initial = ch_initial,
1272 channel_name = info.channel_name,
1273 views = info.views,
1274 time = info.time_ago,
1275 video_id = video_id,
1276 thumb = thumb,
1277 stream_url = stream_url,
1278 final_desc = final_desc,
1279 related_html = related_html,
1280 videos_js = videos_js_obj,
1281 like_svg = yt_svg_like(),
1282 dislike_svg = yt_svg_dislike(),
1283 share_svg = yt_svg_share(),
1284 download_svg = yt_svg_download(),
1285 clip_svg = yt_svg_clip(),
1286 save_svg = yt_svg_save(),
1287 )
1288}
1289
1290pub fn generate_youtube_search_results_html(query: &str) -> String {
1293 let videos = search_videos(query);
1294 let mut list_html = String::new();
1295
1296 for v in &videos {
1297 let ch_initial = v.channel_name.chars().next().unwrap_or('Y');
1298 let svg_bg = generate_rich_thumbnail_svg(v);
1299 list_html.push_str(&format!(
1300 r#"<ytd-video-renderer class="search-video-item">
1301 <a href="https://www.youtube.com/watch?v={id}" class="search-video-link">
1302 <div class="search-thumb-wrap">
1303 {svg_bg}
1304 <img src="https://img.youtube.com/vi/{id}/hqdefault.jpg" class="search-thumb" style="position:absolute;top:0;left:0;width:100%;height:100%;object-fit:cover;border-radius:12px;" onerror="this.style.display='none'" alt="Thumb">
1305 <span class="duration-badge">{duration}</span>
1306 </div>
1307 <div class="search-details">
1308 <h3 class="search-video-title">{title}</h3>
1309 <div class="search-meta">{views} • {time}</div>
1310 <div class="search-channel-info">
1311 <div class="search-channel-avatar"><span>{ch_initial}</span></div>
1312 <span class="search-channel-name">{channel}</span>
1313 </div>
1314 <div class="search-desc">{desc}</div>
1315 </div>
1316 </a>
1317</ytd-video-renderer>"#,
1318 id = v.video_id,
1319 duration = v.duration,
1320 title = v.title,
1321 views = v.views,
1322 time = v.time_ago,
1323 ch_initial = ch_initial,
1324 channel = v.channel_name,
1325 desc = v.description,
1326 svg_bg = svg_bg,
1327 ));
1328 }
1329
1330 let masthead = generate_masthead_html(query);
1331 let guide = generate_guide_html("search");
1332
1333 format!(
1334 r##"<!DOCTYPE html>
1335<html>
1336<head>
1337<meta charset="utf-8">
1338<title>{query} - YouTube</title>
1339<style>
1340* {{ box-sizing: border-box; }}
1341body {{
1342 background-color: #0f0f0f;
1343 color: #f1f1f1;
1344 font-family: Roboto, Arial, sans-serif;
1345 margin: 0;
1346 padding: 0;
1347 overflow-x: hidden;
1348}}
1349ytd-app {{
1350 display: flex;
1351 flex-direction: column;
1352 min-height: 100vh;
1353 background-color: #0f0f0f;
1354}}
1355#masthead-container {{
1356 position: sticky;
1357 top: 0;
1358 z-index: 200;
1359 background-color: #0f0f0f;
1360 border-bottom: 1px solid #222222;
1361}}
1362ytd-masthead#masthead {{
1363 display: flex;
1364 align-items: center;
1365 justify-content: space-between;
1366 height: 56px;
1367 padding: 0 16px;
1368}}
1369#start {{
1370 display: flex;
1371 align-items: center;
1372 gap: 16px;
1373}}
1374#guide-button {{
1375 background: none;
1376 border: none;
1377 color: #f1f1f1;
1378 cursor: pointer;
1379 padding: 8px;
1380 display: flex;
1381 align-items: center;
1382}}
1383#logo {{
1384 display: flex;
1385 align-items: center;
1386 text-decoration: none;
1387 border: none;
1388 outline: none;
1389}}
1390#logo:hover, #logo:focus, #logo:visited {{
1391 text-decoration: none;
1392 border: none;
1393}}
1394#center {{
1395 display: flex;
1396 align-items: center;
1397 flex: 0 1 732px;
1398 justify-content: center;
1399 margin: 0 20px;
1400}}
1401#search-form {{
1402 display: flex;
1403 width: 100%;
1404 max-width: 600px;
1405}}
1406#container.ytd-searchbox {{
1407 display: flex;
1408 flex: 1;
1409 border: 1px solid #303030;
1410 border-radius: 20px 0 0 20px;
1411 background-color: #121212;
1412 overflow: hidden;
1413}}
1414#search-input {{
1415 width: 100%;
1416 background: transparent;
1417 border: none;
1418 padding: 0 16px;
1419 height: 38px;
1420 color: #ffffff;
1421 font-size: 16px;
1422 outline: none;
1423}}
1424#search-icon-legacy {{
1425 width: 64px;
1426 height: 40px;
1427 background-color: #222222;
1428 border: 1px solid #303030;
1429 border-left: none;
1430 border-radius: 0 20px 20px 0;
1431 color: #ffffff;
1432 cursor: pointer;
1433 display: flex;
1434 align-items: center;
1435 justify-content: center;
1436}}
1437#voice-search-button {{
1438 width: 40px;
1439 height: 40px;
1440 border-radius: 50%;
1441 background-color: #222222;
1442 border: none;
1443 color: #ffffff;
1444 margin-left: 12px;
1445 cursor: pointer;
1446 display: flex;
1447 align-items: center;
1448 justify-content: center;
1449}}
1450#end {{
1451 display: flex;
1452 align-items: center;
1453 gap: 12px;
1454}}
1455.masthead-btn {{
1456 background: none;
1457 border: none;
1458 color: #f1f1f1;
1459 cursor: pointer;
1460 padding: 8px;
1461 display: flex;
1462 align-items: center;
1463 border-radius: 50%;
1464}}
1465.signin-btn {{
1466 display: inline-flex;
1467 align-items: center;
1468 justify-content: center;
1469 gap: 6px;
1470 background-color: transparent;
1471 color: #3ea6ff;
1472 border: 1px solid rgba(255,255,255,0.2);
1473 border-radius: 18px;
1474 padding: 6px 14px;
1475 font-size: 14px;
1476 font-weight: 500;
1477 line-height: 20px;
1478 cursor: pointer;
1479 text-decoration: none;
1480}}
1481.signin-btn span {{
1482 line-height: 20px;
1483 vertical-align: middle;
1484}}
1485#page-manager {{
1486 display: flex;
1487 flex: 1;
1488}}
1489#guide {{
1490 width: 240px;
1491 flex-shrink: 0;
1492 padding: 12px;
1493 border-right: 1px solid #1a1a1a;
1494 background-color: #0f0f0f;
1495}}
1496.guide-entry {{
1497 display: flex;
1498 align-items: center;
1499 gap: 24px;
1500 padding: 10px 12px;
1501 border-radius: 10px;
1502 text-decoration: none;
1503 color: #f1f1f1;
1504 font-size: 14px;
1505 margin-bottom: 2px;
1506}}
1507.guide-entry.active {{
1508 background-color: #272727;
1509 font-weight: 500;
1510}}
1511.guide-entry:hover {{
1512 background-color: #222222;
1513}}
1514.guide-section-divider {{
1515 height: 1px;
1516 background-color: #222222;
1517 margin: 12px 0;
1518}}
1519.guide-section-title {{
1520 padding: 6px 12px;
1521 font-size: 14px;
1522 font-weight: 500;
1523 color: #aaaaaa;
1524}}
1525#primary.ytd-search-results {{
1526 flex: 1;
1527 min-width: 0;
1528 padding: 16px 24px 48px;
1529 max-width: 1100px;
1530}}
1531.search-header-meta {{
1532 font-size: 14px;
1533 color: #aaaaaa;
1534 margin-bottom: 16px;
1535 padding-bottom: 8px;
1536 border-bottom: 1px solid #222222;
1537}}
1538.search-video-item {{
1539 display: block;
1540 margin-bottom: 16px;
1541}}
1542.search-video-link {{
1543 display: flex;
1544 gap: 16px;
1545 text-decoration: none;
1546 color: inherit;
1547}}
1548.search-thumb-wrap {{
1549 position: relative;
1550 width: 360px;
1551 aspect-ratio: 16 / 9;
1552 flex-shrink: 0;
1553 border-radius: 12px;
1554 overflow: hidden;
1555 background-color: #222222;
1556}}
1557.search-thumb {{
1558 width: 100%;
1559 height: 100%;
1560 object-fit: cover;
1561}}
1562.duration-badge {{
1563 position: absolute;
1564 bottom: 6px;
1565 right: 6px;
1566 background-color: rgba(0, 0, 0, 0.8);
1567 color: #ffffff;
1568 font-size: 12px;
1569 font-weight: 500;
1570 padding: 2px 4px;
1571 border-radius: 4px;
1572}}
1573.search-details {{
1574 flex: 1;
1575 min-width: 0;
1576}}
1577.search-video-title {{
1578 font-size: 18px;
1579 font-weight: 500;
1580 line-height: 1.3;
1581 color: #f1f1f1;
1582 margin: 0 0 6px;
1583}}
1584.search-meta {{
1585 font-size: 12px;
1586 color: #aaaaaa;
1587 margin-bottom: 8px;
1588}}
1589.search-channel-info {{
1590 display: flex;
1591 align-items: center;
1592 gap: 8px;
1593 margin-bottom: 10px;
1594}}
1595.search-channel-avatar {{
1596 width: 24px;
1597 height: 24px;
1598 border-radius: 50%;
1599 background-color: #ff0000;
1600 color: #ffffff;
1601 font-size: 11px;
1602 font-weight: bold;
1603 display: flex;
1604 align-items: center;
1605 justify-content: center;
1606}}
1607.search-channel-name {{
1608 font-size: 13px;
1609 color: #aaaaaa;
1610}}
1611.search-desc {{
1612 font-size: 13px;
1613 color: #aaaaaa;
1614 line-height: 1.4;
1615 overflow: hidden;
1616 display: -webkit-box;
1617 -webkit-line-clamp: 2;
1618 -webkit-box-orient: vertical;
1619}}
1620</style>
1621</head>
1622<body>
1623<ytd-app>
1624 {masthead}
1625 <div id="page-manager" class="ytd-page-manager">
1626 {guide}
1627 <div id="primary" class="ytd-search-results">
1628 <ytd-two-column-search-results-renderer>
1629 <div id="contents" class="ytd-section-list-renderer">
1630 <div class="search-header-meta">フィルター: 約 12,400 件</div>
1631 {list_html}
1632 </div>
1633 </ytd-two-column-search-results-renderer>
1634 </div>
1635 </div>
1636</ytd-app>
1637<script>
1638var ytInitialData = {{
1639 contents: {{
1640 twoColumnSearchResultsRenderer: {{
1641 primaryContents: {{
1642 sectionListRenderer: {{
1643 contents: []
1644 }}
1645 }}
1646 }}
1647 }}
1648}};
1649</script>
1650</body>
1651</html>"##,
1652 query = query,
1653 masthead = masthead,
1654 guide = guide,
1655 list_html = list_html,
1656 )
1657}
1658
1659pub fn generate_youtube_home_html() -> String {
1662 let videos = get_featured_videos();
1663 let mut grid_html = String::new();
1664
1665 for v in &videos {
1666 let ch_initial = v.channel_name.chars().next().unwrap_or('Y');
1667 let svg_bg = generate_rich_thumbnail_svg(v);
1668 grid_html.push_str(&format!(
1669 r#"<ytd-rich-item-renderer class="rich-item" data-title="{title}" data-desc="{desc}">
1670 <a href="https://www.youtube.com/watch?v={id}" class="rich-media-link">
1671 <div class="rich-thumb-wrap">
1672 {svg_bg}
1673 <img src="https://img.youtube.com/vi/{id}/hqdefault.jpg" class="rich-thumb" style="position:absolute;top:0;left:0;width:100%;height:100%;object-fit:cover;border-radius:12px;" onerror="this.style.display='none'" alt="Thumbnail">
1674 <span class="duration-badge">{duration}</span>
1675 </div>
1676 <div class="rich-details">
1677 <div class="channel-avatar"><span>{ch_initial}</span></div>
1678 <div class="rich-meta-col">
1679 <h3 id="video-title" class="video-title-text">{title}</h3>
1680 <div class="channel-name-text">{channel}</div>
1681 <div class="meta-line-text">{views} • {time}</div>
1682 </div>
1683 </div>
1684 </a>
1685</ytd-rich-item-renderer>"#,
1686 id = v.video_id,
1687 duration = v.duration,
1688 title = v.title,
1689 desc = v.description,
1690 ch_initial = ch_initial,
1691 channel = v.channel_name,
1692 views = v.views,
1693 time = v.time_ago,
1694 svg_bg = svg_bg,
1695 ));
1696 }
1697
1698 let masthead = generate_masthead_html("");
1699 let guide = generate_guide_html("home");
1700
1701 format!(
1702 r##"<!DOCTYPE html>
1703<html>
1704<head>
1705<meta charset="utf-8">
1706<title>YouTube</title>
1707<style>
1708* {{ box-sizing: border-box; }}
1709body {{
1710 background-color: #0f0f0f;
1711 color: #f1f1f1;
1712 font-family: Roboto, Arial, sans-serif;
1713 margin: 0;
1714 padding: 0;
1715 overflow-x: hidden;
1716}}
1717ytd-app {{
1718 display: flex;
1719 flex-direction: column;
1720 min-height: 100vh;
1721 background-color: #0f0f0f;
1722}}
1723#masthead-container {{
1724 position: sticky;
1725 top: 0;
1726 z-index: 200;
1727 background-color: #0f0f0f;
1728 border-bottom: 1px solid #222222;
1729}}
1730ytd-masthead#masthead {{
1731 display: flex;
1732 align-items: center;
1733 justify-content: space-between;
1734 height: 56px;
1735 padding: 0 16px;
1736}}
1737#start {{
1738 display: flex;
1739 align-items: center;
1740 gap: 16px;
1741}}
1742#guide-button {{
1743 background: none;
1744 border: none;
1745 color: #f1f1f1;
1746 cursor: pointer;
1747 padding: 8px;
1748 display: flex;
1749 align-items: center;
1750}}
1751#logo {{
1752 display: flex;
1753 align-items: center;
1754 text-decoration: none;
1755 border: none;
1756 outline: none;
1757}}
1758#logo:hover, #logo:focus, #logo:visited {{
1759 text-decoration: none;
1760 border: none;
1761}}
1762#center {{
1763 display: flex;
1764 align-items: center;
1765 flex: 0 1 732px;
1766 justify-content: center;
1767 margin: 0 20px;
1768}}
1769#search-form {{
1770 display: flex;
1771 width: 100%;
1772 max-width: 600px;
1773}}
1774#container.ytd-searchbox {{
1775 display: flex;
1776 flex: 1;
1777 border: 1px solid #303030;
1778 border-radius: 20px 0 0 20px;
1779 background-color: #121212;
1780 overflow: hidden;
1781}}
1782#search-input {{
1783 width: 100%;
1784 background: transparent;
1785 border: none;
1786 padding: 0 16px;
1787 height: 38px;
1788 color: #ffffff;
1789 font-size: 16px;
1790 outline: none;
1791}}
1792#search-icon-legacy {{
1793 width: 64px;
1794 height: 40px;
1795 background-color: #222222;
1796 border: 1px solid #303030;
1797 border-left: none;
1798 border-radius: 0 20px 20px 0;
1799 color: #ffffff;
1800 cursor: pointer;
1801 display: flex;
1802 align-items: center;
1803 justify-content: center;
1804}}
1805#voice-search-button {{
1806 width: 40px;
1807 height: 40px;
1808 border-radius: 50%;
1809 background-color: #222222;
1810 border: none;
1811 color: #ffffff;
1812 margin-left: 12px;
1813 cursor: pointer;
1814 display: flex;
1815 align-items: center;
1816 justify-content: center;
1817}}
1818#end {{
1819 display: flex;
1820 align-items: center;
1821 gap: 12px;
1822}}
1823.masthead-btn {{
1824 background: none;
1825 border: none;
1826 color: #f1f1f1;
1827 cursor: pointer;
1828 padding: 8px;
1829 display: flex;
1830 align-items: center;
1831 border-radius: 50%;
1832}}
1833.signin-btn {{
1834 display: inline-flex;
1835 align-items: center;
1836 justify-content: center;
1837 gap: 6px;
1838 background-color: transparent;
1839 color: #3ea6ff;
1840 border: 1px solid rgba(255,255,255,0.2);
1841 border-radius: 18px;
1842 padding: 6px 14px;
1843 font-size: 14px;
1844 font-weight: 500;
1845 line-height: 20px;
1846 cursor: pointer;
1847 text-decoration: none;
1848}}
1849.signin-btn span {{
1850 line-height: 20px;
1851 vertical-align: middle;
1852}}
1853#page-manager {{
1854 display: flex;
1855 flex: 1;
1856}}
1857#guide {{
1858 width: 240px;
1859 flex-shrink: 0;
1860 padding: 12px;
1861 border-right: 1px solid #1a1a1a;
1862 background-color: #0f0f0f;
1863}}
1864.guide-entry {{
1865 display: flex;
1866 align-items: center;
1867 gap: 24px;
1868 padding: 10px 12px;
1869 border-radius: 10px;
1870 text-decoration: none;
1871 color: #f1f1f1;
1872 font-size: 14px;
1873 margin-bottom: 2px;
1874}}
1875.guide-entry.active {{
1876 background-color: #272727;
1877 font-weight: 500;
1878}}
1879.guide-entry:hover {{
1880 background-color: #222222;
1881}}
1882.guide-section-divider {{
1883 height: 1px;
1884 background-color: #222222;
1885 margin: 12px 0;
1886}}
1887.guide-section-title {{
1888 padding: 6px 12px;
1889 font-size: 14px;
1890 font-weight: 500;
1891 color: #aaaaaa;
1892}}
1893#primary {{
1894 flex: 1;
1895 min-width: 0;
1896 padding: 0 24px 32px;
1897}}
1898#chips {{
1899 display: flex;
1900 gap: 12px;
1901 padding: 12px 0;
1902 overflow-x: auto;
1903 position: sticky;
1904 top: 56px;
1905 background-color: #0f0f0f;
1906 z-index: 10;
1907}}
1908.chip {{
1909 background-color: #272727;
1910 color: #f1f1f1;
1911 padding: 6px 12px;
1912 border-radius: 8px;
1913 font-size: 14px;
1914 text-decoration: none;
1915 white-space: nowrap;
1916 font-weight: 500;
1917}}
1918.chip.active {{
1919 background-color: #f1f1f1;
1920 color: #0f0f0f;
1921}}
1922#contents.ytd-rich-grid-renderer {{
1923 display: grid;
1924 grid-template-columns: repeat(auto-fill, minmax(310px, 1fr));
1925 gap: 24px 16px;
1926 margin-top: 16px;
1927}}
1928ytd-rich-item-renderer {{
1929 display: flex;
1930 flex-direction: column;
1931}}
1932.rich-media-link {{
1933 text-decoration: none;
1934 color: inherit;
1935 display: flex;
1936 flex-direction: column;
1937}}
1938.rich-thumb-wrap {{
1939 position: relative;
1940 width: 100%;
1941 aspect-ratio: 16 / 9;
1942 border-radius: 12px;
1943 overflow: hidden;
1944 background-color: #222222;
1945}}
1946.rich-thumb {{
1947 width: 100%;
1948 height: 100%;
1949 object-fit: cover;
1950}}
1951.duration-badge {{
1952 position: absolute;
1953 bottom: 8px;
1954 right: 8px;
1955 background-color: rgba(0, 0, 0, 0.8);
1956 color: #ffffff;
1957 font-size: 12px;
1958 font-weight: 500;
1959 padding: 2px 4px;
1960 border-radius: 4px;
1961}}
1962.rich-details {{
1963 display: flex;
1964 gap: 12px;
1965 margin-top: 12px;
1966}}
1967.channel-avatar {{
1968 width: 36px;
1969 height: 36px;
1970 border-radius: 50%;
1971 background-color: #ff0000;
1972 color: #ffffff;
1973 display: flex;
1974 align-items: center;
1975 justify-content: center;
1976 font-size: 14px;
1977 font-weight: bold;
1978 flex-shrink: 0;
1979}}
1980.rich-meta-col {{
1981 flex: 1;
1982 min-width: 0;
1983}}
1984.video-title-text {{
1985 font-size: 16px;
1986 font-weight: 500;
1987 line-height: 1.4;
1988 color: #f1f1f1;
1989 margin: 0 0 4px;
1990 overflow: hidden;
1991 display: -webkit-box;
1992 -webkit-line-clamp: 2;
1993 -webkit-box-orient: vertical;
1994}}
1995.channel-name-text {{
1996 font-size: 14px;
1997 color: #aaaaaa;
1998 margin-bottom: 2px;
1999}}
2000.meta-line-text {{
2001 font-size: 14px;
2002 color: #aaaaaa;
2003}}
2004</style>
2005</head>
2006<body>
2007<ytd-app>
2008 {masthead}
2009 <div id="page-manager" class="ytd-page-manager">
2010 {guide}
2011 <div id="primary">
2012 <div id="chips" class="ytd-feed-filter-chip-bar-renderer">
2013 <a href="#" class="chip active" onclick="filterChip('all', this); return false;">すべて</a>
2014 <a href="#" class="chip" onclick="filterChip('音楽', this); return false;">音楽</a>
2015 <a href="#" class="chip" onclick="filterChip('ゲーム', this); return false;">ゲーム</a>
2016 <a href="#" class="chip" onclick="filterChip('プログラミング', this); return false;">プログラミング</a>
2017 <a href="#" class="chip" onclick="filterChip('アニメ', this); return false;">アニメーション</a>
2018 <a href="#" class="chip" onclick="filterChip('Blender', this); return false;">Blender</a>
2019 <a href="#" class="chip" onclick="filterChip('ベンチマーク', this); return false;">ベンチマーク</a>
2020 </div>
2021 <div id="contents" class="ytd-rich-grid-renderer">
2022 {grid_html}
2023 </div>
2024 </div>
2025 </div>
2026</ytd-app>
2027<script>
2028function filterChip(cat, el) {{
2029 var chips = document.querySelectorAll('.chip');
2030 for (var i = 0; i < chips.length; i++) {{
2031 chips[i].classList.remove('active');
2032 }}
2033 el.classList.add('active');
2034 var items = document.querySelectorAll('ytd-rich-item-renderer');
2035 for (var i = 0; i < items.length; i++) {{
2036 var t = (items[i].getAttribute('data-title') || '') + ' ' + (items[i].getAttribute('data-desc') || '');
2037 if (cat === 'all' || t.indexOf(cat) !== -1) {{
2038 items[i].style.display = '';
2039 }} else {{
2040 items[i].style.display = 'none';
2041 }}
2042 }}
2043}}
2044var ytInitialData = {{
2045 header: {{
2046 masthead: {{
2047 logo: "YouTube"
2048 }}
2049 }},
2050 contents: {{
2051 twoColumnBrowseResultsRenderer: {{
2052 tabs: [{{
2053 tabRenderer: {{
2054 content: {{
2055 richGridRenderer: {{
2056 contents: []
2057 }}
2058 }}
2059 }}
2060 }}]
2061 }}
2062 }}
2063}};
2064</script>
2065</body>
2066</html>"##,
2067 masthead = masthead,
2068 guide = guide,
2069 grid_html = grid_html,
2070 )
2071}
2072
2073pub fn generate_youtube_embed_html(video_id: &str, title: &str) -> String {
2076 generate_youtube_embed_html_with_stream(video_id, title, None)
2077}
2078
2079pub fn generate_youtube_embed_html_with_stream(
2082 video_id: &str,
2083 title: &str,
2084 stream_url_opt: Option<&str>,
2085) -> String {
2086 let thumb = get_youtube_thumbnail_url(video_id);
2087 let stream_url = stream_url_opt
2088 .map(String::from)
2089 .unwrap_or_else(|| get_youtube_stream_url(video_id));
2090 let info = get_video_info_by_id(video_id);
2091 let final_title = if !title.is_empty() && title != "YouTube Video" {
2092 title
2093 } else {
2094 &info.title
2095 };
2096
2097 format!(
2098 r##"<!DOCTYPE html>
2099<html>
2100<head>
2101<meta charset="utf-8">
2102<title>{title} - YouTube</title>
2103<style>
2104* {{ box-sizing: border-box; }}
2105body {{
2106 background-color: #000000;
2107 color: #ffffff;
2108 font-family: Roboto, Arial, sans-serif;
2109 margin: 0;
2110 padding: 0;
2111 overflow: hidden;
2112}}
2113.embed-container {{
2114 position: relative;
2115 width: 100vw;
2116 height: 100vh;
2117 display: flex;
2118 align-items: center;
2119 justify-content: center;
2120 background-color: #000000;
2121}}
2122.video-player {{
2123 width: 100%;
2124 height: 100%;
2125 object-fit: contain;
2126}}
2127.embed-header {{
2128 position: absolute;
2129 top: 0;
2130 left: 0;
2131 right: 0;
2132 padding: 16px 20px;
2133 background: linear-gradient(to bottom, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0) 100%);
2134 display: flex;
2135 align-items: center;
2136 justify-content: space-between;
2137 z-index: 10;
2138}}
2139.embed-title {{
2140 font-size: 16px;
2141 font-weight: 500;
2142 white-space: nowrap;
2143 overflow: hidden;
2144 text-overflow: ellipsis;
2145 color: #ffffff;
2146 text-shadow: 0 1px 2px rgba(0,0,0,0.8);
2147}}
2148.embed-logo {{
2149 text-decoration: none;
2150}}
2151.embed-controls {{
2152 position: absolute;
2153 bottom: 0;
2154 left: 0;
2155 right: 0;
2156 padding: 12px 20px;
2157 background: linear-gradient(to top, rgba(0,0,0,0.85) 0%, rgba(0,0,0,0) 100%);
2158 display: flex;
2159 align-items: center;
2160 gap: 16px;
2161 z-index: 10;
2162}}
2163.ctrl-btn {{
2164 background: none;
2165 border: none;
2166 color: #ffffff;
2167 font-size: 16px;
2168 cursor: pointer;
2169}}
2170.seek-bar {{
2171 flex: 1;
2172 height: 4px;
2173 background-color: rgba(255,255,255,0.3);
2174 border-radius: 2px;
2175 position: relative;
2176}}
2177.seek-progress {{
2178 width: 35%;
2179 height: 100%;
2180 background-color: #ff0000;
2181 border-radius: 2px;
2182}}
2183.time-display {{
2184 font-size: 12px;
2185 color: #dddddd;
2186}}
2187</style>
2188</head>
2189<body>
2190<div class="embed-container">
2191 <div class="embed-header">
2192 <div class="embed-title">{title}</div>
2193 <a href="https://www.youtube.com/watch?v={video_id}" class="embed-logo" title="YouTube で視聴">
2194 {logo_svg}
2195 </a>
2196 </div>
2197 <video id="embed-video-player" src="{stream_url}" poster="{thumb}" class="video-player" controls autoplay>
2198 <track kind="subtitles" srclang="ja" label="日本語" default>
2199 <track kind="subtitles" srclang="en" label="English">
2200 </video>
2201 <div class="embed-controls">
2202 <button class="ctrl-btn" onclick="toggleEmbedPlay(this)">▶</button>
2203 <div class="seek-bar"><div class="seek-progress"></div></div>
2204 <div class="time-display">0:00 / {duration}</div>
2205 <button class="ctrl-btn" onclick="toggleEmbedMute(this)">🔊</button>
2206 <button class="ctrl-btn">⛶</button>
2207 </div>
2208</div>
2209<script>
2210var ev = document.getElementById('embed-video-player');
2211function toggleEmbedPlay(btn) {{
2212 if (!ev) return;
2213 if (ev.paused) {{
2214 ev.play();
2215 btn.innerText = '‖';
2216 }} else {{
2217 ev.pause();
2218 btn.innerText = '▶';
2219 }}
2220}}
2221function toggleEmbedMute(btn) {{
2222 if (!ev) return;
2223 ev.muted = !ev.muted;
2224 btn.innerText = ev.muted ? '🔇' : '🔊';
2225}}
2226window.addEventListener('keydown', function(e) {{
2227 if (!ev) return;
2228 if (e.key === ' ' || e.key === 'k') {{
2229 if (ev.paused) {{ ev.play(); }} else {{ ev.pause(); }}
2230 }} else if (e.key === 'm') {{
2231 ev.muted = !ev.muted;
2232 }}
2233}});
2234</script>
2235</body>
2236</html>"##,
2237 title = final_title,
2238 video_id = video_id,
2239 logo_svg = yt_svg_logo(),
2240 stream_url = stream_url,
2241 thumb = thumb,
2242 duration = info.duration,
2243 )
2244}
2245
2246pub fn generate_youtube_shorts_html(video_id: &str, title: &str, channel: &str) -> String {
2249 generate_youtube_shorts_html_with_stream(video_id, title, channel, None)
2250}
2251
2252pub fn generate_youtube_shorts_html_with_stream(
2255 video_id: &str,
2256 title: &str,
2257 channel: &str,
2258 stream_url_opt: Option<&str>,
2259) -> String {
2260 let thumb = get_youtube_thumbnail_url(video_id);
2261 let stream_url = stream_url_opt
2262 .map(String::from)
2263 .unwrap_or_else(|| get_youtube_stream_url(video_id));
2264 let info = get_video_info_by_id(video_id);
2265 let final_title = if !title.is_empty() { title } else { &info.title };
2266 let final_channel = if !channel.is_empty() { channel } else { &info.channel_name };
2267 let ch_initial = final_channel.chars().next().unwrap_or('Y');
2268 let masthead = generate_masthead_html("");
2269
2270 format!(
2271 r##"<!DOCTYPE html>
2272<html>
2273<head>
2274<meta charset="utf-8">
2275<title>{title} - YouTube Shorts</title>
2276<style>
2277* {{ box-sizing: border-box; }}
2278body {{
2279 background-color: #0f0f0f;
2280 color: #f1f1f1;
2281 font-family: Roboto, Arial, sans-serif;
2282 margin: 0;
2283 padding: 0;
2284 overflow-x: hidden;
2285}}
2286ytd-app {{
2287 display: flex;
2288 flex-direction: column;
2289 min-height: 100vh;
2290 background-color: #0f0f0f;
2291}}
2292#masthead-container {{
2293 position: sticky;
2294 top: 0;
2295 z-index: 200;
2296 background-color: #0f0f0f;
2297 border-bottom: 1px solid #222222;
2298}}
2299ytd-masthead#masthead {{
2300 display: flex;
2301 align-items: center;
2302 justify-content: space-between;
2303 height: 56px;
2304 padding: 0 16px;
2305}}
2306#start {{
2307 display: flex;
2308 align-items: center;
2309 gap: 16px;
2310}}
2311#guide-button {{
2312 background: none;
2313 border: none;
2314 color: #f1f1f1;
2315 cursor: pointer;
2316 padding: 8px;
2317 display: flex;
2318 align-items: center;
2319}}
2320#logo {{
2321 display: flex;
2322 align-items: center;
2323 text-decoration: none;
2324 border: none;
2325 outline: none;
2326}}
2327#logo:hover, #logo:focus, #logo:visited {{
2328 text-decoration: none;
2329 border: none;
2330}}
2331#center {{
2332 display: flex;
2333 align-items: center;
2334 flex: 0 1 732px;
2335 justify-content: center;
2336 margin: 0 20px;
2337}}
2338#search-form {{
2339 display: flex;
2340 width: 100%;
2341 max-width: 600px;
2342}}
2343#container.ytd-searchbox {{
2344 display: flex;
2345 flex: 1;
2346 border: 1px solid #303030;
2347 border-radius: 20px 0 0 20px;
2348 background-color: #121212;
2349 overflow: hidden;
2350}}
2351#search-input {{
2352 width: 100%;
2353 background: transparent;
2354 border: none;
2355 padding: 0 16px;
2356 height: 38px;
2357 color: #ffffff;
2358 font-size: 16px;
2359 outline: none;
2360}}
2361#search-icon-legacy {{
2362 width: 64px;
2363 height: 40px;
2364 background-color: #222222;
2365 border: 1px solid #303030;
2366 border-left: none;
2367 border-radius: 0 20px 20px 0;
2368 color: #ffffff;
2369 cursor: pointer;
2370 display: flex;
2371 align-items: center;
2372 justify-content: center;
2373}}
2374#voice-search-button {{
2375 width: 40px;
2376 height: 40px;
2377 border-radius: 50%;
2378 background-color: #222222;
2379 border: none;
2380 color: #ffffff;
2381 margin-left: 12px;
2382 cursor: pointer;
2383 display: flex;
2384 align-items: center;
2385 justify-content: center;
2386}}
2387#end {{
2388 display: flex;
2389 align-items: center;
2390 gap: 12px;
2391}}
2392.masthead-btn {{
2393 background: none;
2394 border: none;
2395 color: #f1f1f1;
2396 cursor: pointer;
2397 padding: 8px;
2398 display: flex;
2399 align-items: center;
2400 border-radius: 50%;
2401}}
2402.signin-btn {{
2403 display: inline-flex;
2404 align-items: center;
2405 justify-content: center;
2406 gap: 6px;
2407 background-color: transparent;
2408 color: #3ea6ff;
2409 border: 1px solid rgba(255,255,255,0.2);
2410 border-radius: 18px;
2411 padding: 6px 14px;
2412 font-size: 14px;
2413 font-weight: 500;
2414 line-height: 20px;
2415 cursor: pointer;
2416 text-decoration: none;
2417}}
2418.signin-btn span {{
2419 line-height: 20px;
2420 vertical-align: middle;
2421}}
2422.shorts-container {{
2423 max-width: 600px;
2424 margin: 20px auto;
2425 display: flex;
2426 justify-content: center;
2427 align-items: flex-end;
2428 gap: 16px;
2429}}
2430.shorts-player-card {{
2431 position: relative;
2432 width: 380px;
2433 height: 640px;
2434 background-color: #000000;
2435 border-radius: 16px;
2436 overflow: hidden;
2437 box-shadow: 0 4px 20px rgba(0,0,0,0.5);
2438 border: 1px solid #282828;
2439}}
2440.shorts-video {{
2441 width: 100%;
2442 height: 100%;
2443 object-fit: cover;
2444}}
2445.shorts-overlay-bottom {{
2446 position: absolute;
2447 bottom: 0;
2448 left: 0;
2449 right: 0;
2450 padding: 20px 16px;
2451 background: linear-gradient(to top, rgba(0,0,0,0.85) 0%, rgba(0,0,0,0) 100%);
2452 color: #ffffff;
2453}}
2454.shorts-channel {{
2455 display: flex;
2456 align-items: center;
2457 gap: 10px;
2458 margin-bottom: 10px;
2459}}
2460.shorts-avatar {{
2461 width: 36px;
2462 height: 36px;
2463 border-radius: 50%;
2464 background-color: #ff0000;
2465 text-align: center;
2466 line-height: 36px;
2467 font-weight: bold;
2468 font-size: 14px;
2469 color: #ffffff;
2470}}
2471.shorts-channel-name {{
2472 font-size: 14px;
2473 font-weight: 500;
2474}}
2475.shorts-sub-btn {{
2476 background-color: #ffffff;
2477 color: #0f0f0f;
2478 border: none;
2479 padding: 6px 14px;
2480 border-radius: 14px;
2481 font-size: 12px;
2482 font-weight: 500;
2483 cursor: pointer;
2484}}
2485.shorts-title {{
2486 font-size: 14px;
2487 line-height: 1.4;
2488 margin-bottom: 8px;
2489}}
2490.shorts-sound-track {{
2491 font-size: 12px;
2492 color: #aaaaaa;
2493 display: flex;
2494 align-items: center;
2495 gap: 6px;
2496}}
2497.shorts-actions {{
2498 display: flex;
2499 flex-direction: column;
2500 gap: 16px;
2501 padding-bottom: 16px;
2502}}
2503.action-btn {{
2504 background-color: #262626;
2505 border-radius: 50%;
2506 width: 48px;
2507 height: 48px;
2508 display: flex;
2509 align-items: center;
2510 justify-content: center;
2511 color: #ffffff;
2512 font-size: 18px;
2513 cursor: pointer;
2514 border: 1px solid #383838;
2515}}
2516.action-label {{
2517 font-size: 11px;
2518 color: #aaaaaa;
2519 text-align: center;
2520 margin-top: 4px;
2521}}
2522</style>
2523</head>
2524<body>
2525<ytd-app>
2526 {masthead}
2527 <div class="shorts-container">
2528 <div class="shorts-player-card">
2529 <video id="shorts-video-player" src="{stream_url}" poster="{thumb}" class="shorts-video" controls autoplay loop>
2530 <track kind="subtitles" srclang="ja" label="日本語" default>
2531 <track kind="subtitles" srclang="en" label="English">
2532 </video>
2533 <div class="shorts-overlay-bottom">
2534 <div class="shorts-channel">
2535 <div class="shorts-avatar">{ch_initial}</div>
2536 <div class="shorts-channel-name">{channel}</div>
2537 <button class="shorts-sub-btn">チャンネル登録</button>
2538 </div>
2539 <div class="shorts-title">{title} #Shorts</div>
2540 <div class="shorts-sound-track">🎵 オリジナル楽曲 - {channel}</div>
2541 </div>
2542 </div>
2543
2544 <div class="shorts-actions">
2545 <div>
2546 <div class="action-btn">👍</div>
2547 <div class="action-label">24万</div>
2548 </div>
2549 <div>
2550 <div class="action-btn">👎</div>
2551 <div class="action-label">低評価</div>
2552 </div>
2553 <div>
2554 <div class="action-btn">💬</div>
2555 <div class="action-label">1.2千</div>
2556 </div>
2557 <div>
2558 <div class="action-btn">↗</div>
2559 <div class="action-label">共有</div>
2560 </div>
2561 <div>
2562 <div class="action-btn">🎵</div>
2563 <div class="action-label">リミックス</div>
2564 </div>
2565 </div>
2566 </div>
2567</ytd-app>
2568</body>
2569</html>"##,
2570 title = final_title,
2571 masthead = masthead,
2572 ch_initial = ch_initial,
2573 channel = final_channel,
2574 stream_url = stream_url,
2575 thumb = thumb,
2576 )
2577}
2578
2579#[derive(Debug, Clone, PartialEq, Eq)]
2581pub struct YouTubeStreamData {
2582 pub video_id: String,
2583 pub title: String,
2584 pub author: String,
2585 pub length_seconds: u64,
2586 pub video_url: Option<String>,
2587 pub audio_url: Option<String>,
2588 pub thumbnail_url: Option<String>,
2589}
2590
2591pub fn unescape_json_string(s: &str) -> String {
2594 let mut res = String::with_capacity(s.len());
2595 let mut chars = s.chars().peekable();
2596 while let Some(c) = chars.next() {
2597 if c == '\\' {
2598 if let Some(next) = chars.next() {
2599 match next {
2600 '/' => res.push('/'),
2601 '\\' => res.push('\\'),
2602 '"' => res.push('"'),
2603 'n' => res.push('\n'),
2604 'r' => res.push('\r'),
2605 't' => res.push('\t'),
2606 'u' => {
2607 let mut hex = String::with_capacity(4);
2608 for _ in 0..4 {
2609 if let Some(h) = chars.next() {
2610 hex.push(h);
2611 }
2612 }
2613 if let Ok(code) = u32::from_str_radix(&hex, 16) {
2614 if let Some(ch) = core::char::from_u32(code) {
2615 res.push(ch);
2616 continue;
2617 }
2618 }
2619 res.push_str("\\u");
2620 res.push_str(&hex);
2621 }
2622 other => {
2623 res.push('\\');
2624 res.push(other);
2625 }
2626 }
2627 } else {
2628 res.push('\\');
2629 }
2630 } else {
2631 res.push(c);
2632 }
2633 }
2634 res
2635}
2636
2637pub fn extract_json_string_value(json: &str, key: &str) -> Option<String> {
2640 let key_pat = format!("\"{}\"", key);
2641 let mut cur = 0;
2642 while let Some(k_idx) = json.get(cur..).unwrap_or("").find(&key_pat) {
2643 let after_key = json.get(cur + k_idx + key_pat.len()..).unwrap_or("");
2644 let trimmed = after_key.trim_start();
2645 if let Some(rest) = trimmed.strip_prefix(':') {
2646 let val_part = rest.trim_start();
2647 if let Some(val_content) = val_part.strip_prefix('"') {
2648 let mut end = 0;
2649 let mut escaped = false;
2650 for (i, c) in val_content.char_indices() {
2651 if escaped {
2652 escaped = false;
2653 } else if c == '\\' {
2654 escaped = true;
2655 } else if c == '"' {
2656 end = i;
2657 break;
2658 }
2659 }
2660 return Some(unescape_json_string(val_content.get(..end).unwrap_or("")));
2661 }
2662 }
2663 cur += k_idx + key_pat.len();
2664 }
2665 None
2666}
2667
2668pub fn extract_streaming_url(json: &str, mime_prefix: &str) -> Option<String> {
2671 let mut cur = 0;
2672 while let Some(start_brace) = json.get(cur..).unwrap_or("").find('{') {
2673 let abs_start = cur + start_brace;
2674 if let Some(end_brace) = json.get(abs_start..).unwrap_or("").find('}') {
2675 let abs_end = abs_start + end_brace;
2676 let obj = json.get(abs_start..=abs_end).unwrap_or("");
2677 if obj.contains(mime_prefix) {
2678 if let Some(url) = extract_json_string_value(obj, "url") {
2679 return Some(url);
2680 }
2681 }
2682 cur = abs_end + 1;
2683 } else {
2684 break;
2685 }
2686 }
2687 None
2688}