티스토리 뷰

Develop/Node.js

[node.js] transmission

Nebori 2018. 5. 13. 21:26

transmission

install

$ npm install transmission --save

use

const trans = require('transmission')

var transmission = new trans({
port: 9091, // DEFAULT : 9091
host: 'nebori.xxx.xxx', // DEAFULT : 127.0.0.1
username: 'admin', // DEFAULT : BLANK
password: '****' // DEFAULT : BLANK
});
// Get details of all torrents currently queued in transmission app
function getTransmissionStats(){
transmission.sessionStats(function(err, result){
if(err){
console.log(err);
} else {
console.log(result);
}
});
}

// Add a torrent by passing a URL to .torrent file or a magnet link
function addTorrent(url){
transmission.addUrl(url, function(err, result) {
if (err) {
return console.log(err);
}
var id = result.id;
console.log('Just added a new torrent.');
console.log('Torrent ID: ' + id);
});
}

addTorrent('magnet:?xt=urn:btih:0F7BC8453D143159220D691E92FFBE7B81C0FDC0');
getTransmissionStats();

example

  • 실제 사용하는 코드 (좋은 코드는 아닌듯)
  • 모든 샘플 코드를 Promise로 변경
const trans = require('transmission');

class transmissionGrinder {
constructor() {
this.transmission = new trans({
port: 9091, // DEFAULT : 9091
host: 'nebori.xxx.xxx', // DEAFULT : 127.0.0.1
username: 'admin', // DEFAULT : BLANK
password: '****' // DEFAULT : BLANK
});
}
// Get details of all torrents currently queued in transmission app
getTransmissionStats(){
return new Promise((resolve, reject) => {
this.transmission.sessionStats(function(err, result){
var jsonResult;
if(err){
jsonResult = {
'msg': 'error',
'data': err
}
} else {
jsonResult = {
'msg': 'success',
'data': result
}
}
resolve(jsonResult);
});
});
}
// Add a torrent by passing a URL to .torrent file or a magnet link
addTorrent(url){
return new Promise((resolve, reject) => {
this.transmission.addUrl(url, function(err, result) {
var jsonResult;
if(err){
jsonResult = {
'msg': 'error',
'data': err
}
} else {
jsonResult = {
'msg': 'success',
'data': 'Add torrent: ' + id
}
}
resolve(jsonResult);
});
});
}
// Get various stats about a torrent in the queue
getTorrentDetails(id) {
return new Promise((resolve, reject) => {
this.transmission.get(id, function(err, result) {
var jsonResult;
if(err){
jsonResult = {
'msg': 'error',
'data': err
}
} else {
var detail = {};
if(result.torrents.length > 0) {
var detailObject = result.torrents[0];
var detail = {
'name': detailObject.name,
'rateDownload': detailObject.rateDownload/1000,
'rateUpload': detailObject.rateUpload/1000,
'percentDone': detailObject*100,
'eta': detailObject.eta/3600,
'status': detailObject.status
}
}
jsonResult = {
'msg': 'success',
'data': detail
}
}
resolve(jsonResult);
});
});
}
deleteTorrent(id){
return new Promise((resolve, reject) => {
this.transmission.remove(id, true, function(err, result){
var jsonResult;
if(err){
jsonResult = {
'msg': 'error',
'data': err
}
} else {
jsonResult = {
'msg': 'success',
'data': 'Delete torrent: ' + id
}
}
resolve(jsonResult);
});
});
}
// To start a paused / stopped torrent which is still in queue
startTorrent(id){
return new Promise((resolve, reject) => {
this.transmission.start(id, function(err, result){
var jsonResult;
if(err){
jsonResult = {
'msg': 'error',
'data': err
}
} else {
jsonResult = {
'msg': 'success',
'data': 'Start torrent: ' + id
}
}
resolve(jsonResult);
});
});
}
// To get list of all torrents currently in queue (downloading + paused)
// NOTE : This may return null if all torrents are in paused state
getAllActiveTorrents(){
return new Promise((resolve, reject) => {
this.transmission.active(function(err, result){
var jsonResult;
if(err){
jsonResult = {
'msg': 'error',
'data': err
}
} else {
jsonResult = {
'msg': 'success',
'data': result
}
}
resolve(jsonResult);
});
});
}
// Pause / Stop a torrent
stopTorrent(id){
return new Promise((resolve, reject) => {
this.transmission.stop(id, function(err, result){
var jsonResult;
if(err){
jsonResult = {
'msg': 'error',
'data': err
}
} else {
jsonResult = {
'msg': 'success',
'data': 'Stop torrent: ' + id
}
}
resolve(jsonResult);
});
});
}
// Pause / Stop all torrent
stopAllActiveTorrents(){
return new Promise((resolve, reject) => {
this.transmission.active(function(err, result){
var jsonResult;
if(err){
jsonResult = {
'msg': 'error',
'data': err
}
} else {
for (var i=0; i< result.torrents.length; i++){
stopTorrents(result.torrents[i].id);
}
jsonResult = {
'msg': 'success',
'data': 'Stop all active torrents'
}
}
resolve(jsonResult);
});
});
}
// Remove a torrent from download queue
// NOTE : This does not trash torrent data i.e. does not remove it from disk
removeTorrent(id) {
return new Promise((resolve, reject) => {
this.transmission.remove(id, function(err) {
var jsonResult;
if(err){
jsonResult = {
'msg': 'error',
'data': err
}
} else {
jsonResult = {
'msg': 'success',
'data': 'Remove success: ' + id
}
}
resolve(jsonResult);
});
});
}
// Get torrent state
getStatusType(type){
return this.transmission.statusArray[type]
if(type === 0){
return 'STOPPED';
} else if(type === 1){
return 'CHECK_WAIT';
} else if(type === 2){
return 'CHECK';
} else if(type === 3){
return 'DOWNLOAD_WAIT';
} else if(type === 4){
return 'DOWNLOAD';
} else if(type === 5){
return 'SEED_WAIT';
} else if(type === 6){
return 'SEED';
} else if(type === 7){
return 'ISOLATED';
}
}
}

module.exports = new transmissionGrinder();



'Develop > Node.js' 카테고리의 다른 글

[node.js] session 관련 모듈  (1) 2018.06.24
[node.js] 'socket.io' module  (0) 2018.06.17
[node.js] Callback 지옥에서 벗어나보기  (0) 2018.03.26
[node.js] 'url' module  (0) 2018.03.13
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31