-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathgoogleplayreviews.js
213 lines (166 loc) · 7.02 KB
/
googleplayreviews.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const controller = require('./reviews');
var {google} = require('googleapis');
var playScraper = require('google-play-scraper');
var androidVersions = require('android-versions')
exports.startReview = function (config, first_run) {
var appInformation = {};
//scrape Google Play for app information first
playScraper.app({appId: config.appId})
.then(function (appData, error) {
if (error) {
return console.error("ERROR: [" + config.appId + "] Could not scrape Google Play, " + error);
}
appInformation.appName = appData.title;
appInformation.appIcon = appData.icon;
exports.fetchGooglePlayReviews(config, appInformation, function (reviews) {
// If we don't have any published reviews, then treat this as a baseline fetch, we won't post any
// reviews to slack, but new ones from now will be posted
if (first_run) {
var reviewLength = reviews.length;
for (var i = 0; i < reviewLength; i++) {
var initialReview = reviews[i];
controller.markReviewAsPublished(config, initialReview);
}
if (config.dryRun && reviews.length > 0) {
// Force publish a review if we're doing a dry run
publishReview(appInformation, config, reviews[reviews.length - 1], config.dryRun);
}
}
else {
exports.handleFetchedGooglePlayReviews(config, appInformation, reviews);
}
var interval_seconds = config.interval ? config.interval : DEFAULT_INTERVAL_SECONDS;
setInterval(function (config, appInformation) {
if (config.verbose) console.log("INFO: [" + config.appId + "] Fetching Google Play reviews");
exports.fetchGooglePlayReviews(config, appInformation, function (reviews) {
exports.handleFetchedGooglePlayReviews(config, appInformation, reviews);
});
}, interval_seconds * 1000, config, appInformation);
});
});
};
function publishReview(appInformation, config, review, force) {
if (!controller.reviewPublished(config, review) || force) {
if (config.verbose) console.log("INFO: Received new review: " + JSON.stringify(review));
var message = slackMessage(review, config, appInformation);
controller.postToSlack(message, config);
controller.markReviewAsPublished(config, review);
} else {
if (config.verbose) console.log("INFO: Review already published: " + review.text);
}
}
exports.handleFetchedGooglePlayReviews = function (config, appInformation, reviews) {
if (config.verbose) console.log("INFO: [" + config.appId + "] Handling fetched reviews");
for (var n = 0; n < reviews.length; n++) {
var review = reviews[n];
publishReview(appInformation, config, review, false)
}
};
exports.fetchGooglePlayReviews = function (config, appInformation, callback) {
if (config.verbose) console.log("INFO: Fetching Google Play reviews for " + config.appId);
const scopes = ['https://www.googleapis.com/auth/androidpublisher'];
//read publisher json key
var publisherJson;
if (typeof config.publisherKey === 'object') {
publisherJson = config.publisherKey;
} else {
try {
publisherJson = JSON.parse(require('fs').readFileSync(config.publisherKey, 'utf8'));
} catch (e) {
console.warn(e)
}
}
var jwt;
try {
jwt = new google.auth.JWT(publisherJson.client_id, null, publisherJson.private_key, scopes, null);
} catch (e) {
console.warn(e)
}
jwt.authorize(function (err, tokens) {
if (err) {
return console.log(err)
}
//get list of reviews using Google Play Publishing API
google.androidpublisher('v3').reviews.list({
auth: jwt,
packageName: config.appId
}, function (err, resp) {
if (err) {
return console.error("ERROR: [" + config.appId + "] Could not fetch Google Play reviews, " + err);
}
if (config.verbose) console.log("INFO: [" + config.appId + "] Received reviews from Google Play");
if (!resp.data.reviews) {
callback([]);
return;
}
var reviews = resp.data.reviews.map(function (review) {
var comment = review.comments[0].userComment;
var out = {};
out.id = review.reviewId;
out.author = review.authorName;
out.version = comment.appVersionName;
out.versionCode = comment.appVersionCode;
out.osVersion = comment.androidOsVersion;
if (comment.deviceMetadata) {
out.device = comment.deviceMetadata.productName;
}
out.text = comment.text;
out.rating = comment.starRating;
out.link = 'https://play.google.com/store/apps/details?id=' + config.appId + '&reviewId=' + review.reviewId;
out.storeName = "Google Play";
return out;
});
callback(reviews);
})
});
};
var slackMessage = function (review, config, appInformation) {
if (config.verbose) console.log("INFO: Creating message for review " + review.title);
var stars = "";
for (var i = 0; i < 5; i++) {
stars += i < review.rating ? "★" : "☆";
}
var color = review.rating >= 4 ? "good" : (review.rating >= 2 ? "warning" : "danger");
var text = "";
text += review.text + "\n";
var footer = "";
if (review.version) {
footer += " for v" + review.version + ' (' + review.versionCode + ') ';
}
if (review.osVersion) {
footer += ' Android ' + getVersionNameForCode(review.osVersion)
}
if (review.device) {
footer += ', ' + review.device
}
if (review.link) {
footer += " - " + "<" + review.link + "|" + appInformation.appName + ", " + review.storeName + ">";
} else {
footer += " - " + appInformation.appName + ", " + review.storeName;
}
var title = stars;
if (review.title) {
title = title + " – " + review.title;
}
return {
"channel": config.channel,
"attachments": [
{
"mrkdwn_in": ["text", "pretext", "title", "footer"],
"color": color,
"author_name": review.author,
"thumb_url": config.showAppIcon ? appInformation.appIcon : config.botIcon,
"title": title,
"text": text,
"footer": footer
}
]
};
};
var getVersionNameForCode = function (versionCode) {
var version = androidVersions.get(versionCode);
if (version != null) {
return version.semver;
}
return "";
};