Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var cfg = require("./tgcfg");
  2. const TOKEN = cfg.TOKEN;
  3.  
  4. var api = require("node-telegram-bot-api");
  5. var fs = require("fs");
  6. var request = require("request");
  7. request = request.defaults({ jar: true });
  8. var config = require("./config");
  9. var ibf = require("isbinaryfile");
  10. //var mirror = require("./mirror");
  11. //var sleep = require("sleep");
  12. //var san = require("sanitize-filename");
  13.  
  14. var bot = new api(TOKEN, {polling: true});
  15.  
  16. bot.onText(/\/log(|@osumirrorbot) (chat|dump)/i, (msg, match) => {
  17.     if(cfg.admin == msg.from.id) {
  18.         if(match[2] == "dump") {
  19.             bot.sendMessage(msg.chat.id, "Sending useless message as response at dump logger.");
  20.         }
  21.         console.log("Got log in " + msg.chat.id + ", " + msg.chat.title + " with data " + match[2]);
  22.         console.log(msg);
  23.     }
  24.     else {
  25.         bot.sendMessage(msg.chat.id, "Sorry, you are not premitted to use /log command");
  26.         console.log(msg.from.id + ", " + msg.from.username + " in " + msg.chat.title + " not premitted to logging");
  27.     }
  28. });
  29.  
  30. bot.onText(/^\/get(|@osumirrorbot) (https:\/\/osu.ppy.sh\/(d|s)\/(\d+$))/i, (msg, match) => {
  31.     var loginObject = {
  32.       username: config.user,
  33.       password: config.password,
  34.       autologin: "on",
  35.       sid: "",
  36.       redirect: "index.php",
  37.       viewonline: "off",
  38.       login: "Login"
  39.     };
  40.     var filename;
  41.     var apilink = "https://osu.ppy.sh/api/get_beatmaps?k=" + config.apiKey + "&b=" + match[4];
  42.     try {
  43.     request(apilink, (err, resp, body) => {
  44.         console.log(body);
  45.         body = JSON.parse(body);
  46.         console.log(body[0].beatmap_id);
  47.         filename = body[0].beatmap_id + ".osz";
  48.     });
  49.     }
  50.     catch (e) {
  51.         bot.sendMessage(msg.chat.id, "404!");
  52.     }
  53.     bot.sendMessage(msg.chat.id, msg.from.username + ", starting download, this may take a while");
  54.     request.post({ url: "https://osu.ppy.sh/forum/ucp.php?mode=login", form: loginObject }, (err,httpResponse,body) => {
  55.         if (err === null)
  56.             console.log("Page downloading success!");
  57.         else
  58.             bot.sendMessage(msg.chat.id, "Sorry, something went wrong");
  59.         var gotl = match[2];
  60.         gotl[19] = "d";
  61.         var needl = gotl + "n";
  62.        
  63.  
  64.         request(needl, function() {
  65.             console.log("done. checking download worked...");
  66.             console.log(filename);
  67.             if (!ibf("./" + filename)) {
  68.                 console.log("Something went wrong. The test beatmap isn't a binary file. Perhaps you wrote the wrong password?");
  69.             }
  70.             bot.sendMessage(msg.chat.id, msg.from.username + ", looks like map downloaded, sending");
  71.             var file = String(filename);
  72.             bot.sendDocument(msg.chat.id, String(file)).then(() => {
  73.                 bot.sendMessage(msg.chat.id, "Here is your file");
  74.                 fs.unlink(filename);
  75.             }, (err) => {
  76.                 bot.sendMessage(msg.chat.id, "Sorry, I can't send you file");
  77.             });
  78.             console.log("Sending..."); 
  79.         }).pipe(fs.createWriteStream(filename));
  80.        
  81.     });
  82. });
  83. process.stdin.resume();
  84.  
  85. process.on('SIGINT', () => {
  86.     console.log("\nGot SIGINT. Exiting...");
  87.     process.exit();
  88. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement