Added EU fire stats tweets

This commit is contained in:
vjrj 2018-03-22 17:42:13 +01:00
parent 452f6baf45
commit 1eb6e45dfe
5 changed files with 80 additions and 21 deletions

View file

@ -8,7 +8,9 @@ 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 = [
const europe = tbuffer(JSON.parse(getPrivateFile('data/europe-atlas.json')), 0);
const stringsToRemoveIberia = [
['Ciudad Autónoma de', ''],
['País Vasco/', ''],
['Pais Vasco', 'Euskadi'],
@ -28,7 +30,27 @@ const stringsToRemove = [
[' ', '']
];
const composeTweet = (num, stats) => {
const composeEuropeTweet = (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 : `${zoneFires}` : index === 0 ? 'One' : '1';
const what = index > 0 ? ' ' : zoneFires > 1 ? ' active #wildfires ' : ' active #wildfire ';
const subText = `${zoneFiresText}${what}in #${key}`;
const delimiter = index === numZones - 1 ? ' and' : ',';
const subTexts = index === 0 ? subText : `${delimiter} ${subText}`;
text += subTexts;
return '';
});
return `${text}`;
};
const composeIberiaTweet = (num, stats) => {
if (num <= 0) {
return '';
}
@ -38,7 +60,7 @@ const composeTweet = (num, stats) => {
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 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}`;
@ -51,16 +73,30 @@ const composeTweet = (num, stats) => {
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 tweetEuropeFires = () => {
const resultEu = countFires(europe, [[' ', '']]);
if (resultEu.total > 0) {
const tweetText = `${tweetHeaders[0].trim()} ${composeEuropeTweet(resultEu.total, resultEu.fires)}. More info: https://fires.comunes.org/fires`;
if (Meteor.isDevelopment) {
console.log(tweetText);
} else {
tweet(tweetText, 'en');
}
return tweetText;
}
};
const tweetIberiaFires = () => {
const resultEs = countFires(autonomies, stringsToRemoveIberia);
const resultPt = countFires(portugal, stringsToRemoveIberia);
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].trim()} ${composeTweet(total, fires)}. ${tweetFooters[0]}`;
const tweetText = `${tweetHeaders[0].trim()} ${composeIberiaTweet(total, fires)}. ${tweetFooters[0]}`;
if (Meteor.isDevelopment) {
console.log(tweetText);
} else {
@ -72,7 +108,8 @@ const tweetFires = () => {
};
if (Meteor.isDevelopment) {
tweetFires();
tweetIberiaFires();
tweetEuropeFires();
}
export { composeTweet, tweetHeaders, tweetFooters, tweetFires };
export { composeIberiaTweet, tweetHeaders, tweetFooters, tweetIberiaFires, tweetEuropeFires };

View file

@ -2,7 +2,7 @@
/* eslint-disable import/no-absolute-path */
import { Meteor } from 'meteor/meteor';
import { tweetFires } from '/imports/api/ActiveFires/server/tweetFiresInZone';
import { tweetIberiaFires, tweetEuropeFires } from '/imports/api/ActiveFires/server/tweetFiresInZone';
// https://github.com/thesaucecode/meteor-synced-cron/
@ -55,7 +55,7 @@ Meteor.startup(() => {
}
return sched;
},
job: () => tweetFires()
job: () => tweetIberiaFires()
/* console.log('cron is working');
* console.log(this.userID) // Context Object becomes this argument
* console.log(this.magic) /
@ -64,5 +64,18 @@ Meteor.startup(() => {
return tweetFires(); */
});
SyncedCron.add({
name: 'Check for fires stats in Europe and tweet about',
timezone: 'Europe/Madrid',
schedule: (parser) => {
const sched = parser.text(Meteor.settings.private.twitter.en.when);
if (sched.error !== -1) {
console.error(`Twitter cron 'when' field parsed with errors: ${sched.error}`);
}
return sched;
},
job: () => tweetEuropeFires()
});
SyncedCron.start();
});

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,7 @@
"token": "yourtoken"
},
"gmaps": {
"key": ""
"key": "",
"serverKey": ""
},
"piwik": {
@ -39,6 +39,14 @@
"consumer_secret": "...",
"access_token": "...",
"access_token_secret": "..."
},
"en": {
"comment": "using later.js",
"when": "at 9:15 am also at 3:15 pm also at 10:00 pm",
"consumer_key": "...",
"consumer_secret": "...",
"access_token": "...",
"access_token_secret": "..."
}
}
}

View file

@ -3,39 +3,39 @@
/* eslint-disable import/no-absolute-path */
import { chai } from 'meteor/practicalmeteor:chai';
import { composeTweet } from '/imports/api/ActiveFires/server/tweetFiresInZone';
import { composeIberiaTweet } from '/imports/api/ActiveFires/server/tweetFiresInZone';
describe('compose tweets of fires', () => {
it('No fires', async () => {
chai.expect('').to.deep.equal(composeTweet(0, { }));
chai.expect('').to.deep.equal(composeIberiaTweet(0, { }));
});
it('a fire in a zone', async () => {
chai.expect('Un fuego activo en #Asturias').to.deep.equal(composeTweet(1, { Asturias: 1 }));
chai.expect('Un #fuego activo en #Asturias').to.deep.equal(composeIberiaTweet(1, { Asturias: 1 }));
});
it('some fires in a zone', async () => {
chai.expect('4 fuegos activos en #Asturias').to.deep.equal(composeTweet(4, { Asturias: 4 }));
chai.expect('4 #fuegos activos en #Asturias').to.deep.equal(composeIberiaTweet(4, { Asturias: 4 }));
});
it('some fires in two zones', async () => {
chai.expect('Un fuego activo en #Asturias y otros 4 en #Madrid').to.deep.equal(composeTweet(5, { Asturias: 1, Madrid: 4 }));
chai.expect('Un #fuego activo en #Asturias y otros 4 en #Madrid').to.deep.equal(composeIberiaTweet(5, { Asturias: 1, Madrid: 4 }));
});
it('some fires in some zones', async () => {
chai.expect('3 fuegos activos en #Catalunya, otro en #Asturias y otros 4 en #Madrid').to.deep.equal(composeTweet(8, { Catalunya: 3, Asturias: 1, Madrid: 4 }));
chai.expect('3 #fuegos activos en #Catalunya, otro en #Asturias y otros 4 en #Madrid').to.deep.equal(composeIberiaTweet(8, { Catalunya: 3, Asturias: 1, Madrid: 4 }));
});
it('other fires in some zones', async () => {
chai.expect('Un fuego activo en #Catalunya, otro en #Asturias y otros 4 en #Madrid').to.deep.equal(composeTweet(6, { Catalunya: 1, Asturias: 1, Madrid: 4 }));
chai.expect('Un #fuego activo en #Catalunya, otro en #Asturias y otros 4 en #Madrid').to.deep.equal(composeIberiaTweet(6, { Catalunya: 1, Asturias: 1, Madrid: 4 }));
});
it('other fires in two zones', async () => {
chai.expect('Un fuego activo en #Catalunya y otro en #Asturias').to.deep.equal(composeTweet(2, { Catalunya: 1, Asturias: 1 }));
chai.expect('Un #fuego activo en #Catalunya y otro en #Asturias').to.deep.equal(composeIberiaTweet(2, { Catalunya: 1, Asturias: 1 }));
});
it('many fires in many zones', async () => {
chai.expect('3 fuegos activos en #Cataluña, otro en #Cantabria, otros 4 en #Andalucia, otro en #Galicia y otros 2 en #Madrid').to.deep.equal(composeTweet(11, {
chai.expect('3 #fuegos activos en #Cataluña, otro en #Cantabria, otros 4 en #Andalucia, otro en #Galicia y otros 2 en #Madrid').to.deep.equal(composeIberiaTweet(11, {
Cataluña: 3, Cantabria: 1, Andalucia: 4, Galicia: 1, Madrid: 2
}));
});