Skip to content

Commit

Permalink
Merge pull request #85 from srikanthlogic/reviews_fix
Browse files Browse the repository at this point in the history
Uptake scraper updates and fix reviews pagination
  • Loading branch information
srikanthlogic authored May 13, 2024
2 parents b151841 + 80f243e commit a0cc4ca
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
30 changes: 11 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ const router = Express.Router();
const toList = (apps) => ({ results: apps });

router.use((req, res, next) => {

if (!req.query.country) {
req.query.country = process.env.COUNTRY_OF_QUERY || 'US';;
req.query.country = process.env.COUNTRY_OF_QUERY || 'US';
}

if (process.env.LOGGING || false) {
console.log("GPlayAPI", req.url, req.baseUrl, req.params, "Status: ", res.statusCode)
console.log("GPlayAPI", req.url, req.baseUrl, req.params, "Status: ", res.statusCode);
}

next();
});


const cleanUrls = (req) => (app) => Object.assign({}, app, {
playstoreUrl: app.url,
url: buildUrl(req, 'apps/' + app.appId),
Expand Down Expand Up @@ -75,7 +73,7 @@ router.get('/apps/', function (req, res, next) {

/* App list */
router.get('/apps/', function (req, res, next) {
function paginate(apps) {
function paginate (apps) {
const num = parseInt(req.query.num || '60');
const start = parseInt(req.query.start || '0');

Expand Down Expand Up @@ -138,15 +136,16 @@ router.get('/apps/:appId/permissions', function (req, res, next) {

/* App reviews */
router.get('/apps/:appId/reviews', function (req, res, next) {
const opts = Object.assign({ appId: req.params.appId, country: req.query.country }, req.query);
const opts = Object.assign({ appId: req.params.appId, country: req.query.country, num: parseInt(req.query.num) || 100 }, req.query);

const includeUserData = req.query.userdata === 'true';
const includeReplies = req.query.replies == 'true'
const includeReplies = req.query.replies == 'true';

const processReviews = (reviews, includeUserData, includeReplies) => {
const sanitizeReplyText = (text, userName) => {
const userNameParts = userName.split(' ');

function escapeRegExp(string) {
function escapeRegExp (string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

Expand Down Expand Up @@ -178,8 +177,7 @@ router.get('/apps/:appId/reviews', function (req, res, next) {
rest.date = rest.date.split('T')[0];
return rest;
});
}
else {
} else {
reviews.data = reviews.data.map(review => {
const { url, ...rest } = review;
rest.date = rest.date.split('T')[0];
Expand All @@ -190,12 +188,6 @@ router.get('/apps/:appId/reviews', function (req, res, next) {
return reviews;
};

// Check if nextPaginationToken is present and not null
if (req.query.nextPaginationToken !== null) {
opts.paginate = true;
opts.nextPaginationToken = req.query.nextPaginationToken;
}

// Sort criteria - Helpful, Most rated, and Newest reviews
// https://github.com/facundoolano/google-play-scraper/blob/89202849f6054f6ac64790a385abc3c18ae98df1/lib/constants.js#L69C7-L69C7
opts.sort = req.query.sort === 'helpful' ? 1 : req.query.sort === 'rated' ? 3 : 2;
Expand Down Expand Up @@ -259,12 +251,12 @@ router.get('/collections/', async (req, res, next) => {
}
});

function anonymizeReplyText(replyText, userName) {
function anonymizeReplyText (replyText, userName) {
const userPlaceholder = '<<USER>>';
return replyText.replace(new RegExp(userName, 'g'), userPlaceholder);
}

function errorHandler(err, req, res, next) {
function errorHandler (err, req, res, next) {
if (!res.headersSent) {
const status = err.message === "App not found (404)" ? 404 : 400;
res.status(status).json({ error: status === 404 ? "App not found" : "Bad Request", message: err.message, url: req.url });
Expand All @@ -274,4 +266,4 @@ function errorHandler(err, req, res, next) {

router.use(errorHandler);

export default router;
export default router;
34 changes: 21 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"dependencies": {
"express": "^4.19.2",
"google-play-scraper": "^10.0.0",
"google-play-scraper": "https://github.com/srikanthlogic/google-play-scraper.git#c0bdceca9288165f9af625363c621b901f506390",
"morgan": "^1.10.0",
"postman-to-openapi": "^3.0.1",
"swagger-ui-express": "^5.0.0"
Expand Down

0 comments on commit a0cc4ca

Please sign in to comment.