Initial commit
This commit is contained in:
53
patch_detector/index.js
Normal file
53
patch_detector/index.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const { MongoClient, ServerApiVersion } = require("mongodb");
|
||||
|
||||
main()
|
||||
|
||||
async function main() {
|
||||
const client = await connectToDatabase();
|
||||
const newPatch = await fetchLatestPatch();
|
||||
const newDate = new Date()
|
||||
if(!(await compareLatestSavedPatch(client, newPatch, newDate))) {
|
||||
downloadAssets()
|
||||
}
|
||||
|
||||
await client.close()
|
||||
}
|
||||
|
||||
|
||||
async function fetchLatestPatch() {
|
||||
const url = "https://ddragon.leagueoflegends.com/api/versions.json"
|
||||
const patchDataResponse = await fetch(url);
|
||||
const patchData = await patchDataResponse.json();
|
||||
const patch = patchData[0];
|
||||
return patch;
|
||||
}
|
||||
|
||||
async function connectToDatabase() {
|
||||
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
|
||||
const client = new MongoClient(`mongodb://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@mongo:27017`, {
|
||||
serverApi: {
|
||||
version: ServerApiVersion.v1,
|
||||
strict: true,
|
||||
deprecationErrors: true,
|
||||
}
|
||||
})
|
||||
await client.connect()
|
||||
return client
|
||||
}
|
||||
|
||||
async function compareLatestSavedPatch(client, newPatch, newDate) {
|
||||
const database = client.db("patches")
|
||||
const patches = database.collection("patches")
|
||||
const latestPatch = await patches.find().limit(1).sort({date:-1}).next()
|
||||
|
||||
if(latestPatch.patch != newPatch) {
|
||||
await patches.insertOne({patch:newPatch, date:newDate})
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
async function downloadAssets() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user