Browse Source

Optimize back-end search using async

master
KaKi87 3 weeks ago
parent
commit
a115823729
1 changed files with 14 additions and 29 deletions
  1. +14
    -29
      gplay.js

+ 14
- 29
gplay.js View File

@@ -1,33 +1,18 @@
const GPlay = require('google-play-scraper');
module.exports = {
/*
Google Play search using scraper API
*/
search: (query, callback) => {
// First request : get search results
GPlay.search({
term: query.name,
num: query.number,
price: query.price
}).then(res => {
// If no result, no additional request required
if(res.length === 0) return callback(res);
// Second request : get apps details
Promise.all(res.map(app => GPlay.app({ appId: app.appId })))
.then(apps => Promise.all(apps.map((app, index) => new Promise(resolve => {
// Index apps
app.index = index;
// Third request : get apps permissions
GPlay.permissions({ appId: app.appId }).then(permissions => {
app.permissions = permissions;
resolve();
}).catch(() => {
app.permissions = null;
resolve();
});
}))).then(() => callback(apps)));
})
.catch(err => console.log(err));
}
/*
Google Play search using scraper API
*/
search: async query => {
const res = await GPlay.search({ term: query.name, num: query.number, price: query.price });
for(let i = 0; i < res.length; i++){
Object.assign(res[i], {
index: i,
... (await GPlay.app({ appId: res[i].appId })),
... { permissions: (await GPlay.permissions({ appId: res[i].appId })) }
})
}
return res;
}
};

Loading…
Cancel
Save