From afdef2a029668c3beed5a53b0935683679c0230b Mon Sep 17 00:00:00 2001 From: Sampson Date: Thu, 8 Aug 2024 13:04:38 -0500 Subject: [PATCH] dynamically resolves comments path fixes https://github.com/facundoolano/google-play-scraper/issues/682 Revert "dynamically resolves comments path" This reverts commit 5f0546ba7216e1510c9a101b3f932266fef8daa0. Improves comment extraction Comments may be found in either ds:8 or ds:9 paths. This approach will check for expected fields in both paths before determine which path houses the comments themselves. --- lib/app.js | 2 +- lib/utils/mappingHelpers.js | 38 ++++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/app.js b/lib/app.js index 9208b086..c778f82a 100644 --- a/lib/app.js +++ b/lib/app.js @@ -160,7 +160,7 @@ const MAPPINGS = { }, recentChanges: ['ds:5', 1, 2, 144, 1, 1], comments: { - path: ['ds:8', 0], + path: [], isArray: true, fun: helper.extractComments }, diff --git a/lib/utils/mappingHelpers.js b/lib/utils/mappingHelpers.js index d74cff15..ca816e32 100644 --- a/lib/utils/mappingHelpers.js +++ b/lib/utils/mappingHelpers.js @@ -47,9 +47,41 @@ function buildHistogram (container) { * Extract the comments from google play script array * @param {array} comments The comments array */ -function extractComments (comments) { - if (!comments) return []; - return comments.map(R.path([4])).slice(0, 5); +function extractComments (data) { + /** + * Comments have been found to migrate between two + * paths: ds:8 and ds:9. For this reason, we'll check + * for expected fields in both paths to determine + * the correct path to use. + */ + let comments = []; + + for (const path of ['ds:8', 'ds:9']) { + const authorPath = [path, 0, 0, 1, 0]; + const versionPath = [path, 0, 0, 10]; + const datePath = [path, 0, 0, 5, 0]; + + if (R.path(authorPath, data)) { + if (R.path(versionPath, data)) { + if (R.path(datePath, data)) { + /** + * If we have found all expected fields, then + * we will dump the original comments structure + * into the `comments` variable for further + * handling. + */ + comments = R.path([path, 0], data); + break; + } + } + } + } + + if (comments.length > 0) { + comments = comments.map(R.path([4])).slice(0, 5); + } + + return comments; } function extractFeatures (featuresArray) {