Skip to content

Commit

Permalink
feat: Automatically fetch latest tgtg version on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
unixfox committed Jan 2, 2025
1 parent 22053c9 commit a13e366
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lib/toogoodtogo-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import got from "got";
import { CookieJar } from "tough-cookie";
import { config } from "./config.js";

let tgtgVersion = "24.12.0";

await refreshLatestTgtgVersion();

const api = got.extend({
cookieJar: new CookieJar(),
prefixUrl: "https://apptoogoodtogo.com/api/",
headers: _.defaults(config.get("api.headers"), {
"User-Agent":
"TGTG/24.6.1 Dalvik/2.1.0 (Linux; Android 12; SM-G920V Build/MMB29K)",
`TGTG/${tgtgVersion} Dalvik/2.1.0 (Linux; Android 12; SM-G920V Build/MMB29K)`,
"Content-Type": "application/json; charset=utf-8",
Accept: "application/json",
"Accept-Language": "en-US",
Expand All @@ -22,6 +26,7 @@ const api = got.extend({
statusCodes: [401, 403, 408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
},
});

export function authByEmail() {
const credentials = config.get("api.credentials");

Expand Down Expand Up @@ -103,3 +108,30 @@ function updateSession(token) {
config.set("api.session.accessToken", token.access_token);
return token;
}

async function refreshLatestTgtgVersion() {
try {
const gplayResponse = await got.get("https://play.google.com/store/apps/details?id=com.app.tgtg", {
resolveBodyOnly: true
})
const candidateVersions = [];
const matchesInitDataCallback = gplayResponse.matchAll(
/<script class=\"\S+\" nonce=\"\S+\">AF_initDataCallback\((.*?)\);/g,
);
for (const match of matchesInitDataCallback) {
const matchesVersion = match[1].matchAll(/(\d+\.\d+\.\d+)/g);
for (const match of matchesVersion) {
candidateVersions.push(match[1])
}
}
tgtgVersion = candidateVersions.reduce((a, b) =>
0 < a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' })
? a
: b
)
} catch (error) {
console.log(
`Error while retrieving latest version of TGTG on Google Play page: \n${error}`
)
}
}

0 comments on commit a13e366

Please sign in to comment.