This code is a script that downloads assets (files) from...

August 18, 2025 at 02:46 AM

globalThis.settings = { baseUrl: "https://g.igroutka.ru/games/674/7t2Hjn6bRDQk0vKg/e044eb6f-f352-465d-8301-940faec48b5e/", assetList: `index.html bundle.js global.css 6f8f0452819061523e4e7afde89cfb11.png`.split("\n"), outputDir: "TempleRun2FrozenFestival" }; (async function() { const fs = require("fs"); const axios = require("axios"); const user_agent = require("user-agents"); const path = require("path"); const set = settings; const UserAgent = new user_agent().toString(); const origin = new URL(set.baseUrl).origin; const dn = path.join(__dirname, set.outputDir || "output"); if (!set) throw new TypeError("Settings is required and must be an object"); if (!Array.isArray(set.assetList)) throw new TypeError("assetList is required and must be an array"); // Get directory path function getDirectoryPath(assetPath) { return path.dirname(assetPath); } // Create directory if it does not exist function createDirectory(dir) { const fullPath = path.join(dn, dir); if (!fs.existsSync(fullPath)) { fs.mkdirSync(fullPath, { recursive: true }); } } for (let i = 0; i < set.assetList.length; i++) { const asset = set.assetList[i]; const assetDecoded = decodeURIComponent(asset); try { const assetFileName = path.basename(assetDecoded); const progress = ((i + 1) / set.assetList.length * 100).toFixed(2); const response = await axios.get(set.baseUrl + asset, { headers: { "User-Agent": UserAgent, referrer: origin }, responseType: "stream" }); if (response.data) { createDirectory(getDirectoryPath(assetDecoded)); response.data.pipe(fs.createWriteStream(decodeURIComponent(path.join(dn, assetDecoded)))); console.log(`${progress}% - Downloaded: ${assetFileName}`); } else { console.error("No response for:", assetFileName); } } catch (error) { console.error("Error occurred:", error.response ? "HTTP Status " + error.response.status : error.message, "Filename:", assetDecoded); } } console.log("Download has been finished."); })();

This code is a script that downloads assets (files) from a remote server based on a list of asset filenames provided in settings.assetList. Here's a detailed breakdown of what it does:

  1. Global Configuration Settings:

    • The settings object is defined in globalThis. It contains:
      • baseUrl: The base URL where the files will be downloaded from.
      • assetList: A list of filenames (separated by line breaks) representing files that need to be downloaded. The split("\n") converts the string into an array.
      • outputDir: The directory name where the downloaded files will be stored locally.
  2. Modules Required:

    • fs: For file system operations (e.g., directory creation and file writing).
    • axios: For performing HTTP GET requests to fetch assets from the baseUrl.
    • user-agents: For generating a random browser-like User-Agent string for HTTP requests.
    • path: For handling and constructing file paths.
  3. Initialization:

    • Retrieves the settings object into the set variable.
    • Constructs the output directory path using __dirname and set.outputDir (or defaults to "output" if outputDir is not specified).
    • Generates a random User-Agent string and determines the origin of the baseUrl.
  4. Error Handling:

    • Ensures that settings.assetList exists and is an array. If not, it throws a TypeError.
  5. Helper Functions:

    • getDirectoryPath(assetPath): Returns the directory path based on a file's path.
    • createDirectory(dir): Checks if a directory exists and creates it recursively if it does not.
  6. Downloading Assets:

    • Iterates over the settings.assetList array.
    • For each asset:
      • Decodes the asset's URL-encoded filename.
      • Constructs the complete download URL by appending the asset to the baseUrl.
    • Sends a GET request using axios with headers including:
      • A randomly generated User-Agent.
      • referrer set to the base URL's origin.
    • If the server responds with a stream of file data:
      • Creates any necessary directories using createDirectory() and getDirectoryPath().
      • Pipes the response data (file content) to a file in the local output directory.
      • Outputs the download progress to the console in percentage format.
    • Handles errors during download and outputs relevant debug information.
      • Displays HTTP status codes (if applicable) or the error message.
  7. Completion:

    • After all assets in settings.assetList are processed, logs "Download has been finished." to the console.

Summary:

The script downloads files listed in settings.assetList from the server specified by settings.baseUrl and saves them in the directory specified by settings.outputDir (defaulting to "output"). It creates directories if they don't already exist, tracks download progress, and handles errors gracefully.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node