-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapkpure.js
76 lines (63 loc) · 2.25 KB
/
apkpure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import cheerio from 'cheerio';
import { wrap } from './cache.js';
const apkPureBaseURL = 'https://apkpure.com';
async function fetchAppSearchResults(appId) {
return wrap(`apkpure_url:${appId}`, async () => {
const url = `${apkPureBaseURL}/search?q=${appId}`;
const response = await fetch(url);
const html = await response.text();
return html;
}, 60 * 60 * 24);
}
async function findAppURL(appId) {
const searchBody = await fetchAppSearchResults(appId);
const $ = cheerio.load(searchBody);
const appURL = $('p.search-title > a').attr('href');
return appURL;
}
async function fetchAppDetailsPage(appId) {
const appURL = await findAppURL(appId);
if (!appURL) {
return undefined;
}
return wrap(`apkpure_detail:${appId}`, async () => {
const response = await fetch(`${apkPureBaseURL}${appURL}`);
const html = await response.text();
return html;
}, 60 * 60);
}
export async function getAppDetails(appId) {
const body = await fetchAppDetailsPage(appId);
if (!body) {
return undefined;
}
const $ = cheerio.load(body);
const appName = $('div.title-like:first > h1').text();
const appVersion = $('span[itemprop="version"]:first').text();
const appPublishDate = $('p[itemprop="datePublished"]:first').text();
const updateText = $('div#whatsnew:first').text();
const icon = $('div.icon:first > img').attr('src');
let size = $('span.fsize:first > span').text();
const updateDate = new Date(Date.UTC(
Number(appPublishDate.substring(0, 4)),
Number(appPublishDate.substring(5, 7)) - 1,
Number(appPublishDate.substring(8, 10)),
));
if (size.indexOf('MB') !== -1) {
size = Math.floor(Number(size.substring(0, size.indexOf('MB'))) * 1024 * 1024);
} else if (size.indexOf('KB') !== -1) {
size = Math.floor(Number(size.substring(0, size.indexOf('KB'))) * 1024);
} else {
size = Math.floor(Number(size)) || 0;
}
return {
id: appId,
version: appVersion.trim(),
updated: updateDate,
changelog: updateText,
size: size,
name: appName,
icon: icon,
url: `https://play.google.com/store/apps/details?id=${appId}`,
};
}