-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
similar.js
37 lines (33 loc) · 1 KB
/
similar.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
'use strict';
const app = require('./app');
const BASE_URL = 'https://itunes.apple.com/us/app/app/id';
const common = require('./common.js');
function similar (opts) {
return new Promise(function (resolve, reject) {
if (opts.id) {
resolve(opts.id);
} else if (opts.appId) {
app(opts).then((app) => resolve(app.id)).catch(reject);
} else {
throw Error('Either id or appId is required');
}
})
.then((id) => common.request(
`${BASE_URL}${id}`,
{
'X-Apple-Store-Front': `${common.storeId(opts.country)},32`
},
opts.requestOptions
))
.then(function (text) {
const index = text.indexOf('customersAlsoBoughtApps');
if (index === -1) {
return [];
}
const regExp = /customersAlsoBoughtApps":(.*?\])/g;
const match = regExp.exec(text);
const ids = JSON.parse(match[1]);
return common.lookup(ids, 'id', opts.country, opts.lang, opts.requestOptions, opts.throttle);
});
}
module.exports = similar;