Added cron tweets for active fires in 'es' and 'pt'

This commit is contained in:
vjrj 2018-03-17 20:06:00 +01:00
parent c99559d442
commit 0c56bcf7e0
9 changed files with 252 additions and 3 deletions

View file

@ -0,0 +1,64 @@
/* eslint-disable import/no-absolute-path */
import { whichAreFalsePositives, firesUnion } from '/imports/api/FalsePositives/server/publications';
import FalsePositives from '/imports/api/FalsePositives/FalsePositives';
import Industries from '/imports/api/Industries/Industries';
import ActiveFires from '../ActiveFires';
const cleanProv = (prov, stringsToRemove) => {
let lprov = prov;
stringsToRemove.forEach((st) => {
lprov = lprov.replace(st, '');
});
return lprov;
};
const findFiresInRegion = (zone) => {
const result = ActiveFires.find({
ourid: {
$geoWithin: {
$geometry: zone.geometry
}
}
});
/* console.log(`False positive total: ${falsePos.count()}`);
console.log(`False positives: ${JSON.stringify(falsePos.fetch())}`); */
return result;
};
const countFires = (regions, stringsToRemove) => {
let total = 0;
const fireStats = {};
regions.features.forEach((region) => {
const regionName = cleanProv(region.properties.name, stringsToRemove);
try {
const fires = findFiresInRegion(region);
// TODO Also check neighbour fires (when better implementation of that part)
let count = fires.count();
if (count > 0) {
/* console.log(regionName);
* console.log(count); */
fires.forEach((fire) => {
const union = firesUnion([fire]);
const falsePos = whichAreFalsePositives(FalsePositives, union);
const industries = whichAreFalsePositives(Industries, union);
if (falsePos.count() > 0 || industries.count() > 0) {
count -= 1;
}
});
// console.log(count);
if (count > 0) {
total += count;
fireStats[regionName] = count;
}
}
// console.log(countFiresInRegion(prov));
} catch (e) {
console.log(e);
}
});
return { total, fires: fireStats };
};
export default countFires;

View file

@ -0,0 +1,68 @@
/* eslint-disable import/no-absolute-path */
import getPrivateFile from '/imports/modules/server/get-private-file';
import { Meteor } from 'meteor/meteor';
import tbuffer from '@turf/buffer';
import tweet from '/imports/modules/server/twitter';
import countFires from './countFires';
const autonomies = tbuffer(JSON.parse(getPrivateFile('data/es-atlas-autonomies.json')), 0);
const portugal = tbuffer(JSON.parse(getPrivateFile('data/pt-atlas.json')), 0);
const stringsToRemove = [
'Ciudad Autónoma de',
'País Vasco/',
'Comunidad Foral de',
'Región de',
'Comunidad de',
'Cataluña/',
'Castilla-',
'Principado de',
' '
];
const composeTweet = (num, stats) => {
if (num <= 0) {
return '';
}
const keys = Object.keys(stats);
const numZones = keys.length;
let text = '';
keys.map((key, index) => { // , index
const zoneFires = stats[key];
const zoneFiresText = zoneFires > 1 ? index === 0 ? zoneFires : `otros ${zoneFires}` : index === 0 ? 'Un' : 'otro';
const what = index > 0 ? ' ' : zoneFires > 1 ? ' fuegos activos ' : ' fuego activo ';
const subText = `${zoneFiresText}${what}en #${key}`;
const delimiter = index === numZones - 1 ? ' y' : ',';
const subTexts = index === 0 ? subText : `${delimiter} ${subText}`;
text += subTexts;
return '';
});
return `${text}`;
};
const tweetHeaders = ['🔥 '];
const tweetFooters = ['Más info en: https://fuegos.comunes.org/fires'];
const tweetFires = () => {
const resultEs = countFires(autonomies, stringsToRemove);
const resultPt = countFires(portugal, stringsToRemove);
const fires = Object.assign(resultEs.fires, resultPt.fires);
const total = resultEs.total + resultPt.total;
// console.log(`Total fires ${total}, ${JSON.stringify(fires)}`);
if (total > 0) {
const tweetText = `${tweetHeaders[0]} ${composeTweet(total, fires)}. ${tweetFooters[0]}`;
if (Meteor.isDevelopment) {
console.log(tweet);
} else {
tweet(tweetText, 'es');
}
}
};
if (Meteor.isDevelopment) {
tweetFires();
}
export { composeTweet, tweetHeaders, tweetFooters, tweetFires };

View file

@ -18,7 +18,8 @@ Meteor.publish('falsePositivesTotal', function total() {
export const firesUnion = (fires) => {
const group = new L.FeatureGroup();
const remap = fires.fetch().map(function remap(doc) {
const firesArray = Array.isArray(fires) ? fires : fires.fetch(); // if not is a cursor
const remap = firesArray.map(function remap(doc) {
// default scan: 1 for neightbor alerts
return { location: { lat: doc.lat, lon: doc.lon }, distance: doc.scan || 1 };
});

View file

@ -1,6 +1,8 @@
/* global SyncedCron */
/* eslint-disable import/no-absolute-path */
import { Meteor } from 'meteor/meteor';
import { tweetFires } from '/imports/api/ActiveFires/server/tweetFiresInZone';
// https://github.com/thesaucecode/meteor-synced-cron/
@ -42,13 +44,19 @@ Meteor.startup(() => {
// context: {
// userID: 'xyz'
// },
schedule: parser =>
schedule: (parser) => {
// this.magic = true; // Context is accesible here as this context.
// parser is a later.parse object
// return parser.text('every 2 minutes');
// http://bunkat.github.io/later/
parser.text('at 9:15 am also at 15:15 pm also at 22:00 pm'),
const sched = parser.text(Meteor.settings.private.twitter.es.when);
if (sched.error !== -1) {
console.error(`Twitter cron 'when' field parsed with errors: ${sched.error}`);
}
return sched;
},
job: () => {
tweetFires();
// console.log('cron is working');
/* console.log(this.userID) // Context Object becomes this argument
* console.log(this.magic) /