Advertisement
Thatguy5532

Untitled

May 12th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Express Vars
  2. const express = require('express');
  3. const app = express();
  4. const port = 3000;
  5.  
  6. app.use(express.json());       // to support JSON-encoded bodies
  7. app.use(express.urlencoded()); // to support URL-encoded bodies
  8.  
  9. // Node Vars
  10. const fs = require("fs");
  11.  
  12. // Local imports
  13. const { fetchURL } = require('./fetch');
  14. const { steam_key, app_id, results } = require('./config.json');
  15. const { download } = require('./mod_manager');
  16.  
  17. // Make sure config vars are filled out
  18. if (!steam_key || !app_id) {
  19.     console.log("You need to fill out either your Steam Key or your App ID in the config.json");
  20.     return;
  21. }
  22.  
  23. // Express Routing
  24. app.get('/mods', async (req, res) => {
  25.     let html = await createHTML();
  26.     res.send(html);
  27. })
  28.  
  29. // Take a look into callback functions
  30. app.post('/download', async (req, res) => {
  31.     console.log(req.body);
  32.     await dl_status({published_file_id: req.body.file_id, name: req.body.name, updated: req.body.updated}, res, display_status);
  33.    
  34. })
  35.  
  36. app.listen(port, () => {
  37.   console.log(`Example app listening on port ${port}`)
  38. })
  39.  
  40. // Local Functions
  41. async function createHTML() {
  42.     let details = await fetchURL(steam_key, 'https://api.steampowered.com/IPublishedFileService/QueryFiles/v1/?', app_id, results, 1);
  43.     let template = function(title, image, description, link, updated) {
  44.         let idIndex = link.indexOf("?id=");
  45.         return `<div>` +
  46.                     `<a href='${link}'>` +
  47.                        `<img src='${image}' alt='' width='250px'>` +
  48.                         `<p>${title}</p>` +
  49.                     `</a>` +
  50.                     `<form action='/download' method='post'>` +
  51.                         `<input type='hidden' name='name' value='${title}'>` +
  52.                         `<input type='hidden' name='updated' value='${updated}'>` +
  53.                         `<input type='hidden' name='file_id' value='${link.substring(idIndex + 4, link.length)}'>` +
  54.                         `<input type='submit' value='Download' "> ` +
  55.                    `</form>` +
  56.                `</div>`
  57.  
  58.    }
  59.  
  60.    let html = "";
  61.    for (const file in details.response.publishedfiledetails) {
  62.        const element = details.response.publishedfiledetails[file];
  63.        
  64.        html += template(
  65.            element.title,
  66.            element.preview_url,
  67.            element.short_description,
  68.            `https://steamcommunity.com/sharedfiles/filedetails/?id=${element.publishedfileid}`,
  69.            element.time_updated
  70.        )
  71.    }
  72.    return html;
  73. }
  74.  
  75. async function dl_status({published_file_id, name, updated}, res, _callback) {
  76.    console.log("Promise Created...");
  77.    let dl_promise = await new Promise(async (resolve, reject) => {
  78.        let code = download({published_file_id: published_file_id, name: name, updated: updated});
  79.        if (code != null) {
  80.            resolve(code);
  81.        } else {
  82.            reject("Error");
  83.        }
  84.    }).then((value) => {
  85.        console.log("Value:");
  86.        console.log(value);
  87.        _callback(value, res);
  88.    }).catch((error) => {
  89.        console.log("Error:");
  90.        console.log(error);
  91.    });
  92. }
  93.  
  94. async function display_status(code, res) {
  95.    if (code == -1) {
  96.        res.send("Strange error, please report what you did on github or to Thatguy553!");
  97.    } else if (code == -2) {
  98.        res.send("Download Failed, make sure you have internet and SteamCMD downloaded with the path to the executable in the config.json");
  99.    } else if (code == -3) {
  100.        res.send("Download Successful, Couldnt move the mod into game folder. Check SteamCMD/steamapps/common/content/game_id to move the mod manually.");
  101.    } else if (code == -4) {
  102.        res.send("Download Successful, Couldnt move the mod into game folder. This mod already exists in your game folder, instead move to update tab.");
  103.    } else if (code == 1) {
  104.        res.send("Download and Move successful, check your mod folder to confirm!");
  105.    } else if (code == 0) {
  106.        res.send("Strange error, please report what you did on github or to Thatguy553");
  107.    }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement