2 * (C) Copyright 2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2009-2011 John J. Foerch
5 * Use, modification, and distribution are subject to the terms specified in the
11 require("content-buffer.js");
14 var youtube_t_regexp = /"t": "([^"]+)"/;
15 var youtube_title_regexp = /<meta name="title" content="([^"]+)">/;
17 function regexp_exec (regexp, string, group) {
18 var res = regexp.exec(string);
24 function youtube_parse_video_info (info) {
25 var sp = info.split("&");
27 for each (var kv in sp) {
28 let [k, v] = kv.split("=");
29 res[k] = decodeURIComponent(v);
31 if (! res.url_encoded_fmt_stream_map) {
32 dumpln(dump_obj(res));
35 var url_encoded_fmt_stream_map =
36 res.url_encoded_fmt_stream_map.split(",");
38 for each (var chunk in url_encoded_fmt_stream_map) {
39 var url = "", itag = "";
41 for each (kv in chunk.split("&")) {
42 let [k, v] = kv.split("=");
43 d[k] = decodeURIComponent(v);
50 function youtube_get_video_info (url, id, t) {
51 for each (el in ["profilepage", "detailpage"]) {
53 "http://www.youtube.com/get_video_info?&video_id="+
54 encodeURIComponent(id)+"&el="+el+"&ps=default&eurl="+
55 encodeURIComponent(url)+"&hl=en_US&t="+encodeURIComponent(t);
56 var res = yield send_http_request({uri: video_info_url});
58 var info = youtube_parse_video_info(res.responseText);
59 yield co_return(info);
64 function youtube_scrape_text (results, frame, url, id, text) {
65 var title = decodeURIComponent(regexp_exec(youtube_title_regexp, text, 1)
66 || "video"+Date.now());
67 var t = regexp_exec(youtube_t_regexp, text, 1);
70 var info = yield youtube_get_video_info(url, id, t);
71 for each (var d in info) {
72 var extension = mime_service.getPrimaryExtension(
73 d.type, regexp_exec(/\/([^;]+)/, d.type, 1));
74 results.push(load_spec({
77 filename_extension: extension,
80 description: d.quality + " " + extension
85 function youtube_scrape_buffer (buffer, results) {
86 var url = buffer.current_uri.spec;
87 var id = regexp_exec(youtube_uri_regexp, url, 1);
90 var text = buffer.document.documentElement.innerHTML;
91 yield youtube_scrape_text(results, buffer.top_frame, url, id, text);
94 define_page_mode("youtube_mode",
95 $display_name = "YouTube",
96 $enable = function (buffer) {
97 media_setup_local_object_classes(buffer);
100 function youtube_scrape_embedded (buffer, results) {
101 const embedded_youtube_regexp = /^http:\/\/[a-zA-Z0-9\-.]+\.youtube\.com\/v\/([^?]*).*$/;
102 for (let frame in frame_iterator(buffer.top_frame, buffer.focused_frame)) {
103 // Look for embedded YouTube videos
104 let obj_els = frame.document.getElementsByTagName("object");
105 for (let i = 0; i < obj_els.length; ++i) {
106 let obj_el = obj_els[i];
107 let param_els = obj_el.getElementsByTagName("param");
109 for (let j = 0; j < param_els.length; ++j) {
110 let param_el = param_els[j];
112 if (param_el.getAttribute("name").toLowerCase() == "movie" &&
113 (match = embedded_youtube_regexp.exec(param_el.getAttribute("value"))) != null) {
116 let lspec = load_spec({uri: "http://youtube.com/watch?v=" + id});
118 yield buffer.window.minibuffer.wait_for(
119 "Requesting " + lspec.uri + "...",
120 send_http_request(lspec));
121 let text = result.responseText;
122 if (text != null && text.length > 0)
123 yield youtube_scrape_text(results, frame, lspec.uri, id, text);
124 } catch (e if (e instanceof abort)) {
125 // Still allow other media scrapers to try even if the user aborted an http request,
126 // but don't continue looking for embedded youtube videos.
129 // Some other error here means there was some problem with the request.
130 // We'll just ignore it.
140 let youtube_uri_regexp = build_url_regex($domain = /(?:[a-z]+\.)?youtube/,
141 $path = /watch\?v=([A-Za-z0-9\-_]+)/);
142 media_scrapers.unshift([/.*/, youtube_scrape_embedded]);
143 media_scrapers.unshift([youtube_uri_regexp, youtube_scrape_buffer]);
144 auto_mode_list.push([youtube_uri_regexp, youtube_mode]);