From 625f88c63e623d9c990566bbe0d4da0dd03a8b8a Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 8 Jan 2018 15:01:10 +0100 Subject: [PATCH] node client.js manage [instancename] mods download [modname] --- client.js | 2 ++ lib/manager/modManager.js | 65 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/client.js b/client.js index db823519..5a376305 100644 --- a/client.js +++ b/client.js @@ -144,6 +144,8 @@ if (!command || command == "help" || command == "--help") { modManager.listMods(instance); } else if(action == "search"){ modManager.findMods(process.argv[6]); + } else if(action == "add" || action == "download"){ + modManager.addMod(process.argv[6], instance); } else if(action == "update"){ modManager.updateAllMods(); } else { diff --git a/lib/manager/modManager.js b/lib/manager/modManager.js index 48e6dbc4..8d4a6025 100644 --- a/lib/manager/modManager.js +++ b/lib/manager/modManager.js @@ -6,6 +6,7 @@ Provides tools to manage mods in a clusterio install const rightpad = require("right-pad"); const asTable = require("as-table").configure({delimiter: ' | '}); const moment = require("moment"); +const syncRequest = require("sync-request") const factorioAPI = require("node-factorio-api"); const fileOps = require("./../fileOps.js"); @@ -25,7 +26,7 @@ modManager.listMods("shared"); */ module.exports.listMods = function(instance){ if(instance && instances.includes(instance)){ - factorioAPI.init(false, 'instances/'+instance+'/instanceMods', 'instances/'+instance+'/saves', '0.15.37'); + factorioAPI.init(false, 'instances/'+instance+'/instanceMods', 'instances/'+instance+'/saves', '0.16.15'); console.log("Authenticating to factorio.com...") factorioAPI.authenticate({ username: config.username, @@ -56,7 +57,7 @@ module.exports.listMods = function(instance){ }); } else { // list shared mods in /sharedMods/ - factorioAPI.init(false, 'sharedMods/', '', '0.15.37'); + factorioAPI.init(false, 'sharedMods/', '', '0.16.15'); console.log("Authenticating to factorio.com...") factorioAPI.authenticate({ username: config.username, @@ -77,6 +78,66 @@ module.exports.listMods = function(instance){ console.log(JSON.parse(res.getBody())) */ } +module.exports.addMod = function(term, instance){ + if(instance == "shared"){ // modDirectory has to end in / + var modDirectory = "sharedMods/"; + } else { + var modDirectory = "instances/"+instance+"/instanceMods/"; + } + if(term && term.length > 0){ + if(term.includes("mods.factorio.com/mods/")){ // https://mods.factorio.com/mods/Danielv123/ChooChoo + // treat it as a link download + factorioAPI.init(false, modDirectory, "", "0.16.15"); + factorioAPI.authenticate({ + username: config.username, + token: config.token, + require_ownership: false + }).then(() => { + factorioAPI.searchMods( + {q:term.split("/")[term.split("/").length-1], order: 'top', page_size:1} + ).then(body => { + console.log("Im gonna download: "+body.results[0].name); + factorioAPI.downloadMod(body.results[0]).then(()=>{ + console.log("Mod downloaded"); + }); + }); + }); + } else if(term.includes("github.com")){ // https://github.com/Danielv123/factorioClusterioMod + // treat it as a github releases download + let apiURL = "https://api.github.com/repos/" + term.split("/")[3] + "/" + term.split("/")[4] + "/releases"; // https://api.github.com/repos/Danielv123/factorioClusterioMod/releases + + let res = syncRequest('GET', apiURL, {"headers":{"User-Agent":"Fuck you for requiring user agents!"}}); + let url = JSON.parse(res.getBody())[0].assets[0].browser_download_url; + let name = JSON.parse(res.getBody())[0].assets[0].name; + + if(url) { + console.log("Downloading " + url); + let file = fs.createWriteStream(modDirectory + name); + https.get(url, function(response) { + response.pipe(file); + console.log("Downloaded "+name); + }); + } + } else { + // treat it as a search and download the top result + factorioAPI.init(false, modDirectory, "", "0.16.15"); + factorioAPI.authenticate({ + username: config.username, + token: config.token, + require_ownership: false + }).then(() => { + factorioAPI.searchMods( + {q:term, order: 'top', page_size:1} + ).then(body => { + console.log("Im gonna download: "+body.results[0].name); + factorioAPI.downloadMod(body.results[0]).then(()=>{ + console.log("Mod downloaded"); + }); + }); + }); + } + } +} /** Searches mods.factorio.com for mods matching a search term and prints the results to console as a neat looking table