53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { MongoClient } from "mongodb";
|
|
|
|
main()
|
|
|
|
async function main() {
|
|
const client = await connectToDatabase()
|
|
const newPatch = await fetchLatestPatch()
|
|
|
|
console.log("Latest patch is: " + newPatch)
|
|
|
|
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 : string = 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}@${process.env.MONGO_HOST}`)
|
|
await client.connect()
|
|
return client
|
|
}
|
|
|
|
async function compareLatestSavedPatch(client: MongoClient, newPatch : string, newDate : Date) {
|
|
const database = client.db("patches")
|
|
const patches = database.collection("patches")
|
|
const latestPatch = await patches.find().limit(1).sort({date:-1}).next()
|
|
|
|
console.log("Latest patch in database is: " + latestPatch.patch)
|
|
|
|
if(latestPatch.patch != newPatch) {
|
|
await patches.insertOne({patch:newPatch, date:newDate})
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
async function downloadAssets() {
|
|
|
|
}
|