Advertisement
Guest User

t411 cli 0.1

a guest
Sep 30th, 2014
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*************************************************
  2.  * T411 CLI
  3.  *
  4.  * Interroger l'API T411 en ligne de commande
  5.  * Auteur : T00rk <http://www.twitter.com/T00rk>
  6.  *
  7.  * PREREQUIS :
  8.  *
  9.  * NodeJS (http://nodejs.org/)
  10.  *
  11.  * README :
  12.  *
  13.  * - Installation des dépendances :
  14.  *   > npm install request cli-table
  15.  *
  16.  * - Lancement de l'application
  17.  *   > node app.js
  18.  *
  19.  *************************************************/
  20.  
  21. // -- INCLUDES -- //
  22. var READLINE = require("readline");
  23. var EVENTS = require("events");
  24. var COLORS = require("colors");
  25. var REQUEST = require("request");
  26. var TABLE = require("cli-table");
  27. var FS = require("fs");
  28.  
  29. // -- SETTINGS -- //
  30. process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
  31.  
  32. // -- CODE -- //
  33. var t411 = function()
  34. {
  35.     this.USERID = null;
  36.     this.TOKEN = null;
  37.  
  38.     this.secure = false;
  39.  
  40.     this.Results = null;
  41.  
  42.     this.Emitter = new EVENTS.EventEmitter();
  43.     this.Input = READLINE.createInterface
  44.     ({
  45.         input: process.stdin,
  46.         output: process.stdout
  47.     });
  48.     this.CommandList =
  49.     [
  50.         {
  51.             name : "login",
  52.             description : "Permet de vous logger sur l'API de T411",
  53.             exec : function(obj)
  54.             {
  55.                 obj.Input.question("Login : ", function(login)
  56.                 {
  57.                     obj.secure = true;
  58.                     obj.hidden("Password : ", obj.Input, function(password)
  59.                     {
  60.                         obj.secure = false;
  61.                         obj.sendPostRequest("http://api.t411.me/auth", { username: login, password : password }, function(response)
  62.                         {
  63.                             var _credentials = JSON.parse(response);
  64.  
  65.                             obj.TOKEN = _credentials.token;
  66.                             obj.USERID = _credentials.uid;
  67.  
  68.                             if(obj.TOKEN !== null && obj.USERID !== null)
  69.                             {
  70.                                 console.log("Vous êtes maintenant logué sur T411 !\r\n".green);
  71.                             }
  72.                             else
  73.                             {
  74.                                 console.log("Utilisateur Inconnu".red)
  75.                             }
  76.  
  77.                             obj.Emitter.emit("CommandFinished");
  78.                         });
  79.                     });
  80.                 });
  81.             }
  82.         },
  83.         {
  84.             name : "profil",
  85.             description : "Voir le profil d'un utilisateur T411",
  86.             exec : function(obj)
  87.             {
  88.                 if(obj.TOKEN !== null && obj.USERID !== null)
  89.                 {
  90.                     obj.Input.question("Identifiant de l'Utilisateur : ", function(UID)
  91.                     {
  92.                         if(UID.length <= 0)
  93.                         {
  94.                             UID = obj.USERID;
  95.                         }
  96.                         obj.sendGetRequest("http://api.t411.me/users/profile/" + UID, function(response)
  97.                         {
  98.                             var _user = JSON.parse(response);
  99.  
  100.                             var table = new TABLE
  101.                             ({
  102.                                 colWidths: [50, 50]
  103.                             });
  104.  
  105.                             table.push
  106.                             (
  107.                                 ["Nom d'Utilisateur", _user.username],
  108.                                 ["Sexe", _user.gender],
  109.                                 ["Age", _user.age],
  110.                                 ["Ratio", (parseFloat(_user.uploaded) / parseFloat(_user.downloaded)).toFixed(4)]
  111.                             );
  112.  
  113.                             console.log("");
  114.                             console.log(table.toString());
  115.                             console.log("");
  116.  
  117.                             obj.Emitter.emit("CommandFinished");
  118.                         });
  119.                     });
  120.                 }
  121.                 else
  122.                 {
  123.                     console.log("\r\nVous devez d'abord vous logger !\r\n".red);
  124.                     obj.Emitter.emit("CommandFinished");
  125.                 }
  126.             }
  127.         },
  128.         {
  129.             name : "100",
  130.             description : "Voir le Top 100 des Torrents",
  131.             exec : function(obj)
  132.             {
  133.                 if(obj.TOKEN !== null && obj.USERID !== null)
  134.                 {
  135.                     obj.sendGetRequest("http://api.t411.me/torrents/top/100", function(response)
  136.                     {
  137.                         var _top = JSON.parse(response);
  138.                         if(_top !== null)
  139.                         {
  140.                             var table = new TABLE
  141.                             ({
  142.                                 head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
  143.                                 colWidths: [10, 120, 20, 20, 10, 10]
  144.                             });
  145.  
  146.                             for(var i = 0; i < _top.length; i++)
  147.                             {
  148.                                 table.push([_top[i].id, _top[i].name, _top[i].categoryname, _top[i].size, _top[i].seeders, _top[i].leechers]);
  149.                             }
  150.  
  151.                             console.log("");
  152.                             console.log(table.toString());
  153.                             console.log("");
  154.                            
  155.                             obj.Emitter.emit("CommandFinished");
  156.                         }
  157.                     });
  158.                 }
  159.                 else
  160.                 {
  161.                     console.log("\r\nVous devez d'abord vous logger !\r\n".red);
  162.                     obj.Emitter.emit("CommandFinished");
  163.                 }
  164.             }
  165.         },
  166.         {
  167.             name : "day",
  168.             description : "Voir la liste des Torrents du jour",
  169.             exec : function(obj)
  170.             {
  171.                 if(obj.TOKEN !== null && obj.USERID !== null)
  172.                 {
  173.                     obj.sendGetRequest("http://api.t411.me/torrents/top/today", function(response)
  174.                     {
  175.                         var _top = JSON.parse(response);
  176.                         if(_top !== null)
  177.                         {
  178.                             obj.Results = _top;
  179.                             var _show = obj.Results.splice(0, 20);
  180.                            
  181.                             var table = new TABLE
  182.                             ({
  183.                                 head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
  184.                                 colWidths: [10, 120, 20, 20, 10, 10]
  185.                             });
  186.  
  187.                             for(var i = 0; i < _show.length; i++)
  188.                             {
  189.                                 table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
  190.                             }
  191.  
  192.                             console.log("");
  193.                             console.log(table.toString());
  194.                             console.log("");
  195.                            
  196.                             obj.Emitter.emit("CommandFinished");
  197.                         }
  198.                     });
  199.                 }
  200.                 else
  201.                 {
  202.                     console.log("\r\nVous devez d'abord vous logger !\r\n".red);
  203.                     obj.Emitter.emit("CommandFinished");
  204.                 }
  205.             }
  206.         },
  207.         {
  208.             name : "week",
  209.             description : "Voir la liste des Torrents de la Semaine",
  210.             exec : function(obj)
  211.             {
  212.                 if(obj.TOKEN !== null && obj.USERID !== null)
  213.                 {
  214.                     obj.sendGetRequest("http://api.t411.me/torrents/top/week", function(response)
  215.                     {
  216.                         var _top = JSON.parse(response);
  217.                         if(_top !== null)
  218.                         {
  219.                             obj.Results = _top;
  220.                             var _show = obj.Results.splice(0, 20);
  221.                            
  222.                             var table = new TABLE
  223.                             ({
  224.                                 head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
  225.                                 colWidths: [10, 120, 20, 20, 10, 10]
  226.                             });
  227.  
  228.                             for(var i = 0; i < _show.length; i++)
  229.                             {
  230.                                 table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
  231.                             }
  232.  
  233.                             console.log("");
  234.                             console.log(table.toString());
  235.                             console.log("");
  236.                            
  237.                             obj.Emitter.emit("CommandFinished");
  238.                         }
  239.                     });
  240.                 }
  241.                 else
  242.                 {
  243.                     console.log("\r\nVous devez d'abord vous logger !\r\n".red);
  244.                     obj.Emitter.emit("CommandFinished");
  245.                 }
  246.             }
  247.         },
  248.         {
  249.             name : "month",
  250.             description : "Voir la liste des Torrents du Mois",
  251.             exec : function(obj)
  252.             {
  253.                 if(obj.TOKEN !== null && obj.USERID !== null)
  254.                 {
  255.                     obj.sendGetRequest("http://api.t411.me/torrents/top/month", function(response)
  256.                     {
  257.                         var _top = JSON.parse(response);
  258.                         if(_top !== null)
  259.                         {
  260.                             obj.Results = _top;
  261.                             var _show = obj.Results.splice(0, 20);
  262.                            
  263.                             var table = new TABLE
  264.                             ({
  265.                                 head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
  266.                                 colWidths: [10, 120, 20, 20, 10, 10]
  267.                             });
  268.  
  269.                             for(var i = 0; i < _show.length; i++)
  270.                             {
  271.                                 table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
  272.                             }
  273.  
  274.                             console.log("");
  275.                             console.log(table.toString());
  276.                             console.log("");
  277.                            
  278.                             obj.Emitter.emit("CommandFinished");
  279.                         }
  280.                     });
  281.                 }
  282.                 else
  283.                 {
  284.                     console.log("\r\nVous devez d'abord vous logger !\r\n".red);
  285.                     obj.Emitter.emit("CommandFinished");
  286.                 }
  287.             }
  288.         },
  289.         {
  290.             name : "show",
  291.             description : "Voir les détails d'un Torrent",
  292.             exec : function(obj)
  293.             {
  294.                 if(obj.TOKEN !== null && obj.USERID !== null)
  295.                 {
  296.                     obj.Input.question("Identifiant du Torrent : ", function(tid)
  297.                     {
  298.                         obj.sendGetRequest("http://api.t411.me/torrents/details/" + tid, function(response)
  299.                         {
  300.                             var _top = JSON.parse(response);
  301.                             if(_top !== null)
  302.                             {
  303.                                 obj.Results = _top;
  304.                                 var _show = obj.Results.splice(0, 20);
  305.                                
  306.                                 var table = new TABLE
  307.                                 ({
  308.                                     head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
  309.                                     colWidths: [10, 120, 20, 20, 10, 10]
  310.                                 });
  311.  
  312.                                 for(var i = 0; i < _show.length; i++)
  313.                                 {
  314.                                     table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
  315.                                 }
  316.  
  317.                                 console.log("");
  318.                                 console.log(table.toString());
  319.                                 console.log("");
  320.                            
  321.                                 obj.Emitter.emit("CommandFinished");
  322.                             }
  323.                         });
  324.                     });
  325.                 }
  326.                 else
  327.                 {
  328.                     console.log("\r\nVous devez d'abord vous logger !\r\n".red);
  329.                     obj.Emitter.emit("CommandFinished");
  330.                 }
  331.             }
  332.         },
  333.         {
  334.             name : "down",
  335.             description : "Télécharger un Torrent",
  336.             exec : function(obj)
  337.             {
  338.                 if(obj.TOKEN !== null && obj.USERID !== null)
  339.                 {
  340.                     obj.Input.question("Identifiant du Torrent : ", function(tid)
  341.                     {
  342.                         obj.sendGetRequest("http://api.t411.me/torrents/download/" + tid, function(response)
  343.                         {
  344.                             FS.writeFile("./" + tid + ".torrent", response, function(err)
  345.                             {
  346.                                 if(err)
  347.                                 {
  348.                                     console.log(err);
  349.                                     obj.Emitter.emit("CommandFinished");
  350.                                 }
  351.                                 else
  352.                                 {
  353.                                     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);
  354.                                     obj.Emitter.emit("CommandFinished");
  355.                                 }
  356.                             });
  357.                         });
  358.                     });
  359.                 }
  360.                 else
  361.                 {
  362.                     console.log("\r\nVous devez d'abord vous logger !\r\n".red);
  363.                     obj.Emitter.emit("CommandFinished");
  364.                 }
  365.             }
  366.         },
  367.         {
  368.             name : "search",
  369.             description : "Rechercher un Torrent",
  370.             exec : function(obj)
  371.             {
  372.                 if(obj.TOKEN !== null && obj.USERID !== null)
  373.                 {
  374.                     obj.Input.question("Recherche : ", function(query)
  375.                     {
  376.                         obj.sendGetRequest("http://api.t411.me/torrents/search/" + query, function(response)
  377.                         {
  378.                             var _result = JSON.parse(response);
  379.                             if(_result !== null)
  380.                             {                          
  381.                                 console.log("");
  382.                                 console.log("Résultat de la recherche pour \"".yellow + query.yellow + "\" : ".yellow + _result.total.red + " torrents trouvés\r\n".yellow);
  383.                                
  384.                                 var table = new TABLE
  385.                                 ({
  386.                                     head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
  387.                                     colWidths: [10, 120, 20, 20, 10, 10]
  388.                                 });
  389.  
  390.                                 for(var i = 0; i < _result.torrents.length; i++)
  391.                                 {
  392.                                     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]);
  393.                                 }
  394.  
  395.                                 console.log("");
  396.                                 console.log(table.toString());
  397.                                 console.log("");
  398.                            
  399.                                 obj.Emitter.emit("CommandFinished");
  400.                             }
  401.                         });
  402.                     });
  403.                 }
  404.                 else
  405.                 {
  406.                     console.log("\r\nVous devez d'abord vous logger !\r\n".red);
  407.                     obj.Emitter.emit("CommandFinished");
  408.                 }
  409.             }
  410.         },
  411.         {
  412.             name : "next",
  413.             description : "Voir la suite des résultats d'une requête",
  414.             exec : function(obj)
  415.             {
  416.                 if(obj.TOKEN !== null && obj.USERID !== null)
  417.                 {
  418.                     if(obj.Results !== null && obj.Results.length > 0)
  419.                     {
  420.                         var _show = obj.Results.splice(0, 20);
  421.                        
  422.                         var table = new TABLE
  423.                         ({
  424.                             head: ['ID', 'Nom', 'Catégorie', 'Taille', 'Seeders', 'Leechers'],
  425.                             colWidths: [10, 120, 20, 20, 10, 10]
  426.                         });
  427.  
  428.                         for(var i = 0; i < _show.length; i++)
  429.                         {
  430.                             table.push([_show[i].id, _show[i].name, _show[i].categoryname, _show[i].size, _show[i].seeders, _show[i].leechers]);
  431.                         }
  432.  
  433.                         console.log("");
  434.                         console.log(table.toString());
  435.                         console.log("");
  436.                            
  437.                         obj.Emitter.emit("CommandFinished");
  438.                     }
  439.                     else
  440.                     {
  441.                         console.log("\r\nPlus de Résultats !".orange);
  442.                     }                  
  443.                 }
  444.                 else
  445.                 {
  446.                     console.log("\r\nVous devez d'abord vous logger !\r\n".red);
  447.                     obj.Emitter.emit("CommandFinished");
  448.                 }
  449.             }
  450.         },
  451.         {
  452.             name : "exit",
  453.             description : "Quitter le programme",
  454.             exec : function(obj)
  455.             {
  456.                 obj.Input.close();
  457.             }
  458.         },
  459.         {
  460.             name : "help",
  461.             description : "Affiche cet écran",
  462.             exec : function(obj)
  463.             {
  464.                 console.log("Liste des Commandes : " + "\r\n");
  465.                 for(var i = 0; i < obj.CommandList.length; i++)
  466.                 {
  467.                     console.log("\t" + obj.CommandList[i].name.yellow + "\t\t : ".yellow + obj.CommandList[i].description + "\r\n");
  468.                 }
  469.                 obj.Emitter.emit("CommandFinished");
  470.             }
  471.         }
  472.     ];
  473. };
  474. t411.prototype =
  475. {
  476.     ask : function()
  477.     {
  478.         var _self = this;
  479.          this.Input.question("T411 > ", function(answer)
  480.         {
  481.             _self.Emitter.emit("CommandSet", answer);
  482.         });
  483.     },
  484.     launch : function()
  485.     {
  486.         var _self = this;
  487.  
  488.         console.log("-------------------------------------------------");
  489.         console.log("Bienvenue sur T411".green);
  490.         console.log("Tapez \"help\" pour avoir la liste des commandes.".yellow);
  491.         console.log("-------------------------------------------------\r\n");
  492.  
  493.         this.Emitter.on("CommandSet", function(c)
  494.         {
  495.             _self.executeCommand(c);
  496.         });
  497.         this.Emitter.on("CommandFinished", function()
  498.         {
  499.             _self.ask();
  500.         });
  501.         this.Emitter.emit("CommandFinished");
  502.     },
  503.     executeCommand : function(c)
  504.     {
  505.         var found = false;
  506.         var command = c.toLowerCase().trim();
  507.  
  508.         for(var i = 0; i < this.CommandList.length; i++)
  509.         {
  510.             if(this.CommandList[i].name === command)
  511.             {
  512.                 this.CommandList[i].exec(this);
  513.                 found = true;
  514.                 break;
  515.             }
  516.         }
  517.  
  518.         if(!found)
  519.         {
  520.             console.log("\r\nCommande Inconnue\r\n".red);
  521.             this.Emitter.emit("CommandFinished");
  522.         }
  523.     },
  524.     hidden : function(query, input, callback)
  525.     {
  526.         var _self = this;
  527.         var stdin = process.openStdin(),
  528.         i = 0;
  529.         process.stdin.on("data", function(char)
  530.         {
  531.             if(_self.secure)
  532.             {
  533.                 char = char + "";
  534.                 switch (char)
  535.                 {
  536.                     case "\n":
  537.                     case "\r":
  538.                     case "\u0004":
  539.                         //stdin.pause();
  540.                         break;
  541.                     default:
  542.                         process.stdout.write("\033[2K\033[200D"+query+"["+((i%2==1)?"=-":"-=")+"]");
  543.                         i++;
  544.                         break;
  545.                 }
  546.             }
  547.         });
  548.  
  549.         var _self = this;
  550.         input.question(query, function(value)
  551.         {
  552.             input.history = input.history.slice(1);
  553.  
  554.             callback(value);
  555.         });
  556.     },
  557.     sendPostRequest : function(url, params, callback)
  558.     {
  559.         REQUEST
  560.         ({
  561.             rejectUnauthorized: false,
  562.             uri: url,
  563.             method: "POST",
  564.             form: params,
  565.             headers:
  566.             {
  567.                 'User-Agent': 'T411 CLI'
  568.             },
  569.         }, function(error, response, body)
  570.         {
  571.             callback(body);
  572.         });
  573.     },
  574.     sendGetRequest : function(url, callback)
  575.     {
  576.         var _self = this;
  577.         REQUEST
  578.         ({
  579.             rejectUnauthorized: false,
  580.             uri: url,
  581.             method: "GET",
  582.             headers:
  583.             {
  584.                 'User-Agent': 'T411 CLI',
  585.                 'Authorization': _self.TOKEN
  586.             },
  587.         }, function(error, response, body)
  588.         {
  589.             callback(body);
  590.         });
  591.     }
  592. };
  593.  
  594. var T411Command = new t411();
  595. T411Command.launch();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement