初始代码

This commit is contained in:
ronggang.zhou@gmail.com 2012-11-16 12:46:32 +00:00
commit 81c3649b8d
6 changed files with 456 additions and 0 deletions

11
.project Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MyTransmissionControl</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

217
index.html Normal file
View File

@ -0,0 +1,217 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Transmission 种子增加</title>
<script type="text/javascript" src="script/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="script/json2.min.js"></script>
<script type="text/javascript" src="script/Base64.js"></script>
<script type="text/javascript" src="script/transmission.js?v=20121112"></script>
</head>
<style>
body{
font-size:12px;
font-family:微软雅黑,宋体;
}
div.torrent input,div.torrent select,div.torrent textarea{
width:100%;
}
div.torrent table{
width:100%;
}
td.title{
text-align:right;
}
</style>
<body>
<div id="divserver">
服务器:<input type="text" id="host" value="http://"/>
端口:<input type="text" id="port" value="9091"/>
用户名:<input type="text" id="username"/>
密码:<input type="password" id="password"/>
<button onclick="javascript:link();">连接服务器</button>
<button onclick="javascript:link(true);">本地服务器</button>
<br/>
<button onclick="javascript:startall();" style="display:none;">开始所有</button>
<button onclick='javascript:torrentget();' style="display:none;">torrent-get</button>
</div>
<div id="divstatus">
<table style="width:100%;">
<tr>
<td width="10%" class="title">版本:</td>
<td width="90%"><span id="version"></span></td>
</tr>
<tr>
<td class="title">种子数量:</td>
<td><span id="activeTorrentCount"></span></td>
</tr>
</table>
</div>
<hr/>
<div id="divtorrent" class="torrent" style="display:none;">
<table>
<tr>
<td width="10%" class="title">保存目录:</td>
<td width="90%"><input id="download_path" value=""/></td>
</tr>
<tr>
<td class="title">种子地址:</td>
<td>
<span>提示:复制正确的种子链接到列表中,然后点击“增加种子”;多个种子用“回车”分隔。</span>
<textarea id="torrent_url" rows="10" style="height:150;"></textarea>
<select id="sellog" multiple="true" style="height:150;display:none;"></select>
</td>
</tr>
<tr>
<td class="title">自动开始:</td>
<td>
<input type="checkbox" id="chkautostart" style="width:20px;"/><label for="chkautostart">选中时,新添加的种子将自动开始下载(如不选择,则为暂停状态)</label>
</td>
</tr>
<tr>
<td></td>
<td><button onclick='javascript:addTorrents();' id="btnAdd">增加种子</button><span id="spnState" style="display:none;"></span></td>
</tr>
</table>
</div>
</body>
<script type="text/javascript">
// 连接服务器并获取信息
function link(islocal)
{
// 初始化连接
transmission.init(
{
host:jQuery("#host").val()
,port:jQuery("#port").val()
,username:jQuery("#username").val()
,password:jQuery("#password").val()
,islocal:islocal
}
,function (){
transmission.exec(
{
method:"session-get"
}
,function(data){
if (data.result=="success")
{
jQuery("#download_path").val(data.arguments["download-dir"]);
jQuery("#version").html(data.arguments["version"]);
jQuery("#divtorrent").show();
jQuery("#divserver").hide();
}
}
);
transmission.getStatus(function(data){
jQuery("#activeTorrentCount").html(data["activeTorrentCount"]);
});
}
);
}
function startall()
{
transmission.exec("torrent-start",function(data){
if (data.result=="success")
{
//
alert(data.arguments.version);
}
});
}
function torrentget()
{
transmission.exec(
{
method:"torrent-get"
,arguments:{
fields:("id,name,totalSize,status,hashString").split(",")
}
}
);
}
function addTorrents()
{
var urls = (jQuery("#torrent_url").val()).split("\n");
var autostart = jQuery("#chkautostart").prop("checked");
jQuery("#sellog").empty().show();
jQuery.each(urls,function(i,item)
{
jQuery("<option/>").attr("id","opt"+i).text((i+1)+"."+item).appendTo(jQuery("#sellog"));
});
jQuery("#torrent_url").val("").hide();
torrentAdd(urls,urls.length,autostart,jQuery("#download_path").val());
urls = null;
}
//
function showsate(msg,outtime)
{
$("#spnState").show();
$("#spnState").text(msg);
if (outtime==0)
{
return;
}
if (outtime==undefined)
{
outtime=3000;
}
$("#spnState").fadeOut(outtime);
}
// 添加种子
function torrentAdd(urls,count,autostart,savepath)
{
var index = count-urls.length;
var url = urls.shift();
if (!url)
{
jQuery("#torrent_url").show();
jQuery("#sellog").hide();
showsate("本次队列完成。");
return;
}
jQuery("#btnAdd").prop("disabled", true);
var option = jQuery("#opt"+index);
var text = option.text();
showsate("队列:"+(index+1)+"/"+(count),0);
option.text(text+"|正在增加...");
transmission.exec(
{
method:"torrent-add"
,arguments:{
filename:url
,"download-dir":savepath
,paused:(!autostart)
}
}
,function(data){
if (data.result=="success")
{
var name = "[已增加] " + data.arguments["torrent-added"].name;
option.text(text+"|"+name);
jQuery("#btnAdd").prop("disabled", false);
torrentAdd(urls,count,autostart,savepath);
}
}
);
}
(function init()
{
link(true);
})();
</script>
</html>

113
script/Base64.js Normal file
View File

@ -0,0 +1,113 @@
/**
*
* Base64 encode / decode
*
* @author haitao.tu
* @date 2010-04-26
* @email tuhaitao@foxmail.com
*
*/
function Base64() {
// private property
_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// public method for encoding
this.encode = function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = _utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
}
return output;
}
// public method for decoding
this.decode = function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = _keyStr.indexOf(input.charAt(i++));
enc2 = _keyStr.indexOf(input.charAt(i++));
enc3 = _keyStr.indexOf(input.charAt(i++));
enc4 = _keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = _utf8_decode(output);
return output;
}
// private method for UTF-8 encoding
_utf8_encode = function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
// private method for UTF-8 decoding
_utf8_decode = function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}

4
script/jquery-1.7.2.min.js vendored Normal file

File diff suppressed because one or more lines are too long

1
script/json2.min.js vendored Normal file
View File

@ -0,0 +1 @@
var JSON;JSON||(JSON={}),(function(){"use strict";function i(n){return n<10?"0"+n:n}function f(n){return o.lastIndex=0,o.test(n)?'"'+n.replace(o,function(n){var t=s[n];return typeof t=="string"?t:"\\u"+("0000"+n.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+n+'"'}function r(i,e){var h,l,c,a,v=n,s,o=e[i];o&&typeof o=="object"&&typeof o.toJSON=="function"&&(o=o.toJSON(i)),typeof t=="function"&&(o=t.call(e,i,o));switch(typeof o){case"string":return f(o);case"number":return isFinite(o)?String(o):"null";case"boolean":case"null":return String(o);case"object":if(!o)return"null";n+=u,s=[];if(Object.prototype.toString.apply(o)==="[object Array]"){for(a=o.length,h=0;h<a;h+=1)s[h]=r(h,o)||"null";return c=s.length===0?"[]":n?"[\n"+n+s.join(",\n"+n)+"\n"+v+"]":"["+s.join(",")+"]",n=v,c}if(t&&typeof t=="object")for(a=t.length,h=0;h<a;h+=1)typeof t[h]=="string"&&(l=t[h],c=r(l,o),c&&s.push(f(l)+(n?": ":":")+c));else for(l in o)Object.prototype.hasOwnProperty.call(o,l)&&(c=r(l,o),c&&s.push(f(l)+(n?": ":":")+c));return c=s.length===0?"{}":n?"{\n"+n+s.join(",\n"+n)+"\n"+v+"}":"{"+s.join(",")+"}",n=v,c}}typeof Date.prototype.toJSON!="function"&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+i(this.getUTCMonth()+1)+"-"+i(this.getUTCDate())+"T"+i(this.getUTCHours())+":"+i(this.getUTCMinutes())+":"+i(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()});var e=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,o=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,n,u,s={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},t;typeof JSON.stringify!="function"&&(JSON.stringify=function(i,f,e){var o;n="",u="";if(typeof e=="number")for(o=0;o<e;o+=1)u+=" ";else typeof e=="string"&&(u=e);t=f;if(f&&typeof f!="function"&&(typeof f!="object"||typeof f.length!="number"))throw new Error("JSON.stringify");return r("",{"":i})}),typeof JSON.parse!="function"&&(JSON.parse=function(n,t){function r(n,i){var f,e,u=n[i];if(u&&typeof u=="object")for(f in u)Object.prototype.hasOwnProperty.call(u,f)&&(e=r(u,f),e!==undefined?u[f]=e:delete u[f]);return t.call(n,i,u)}var i;n=String(n),e.lastIndex=0,e.test(n)&&(n=n.replace(e,function(n){return"\\u"+("0000"+n.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(n.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return i=eval("("+n+")"),typeof t=="function"?r({"":i},""):i;throw new SyntaxError("JSON.parse");})})()

110
script/transmission.js Normal file
View File

@ -0,0 +1,110 @@
// transmission RPC 操作类
var transmission = {
SessionId:""
,isInitialized:false
,host:""
,port:"9091"
,path:"/transmission/rpc"
,fullpath:""
,username:""
,password:""
,headers:{}
,islocal:false
,getSessionId:function(me,callback)
{
var settings = {
type: "POST"
,url:this.fullpath
,crossDomain:true
,error: function(request,event,settings)
{
var SessionId = "";
if (request.status === 409 && (SessionId = request.getResponseHeader('X-Transmission-Session-Id')))
{
me.isInitialized = true;
me.headers["X-Transmission-Session-Id"] = SessionId;
if (callback)
{
callback();
}
}
}
,headers:this.headers
};
jQuery.ajax(settings);
}
,init:function(config,callback)
{
jQuery.extend(this, config);
if (this.islocal)
{
this.fullpath = this.path;
}
else
{
if (this.fullpath=="")
{
this.fullpath = this.host + (this.port?":"+this.port:"") + this.path;
}
if (this.username&&this.password)
{
this.headers["Authorization"] = "Basic "+(new Base64()).encode(this.username+":"+this.password);
}
}
this.getSessionId(this,callback);
}
,exec:function(config,callback)
{
if (!this.isInitialized)
{
return false;
}
var data = {
method:""
,arguments:{}
,tag:""
};
jQuery.extend(data, config);
var settings = {
type: "POST"
,url:this.fullpath
,dataType: 'json'
,data:JSON.stringify(data)
,crossDomain:true
,success:function(resultData,textStatus)
{
if (callback)
{
callback(resultData);
}
}
,error:function(request,event,settings)
{
alert("数据提交错误,错误状态:"+request.status);
}
,headers:this.headers
};
jQuery.ajax(settings);
}
,getStatus:function(callback)
{
this.exec(
{
method:"session-stats"
}
,function(data)
{
if (data.result=="success")
{
if (callback)
{
callback(data.arguments);
}
}
}
);
}
}