From 793fbd746ec640d884ff1fd859231404c31a05fd Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 29 Apr 2021 18:54:20 +0200 Subject: done :tada: --- index.ts | 78 ++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/index.ts b/index.ts index 2c95d06..44c7b66 100644 --- a/index.ts +++ b/index.ts @@ -4,55 +4,65 @@ import cheerio from 'cheerio'; import * as types from './types'; var browser = await puppeteer.launch(); -var page = await browser.newPage(); -await page.goto("https://optifine.net/downloads", { waitUntil: 'domcontentloaded' }); - -var html = await page.content(); +async function getPageHTML(url: string) { + var page = await browser.newPage(); + await page.goto(url, { waitUntil: 'domcontentloaded' }); + var content = await page.content(); + await page.close(); + return content +} -await browser.close(); +var html = await getPageHTML('https://optifine.net/downloads'); var $ = cheerio.load(html); var versions: types.OptifineVersion[] = []; -$("td.content span.downloads .downloadLine").each((i, el) => { - var downloadLinkPre = $(el).find(".colMirror a").attr("href"); +var downloads = $('td.content span.downloads .downloadLine'); +var downloadCount = downloads.length; +downloads.each(async (i, el) => { + var downloadLinkPre = $(el).find('.colMirror a').attr('href').replace("http", "https"); + var downloadPageHTML = await getPageHTML(downloadLinkPre); + var d$ = cheerio.load(downloadPageHTML); + var link = "https://optifine.net/" + d$("#Download .downloadButton a").attr("href"); var version: types.OptifineVersion = { name: $(el).find(".colFile").text(), minecraft: downloadLinkPre.match(/1\.\d{1,2}(\.\d{1,2})?/)[0], - forge: $(el).find(".colForge").text().toLowerCase().replace("forge", "").trim(), - date: $(el).find(".colDate").text(), - preview: downloadLinkPre.toLowerCase().includes("preview"), + forge: $(el).find('.colForge').text().toLowerCase().replace('forge', '').trim(), + date: $(el).find('.colDate').text(), + preview: downloadLinkPre.toLowerCase().includes('preview'), download: { - link: downloadLinkPre, - token: "", + link, + token: link.match(/(?<=x=).+/)[0], filename: downloadLinkPre.match(/OptiFine.+?\.jar/).toString() } } versions.push(version); -}); + if( i == downloadCount - 1 ) { + var minecraftVersions = {}; + var latest: types.OptifineVersion; + var latestPre: types.OptifineVersion; + for(var version of versions) { + if (typeof minecraftVersions[version.minecraft] === "undefined") + minecraftVersions[version.minecraft] = []; + minecraftVersions[version.minecraft].push(version); -var minecraftVersions = {}; -var latest: types.OptifineVersion; -var latestPre: types.OptifineVersion; -for(var version of versions) { - if (typeof minecraftVersions[version.minecraft] === "undefined") - minecraftVersions[version.minecraft] = []; - minecraftVersions[version.minecraft].push(version); - - if (!latest && version.preview == false) - latest = version; - if (!latestPre && version.preview == true) - latestPre = version; -} + if (!latest && version.preview == false) + latest = version; + if (!latestPre && version.preview == true) + latestPre = version; + } -var response: types.APIResponse = { - lastUpdate: Date.now(), - all: versions, - versions: minecraftVersions, - latest, - latestPre -} + var response: types.APIResponse = { + lastUpdate: Date.now(), + all: versions, + versions: minecraftVersions, + latest, + latestPre + } -console.log(JSON.stringify(response)); + console.log(JSON.stringify(response)); + process.exit(0); + } +}); -- cgit v1.2.3