Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Express Vars
- const express = require('express');
- const app = express();
- const port = 3000;
- app.use(express.json()); // to support JSON-encoded bodies
- app.use(express.urlencoded()); // to support URL-encoded bodies
- // Node Vars
- const fs = require("fs");
- // Local imports
- const { fetchURL } = require('./fetch');
- const { steam_key, app_id, results } = require('./config.json');
- const { download } = require('./mod_manager');
- // Make sure config vars are filled out
- if (!steam_key || !app_id) {
- console.log("You need to fill out either your Steam Key or your App ID in the config.json");
- return;
- }
- // Express Routing
- app.get('/mods', async (req, res) => {
- let html = await createHTML();
- res.send(html);
- })
- // Take a look into callback functions
- app.post('/download', async (req, res) => {
- console.log(req.body);
- await dl_status({published_file_id: req.body.file_id, name: req.body.name, updated: req.body.updated}, res, display_status);
- })
- app.listen(port, () => {
- console.log(`Example app listening on port ${port}`)
- })
- // Local Functions
- async function createHTML() {
- let details = await fetchURL(steam_key, 'https://api.steampowered.com/IPublishedFileService/QueryFiles/v1/?', app_id, results, 1);
- let template = function(title, image, description, link, updated) {
- let idIndex = link.indexOf("?id=");
- return `<div>` +
- `<a href='${link}'>` +
- `<img src='${image}' alt='' width='250px'>` +
- `<p>${title}</p>` +
- `</a>` +
- `<form action='/download' method='post'>` +
- `<input type='hidden' name='name' value='${title}'>` +
- `<input type='hidden' name='updated' value='${updated}'>` +
- `<input type='hidden' name='file_id' value='${link.substring(idIndex + 4, link.length)}'>` +
- `<input type='submit' value='Download' "> ` +
- `</form>` +
- `</div>`
- }
- let html = "";
- for (const file in details.response.publishedfiledetails) {
- const element = details.response.publishedfiledetails[file];
- html += template(
- element.title,
- element.preview_url,
- element.short_description,
- `https://steamcommunity.com/sharedfiles/filedetails/?id=${element.publishedfileid}`,
- element.time_updated
- )
- }
- return html;
- }
- async function dl_status({published_file_id, name, updated}, res, _callback) {
- console.log("Promise Created...");
- let dl_promise = await new Promise(async (resolve, reject) => {
- let code = download({published_file_id: published_file_id, name: name, updated: updated});
- if (code != null) {
- resolve(code);
- } else {
- reject("Error");
- }
- }).then((value) => {
- console.log("Value:");
- console.log(value);
- _callback(value, res);
- }).catch((error) => {
- console.log("Error:");
- console.log(error);
- });
- }
- async function display_status(code, res) {
- if (code == -1) {
- res.send("Strange error, please report what you did on github or to Thatguy553!");
- } else if (code == -2) {
- res.send("Download Failed, make sure you have internet and SteamCMD downloaded with the path to the executable in the config.json");
- } else if (code == -3) {
- res.send("Download Successful, Couldnt move the mod into game folder. Check SteamCMD/steamapps/common/content/game_id to move the mod manually.");
- } else if (code == -4) {
- res.send("Download Successful, Couldnt move the mod into game folder. This mod already exists in your game folder, instead move to update tab.");
- } else if (code == 1) {
- res.send("Download and Move successful, check your mod folder to confirm!");
- } else if (code == 0) {
- res.send("Strange error, please report what you did on github or to Thatguy553");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement