This repository has been archived by the owner on May 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
657e307
commit 897b4db
Showing
10 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"rules": { | ||
"indent": ["error", "tab"], | ||
"quotes": ["error", "single"], | ||
"quote-props": ["error", "consistent"], | ||
"semi": ["error", "always"], | ||
"keyword-spacing": ["error"], | ||
"no-unused-vars": ["warn", { "vars": "all", "args": "none" }], | ||
"no-console": ["off"], | ||
"no-mixed-spaces-and-tabs": ["error", "smart-tabs"], | ||
"no-new-object": ["error"], | ||
"no-array-constructor": ["error"], | ||
"space-before-blocks": ["error", "always"], | ||
"space-before-function-paren": ["error", "never"], | ||
"space-in-parens": ["error", "never"], | ||
"space-infix-ops": ["error"], | ||
"space-unary-ops": ["error", { "words": true, "nonwords": false }], | ||
"object-curly-spacing": ["error", "always"], | ||
"array-bracket-spacing": ["error", "never"], | ||
"guard-for-in": "error", | ||
"no-spaced-func": "error", | ||
"no-trailing-spaces": ["error", { "skipBlankLines": true }], | ||
"handle-callback-err": "error", | ||
"comma-spacing": ["error", {"before": false, "after": true}], | ||
"one-var": ["error", "never"], | ||
"no-redeclare": 0, | ||
"comma-dangle": ["error", "never"], | ||
"arrow-parens": ["warn", "always"], | ||
"arrow-spacing": ["error"], | ||
"no-confusing-arrow": ["error"], | ||
"no-useless-computed-key": ["error"], | ||
"object-shorthand": ["warn", "methods"], | ||
"prefer-arrow-callback": ["error"], | ||
"prefer-rest-params": ["warn"], | ||
"template-curly-spacing": ["warn", "never"] | ||
}, | ||
"env": { | ||
"node": true, | ||
"mocha": true, | ||
"es6": true | ||
}, | ||
"extends": "eslint:recommended" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const bole = require('bole'); | ||
|
||
// Initialize the logger | ||
bole.output([{ level: 'debug', stream: process.stdout }]); | ||
|
||
let logger = bole('index'); | ||
logger.info('TerremotiBot Social is booting...'); | ||
|
||
// When an expection occurs, | ||
// log the 'Error' and euthanasia | ||
process.on('uncaughtException', (err) => { | ||
logger.error(err); | ||
// We can safely exit because the only logger output is stdout, | ||
// which is flushed automatically when the process shuts down | ||
process.exit(1); | ||
}); | ||
|
||
require('./src/social.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "terremotibot-social", | ||
"version": "1.0.0", | ||
"description": "Automatic earthquakes poster to Twitter and Facebook", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint src" | ||
}, | ||
"repository": "botfactoryit/terremotibot-social", | ||
"author": "Matteo Contrini <m.contrini@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "/~https://github.com/botfactoryit/terremotibot-social/issues" | ||
}, | ||
"homepage": "/~https://github.com/botfactoryit/terremotibot-social#readme", | ||
"dependencies": { | ||
"aws-sdk": "^2.11.0", | ||
"bole": "^3.0.2", | ||
"fb": "^1.1.1", | ||
"sqs-consumer": "^3.4.0", | ||
"twit": "^2.2.5" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^3.15.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"sqs": { | ||
"queueUrl": "", | ||
"accessKeyId": "", | ||
"secretAccessKey": "", | ||
"region": "eu-central-1", | ||
"apiVersion": "2012-11-05" | ||
}, | ||
"facebook": { | ||
"accessToken": "", | ||
"pageId": "" | ||
}, | ||
"twitter": { | ||
"consumerKey": "", | ||
"consumerSecret": "", | ||
"accessToken": "", | ||
"accessTokenSecret": "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const fs = require('fs'); | ||
|
||
let content = fs.readFileSync(__dirname + '/config.json').toString(); | ||
const config = JSON.parse(content); | ||
|
||
module.exports = function(module) { | ||
return config[module] || {}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const FB = require('fb'); | ||
const config = require('../config')('facebook'); | ||
|
||
let fb = new FB.Facebook(); | ||
|
||
fb.setAccessToken(config.accessToken); | ||
|
||
module.exports.upload = function(options, callback) { | ||
let req = { | ||
source: { | ||
value: options['buffer'], | ||
options: { | ||
contentType: 'image/jpeg', | ||
filename: 'terremoto.jpg' // because facebook | ||
} | ||
}, | ||
backdated_time: Math.round(options['date'].getTime() / 1000), | ||
caption: options['text'] | ||
}; | ||
|
||
fb.api(config.pageId + '/photos', 'post', req, (res) => { | ||
if (res.error) { | ||
callback && callback(res.error); | ||
} | ||
else { | ||
callback && callback(null, res); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports.twitter = require('./twitter.js'); | ||
module.exports.facebook = require('./facebook.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const Twit = require('twit'); | ||
const config = require('../config')('twitter'); | ||
|
||
let tw = new Twit({ | ||
consumer_key: config.consumerKey, | ||
consumer_secret: config.consumerSecret, | ||
access_token: config.accessToken, | ||
access_token_secret: config.accessTokenSecret | ||
}); | ||
|
||
module.exports.upload = function upload(options, callback) { | ||
let b64 = options['buffer'].toString('base64'); | ||
|
||
tw.post('media/upload', { media_data: b64 }, (err, data, response) => { | ||
if (err) { | ||
callback && callback(err); | ||
return; | ||
} | ||
|
||
let mediaId = data['media_id_string']; | ||
|
||
let params = { | ||
status: options['text'], | ||
lat: options['origin']['lat'], | ||
long: options['origin']['lon'], | ||
media_ids: [mediaId] | ||
}; | ||
|
||
tw.post('statuses/update', params, (err, data, response) => { | ||
if (err) { | ||
callback && callback(err); | ||
} | ||
else { | ||
callback && callback(null, data); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const AWS = require('aws-sdk'); | ||
const Consumer = require('sqs-consumer'); | ||
const config = require('./config'); | ||
const networks = require('./networks'); | ||
const logger = require('bole')('social'); | ||
|
||
const sqs = Consumer.create({ | ||
queueUrl: config('sqs').queueUrl, | ||
messageAttributeNames: ['Card'], | ||
handleMessage: handleMessage, | ||
sqs: new AWS.SQS({ | ||
accessKeyId: config('sqs').accessKeyId, | ||
secretAccessKey: config('sqs').secretAccessKey, | ||
region: config('sqs').region, | ||
apiVersion: config('sqs').apiVersion | ||
}), | ||
batchSize: 1 | ||
}); | ||
|
||
sqs.on('error', (err) => { | ||
logger.error(err); | ||
}); | ||
|
||
sqs.start(); | ||
|
||
function handleMessage(message, done) { | ||
logger.info('Received new message'); | ||
|
||
let body = JSON.parse(message['Body']); | ||
let buffer = message.MessageAttributes.Card.BinaryValue; | ||
|
||
let date = new Date(body['date']); | ||
let time = ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2); | ||
|
||
let magnitude = body['magnitude']['value']; | ||
let uncertainty = body['magnitude']['uncertainty']; | ||
let type = body['magnitude']['type']; | ||
let city = body['city']; | ||
|
||
let text = `#terremoto alle ${time}\n\nEpicentro: ${city}\nMagnitudo: ${magnitude} ± ${uncertainty} (${type})`; | ||
|
||
let options = { | ||
origin: body['origin'], | ||
text: text, | ||
buffer: buffer, | ||
date: date | ||
}; | ||
|
||
logger.info('Uploading to Twitter'); | ||
|
||
networks.twitter.upload(options, (err) => { | ||
if (err) { | ||
logger.error(err); | ||
done(err); | ||
return; | ||
} | ||
|
||
logger.info('Done'); | ||
logger.info('Uploading to Facebook'); | ||
|
||
networks.facebook.upload(options, (err) => { | ||
if (err) { | ||
logger.error(err); | ||
} | ||
|
||
logger.info('Done'); | ||
done(); | ||
}); | ||
}); | ||
} |