Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.js - app search and app reviews functions #89

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 119 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,82 @@ router.get('/', (req, res) =>
categories: buildUrl(req, 'categories')
}));

/* App search */
router.get('/apps/', function (req, res, next) {
/* App search */

/*
- The code addresses the limitation of Google Play Store and API only displaying 30 results by performing additional searches based on initial app titles.
- Merges the results of each secondary search into an object (arr) to prevent duplicate entries.
- Allows for retrieving more apps beyond the 30-app limit imposed by the direct search.
*/

router.get('/apps/', async function (req, res, next) {
if (!req.query.q) {
return next();
}

const opts = Object.assign({ term: req.query.q }, req.query);

gplay.search(opts)
.then((apps) => apps.map(cleanUrls(req)))
.then(toList)
.then(res.json.bind(res))
.catch(next);
try {
const searchTermMore = async (term, arr = {}) => {
if (!term) {
console.error("Error: search term is undefined");
return;
}

let total = 0;
let count = 0;

console.log("searchTermMore called with term:", term);

try {
const apps = await gplay.search({
term: term,
num: 20,
});

total = apps.length;

if (apps.length === 0) {
console.log(`No apps found for term: ${term}`);
return Object.values(arr);
}

for (let app of apps) {
try {
const apps2 = await gplay.search({
term: app.title,
num: 100,
});

for (let app2 of apps2) {
if (!(app2.appId in arr)) {
arr[app2.appId] = app2;
}
}
} catch (err) {
console.error(`Error occurred during subterm search for ${app.title}:`, err);
}
count++;
}

if (count < total) {
console.log(`Not all apps for term: ${term} have been processed, calling searchTermMore again`);
return await searchTermMore(term, arr);
} else {
return Object.values(arr);
}
} catch (err) {
console.error("Error occurred during search:", err);
throw err;
}
};

const apps = await gplay.search(opts);
const appList = await searchTermMore(opts.term, apps);
res.json(appList);
} catch (error) {
next(error);
}
});

/* Search suggest */
Expand Down Expand Up @@ -131,11 +194,54 @@ router.get('/apps/:appId/permissions', function (req, res, next) {
});

/* App reviews */
router.get('/apps/:appId/reviews', function (req, res, next) {
function paginate(apps) {

/*
- Fetches app reviews based on app ID.
- Efficiently handles pagination using nextPaginationToken.
- Formats response with pagination links and handles errors appropriately.
- Enhances functionality, performance, and usability of the endpoint.
*/

router.get('/apps/:appId/reviews', async function (req, res, next) {
try {
const appId = req.params.appId;

const options = {
appId: appId,
lang: 'en',
country: 'us',
paginate: true,
};

let reviews = [];
let nextToken;

do {
if (nextToken) {
options.nextPaginationToken = nextToken;
}

const result = await gplay.reviews(options);
const newData = result.data || [];

if (!newData.length) {
break;
}

reviews = reviews.concat(newData);
nextToken = result.nextPaginationToken;
} while (nextToken);

if (!reviews.length) {
return res.status(404).json({ error: 'No reviews found for app' });
}

console.log(`Received ${reviews.length} reviews for app: ${appId}`);

const page = parseInt(req.query.page || '0');
const apps = toList(reviews);

const subpath = '/apps/' + req.params.appId + '/reviews/';
const subpath = '/apps/' + appId + '/reviews/';
if (page > 0) {
req.query.page = page - 1;
apps.prev = buildUrl(req, subpath) + '?' + qs.stringify(req.query);
Expand All @@ -146,15 +252,10 @@ router.get('/apps/:appId/reviews', function (req, res, next) {
apps.next = buildUrl(req, subpath) + '?' + qs.stringify(req.query);
}

return apps;
res.json(apps);
} catch (err) {
next(err);
}

const opts = Object.assign({ appId: req.params.appId }, req.query);
gplay.reviews(opts)
.then(toList)
.then(paginate)
.then(res.json.bind(res))
.catch(next);
});

/* Apps by developer */
Expand Down