transmission-web-control/src/tr-web-control/script/min/transmission.torrents.min.js

15 lines
6.8 KiB
JavaScript

transmission.torrents={all:null,puased:null,downloading:null,actively:null,searchResult:null,error:null,warning:null,folders:{},status:{},count:0,totalSize:0,loadSimpleInfo:!1,activeTorrentCount:0,pausedTorrentCount:0,fields:{base:"id,name,status,hashString,totalSize,percentDone,addedDate,trackerStats,leftUntilDone,rateDownload,rateUpload,recheckProgress,rateDownload,rateUpload,peersGettingFromUs,peersSendingToUs,uploadRatio,uploadedEver,downloadedEver,downloadDir,error,errorString,doneDate,queuePosition,activityDate",
status:"id,name,status,totalSize,percentDone,trackerStats,leftUntilDone,rateDownload,rateUpload,recheckProgress,rateDownload,rateUpload,peersGettingFromUs,peersSendingToUs,uploadRatio,uploadedEver,downloadedEver,error,errorString,doneDate,queuePosition,activityDate",config:"downloadLimit,downloadLimited,peer-limit,seedIdleLimit,seedIdleMode,seedRatioLimit,seedRatioMode,uploadLimit,uploadLimited"},datas:{},recently:null,removed:null,isRecentlyActive:!1,newIds:[],btItems:[],getallids:function(b,d,c){var a=
this.fields.base;this.loadSimpleInfo&&this.all&&(a=this.fields.status);a=a.split(",");$.isArray(c)&&$.unique($.merge(a,c));c={fields:a};this.isRecentlyActive=!1;this.all&&void 0==d?(c.ids="recently-active",this.isRecentlyActive=!0):d&&(c.ids=d);this.all||(this.all={});transmission.exec({method:"torrent-get",arguments:c},function(a){"success"==a.result?(transmission.torrents.newIds.length=0,transmission.torrents.loadSimpleInfo=!0,transmission.torrents.recently=a.arguments.torrents,transmission.torrents.removed=
a.arguments.removed,transmission.torrents.splitid(),b&&b(a.arguments.torrents)):(transmission.torrents.datas=null,b&&b(null))})},splitid:function(){this.downloading=[];this.puased=[];this.actively=[];this.error=[];this.warning=[];this.btItems=[];transmission.downloadDirs=[];var b=transmission._status;this.status={};transmission.trackers={};this.totalSize=0;this.folders={};this.count=0;var d=new Base64,c;for(c in this.recently){var a=this.recently[c];this.datas[a.id]=a}var f=[];for(c in this.removed)a=
this.removed[c],f.push(a);for(c in this.datas){a=this.datas[c];if(!a)return;if(-1!=$.inArray(a.id,f)&&0<f.length)this.all[a.id]&&(this.all[a.id]=null,delete this.all[a.id]),this.datas[c]=null,delete this.datas[c];else{this.isRecentlyActive&&!this.all[a.id]&&this.newIds.push(a.id);a=$.extend(this.all[a.id],a);0==a.uploadedEver&&0==a.downloadedEver&&(a.uploadRatio=-1);a.uploadRatio=parseFloat(a.uploadRatio);a.infoIsLoading=!1;var e=this.status[a.status];this.addTracker(a);e||(this.status[a.status]=
[],e=this.status[a.status]);this.totalSize+=a.totalSize;a.remainingTime=0<a.rateDownload&&0<a.leftUntilDone?Math.floor(a.leftUntilDone/a.rateDownload*1E3):0==a.rateDownload&&0==a.leftUntilDone&&0!=a.totalSize?0:31536E8;e.push(a);0!=a.error&&this.error.push(a);(0<a.rateUpload||0<a.rateDownload)&&this.actively.push(a);switch(a.status){case b.stopped:this.puased.push(a);break;case b.download:this.downloading.push(a)}this.all[a.id]=a;-1==$.inArray(a.downloadDir,transmission.downloadDirs)&&transmission.downloadDirs.push(a.downloadDir);
if(transmission.options.getFolders&&a.downloadDir){e=a.downloadDir.replace(/\\/g,"/").split("/");var l="folders-",h;for(h in e){var g=e[h];""!=g&&(g=d.encode(g),l+=g.replace(/[+|\/|=]/g,"0"),(g=this.folders[l])||(g={count:0,torrents:[],size:0,nodeid:l}),g.torrents.push(a),g.count++,g.size+=a.totalSize,this.folders[l]=g)}}this.count++}}transmission.downloadDirs=transmission.downloadDirs.sort();0<this.newIds.length&&this.getallids(null,this.newIds)},addTracker:function(b){var d=b.trackerStats,c=[];
b.leecherCount=0;b.seederCount=0;if(0<d.length){var a=[],f;for(f in d){var e=d[f],l=e.lastAnnounceResult.toLowerCase(),h=e.host.getHostName().split(".");-1!=$.inArray(h[0],["www","tracker","announce"])&&h.shift();h=h.join(".");var g="tracker-"+h.replace(/\./g,"-"),k=transmission.trackers[g];k||(transmission.trackers[g]={count:0,torrents:[],size:0,connected:!0,isBT:5<d.length},k=transmission.trackers[g]);k.name=h;k.nodeid=g;k.host=e.host;e.lastAnnounceSucceeded||e.announceState==transmission._trackerStatus.inactive||
(a.push(e.lastAnnounceResult),"could not connect to tracker"==l&&(k.connected=!1));-1==k.torrents.indexOf(b)&&(k.torrents.push(b),k.count++,k.size+=b.totalSize);b.leecherCount+=e.leecherCount;b.seederCount+=e.seederCount;-1==c.indexOf(h)&&c.push(h)}5<d.length&&this.btItems.push(b);a.length==d.length&&(""==a.join(";").replace(/;/g,"")?b.warning="":b.warning=a.join(";"),b.nextAnnounceTime?b.nextAnnounceTime>e.nextAnnounceTime&&(b.nextAnnounceTime=e.nextAnnounceTime):b.nextAnnounceTime=e.nextAnnounceTime,
this.warning.push(b));0>b.leecherCount&&(b.leecherCount=0);0>b.seederCount&&(b.seederCount=0);b.leecher=b.leecherCount+" ("+b.peersGettingFromUs+")";b.seeder=b.seederCount+" ("+b.peersSendingToUs+")";b.trackers=c.join(";")}},getPeers:function(b){transmission.exec({method:"torrent-get",arguments:{fields:["peers","peersFrom"],ids:b}},function(b){console.log("data:",b)})},getMoreInfos:function(b,d,c){transmission.exec({method:"torrent-get",arguments:{fields:b.split(","),ids:d}},function(a){"success"==
a.result?c&&c(a.arguments.torrents):c&&c(null)})},search:function(b,d){if(!b)return null;d||(d=this.all);var c=[];$.each(d,function(a,f){-1!=d[a].name.toLowerCase().indexOf(b.toLowerCase())&&c.push(d[a])});return this.searchResult=c},getFiles:function(b,d){transmission.exec({method:"torrent-get",arguments:{fields:["files","fileStats"],ids:b}},function(b){"success"==b.result?d&&d(b.arguments.torrents):d&&d(null)})},getConfig:function(b,d){this.getMoreInfos(this.fields.config,b,d)},getErrorIds:function(b,
d){var c=[],a=new Date;1==d&&(a=a.getTime()/1E3);for(var f in this.error){var e=this.error[f];-1!=$.inArray(e.id,b)&&0<b.length||1==d&&a<e.nextAnnounceTime||e.status!=transmission._status.stopped&&c.push(e.id)}for(f in this.warning)e=this.warning[f],-1!=$.inArray(e.id,b)&&0<b.length||1==d&&a<e.nextAnnounceTime||c.push(e.id);return c},searchAndReplaceTrackers:function(b,d,c){if(b&&d){var a={},f=0,e;for(e in this.all){var l=this.all[e];if(!l)return;var h=l.trackerStats,g;for(g in h)h[g].announce==b&&
(a[g]||(a[g]={ids:[],tracker:d}),a[g].ids.push(l.id),f++)}0==f&&c&&c(null,0);for(e in a)transmission.exec({method:"torrent-set",arguments:{ids:a[e].ids,trackerReplace:[parseInt(e),a[e].tracker]}},function(a,b){"success"==a.result?c&&c(b,f):c&&c(null)},a[e].ids)}},getMagnetLink:function(b,d){var c="";"Array"!=b.constructor.name&&(b=[b]);if(0==b.length)d&&d(c);else{var a=[];for(f in b){var f=b[f];this.all[f]&&(this.all[f].magnetLink?c+=this.all[f].magnetLink+"\n":a.push(f))}0==a.length?d&&d(c.trim()):
transmission.exec({method:"torrent-get",arguments:{fields:["id","magnetLink"],ids:a}},function(a){if("success"==a.result){for(var b in a.arguments.torrents)b=a.arguments.torrents[b],transmission.torrents.all[b.id].magnetLink=b.magnetLink,c+=b.magnetLink+"\n";d&&d(c.trim())}})}}};