From a4d4826ebd0712db1763d8b67168049357278fd5 Mon Sep 17 00:00:00 2001 From: tanishqmanuja Date: Mon, 1 Apr 2024 17:35:22 +0530 Subject: [PATCH] feat: find latest stable version --- src/index.ts | 9 +++++++-- src/scrapping.ts | 20 +++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1a163c9..481b5f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,11 @@ import { existsSync } from "fs"; import minimist from "minimist"; import ora from "ora-classic"; -import { getDownloadUrl, getLatestVersion, getVariants } from "./scrapping"; +import { + getDownloadUrl, + getStableLatestVersion, + getVariants, +} from "./scrapping"; import { downloadAPK, waitForKeypressExit } from "./utils"; import { FALLBACK_CONFIG, parseConfig, type Config } from "./config"; @@ -29,7 +33,8 @@ async function processConfig(config: Config) { const p = await Promise.allSettled( apps.map(async (app) => { - app.version = app.version ?? (await getLatestVersion(app.org, app.repo)); + app.version = + app.version ?? (await getStableLatestVersion(app.org, app.repo)); if (!app.version) { throw new Error(`Could not find a version for ${app.org}/${app.repo}`); diff --git a/src/scrapping.ts b/src/scrapping.ts index 2641f0f..a85d071 100644 --- a/src/scrapping.ts +++ b/src/scrapping.ts @@ -39,18 +39,28 @@ function extractVersion(input: string) { return match ? match[0] : undefined; } -export async function getLatestVersion(org: string, repo: string) { +export async function getStableLatestVersion(org: string, repo: string) { const apkmUrl = `https://www.apkmirror.com/apk/${org}/${repo}`; const response = await fetch(apkmUrl); const html = await response.text(); const $ = cheerio.load(html); - const version = $( - `#primary > div.listWidget.p-relative > div:nth-child(2) > div.appRow > div > div:nth-child(2) > div > h5 > a` - ); + const versions = $( + `#primary > div.listWidget.p-relative > div > div.appRow > div > div:nth-child(2) > div > h5 > a` + ) + .toArray() + .map((v) => $(v).text()); + + const stableVersion = versions.filter( + (v) => !v.includes("alpha") && !v.includes("beta") + )[0]; + + if (!stableVersion) { + throw new Error("Could not find stable version"); + } - return extractVersion(version.text()); + return extractVersion(stableVersion); } export async function getDownloadUrl(downloadPageUrl: string) {