generated from ecelis/ianseo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from ecelis/feat/automate_fetch_release
Feat/automate fetch release
- Loading branch information
Showing
1,290 changed files
with
157,792 additions
and
13,669 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const https = require("https"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const unzipper = require("unzipper"); | ||
|
||
(async () => { | ||
const IANSEO_URL = "https://ianseo.net"; | ||
let html = null; | ||
|
||
const getHtml = () => { | ||
https | ||
.get(`${IANSEO_URL}/Releases.php`, (res) => { | ||
let data = []; | ||
console.log("Status Code:", res.statusCode); | ||
|
||
res.on("data", (chunk) => { | ||
data.push(chunk); | ||
}); | ||
|
||
res.on("end", async () => { | ||
html = Buffer.concat(data).toString(); | ||
const fileName = getFilename(html); | ||
console.log(fileName); | ||
if (fileName === null) { | ||
throw new Error("No file found"); | ||
} | ||
await downloadFile(fileName); | ||
try { | ||
await unzipFile( | ||
path.join(__dirname, "..", fileName), | ||
path.join(__dirname, "..", ".tmp") | ||
); | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
}); | ||
}) | ||
.on("error", (err) => { | ||
console.log("Error: ", err.message); | ||
}); | ||
}; | ||
|
||
const getFilename = (html) => { | ||
const regex = /Ianseo_\d{8}\.zip/g; | ||
const match = regex.exec(html); | ||
if (match.length === 0) { | ||
return null; | ||
} | ||
return match[0]; | ||
}; | ||
|
||
const downloadFile = async (fileName) => { | ||
const url = `${IANSEO_URL}/Release/${fileName}`; | ||
const filePath = path.join(__dirname, "..", fileName); | ||
|
||
return new Promise((resolve, reject) => { | ||
const file = fs.createWriteStream(filePath); | ||
https | ||
.get(url, (response) => { | ||
if (response.statusCode !== 200) { | ||
reject( | ||
new Error(`Failed to get '${url}' (${response.statusCode})`) | ||
); | ||
return; | ||
} | ||
|
||
response.pipe(file); | ||
|
||
file.on("finish", () => { | ||
file.close(resolve); | ||
}); | ||
|
||
file.on("error", (err) => { | ||
fs.unlink(filePath, () => reject(err)); | ||
}); | ||
}) | ||
.on("error", (err) => { | ||
fs.unlink(filePath, () => reject(err)); | ||
}); | ||
}); | ||
}; | ||
|
||
const unzipFile = async (zipFilePath, outputDir) => { | ||
return new Promise((resolve, reject) => { | ||
fs.createReadStream(zipFilePath) | ||
.pipe(unzipper.Extract({ path: outputDir })) | ||
.on("close", resolve) | ||
.on("error", reject); | ||
}); | ||
}; | ||
|
||
getHtml(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.