Skip to content

Commit

Permalink
feat: find latest stable version
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishqmanuja committed Apr 1, 2024
1 parent 01cdb52 commit a4d4826
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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}`);
Expand Down
20 changes: 15 additions & 5 deletions src/scrapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a4d4826

Please sign in to comment.