├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── package.json ├── twitter-call.js └── twitter-integrator.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .env 3 | wedeploy.json -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Yan Magalhães 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Twitter Event Audience 2 | 3 | A node service to return tweets what people saying about your event 4 | 5 | # How it works 6 | This project using a [Twitter JS Client](https://github.com/BoyCook/TwitterJSClient) to connect with Twitter API and get informations 7 | 8 | # How to Use 9 | 10 | - Setup 11 | - Clone this project 12 | - Run `npm i` 13 | - Create a `.env` file with TwitterAPI Credentials. 14 | 15 | > Example 16 | 17 | ``` 18 | hastag = "#myawesomehastag" 19 | consumerKey = "myawesomeproject" 20 | consumerSecret = "myawesomeproject" 21 | accessToken = "myawesomeproject" 22 | accessTokenSecret = "myawesomeproject" 23 | callBackUrl = "myawesomeproject" 24 | ``` 25 | _You will get this informations at [Twitter API](https://developer.twitter.com/en/docs/basics/getting-started)_ 26 | 27 | - Run `node app.js` in your terminal 28 | - Call `http://localhost:3000/event-audience` 29 | 30 | **You will received a similar response to this:** 31 | 32 | ``` 33 | { 34 | "item":"E taa chegaaando o #dfne2k17 :D :D", 35 | "name":"Yan Magalhães", 36 | "username":"yaanmagale", 37 | "image":"http://pbs.twimg.com/profile_images/573808379103158272/jUaUrzy4_normal.jpeg" 38 | } 39 | ``` 40 | 41 | # Next Steps 42 | - Update online service with new features 43 | - Improve error responses 44 | - Improve structure to mount response 45 | - Create a unit tests -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const cors = require('cors'); 3 | const app = express(); 4 | const twitterIntegrator = require('./twitter-integrator.js'); 5 | 6 | app.use(cors()); 7 | 8 | app.get('/event-audience', function (req, res) { 9 | twitterIntegrator.getTweets(req.query.hashtag) 10 | .then((response) => { 11 | res.send(response); 12 | }) 13 | .catch((error) => { 14 | res.send(error); 15 | }); 16 | }); 17 | 18 | app.listen(3000, function () { 19 | console.log('CORS-enabled, web server listening on port 3000'); 20 | 21 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "twitter-test", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "start": "node app.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "", 11 | "license": "MIT", 12 | "dependencies": { 13 | "cors": "^2.7.1", 14 | "dotenv": "^4.0.0", 15 | "express": "^4.13.4", 16 | "promise": "^8.0.0", 17 | "twitter-node-client": "0.0.6" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /twitter-call.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config() 2 | 3 | let Twitter = require('twitter-node-client').Twitter; 4 | 5 | const config = { 6 | "consumerKey": process.env.consumerKey, 7 | "consumerSecret": process.env.consumerSecret, 8 | "accessToken": process.env.accessToken, 9 | "accessTokenSecret": process.env.accessTokenSecret, 10 | "callBackUrl": process.env.callBackUrl 11 | } 12 | 13 | const twitter = new Twitter(config); 14 | 15 | exports.searchTweets = function (hashtag = '') { 16 | return new Promise((resolve, reject) => { 17 | const hastagSearch = treatmentsHastagToSearch(hashtag) 18 | twitter.getSearch({ 'q': hastagSearch, 'result_type': 'mixed', 'exclude': 'retweets', 'count': 100 }, 19 | function (err, response, body) { 20 | console.log('ERROR Search Twitter API', err); 21 | reject(new Error('object failed')); 22 | }, 23 | function (data) { 24 | console.log('Data Result'); 25 | const result = JSON.parse(data); 26 | resolve(result); 27 | }); 28 | }); 29 | } 30 | 31 | function treatmentsHastagToSearch(hashtag) { 32 | if (hashtag.length > 0) { 33 | return "#" + hashtag 34 | } 35 | return process.env.hastag 36 | } 37 | -------------------------------------------------------------------------------- /twitter-integrator.js: -------------------------------------------------------------------------------- 1 | const Promise = require('promise'); 2 | const twitter = require('./twitter-call.js'); 3 | let objData = []; 4 | 5 | exports.getTweets = function(hashtag = ''){ 6 | return new Promise((resolve, reject) => { 7 | twitter.searchTweets(hashtag) 8 | .then((response) => { 9 | response.statuses.map((item) => { 10 | objData.push({'item': item.text, 'name':item.user.name, 11 | 'username': item.user.screen_name, 'image': item.user.profile_image_url}); 12 | }) 13 | return true; 14 | }) 15 | .then(() => { 16 | resolve(objData); 17 | }) 18 | .catch((err) => { 19 | reject(err); 20 | }); 21 | objData = []; 22 | }); 23 | } --------------------------------------------------------------------------------