Skip to content

Commit

Permalink
feat: Downloading of APKs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Dec 9, 2017
1 parent 73709b0 commit bf8674e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict'

const request = require('request')
const debug = require('debug')
const log = debug('apkmirror-client')

module.exports = {
searchForApps: (query, cb) =>
get('https://www.apkmirror.com/?post_type=app_release&searchtype=app&s=' + encodeURI(query), {parser: 'appSearch'}, cb),
Expand All @@ -9,8 +13,6 @@ module.exports = {
get(app, {parser: 'appReleasePage'}, cb),
appVariantPage: (app, cb) =>
get(app, {parser: 'appVariantPage'}, cb),
downloadAPK: (app, cb) =>
get(app, {parser: 'appDownloadPage'}, cb),
estimateBestCandidate: (list, arch) => {
const orig = list
if (arch == 'x86_64') arch = ['x64']
Expand Down Expand Up @@ -51,6 +53,13 @@ module.exports = {

const res = list.pop()
return res ? orig.filter(i => i.id === res.id)[0] : false
},
downloadAPK: (url, cb) => {
get(url, {parser: 'appDownloadPage'}, (err, url) => {
if (err) return cb(err)
log('download apk from %s', url)
cb(null, request.get(url))
})
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/parser/appDownloadPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

module.exports = ($, window, cb) => {
cb(null, $('a[rel=nofollow]')[0].href)
}
2 changes: 1 addition & 1 deletion src/parser/appReleasePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cell = [
{key: 'date', value: cleanText(e.find('.dateyear_utc').text())},
{key: 'id', value: cleanText(e.find('a').eq(0).text())},
{key: 'url', value: e.find('a').eq(0)[0].href},
{key: 'downloadAPK', value: get.appVariantPage.bind(null, e.find('a').eq(1).text())}
{key: 'loadVariant', value: get.appVariantPage.bind(null, e.find('a').eq(0)[0].href)}
],
e => [ // arch
{key: 'arch', value: cleanText(e.text())},
Expand Down
4 changes: 3 additions & 1 deletion src/parser/appVariantPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const appPageBasic = require('./appPageBasic')
const cleanText = s => s.replace(/\n/g, '').trim()
const get = require('..')

function getMatches (string, regex, index) {
index = index || [1] // default to the first capturing group
Expand All @@ -18,7 +19,7 @@ module.exports = ($, window, cb) => {
res.changelog = $('.notes').eq(1).text()
res.download = $('.downloadButton')[0].href
const [ver, size, aver, dpi] = $('.appspec-value').toArray()
console.log(ver)
cleanText($(ver).text()) // TODO: use this element
res.dpi = cleanText($(dpi).text())
res.url = window.location.href
res.size = cleanText($(size).text())
Expand All @@ -32,6 +33,7 @@ module.exports = ($, window, cb) => {
a[b[0]] = b[1]
return a
}, {})
res.downloadAPK = get.downloadAPK.bind(null, res.download)

cb(null, res)
}
5 changes: 4 additions & 1 deletion src/parser/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const request = require('request')

module.exports = {
appSearch: require('./appSearch'),
appPage: require('./appPage'),
appReleasePage: require('./appReleasePage'),
appVariantPage: require('./appVariantPage')
appVariantPage: require('./appVariantPage'),
appDownloadPage: require('./appDownloadPage')
}
27 changes: 26 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('search', () => {
})
})

it('should get latest non-beta ver for whatsapp', cb => {
it('should get latest non-beta release for whatsapp', cb => {
wa.latest.loadRelease((err, res) => {
expect(err).to.not.exist()
// console.log(res, res.estimateBestCandidate('arm64'))
Expand All @@ -60,4 +60,29 @@ describe('search', () => {
cb()
})
})

it('should get the variant of that release', cb => {
wa.dl.loadVariant((err, res) => {
expect(err).to.not.exist()
// console.log(res)
wa.dlfinal = res
expect(res.play.url).to.equal('https://play.google.com/store/apps/details?id=com.whatsapp')
expect(res.play.id).to.equal('com.whatsapp')
expect(res.play.category).to.equal('Communication')
expect(res.play.categoryUrl).to.equal('https://www.apkmirror.com/categories/communication/')
expect(res.app.name).to.equal(wa.app.name)
expect(res.app.url).to.equal(wa.app.url)
expect(res.dev.name).to.equal(wa.dev.name)
expect(res.dev.url).to.equal(wa.dev.url)
cb()
})
})

it('should download the apk', cb => {
wa.dlfinal.downloadAPK((err, apk) => {
expect(err).to.not.exist()
apk.pipe(require('fs').createWriteStream(require('path').join(require('os').tmpdir(), Math.random().toString())))
apk.once('data', () => cb())
})
})
})

0 comments on commit bf8674e

Please sign in to comment.