Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************
- * T411 CLI
- *
- * Interroger l'API T411 en ligne de commande
- * Auteur : T00rk <http://www.twitter.com/T00rk>
- *
- * PREREQUIS :
- *
- * NodeJS (http://nodejs.org/)
- *
- * README :
- *
- * - Installation des dépendances :
- * > npm install request cli-table
- *
- * - Lancement de l'application
- * > node app.js
- *
- *************************************************/
- // -- INCLUDES -- //
- var READLINE = require("readline");
- var EVENTS = require("events");
- var COLORS = require("colors");
- var REQUEST = require("request");
- var TABLE = require("cli-table");
- var FS = require("fs");
- // -- SETTINGS -- //
- process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
- // -- CODE -- //
- var t411 = function()
- {
- this.USERID = null;
- this.TOKEN = null;
- this.secure = false;
- this.Results = null;
- this.Emitter = new EVENTS.EventEmitter();
- this.Input = READLINE.createInterface
- ({
- input: process.stdin,
- output: process.stdout
- });
- this.CommandList =
- [
- {
- name : "login",
- description : "Permet de vous logger sur l'API de T411",
- exec : function(obj)
- {
- obj.Input.question("Login : ", function(login)
- {
- obj.secure = true;
- obj.hidden("Password : ", obj.Input, function(password)
- {
- obj.secure = false;
- obj.sendPostRequest("http://api.t411.me/auth", { username: login, password : password }, function(response)
- {
- var _credentials = JSON.parse(response);
- obj.TOKEN = _credentials.token;
- obj.USERID = _credentials.uid;
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- console.log("Vous êtes maintenant logué sur T411 !\r\n".green);
- }
- else
- {
- console.log("Utilisateur Inconnu".red)
- }
- obj.Emitter.emit("CommandFinished");
- });
- });
- });
- }
- },
- {
- name : "profil",
- description : "Voir le profil d'un utilisateur T411",
- exec : function(obj)
- {
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- obj.Input.question("Identifiant de l'Utilisateur : ", function(UID)
- {
- if(UID.length <= 0)
- {
- UID = obj.USERID;
- }
- obj.sendGetRequest("http://api.t411.me/users/profile/" + UID, function(response)
- {
- var _user = JSON.parse(response);
- var table = new TABLE
- ({
- colWidths: [50, 50]
- });
- table.push
- (
- ["Nom d'Utilisateur", _user.username],
- ["Sexe", _user.gender],
- ["Age", _user.age],
- ["Ratio", (parseFloat(_user.uploaded) / parseFloat(_user.downloaded)).toFixed(4)]
- );
- console.log("");
- console.log(table.toString());
- console.log("");
- obj.Emitter.emit("CommandFinished");
- });
- });
- }
- else
- {
- console.log("\r\nVous devez d'abord vous logger !\r\n".red);
- obj.Emitter.emit("CommandFinished");
- }
- }
- },
- {
- name : "100",
- description : "Voir le Top 100 des Torrents",
- exec : function(obj)
- {
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- obj.sendGetRequest("http://api.t411.me/torrents/top/100", function(response)
- {
- var _top = JSON.parse(response);
- if(_top !== null)
- {
- var table = new TABLE
- ({
- head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
- colWidths: [10, 120, 20, 20, 10, 10]
- });
- for(var i = 0; i < _top.length; i++)
- {
- table.push([_top[i].id, _top[i].name, _top[i].categoryname, _top[i].size, _top[i].seeders, _top[i].leechers]);
- }
- console.log("");
- console.log(table.toString());
- console.log("");
- obj.Emitter.emit("CommandFinished");
- }
- });
- }
- else
- {
- console.log("\r\nVous devez d'abord vous logger !\r\n".red);
- obj.Emitter.emit("CommandFinished");
- }
- }
- },
- {
- name : "day",
- description : "Voir la liste des Torrents du jour",
- exec : function(obj)
- {
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- obj.sendGetRequest("http://api.t411.me/torrents/top/today", function(response)
- {
- var _top = JSON.parse(response);
- if(_top !== null)
- {
- obj.Results = _top;
- var _show = obj.Results.splice(0, 20);
- var table = new TABLE
- ({
- head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
- colWidths: [10, 120, 20, 20, 10, 10]
- });
- for(var i = 0; i < _show.length; i++)
- {
- table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
- }
- console.log("");
- console.log(table.toString());
- console.log("");
- obj.Emitter.emit("CommandFinished");
- }
- });
- }
- else
- {
- console.log("\r\nVous devez d'abord vous logger !\r\n".red);
- obj.Emitter.emit("CommandFinished");
- }
- }
- },
- {
- name : "week",
- description : "Voir la liste des Torrents de la Semaine",
- exec : function(obj)
- {
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- obj.sendGetRequest("http://api.t411.me/torrents/top/week", function(response)
- {
- var _top = JSON.parse(response);
- if(_top !== null)
- {
- obj.Results = _top;
- var _show = obj.Results.splice(0, 20);
- var table = new TABLE
- ({
- head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
- colWidths: [10, 120, 20, 20, 10, 10]
- });
- for(var i = 0; i < _show.length; i++)
- {
- table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
- }
- console.log("");
- console.log(table.toString());
- console.log("");
- obj.Emitter.emit("CommandFinished");
- }
- });
- }
- else
- {
- console.log("\r\nVous devez d'abord vous logger !\r\n".red);
- obj.Emitter.emit("CommandFinished");
- }
- }
- },
- {
- name : "month",
- description : "Voir la liste des Torrents du Mois",
- exec : function(obj)
- {
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- obj.sendGetRequest("http://api.t411.me/torrents/top/month", function(response)
- {
- var _top = JSON.parse(response);
- if(_top !== null)
- {
- obj.Results = _top;
- var _show = obj.Results.splice(0, 20);
- var table = new TABLE
- ({
- head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
- colWidths: [10, 120, 20, 20, 10, 10]
- });
- for(var i = 0; i < _show.length; i++)
- {
- table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
- }
- console.log("");
- console.log(table.toString());
- console.log("");
- obj.Emitter.emit("CommandFinished");
- }
- });
- }
- else
- {
- console.log("\r\nVous devez d'abord vous logger !\r\n".red);
- obj.Emitter.emit("CommandFinished");
- }
- }
- },
- {
- name : "show",
- description : "Voir les détails d'un Torrent",
- exec : function(obj)
- {
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- obj.Input.question("Identifiant du Torrent : ", function(tid)
- {
- obj.sendGetRequest("http://api.t411.me/torrents/details/" + tid, function(response)
- {
- var _top = JSON.parse(response);
- if(_top !== null)
- {
- obj.Results = _top;
- var _show = obj.Results.splice(0, 20);
- var table = new TABLE
- ({
- head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
- colWidths: [10, 120, 20, 20, 10, 10]
- });
- for(var i = 0; i < _show.length; i++)
- {
- table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
- }
- console.log("");
- console.log(table.toString());
- console.log("");
- obj.Emitter.emit("CommandFinished");
- }
- });
- });
- }
- else
- {
- console.log("\r\nVous devez d'abord vous logger !\r\n".red);
- obj.Emitter.emit("CommandFinished");
- }
- }
- },
- {
- name : "down",
- description : "Télécharger un Torrent",
- exec : function(obj)
- {
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- obj.Input.question("Identifiant du Torrent : ", function(tid)
- {
- obj.sendGetRequest("http://api.t411.me/torrents/download/" + tid, function(response)
- {
- FS.writeFile("./" + tid + ".torrent", response, function(err)
- {
- if(err)
- {
- console.log(err);
- obj.Emitter.emit("CommandFinished");
- }
- else
- {
- console.log("\r\nLe Torrent a été téléchargé. Il est disponible à cette adresse : ".green + __dirname.green + "/".green + tid.green + ".torrent\r\n".green);
- obj.Emitter.emit("CommandFinished");
- }
- });
- });
- });
- }
- else
- {
- console.log("\r\nVous devez d'abord vous logger !\r\n".red);
- obj.Emitter.emit("CommandFinished");
- }
- }
- },
- {
- name : "search",
- description : "Rechercher un Torrent",
- exec : function(obj)
- {
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- obj.Input.question("Recherche : ", function(query)
- {
- obj.sendGetRequest("http://api.t411.me/torrents/search/" + query, function(response)
- {
- var _result = JSON.parse(response);
- if(_result !== null)
- {
- console.log("");
- console.log("Résultat de la recherche pour \"".yellow + query.yellow + "\" : ".yellow + _result.total.red + " torrents trouvés\r\n".yellow);
- var table = new TABLE
- ({
- head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
- colWidths: [10, 120, 20, 20, 10, 10]
- });
- for(var i = 0; i < _result.torrents.length; i++)
- {
- table.push([_result.torrents[i].id, _result.torrents[i].name, _result.torrents[i].categoryname, _result.torrents[i].size, _result.torrents[i].seeders, _result.torrents[i].leechers]);
- }
- console.log("");
- console.log(table.toString());
- console.log("");
- obj.Emitter.emit("CommandFinished");
- }
- });
- });
- }
- else
- {
- console.log("\r\nVous devez d'abord vous logger !\r\n".red);
- obj.Emitter.emit("CommandFinished");
- }
- }
- },
- {
- name : "next",
- description : "Voir la suite des résultats d'une requête",
- exec : function(obj)
- {
- if(obj.TOKEN !== null && obj.USERID !== null)
- {
- if(obj.Results !== null && obj.Results.length > 0)
- {
- var _show = obj.Results.splice(0, 20);
- var table = new TABLE
- ({
- head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
- colWidths: [10, 120, 20, 20, 10, 10]
- });
- for(var i = 0; i < _show.length; i++)
- {
- table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
- }
- console.log("");
- console.log(table.toString());
- console.log("");
- obj.Emitter.emit("CommandFinished");
- }
- else
- {
- console.log("\r\nPlus de Résultats !".orange);
- }
- }
- else
- {
- console.log("\r\nVous devez d'abord vous logger !\r\n".red);
- obj.Emitter.emit("CommandFinished");
- }
- }
- },
- {
- name : "exit",
- description : "Quitter le programme",
- exec : function(obj)
- {
- obj.Input.close();
- }
- },
- {
- name : "help",
- description : "Affiche cet écran",
- exec : function(obj)
- {
- console.log("Liste des Commandes : " + "\r\n");
- for(var i = 0; i < obj.CommandList.length; i++)
- {
- console.log("\t" + obj.CommandList[i].name.yellow + "\t\t : ".yellow + obj.CommandList[i].description + "\r\n");
- }
- obj.Emitter.emit("CommandFinished");
- }
- }
- ];
- };
- t411.prototype =
- {
- ask : function()
- {
- var _self = this;
- this.Input.question("T411 > ", function(answer)
- {
- _self.Emitter.emit("CommandSet", answer);
- });
- },
- launch : function()
- {
- var _self = this;
- console.log("-------------------------------------------------");
- console.log("Bienvenue sur T411".green);
- console.log("Tapez \"help\" pour avoir la liste des commandes.".yellow);
- console.log("-------------------------------------------------\r\n");
- this.Emitter.on("CommandSet", function(c)
- {
- _self.executeCommand(c);
- });
- this.Emitter.on("CommandFinished", function()
- {
- _self.ask();
- });
- this.Emitter.emit("CommandFinished");
- },
- executeCommand : function(c)
- {
- var found = false;
- var command = c.toLowerCase().trim();
- for(var i = 0; i < this.CommandList.length; i++)
- {
- if(this.CommandList[i].name === command)
- {
- this.CommandList[i].exec(this);
- found = true;
- break;
- }
- }
- if(!found)
- {
- console.log("\r\nCommande Inconnue\r\n".red);
- this.Emitter.emit("CommandFinished");
- }
- },
- hidden : function(query, input, callback)
- {
- var _self = this;
- var stdin = process.openStdin(),
- i = 0;
- process.stdin.on("data", function(char)
- {
- if(_self.secure)
- {
- char = char + "";
- switch (char)
- {
- case "\n":
- case "\r":
- case "\u0004":
- //stdin.pause();
- break;
- default:
- process.stdout.write("\033[2K\033[200D"+query+"["+((i%2==1)?"=-":"-=")+"]");
- i++;
- break;
- }
- }
- });
- var _self = this;
- input.question(query, function(value)
- {
- input.history = input.history.slice(1);
- callback(value);
- });
- },
- sendPostRequest : function(url, params, callback)
- {
- REQUEST
- ({
- rejectUnauthorized: false,
- uri: url,
- method: "POST",
- form: params,
- headers:
- {
- 'User-Agent': 'T411 CLI'
- },
- }, function(error, response, body)
- {
- callback(body);
- });
- },
- sendGetRequest : function(url, callback)
- {
- var _self = this;
- REQUEST
- ({
- rejectUnauthorized: false,
- uri: url,
- method: "GET",
- headers:
- {
- 'User-Agent': 'T411 CLI',
- 'Authorization': _self.TOKEN
- },
- }, function(error, response, body)
- {
- callback(body);
- });
- }
- };
- var T411Command = new t411();
- T411Command.launch();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement