move downloads, and user-agent to config.json

This commit is contained in:
2025-05-06 23:40:03 -07:00
parent d41d3d812d
commit cb62bfd3e0
2 changed files with 12 additions and 8 deletions

View File

@ -1,4 +1,8 @@
{ {
"global": {
"useragent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36",
"downloadPath": "downloads"
},
"urls": [ "urls": [
"https://github.com/McPlugin/SoCool", "https://github.com/McPlugin/SoCool",
], ],

View File

@ -60,20 +60,20 @@ if (sftpConfig.password && !sftpConfig.encryptedPassword) {
const ensureDir = (dir) => { const ensureDir = (dir) => {
if (!fs.existsSync(dir)) fs.mkdirSync(dir); if (!fs.existsSync(dir)) fs.mkdirSync(dir);
}; };
const DOWNLOAD_PATH = config.global.downloadPath || "downloads";
const isDownloaded = (filename) => fs.existsSync(path.join("downloads", filename)); const isDownloaded = (filename) => fs.existsSync(path.join(DOWNLOAD_PATH, filename));
const downloadJar = async (url, name) => { const downloadJar = async (url, name) => {
if (isDownloaded(name)) { if (isDownloaded(name)) {
console.log(`🟡 Skipped (already exists): ${name}`); console.log(`🟡 Skipped (already exists): ${name}`);
return; return;
} }
const USER_AGENT = config.global?.userAgent || "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36";
const response = await axios.get(url, { const response = await axios.get(url, {
responseType: "stream", responseType: "stream",
headers: { headers: {
"User-Agent": "User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36", USER_AGENT,
}, },
}); });
@ -84,7 +84,7 @@ const downloadJar = async (url, name) => {
response.data.on("data", (chunk) => bar.increment(chunk.length)); response.data.on("data", (chunk) => bar.increment(chunk.length));
response.data.on("end", () => bar.stop()); response.data.on("end", () => bar.stop());
const filePath = path.join("downloads", name); const filePath = path.join(DOWNLOAD_PATH, name);
await pipeline(response.data, fs.createWriteStream(filePath)); await pipeline(response.data, fs.createWriteStream(filePath));
console.log(`✔️ Saved: ${name}`); console.log(`✔️ Saved: ${name}`);
}; };
@ -245,7 +245,7 @@ const uploadToSFTP = async () => {
const sftp = new SftpClient(); const sftp = new SftpClient();
const remote = sftpConfig.remotePath || "/"; const remote = sftpConfig.remotePath || "/";
const files = fs.readdirSync("downloads").filter((f) => f.endsWith(".jar")); const files = fs.readdirSync(DOWNLOAD_PATH).filter((f) => f.endsWith(".jar"));
const connectOptions = { const connectOptions = {
host: sftpConfig.host, host: sftpConfig.host,
@ -264,7 +264,7 @@ const uploadToSFTP = async () => {
try { try {
await sftp.connect(connectOptions); await sftp.connect(connectOptions);
for (const file of files) { for (const file of files) {
const local = path.join("downloads", file); const local = path.join(DOWNLOAD_PATH, file);
const remoteFile = path.join(remote, file).replace(/\\/g, "/"); const remoteFile = path.join(remote, file).replace(/\\/g, "/");
console.log(`🚀 Uploading ${file}${remoteFile}`); console.log(`🚀 Uploading ${file}${remoteFile}`);
await sftp.put(local, remoteFile); await sftp.put(local, remoteFile);
@ -278,7 +278,7 @@ const uploadToSFTP = async () => {
// --- Main --- // --- Main ---
(async () => { (async () => {
ensureDir("downloads"); ensureDir(DOWNLOAD_PATH);
for (const url of config.urls) { for (const url of config.urls) {
console.log(`\n📥 ${url}`); console.log(`\n📥 ${url}`);