Skip to content

Commit

Permalink
Reverted changes on processFullDetailApps and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbigman committed Aug 11, 2023
1 parent 205a87c commit 3984345
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ Once that limit is reached, further requests will be held until the second passe
import gplay from "google-play-scraper";

// the following method will perform batches of 10 requests per second
gplay.search({term: 'panda', throttle: 10}).then(console.log);
gplay.search({term: 'panda', throttle: {limit: 10, interval: 1000}}).then(console.log);
```

By default, no throttling is applied.
36 changes: 15 additions & 21 deletions lib/utils/processPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,18 @@ async function processPages (html, opts, savedApps, mappings) {
}

async function processFullDetailApps (apps, opts) {
const getAppDetails = (app) => appDetails({
appId: app.appId,
lang: opts.lang,
country: opts.country,
cache: opts.cache,
throttle: opts.throttle,
requestOptions: opts.requestOptions
});

if (opts.throttle !== undefined) {
const throttle = pThrottle({
limit: opts.throttle.limit ? opts.throttle.limit : 1,
interval: opts.throttle.interval ? opts.throttle.interval : 1000
});
return Promise.all(apps.map(await throttle(getAppDetails)));
} else {
return Promise.all(apps.map(getAppDetails));
}
const promises = apps.map(app => (
appDetails({
appId: app.appId,
lang: opts.lang,
country: opts.country,
cache: opts.cache,
throttle: opts.throttle,
requestOptions: opts.requestOptions
})
));

return Promise.all(promises);
}

const REQUEST_MAPPINGS = {
Expand Down Expand Up @@ -89,9 +83,9 @@ function checkFinished (opts, savedApps, nextToken) {
}

function getBodyForRequests ({
numberOfApps = 100,
withToken = '%token%'
}) {
numberOfApps = 100,

Check failure on line 86 in lib/utils/processPages.js

View workflow job for this annotation

GitHub Actions / checks (16.x)

Expected indentation of 2 spaces but found 31

Check failure on line 86 in lib/utils/processPages.js

View workflow job for this annotation

GitHub Actions / checks (18.x)

Expected indentation of 2 spaces but found 31

Check failure on line 86 in lib/utils/processPages.js

View workflow job for this annotation

GitHub Actions / checks (20.x)

Expected indentation of 2 spaces but found 31
withToken = '%token%'

Check failure on line 87 in lib/utils/processPages.js

View workflow job for this annotation

GitHub Actions / checks (16.x)

Expected indentation of 2 spaces but found 31

Check failure on line 87 in lib/utils/processPages.js

View workflow job for this annotation

GitHub Actions / checks (18.x)

Expected indentation of 2 spaces but found 31

Check failure on line 87 in lib/utils/processPages.js

View workflow job for this annotation

GitHub Actions / checks (20.x)

Expected indentation of 2 spaces but found 31
}) {

Check failure on line 88 in lib/utils/processPages.js

View workflow job for this annotation

GitHub Actions / checks (16.x)

Expected indentation of 0 spaces but found 29

Check failure on line 88 in lib/utils/processPages.js

View workflow job for this annotation

GitHub Actions / checks (18.x)

Expected indentation of 0 spaces but found 29

Check failure on line 88 in lib/utils/processPages.js

View workflow job for this annotation

GitHub Actions / checks (20.x)

Expected indentation of 0 spaces but found 29
const body = `f.req=%5B%5B%5B%22qnKhOb%22%2C%22%5B%5Bnull%2C%5B%5B10%2C%5B10%2C${numberOfApps}%5D%5D%2Ctrue%2Cnull%2C%5B96%2C27%2C4%2C8%2C57%2C30%2C110%2C79%2C11%2C16%2C49%2C1%2C3%2C9%2C12%2C104%2C55%2C56%2C51%2C10%2C34%2C77%5D%5D%2Cnull%2C%5C%22${withToken}%5C%22%5D%5D%22%2Cnull%2C%22generic%22%5D%5D%5D`;

return body;
Expand Down
113 changes: 93 additions & 20 deletions test/lib.search.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,54 @@ describe('Search method', () => {
// preregister tend to have some fields missing, increasing chances of failure
// by searching "preregister" we have more chances of getting some in the results
it('should search for pre register', () =>
gplay.search({ term: 'preregister', num: 10 })
gplay.search({
term: 'preregister',
num: 10
})
.then((apps) => apps.map(assertValidApp)));

it('should fetch multiple pages of distinct results', () =>
gplay.search({ term: 'p', num: 55 })
gplay.search({
term: 'p',
num: 55
})
.then((apps) => {
assert.equal(apps.length, 55, 'should return as many apps as requested');
}));

it('should fetch multiple pages of when not starting from cluster of subsections', () =>
gplay.search({ term: 'p', num: 65 })
gplay.search({
term: 'p',
num: 65
})
.then((apps) => {
assert.equal(apps.length, 65, 'should return as many apps as requested');
}));

describe('country and language specific', () => {
describe('without more results section', () => {
it('should fetch a valid application list for eu country', () => {
return gplay.search({ term: 'Panda vs Zombies', country: 'GH' })
return gplay.search({
term: 'Panda vs Zombies',
country: 'GH'
})
.then((apps) => apps.map(assertValidApp));
});

it('should fetch a valid application list for non eu country', () => {
return gplay.search({ term: 'Facebook', country: 'GE' })
return gplay.search({
term: 'Facebook',
country: 'GE'
})
.then((apps) => apps.map(assertValidApp));
});

it('should fetch a valid application list for eu country with specific language', () => {
return gplay.search({ term: 'Panda vs Zombies', country: 'BE', lang: 'it' })
return gplay.search({
term: 'Panda vs Zombies',
country: 'BE',
lang: 'it'
})
.then((apps) => apps.map(assertValidApp));
});
});
Expand All @@ -67,7 +86,11 @@ describe('Search method', () => {
});

it('should return few netflix apps from german store with german language', () => {
return gplay.search({ term: 'netflix', lang: 'de', country: 'DE' })
return gplay.search({
term: 'netflix',
lang: 'de',
country: 'DE'
})
.then((apps) => {
assert.equal(apps[0].appId, 'com.netflix.mediaclient');
// Don't check specific ids, as results may vary
Expand All @@ -94,12 +117,20 @@ describe('Search method', () => {
});

it('should return empty set when no results found in eu country store', () => {
return gplay.search({ term: 'ASyyDASDyyASDASD', country: 'DE', lang: 'SP' })
return gplay.search({
term: 'ASyyDASDyyASDASD',
country: 'DE',
lang: 'SP'
})
.then(assert.isEmpty);
});

it('should return empty set when no results found in us store with other language', () => {
return gplay.search({ term: 'ASyyDASDyyASDASD', country: 'US', lang: 'FR' })
return gplay.search({
term: 'ASyyDASDyyASDASD',
country: 'US',
lang: 'FR'
})
.then(assert.isEmpty);
});
});
Expand All @@ -114,7 +145,10 @@ describe('Search method', () => {
});

it('should return apps from suggested search in european country', () => {
return gplay.search({ term: 'runing tracker', country: 'GR' })
return gplay.search({
term: 'runing tracker',
country: 'GR'
})
.then((apps) => {
apps.map(assertValidApp);
assertIdsInArray(apps, 'com.runtastic.android', 'running.tracker.gps.map');
Expand All @@ -124,25 +158,26 @@ describe('Search method', () => {

describe('search with full details', () => {
it('should search for "runing app" with fullDetail', () => {
const options = {
term: 'preregister',
num: 10,
fullDetail: true
};
gplay.search(options).then((apps) =>
apps.map(assertValidApp)
);
}
const options = {

Check failure on line 161 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (16.x)

Expected indentation of 6 spaces but found 8

Check failure on line 161 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (18.x)

Expected indentation of 6 spaces but found 8

Check failure on line 161 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (20.x)

Expected indentation of 6 spaces but found 8
term: 'preregister',

Check failure on line 162 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (16.x)

Expected indentation of 8 spaces but found 10

Check failure on line 162 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (18.x)

Expected indentation of 8 spaces but found 10

Check failure on line 162 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (20.x)

Expected indentation of 8 spaces but found 10
num: 10,

Check failure on line 163 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (16.x)

Expected indentation of 8 spaces but found 10

Check failure on line 163 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (18.x)

Expected indentation of 8 spaces but found 10

Check failure on line 163 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (20.x)

Expected indentation of 8 spaces but found 10
fullDetail: true

Check failure on line 164 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (16.x)

Expected indentation of 8 spaces but found 10

Check failure on line 164 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (18.x)

Expected indentation of 8 spaces but found 10

Check failure on line 164 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (20.x)

Expected indentation of 8 spaces but found 10
};

Check failure on line 165 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (16.x)

Expected indentation of 6 spaces but found 8

Check failure on line 165 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (18.x)

Expected indentation of 6 spaces but found 8

Check failure on line 165 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (20.x)

Expected indentation of 6 spaces but found 8
gplay.search(options).then((apps) =>

Check failure on line 166 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (16.x)

Expected indentation of 6 spaces but found 8

Check failure on line 166 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (18.x)

Expected indentation of 6 spaces but found 8

Check failure on line 166 in test/lib.search.js

View workflow job for this annotation

GitHub Actions / checks (20.x)

Expected indentation of 6 spaces but found 8
apps.map(assertValidApp)
);
}
);

it('should search for "runing app" with fullDetail and throttling interval', async () => {
it('should search for "runing app" with fullDetail and throttling limit + interval', async () => {
const dateBefore = new Date();
const throttle = {
limit: 1,
interval: 500
};
return gplay.search({
term: 'runing app',
num: 5,
fullDetail: true,
throttle
})
Expand All @@ -153,5 +188,43 @@ describe('Search method', () => {
assert.isAbove(interval, apps.length * throttle.interval);
});
}).timeout(10000);

it('should search for "runing app" with fullDetail and throttling limit', async () => {
const dateBefore = new Date();
const throttle = {
limit: 1,
};
return gplay.search({
term: 'runing app',
num: 5,
fullDetail: true,
throttle
})
.then((apps) => {
apps.map(assertValidApp);
const dateAfter = new Date();
const interval = dateAfter - dateBefore;
assert.isBelow(interval, 5000);
});
}).timeout(10000);

it('should search for "runing app" with fullDetail and throttling interval', async () => {
const dateBefore = new Date();
const throttle = {
interval: 500
};
return gplay.search({
term: 'runing app',
num: 5,
fullDetail: true,
throttle
})
.then((apps) => {
apps.map(assertValidApp);
const dateAfter = new Date();
const interval = dateAfter - dateBefore;
assert.isBelow(interval, 5000);
});
}).timeout(10000);
});
});

0 comments on commit 3984345

Please sign in to comment.