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

Change setting method of periodic execution to cronjob style #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ ReviewMe requires a config file. A simple config looks something like:
* **botIcon** An image url to use for the bot avatar
* **showAppIcon** Determines if app icon will be displayed (overrides botIcon)
* **channel** Overrides the default Slack channel messages will be posted to
* **interval** The interval (in seconds) to check for new reviews. Default: `300`.
* **interval** The interval (in seconds) to check for new reviews. Default: 300.
* **cronStyleSchedule** CronJob style schedule to check for new reviews(Can be used instead of interval). Default: `00 * * * *`.
* **apps** A list of apps to fetch reviews for. See App Options below
* **publisherKey** *Android Only* The path to a Google Play Publisher private key (`.json` file). Used for accessing the Google Play Publisher API.

Expand Down
55 changes: 32 additions & 23 deletions appstorereviews.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const controller = require('./reviews');
const fs = require('fs');
var request = require('request');
var schedule = require('node-schedule');
require('./constants');

exports.startReview = function (config, first_run) {

if (config.regions === false){
if (config.regions === false) {
try {
config.regions = JSON.parse(fs.readFileSync(__dirname + '/regions.json'));
config.regions = JSON.parse(fs.readFileSync(__dirname + '/regions.json'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe leaving the styling thing along will be better when implementing some functions? Otherwise the diff will including so many changes not related to the topic, make the review and code tracing a little bit harder.

It will be very great if the styling will be fixed with CI testing in another PR 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so too. This PR undo unnecessary diffs 🙇

} catch (err) {
config.regions = ["us"];
}
Expand All @@ -16,16 +17,16 @@ exports.startReview = function (config, first_run) {
config.regions = ["us"];
}

if (!config.interval) {
config.interval = DEFAULT_INTERVAL_SECONDS
if (!config.schedule) {
config.schedule = DEFAULT_INTERVAL_SECONDS;
}

// Find the app information to get a icon URL
exports.fetchAppInformation(config, function (globalAppInformation) {
for (var i = 0; i < config.regions.length; i++) {
const region = config.regions[i];

const appInformation = Object.assign({},globalAppInformation);
const appInformation = Object.assign({}, globalAppInformation);
appInformation.region = region;

exports.fetchAppStoreReviews(config, appInformation, function (reviews) {
Expand All @@ -49,23 +50,31 @@ exports.startReview = function (config, first_run) {
exports.handleFetchedAppStoreReviews(config, appInformation, reviews);
}

//calculate the interval with an offset, to avoid spamming the server
var interval_seconds = config.interval + (i * 10);
// calculate the interval with an offset, to avoid spamming the server
var interval_seconds = typeof config.schedule === 'number' ? config.schedule + (i * 10) : i * 10;

setInterval(function (config, appInformation) {
var fetch = function (config, appInformation) {
if (config.verbose) console.log("INFO: [" + config.appId + "] Fetching App Store reviews");

exports.fetchAppStoreReviews(config, appInformation, function (reviews) {
exports.handleFetchedAppStoreReviews(config, appInformation, reviews);
});
}, interval_seconds * 1000, config, appInformation);
};

if (typeof config.schedule === 'number') {
setInterval(fetch, interval_seconds * 1000, config, appInformation);
} else {
schedule.scheduleJob(config.schedule, function () {
setTimeout(fetch, interval_seconds * 1000, config, appInformation);
});
}
});
}
});
};

var fetchAppStoreReviewsByPage = function(config, appInformation, page, callback){
const url = "https://itunes.apple.com/" + appInformation.region + "/rss/customerreviews/page="+page+"/id=" + config.appId + "/sortBy=mostRecent/json";
var fetchAppStoreReviewsByPage = function (config, appInformation, page, callback) {
const url = "https://itunes.apple.com/" + appInformation.region + "/rss/customerreviews/page=" + page + "/id=" + config.appId + "/sortBy=mostRecent/json";

request(url, function (error, response, body) {
if (error) {
Expand All @@ -80,7 +89,7 @@ var fetchAppStoreReviewsByPage = function(config, appInformation, page, callback
var rss;
try {
rss = JSON.parse(body);
} catch(e) {
} catch (e) {
console.error("Error parsing app store reviews");
console.error(e);

Expand All @@ -99,13 +108,13 @@ var fetchAppStoreReviewsByPage = function(config, appInformation, page, callback
if (config.verbose) console.log("INFO: Received reviews from App Store for (" + config.appId + ") (" + appInformation.region + ")");

var reviews = entries
.filter(function (review) {
return !isAppInformationEntry(review)
})
.reverse()
.map(function (review) {
return exports.parseAppStoreReview(review, config, appInformation);
});
.filter(function (review) {
return !isAppInformationEntry(review)
})
.reverse()
.map(function (review) {
return exports.parseAppStoreReview(review, config, appInformation);
});

callback(reviews)
});
Expand All @@ -114,9 +123,9 @@ var fetchAppStoreReviewsByPage = function(config, appInformation, page, callback
exports.fetchAppStoreReviews = function (config, appInformation, callback) {
var page = 1;
var allReviews = [];
function pageCallback(reviews){
function pageCallback(reviews) {
allReviews = allReviews.concat(reviews);
if (reviews.length > 0 && page < 10){
if (reviews.length > 0 && page < 10) {
page++;
fetchAppStoreReviewsByPage(config, appInformation, page, pageCallback);
} else {
Expand Down Expand Up @@ -198,7 +207,7 @@ exports.fetchAppInformation = function (config, callback) {
var data;
try {
data = JSON.parse(body);
} catch(e) {
} catch (e) {
console.error("Error parsing app store data");
console.error(e);

Expand All @@ -220,7 +229,7 @@ exports.fetchAppInformation = function (config, callback) {
appInformation.appName = entry.trackCensoredName;
}

if (!config.appIcon && entry.artworkUrl100 ) {
if (!config.appIcon && entry.artworkUrl100) {
appInformation.appIcon = entry.artworkUrl100;
}

Expand Down
19 changes: 13 additions & 6 deletions googleplayreviews.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const controller = require('./reviews');
var {google} = require('googleapis');
var { google } = require('googleapis');

var playScraper = require('google-play-scraper');
var androidVersions = require('android-versions')
Expand All @@ -8,7 +8,7 @@ exports.startReview = function (config, first_run) {
var appInformation = {};

//scrape Google Play for app information first
playScraper.app({appId: config.appId})
playScraper.app({ appId: config.appId })
.then(function (appData, error) {
if (error) {
return console.error("ERROR: [" + config.appId + "] Could not scrape Google Play, " + error);
Expand Down Expand Up @@ -37,17 +37,24 @@ exports.startReview = function (config, first_run) {
exports.handleFetchedGooglePlayReviews(config, appInformation, reviews);
}

var interval_seconds = config.interval ? config.interval : DEFAULT_INTERVAL_SECONDS;
if (!config.schedule) {
config.schedule = DEFAULT_INTERVAL_SECONDS;
}

setInterval(function (config, appInformation) {
var fetch = 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);
});
}

if (typeof config.schedule === 'number') {
setInterval(fetch, config.schedule * 1000, config, appInformation);
} else {
schedule.scheduleJob(config.schedule, fetch(config, appInformation));
}
});
});
};

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports.start = function start(config) {
slackHook: config.slackHook,
verbose: config.verbose,
dryRun: config.dryRun,
interval: config.interval,
schedule: config.interval || config.cronStyleSchedule,
botIcon: app.botIcon || config.botIcon,
showAppIcon: app.showAppIcon || config.showAppIcon,
channel: app.channel || config.channel,
Expand Down
77 changes: 70 additions & 7 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"commander": "^4.1.1",
"google-play-scraper": "^8.0.2",
"googleapis": "^52.1.0",
"node-schedule": "^1.3.2",
"request": "^2.88.0"
}
}