index.js (4622B)
1 const fetch = require('node-fetch'); 2 var twit = require('twit'); 3 const ejs = require ('ejs'); 4 const path = require('path'); 5 var creds = require('./config.js'); 6 const bodyparser = require('body-parser'); 7 const express = require('express'); 8 const app = express(); 9 const server = require('http').Server(app); 10 const io = require('socket.io')(server); 11 12 13 14 server.listen(3000, function(){ 15 console.log("you are now rockin with port 3000 and... localhost"); 16 }); 17 18 app.engine('html', require('ejs').renderFile); 19 app.set('view engine', 'ejs'); 20 21 app.use(express.static(path.join(__dirname + '/public'))); 22 app.use(bodyparser.urlencoded({ 23 extended: true 24 })); 25 26 // index routing 27 app.get('/', function (req, res) { 28 console.log('got request made on / route! Sending the EJS page now!'); 29 res.render('index'); 30 }); 31 32 33 34 35 //decalre the post route 36 app.post('/sendit', function(req, res){ 37 console.log(' still gonna send it!'); 38 //get the input from the user and log it in the console. 39 var tag = req.body.tag; 40 console.log(tag); 41 42 //add my credentials to allow access to the api. 43 var T = new twit(creds); 44 45 //connect socket to the index page. 46 io.on('connection', function (socket) { 47 console.log('Connected'); 48 49 //use the twit API to get a constant flow of data from twitter about a certain keyword.(s) 50 var stream = T.stream('statuses/filter', { track: tag , language: 'en' }, function(){ 51 console.log(tag); 52 }); 53 //twit fucntion with callback to log the tweets and send them to socket.io 54 stream.on('tweet', function (tweet) { 55 console.log(tweet.text); 56 //socket.io function to send the text of the tweet to the front end page . 57 socket.emit('stream',tweet.text); 58 59 //render the sendit page 60 }); 61 62 }); 63 res.render("postpage"); 64 }); 65 66 app.post('/userstatspage', function(req, res){ 67 68 console.log('triggered the userstats page'); 69 var username = req.body.username; 70 // console.log(username); 71 var bearer = 'AAAAAAAAAAAAAAAAAAAAAGLfBQEAAAAAVeDxlZsqNtyYsN4qMmfrrKBDbUM%3DisXeBus50iY7LzBBJBiNXJpyllGkEaVnRNMdLJTvtdHX7MLxQ0'; 72 async function userdata (username){ 73 var url = 'https://api.twitter.com/1.1/users/show.json?screen_name='+ username; 74 var request =await fetch(url, { 75 method: 'GET', 76 headers: { 77 Authorization: 'Bearer '+ bearer 78 } 79 }); 80 var response = await request.json() 81 //test to make sure the data is coming in correctly 82 // console.log (response); 83 //v ars 84 var friends = response.friends_count; 85 var followers = response.followers_count; 86 console.log('friends ' + typeof friends ); 87 console.log('followers ' + followers); 88 var ratio = ( friends / followers); 89 //console.log(response.screen_name); 90 //console.log(response.followers_count); 91 //console.log(response.friends_count) 92 93 //calcs 94 //var ratio = (numeric(friends) / numeric(followers)); 95 96 97 //TODO: grab the actual data that we want to display to the end user 98 //which would be : 99 // neatly displayed list of followers 100 // thier ratio of followers to friends 101 // follwers who follow them (friends) 102 // maybe longest friends , friends with most followers 103 104 } 105 userdata(username); 106 107 res.render('userstats', { 108 username: username, 109 //pass the variables outside of the function. 110 ratio: ratio, 111 friends: friends, 112 followers: followers 113 114 }) 115 }) 116 117 app.post('/locations', function(req, res){ 118 console.log(' sending the locations page!'); 119 //get the input from the user and log it in the console. 120 var tag = req.body.tag; 121 console.log(tag); 122 123 //add my credentials to allow access to the api. 124 var T = new twit(creds); 125 126 //connect socket to the index page. 127 io.on('connection', function (socket) { 128 console.log('Connected'); 129 130 //use the twit API to get a constant flow of data from twitter about a certain keyword.(s) 131 var sanFrancisco = [ '-122.75', '36.8', '-121.75', '37.8' ] 132 133 var stream = T.stream('statuses/filter', { locations: sanFrancisco }) 134 135 //twit fucntion with callback to log the tweets and send them to socket.io 136 stream.on('tweet', function (tweet) { 137 console.log(tweet.text); 138 //socket.io function to send the text of the tweet to the front end page . 139 socket.emit('stream',tweet.text); 140 141 //render the sendit page 142 }); 143 144 }); 145 res.render("locationspage"); 146 });