Added EU fire stats tweets
This commit is contained in:
parent
452f6baf45
commit
1eb6e45dfe
5 changed files with 80 additions and 21 deletions
|
|
@ -8,7 +8,9 @@ import countFires from './countFires';
|
||||||
const autonomies = tbuffer(JSON.parse(getPrivateFile('data/es-atlas-autonomies.json')), 0);
|
const autonomies = tbuffer(JSON.parse(getPrivateFile('data/es-atlas-autonomies.json')), 0);
|
||||||
const portugal = tbuffer(JSON.parse(getPrivateFile('data/pt-atlas.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', ''],
|
['Ciudad Autónoma de', ''],
|
||||||
['País Vasco/', ''],
|
['País Vasco/', ''],
|
||||||
['Pais Vasco', 'Euskadi'],
|
['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) {
|
if (num <= 0) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +60,7 @@ const composeTweet = (num, stats) => {
|
||||||
keys.map((key, index) => { // , index
|
keys.map((key, index) => { // , index
|
||||||
const zoneFires = stats[key];
|
const zoneFires = stats[key];
|
||||||
const zoneFiresText = zoneFires > 1 ? index === 0 ? zoneFires : `otros ${zoneFires}` : index === 0 ? 'Un' : 'otro';
|
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 subText = `${zoneFiresText}${what}en #${key}`;
|
||||||
const delimiter = index === numZones - 1 ? ' y' : ',';
|
const delimiter = index === numZones - 1 ? ' y' : ',';
|
||||||
const subTexts = index === 0 ? subText : `${delimiter} ${subText}`;
|
const subTexts = index === 0 ? subText : `${delimiter} ${subText}`;
|
||||||
|
|
@ -51,16 +73,30 @@ const composeTweet = (num, stats) => {
|
||||||
const tweetHeaders = ['🔥'];
|
const tweetHeaders = ['🔥'];
|
||||||
const tweetFooters = ['Más info en: https://fuegos.comunes.org/fires'];
|
const tweetFooters = ['Más info en: https://fuegos.comunes.org/fires'];
|
||||||
|
|
||||||
const tweetFires = () => {
|
const tweetEuropeFires = () => {
|
||||||
const resultEs = countFires(autonomies, stringsToRemove);
|
const resultEu = countFires(europe, [[' ', '']]);
|
||||||
const resultPt = countFires(portugal, stringsToRemove);
|
|
||||||
|
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 fires = Object.assign(resultEs.fires, resultPt.fires);
|
||||||
const total = resultEs.total + resultPt.total;
|
const total = resultEs.total + resultPt.total;
|
||||||
|
|
||||||
// console.log(`Total fires ${total}, ${JSON.stringify(fires)}`);
|
// console.log(`Total fires ${total}, ${JSON.stringify(fires)}`);
|
||||||
if (total > 0) {
|
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) {
|
if (Meteor.isDevelopment) {
|
||||||
console.log(tweetText);
|
console.log(tweetText);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -72,7 +108,8 @@ const tweetFires = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
if (Meteor.isDevelopment) {
|
if (Meteor.isDevelopment) {
|
||||||
tweetFires();
|
tweetIberiaFires();
|
||||||
|
tweetEuropeFires();
|
||||||
}
|
}
|
||||||
|
|
||||||
export { composeTweet, tweetHeaders, tweetFooters, tweetFires };
|
export { composeIberiaTweet, tweetHeaders, tweetFooters, tweetIberiaFires, tweetEuropeFires };
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
/* eslint-disable import/no-absolute-path */
|
/* eslint-disable import/no-absolute-path */
|
||||||
|
|
||||||
import { Meteor } from 'meteor/meteor';
|
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/
|
// https://github.com/thesaucecode/meteor-synced-cron/
|
||||||
|
|
||||||
|
|
@ -55,7 +55,7 @@ Meteor.startup(() => {
|
||||||
}
|
}
|
||||||
return sched;
|
return sched;
|
||||||
},
|
},
|
||||||
job: () => tweetFires()
|
job: () => tweetIberiaFires()
|
||||||
/* console.log('cron is working');
|
/* console.log('cron is working');
|
||||||
* console.log(this.userID) // Context Object becomes this argument
|
* console.log(this.userID) // Context Object becomes this argument
|
||||||
* console.log(this.magic) /
|
* console.log(this.magic) /
|
||||||
|
|
@ -64,5 +64,18 @@ Meteor.startup(() => {
|
||||||
return tweetFires(); */
|
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();
|
SyncedCron.start();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
1
private/data/europe-atlas.json
Normal file
1
private/data/europe-atlas.json
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -4,7 +4,7 @@
|
||||||
"token": "yourtoken"
|
"token": "yourtoken"
|
||||||
},
|
},
|
||||||
"gmaps": {
|
"gmaps": {
|
||||||
"key": ""
|
"key": "",
|
||||||
"serverKey": ""
|
"serverKey": ""
|
||||||
},
|
},
|
||||||
"piwik": {
|
"piwik": {
|
||||||
|
|
@ -39,6 +39,14 @@
|
||||||
"consumer_secret": "...",
|
"consumer_secret": "...",
|
||||||
"access_token": "...",
|
"access_token": "...",
|
||||||
"access_token_secret": "..."
|
"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": "..."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,39 +3,39 @@
|
||||||
/* eslint-disable import/no-absolute-path */
|
/* eslint-disable import/no-absolute-path */
|
||||||
|
|
||||||
import { chai } from 'meteor/practicalmeteor:chai';
|
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', () => {
|
describe('compose tweets of fires', () => {
|
||||||
it('No fires', async () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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 () => {
|
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
|
Cataluña: 3, Cantabria: 1, Andalucia: 4, Galicia: 1, Madrid: 2
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue