diff options
-rw-r--r-- | ldrize_cooperation.js | 28 | ||||
-rw-r--r-- | ldrize_cooperation_fetch_flv.js | 287 |
2 files changed, 255 insertions, 60 deletions
diff --git a/ldrize_cooperation.js b/ldrize_cooperation.js index 70d165c..966fde4 100644 --- a/ldrize_cooperation.js +++ b/ldrize_cooperation.js @@ -1,6 +1,6 @@ // Vimperator plugin: 'Cooperation LDRize Mappings'
-// Version: 0.24
-// Last Change: 08-May-2009. Jan 2008
+// Version: 0.25
+// Last Change: 12-Jun-2009. Jan 2008
// License: Creative Commons
// Maintainer: Trapezoid <trapezoid.g@gmail.com> - http://unsigned.g.hatena.ne.jp/Trapezoid
//
@@ -68,7 +68,9 @@ if (liberator.plugins.LDRizeCooperation == undefined) (function(){ var handlerInfo = [
//{
// pattern: "http://www.nicovideo.jp/*",
- // handler: ["c:\\usr\\SmileDownloader\\SmileDownloader.exe",["%URL%"]],
+ // handler: {
+ // download: ["c:\\usr\\SmileDownloader\\SmileDownloader.exe",["%URL%"]]
+ // },
// wait: 5000
//},
//{
@@ -263,7 +265,9 @@ if (liberator.plugins.LDRizeCooperation == undefined) (function(){ }
});
liberator.modules.commands.addUserCommand(["pindownload"],"Download pinned links by any software",
- function(arg){ self.downloadLinksByProgram(self.getPinnedItems());},{});
+ function(arg){ self.downloadLinksByProgram("download",self.getPinnedItems());},{});
+ liberator.modules.commands.addUserCommand(["pindo"],"Do external command, with pinned links",
+ function(arg){ self.downloadLinksByProgram(arg.string, self.getPinnedItems());},{});
liberator.modules.commands.addUserCommand(["toggleldrizecooperation","toggleldrc"],"Toggle LDRize Cooperation",
function(arg){ self.isEnable = !self.isEnable},{});
//Options
@@ -309,20 +313,20 @@ if (liberator.plugins.LDRizeCooperation == undefined) (function(){ return [linkResult,viewResult ? viewResult.textContent : null];
});
},
- downloadLinksByProgram: function(links){
+ downloadLinksByProgram: function(command, links){
var self = this;
var count = 0;
links.forEach(function([url,title]){
for each(let x in self.handlerInfo){
if(x.include.test(url)){
setTimeout(function(){
- if(typeof x.handler == "object"){
- let args = x.handler[1].map(function(s) s.replace(/%URL%/g,url).replace(/%TITLE%/g,title));
- liberator.modules.io.run(x.handler[0],args,false);
- }else if(typeof x.handler == "string"){
- liberator.modules.io.run(x.handler,[url],false);
- }else if(typeof x.handler == "function"){
- x.handler(url.toString(),title);
+ if(typeof x.handler[command] == "object"){
+ let args = x.handler[command][1].map(function(s) s.replace(/%URL%/g,url).replace(/%TITLE%/g,title));
+ liberator.modules.io.run(x.handler[command][0],args,false);
+ }else if(typeof x.handler[command] == "string"){
+ liberator.modules.io.run(x.handler[command],[url],false);
+ }else if(typeof x.handler[command] == "function"){
+ x.handler[command](url.toString(),title);
}
},x.wait != undefined ? x.wait * count++ : 0);
return;
diff --git a/ldrize_cooperation_fetch_flv.js b/ldrize_cooperation_fetch_flv.js index fa6ee39..6f1d2f9 100644 --- a/ldrize_cooperation_fetch_flv.js +++ b/ldrize_cooperation_fetch_flv.js @@ -1,61 +1,233 @@ -// Vimperator plugin: 'Cooperation LDRize Mappings - Niconico Flv Fetchearg || liberator.buffer.URLr'
-// Version: 0.4
-// Last Change: 22-Nov-2008. Jan 2008
-// License: Creative Commons
-// Maintainer: Trapezoid <trapezoid.g@gmail.com> - http://unsigned.g.hatena.ne.jp/Trapezoid
-//
-// Cooperation LDRize Mappings - Niconico Flv Fetcher for vimperator0.6.*
-// Require LDRize Cooperation ver 0.14
+// Last Change: 12-Jun-2009. Jan 2008
+var PLUGIN_INFO =
+<VimperatorPlugin>
+ <name>{NAME}</name>
+ <description>Flv Downloader for Nicovideo</description>
+ <author mail="trapezoid.g@gmail.com" homepage="http://unsigned.g.hatena.ne.jp/Trapezoid">Trapezoid</author>
+ <version>0.5</version>
+ <license>MIT License</license>
+ <minVersion>2.0pre</minVersion>
+ <maxVersion>2.0pre</maxVersion>
+ <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/ldrizecooperation_fetch_flv.js</updateURL>
+ <detail><![CDATA[
+== Detail ==
+Flv downloader for nicovideo.
-( function () {
+
+== Needs ==
+- LDRize
+- Minibuffer
+- ldrize_cooperation.js 0.25<
+
+== Commands ==
+=== :fetchflv ===
+>||
+ Direct download flv from Nicovideo.
+||<
+=== :nicomylist ===
+>||
+ Select Nicovideo Mylist of registration destination.
+||<
+
+== Parts ==
+- http://d.hatena.ne.jp/fls/20080309/p1
+- Pagerization (c) id:ofk
+- AutoPagerize (c) id:swdyjh
+- JSDeferred id:cho45
+
+
+== Variables ==
+=== g:nicovideo_mylist ==
+>||
+ Nicovideo Mylist of registration destination.
+||<
+ ]]></detail>
+</VimperatorPlugin>;
+
+(function () {
+function Deferred () this instanceof Deferred ? this.init(this) : new Deferred();
+Deferred.prototype = {
+ init : function () {
+ this._next = null;
+ this.callback = {
+ ok: function (x) x,
+ ng: function (x) { throw x }
+ };
+ return this;
+ },
+
+ next : function (fun) this._post("ok", fun),
+ error : function (fun) this._post("ng", fun),
+ call : function (val) this._fire("ok", val),
+ fail : function (err) this._fire("ng", err),
+
+ cancel : function () {
+ (this.canceller || function () {})();
+ return this.init();
+ },
+
+ _post : function (okng, fun) {
+ this._next = new Deferred();
+ this._next.callback[okng] = fun;
+ return this._next;
+ },
+
+ _fire : function (okng, value) {
+ var self = this, next = "ok";
+ try {
+ value = self.callback[okng].call(self, value);
+ } catch (e) {
+ next = "ng";
+ value = e;
+ }
+ if (value instanceof Deferred) {
+ value._next = self._next;
+ } else if (self._next) {
+ self._next._fire(next, value);
+ }
+ return this;
+ }
+};
+
+Deferred.next = function (fun) {
+ var d = new Deferred();
+ var id = setTimeout(function () { clearTimeout(id); d.call() }, 0);
+ if (fun) d.callback.ok = fun;
+ d.canceller = function () { try { clearTimeout(id) } catch (e) {} };
+ return d;
+};
+
+function http (opts) {
+ var d = Deferred();
+ var req = new XMLHttpRequest();
+ req.open(opts.method, opts.url, true, opts.user || null, opts.password || null);
+ if (opts.headers) {
+ for (var k in opts.headers) if (opts.headers.hasOwnProperty(k)) {
+ req.setRequestHeader(k, opts.headers[k]);
+ }
+ }
+ req.onreadystatechange = function () {
+ if (req.readyState == 4) d.call(req);
+ };
+ req.send(opts.data || null);
+ d.xhr = req;
+ return d;
+}
+http.get = function (url) http({method:"get", url:url});
+http.post = function (url, data) http({method:"post", url:url, data:data, headers:{"Content-Type":"application/x-www-form-urlencoded"}});
+
+Deferred.Deferred = Deferred;
+Deferred.http = http;
+
+
+// copied from AutoPagerize (c) id:swdyh
+function getElementsByXPath(xpath, node){
+ node = node || document;
+ var nodesSnapshot = (node.ownerDocument || node).evaluate(xpath, node, null,
+ XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
+ var data = [];
+ for(var i = 0, l = nodesSnapshot.snapshotLength; i < l;
+ data.push(nodesSnapshot.snapshotItem(i++)));
+ return (data.length > 0) ? data : null;
+}
+
+function getFirstElementByXPath(xpath, node){
+ node = node || document;
+ var result = (node.ownerDocument || node).evaluate(xpath, node, null,
+ XPathResult.FIRST_ORDERED_NODE_TYPE, null);
+ return result.singleNodeValue ? result.singleNodeValue : null;
+}
+
+// copied from Pagerization (c) id:ofk
+function parseHTML(str, ignoreTags) {
+ var exp = "^[\\s\\S]*?<html(?:\\s[^>]*)?>|</html\\s*>[\\S\\s]*$";
+ if (ignoreTags) {
+ if (typeof ignoreTags == "string") ignoreTags = [ignoreTags];
+ var stripTags = [];
+ ignoreTags = ignoreTags.filter(function(tag) tag[tag.length - 1] == "/" || !stripTags.push(tag))
+ .map(function(tag) tag.replace(/\/$/, ""));
+ if (stripTags.length > 0) {
+ stripTags = stripTags.length > 1
+ ? "(?:" + stripTags.join("|") + ")"
+ : String(stripTags);
+ exp += "|<" + stripTags + "(?:\\s[^>]*|/)?>|</" + stripTags + "\\s*>";
+ }
+ }
+ str = str.replace(new RegExp(exp, "ig"), "");
+ var res = document.implementation.createDocument(null, "html", null);
+ var range = document.createRange();
+ range.setStartAfter(window.content.document.body);
+ res.documentElement.appendChild(res.importNode(range.createContextualFragment(str), true));
+ if (ignoreTags) ignoreTags.forEach(function(tag) {
+ var elements = res.getElementsByTagName(tag);
+ for (var i = elements.length, el; el = elements.item(--i); el.parentNode.removeChild(el));
+ });
+ return res;
+}
+
+var DownloadManager = Cc['@mozilla.org/download-manager;1']
+ .getService(Ci.nsIDownloadManager);
+
+const nicoApiEndPoint = 'http://www.nicovideo.jp/api/getflv?v=';
+const nicoWatchEndPoint = 'http://www.nicovideo.jp/watch/';
+
+var groupId = liberator.globalVariables.nicovideo_mylist || '';
function NiconicoFlvHandler(url, title) {
- const nicoApiEndPoint = 'http://www.nicovideo.jp/api/getflv?v=';
- const nicoWatchEndPoint = 'http://www.nicovideo.jp/watch/';
let videoId = url.match(/\w{2}\d+/)[0];
let fileName = title.replace(/[?\\*\/:<>|"]/g, '_') + '.flv';
- httpGET(
- nicoApiEndPoint + videoId,
- function (apiResult) {
- let flvUrl = decodeURIComponent(apiResult.match(/url=(.*?)&/)[1]);
-
- httpGET(
- nicoWatchEndPoint + videoId,
- function (watchPage) {
- try {
- let DownloadManager = Cc['@mozilla.org/download-manager;1']
- .getService(Ci.nsIDownloadManager);
- let WebBrowserPersist = Cc['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
- .createInstance(Ci.nsIWebBrowserPersist);
-
- let sourceUri = makeURI(flvUrl, null, null);
- let file = DownloadManager.userDownloadsDirectory;
- file.appendRelativePath(fileName);
- let fileUri = makeFileURI(file);
-
- let download = DownloadManager.addDownload(
- 0, sourceUri, fileUri, fileName,
- null, null, null, null, WebBrowserPersist
- );
- WebBrowserPersist.progressListener = download;
- WebBrowserPersist.saveURI(sourceUri, null, null, null, null, file);
- }
- catch (e) {
- log(e);
- liberator.echoerr(e);
- }
- } // function (watchPage)
- ); // httpGET
- } // function (apiResult)
- ); // httpGET
+ Deferred.http.get(nicoApiEndPoint + videoId).next(function(apiResult){
+ let flvUrl = decodeURIComponent(apiResult.responseText.match(/url=(.*?)&/)[1]);
+ return Deferred.http.get(nicoWatchEndPoint + videoId).next(function(watchPage){
+ let WebBrowserPersist = Cc['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
+ .createInstance(Ci.nsIWebBrowserPersist);
+ let sourceUri = makeURI(flvUrl, null, null);
+ let file = DownloadManager.userDownloadsDirectory;
+ file.appendRelativePath(fileName);
+ let fileUri = makeFileURI(file);
+
+ let download = DownloadManager.addDownload(
+ 0, sourceUri, fileUri, fileName,
+ null, null, null, null, WebBrowserPersist
+ );
+ WebBrowserPersist.progressListener = download;
+ WebBrowserPersist.saveURI(sourceUri, null, null, null, null, file);
+ });
+ }).error(function(e){
+ log(e);
+ liberator.echoerr(e);
+ });
+}
+function NiconicoMylistHandler(url, title){
+ let videoId = url.match(/\w{2}\d+/)[0];
+
+ Deferred.http.get(nicoWatchEndPoint + videoId).next(function(watchResult){
+ var html = parseHTML(watchResult.responseText, ['img', 'script']);
+ Firebug.Console.log(html);
+ var csrfToken = getElementsByXPath('//input[@name="csrf_token"]', html)[0].value;
+ Firebug.Console.log(csrfToken);
+ var mylists = getElementsByXPath('id("mylist_add_group_id")/option', html).map(function(element) [element.innerHTML, element.value]);
+ Firebug.Console.log(mylists);
+
+ var params = [['ajax', '1'], ['mylist', 'add'], ['mylist_add', '“o˜^'], ['csrf_token', csrfToken], ['group_id', groupId]].map(function(p) p[0] + "=" + encodeURIComponent(p[1])).join("&");
+ return Deferred.http.post(nicoWatchEndPoint + videoId, params).next(function(mylistResult){
+ liberator.log(mylistResult.responseText);
+ });
+ }).error(function(e){
+ log(e);
+ liberator.echoerr(e);
+ });
}
function setupLDRizeCooperationNiconicoFlvFetcher() {
let NiconicoFlvFetcher = {
pattern: 'http://www.nicovideo.jp/watch/*',
- handler: NiconicoFlvHandler,
- wait: 5000,
+ handler: {
+ download: NiconicoFlvHandler,
+ mylist: NiconicoMylistHandler
+ },
+ wait: 5000,
};
this.convertHandlerInfo([NiconicoFlvFetcher]);
this.handlerInfo.unshift(NiconicoFlvFetcher);
@@ -96,5 +268,24 @@ liberator.modules.commands.addUserCommand( },
{}
);
-
+liberator.modules.commands.addUserCommand(
+ ['nicomylist'],
+ 'Select Nicovideo mylist',
+ function (arg) {
+ groupId = arg.string;
+ },
+ {
+ completer: function(context,arg,special){
+ Deferred.http.get(nicoWatchEndPoint + "sm2757983").next(function(watchResult){
+ var html = parseHTML(watchResult.responseText, ['img', 'script']);
+ var mylists = getElementsByXPath('id("mylist_add_group_id")/option', html).map(function(element) [element.value, element.innerHTML]);
+ context.title = ['ID', 'Name'];
+ context.advance = mylists.length;
+ context.completions = mylists;
+ }).error(function(e){
+ log(e);
+ liberator.echoerr(e);
+ });
+ }
+ });
})();
|