week6
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>SEARCH TWITTER</title>
|
||||
<style>
|
||||
/* CSS Styling */
|
||||
#frame {
|
||||
width: 100%;
|
||||
}
|
||||
#inputs{
|
||||
width: 50em;
|
||||
bborder: 1px solid black;
|
||||
}
|
||||
#map {
|
||||
height: 500px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
</style>
|
||||
<!-- SCRIPTS -->
|
||||
<!-- import jquery -->
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<!-- HTML -->
|
||||
<label>SEARCH TWITTER</label>
|
||||
<div id="frame">
|
||||
<!-- inputs for the search to send to server -->
|
||||
<div id="inputs">
|
||||
<label>keyword: </label>
|
||||
<input id="text_field" type="text"/>
|
||||
<label>results </label>
|
||||
<input id="nbr_results" type="number" value="20" min="1" max="50">
|
||||
<input id="search_button" type="button" value="Search" onclick="search();">
|
||||
</div>
|
||||
|
||||
<div id="map"></div>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- SCRIPTS -->
|
||||
<!-- import socket.io -->
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
// connect to localhost on its port (see server.js -- 8088)
|
||||
var socket = io().connect('http://localhost:8088');
|
||||
|
||||
// the map object -- (which needs to be initialised -- see init_map())
|
||||
var map = null;
|
||||
|
||||
// all maps markers
|
||||
var markers = [];
|
||||
|
||||
// when connecting
|
||||
socket.on('connect', function (data) {
|
||||
console.log('connected');
|
||||
});
|
||||
|
||||
// when receiving a custom message form the server ('search_twitter_results')
|
||||
socket.on('search_twitter_results', function(msg) {
|
||||
//console.log('search_twitter_results: ' + JSON.stringify(msg));
|
||||
geolocate_results_and_dispay(msg, map);
|
||||
});
|
||||
|
||||
// when search button is pressed
|
||||
function search() {
|
||||
|
||||
var keyword = document.getElementById("text_field").value;
|
||||
var hits = document.getElementById("nbr_results").value;
|
||||
// create search parameters that will be sent to the server
|
||||
var search_params = {keyword_value: keyword, nbr_hits: hits};
|
||||
|
||||
// send request to the server
|
||||
socket.emit('search_twitter', search_params);
|
||||
|
||||
}
|
||||
|
||||
function init_map() {
|
||||
map = new google.maps.Map(document.getElementById('map'), {
|
||||
zoom: 5,
|
||||
center: {lat: 52.373629, lng: 4.887848} // Amsterdam
|
||||
});
|
||||
}
|
||||
|
||||
function add_marker_information(marker) {
|
||||
|
||||
markers.push(marker);
|
||||
|
||||
google.maps.event.addListener(marker, 'click', (function(marker) {
|
||||
|
||||
return function(evt) {
|
||||
|
||||
var content = marker.getTitle();
|
||||
var info = google.maps.InfoWindow();
|
||||
info.setContent(content);
|
||||
info.open(a_map, marker);
|
||||
|
||||
}
|
||||
|
||||
})(marker));
|
||||
|
||||
|
||||
}
|
||||
|
||||
function geolocate_results_and_dispay(results, a_map) {
|
||||
|
||||
var geocoder = new google.maps.Geocoder();
|
||||
|
||||
for(r of results) {
|
||||
|
||||
//console.log(r.location);
|
||||
|
||||
geocoder.geocode({'address': r.location}, function(geo_results, status) {
|
||||
|
||||
if(status === 'OK') {
|
||||
|
||||
var marker = new google.maps.Marker({
|
||||
map: a_map,
|
||||
position: geo_results[0].geometry.location,
|
||||
title: r.tweet_text
|
||||
});
|
||||
|
||||
add_marker_information(marker); // does not work...
|
||||
|
||||
} else {
|
||||
console.log('No geocode for ' + r.location);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCi7-OtKvhvY5W8GGrbY8yUIq4JU3vHGPw&callback=init_map">
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "search-twitter",
|
||||
"version": "1.0.0",
|
||||
"description": "simple twitter search",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "gauthier",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"express": "^4.14.0",
|
||||
"socket.io": "^1.6.0",
|
||||
"twitter": "^1.4.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// server.js
|
||||
|
||||
// import express ()
|
||||
var express = require('express'); // npm install --save express
|
||||
var app = express();
|
||||
|
||||
// import node.js http
|
||||
var server = require('http').Server(app);
|
||||
|
||||
// import socket.io
|
||||
var io = require('socket.io')(server); // npm install --save socket.io
|
||||
|
||||
// import filesystem (aka fs)
|
||||
var fs = require('fs');
|
||||
|
||||
//import twitter
|
||||
var twitter = require('twitter'); // npm install --save twitter
|
||||
|
||||
|
||||
/* ----------------------
|
||||
Twitter Configuration
|
||||
-------------------------*/
|
||||
|
||||
// load configuration file with all secrets, etc.
|
||||
var config_file = "./twitter_credentials.json";
|
||||
var config = JSON.parse(fs.readFileSync(config_file, "utf8"));
|
||||
|
||||
// create the twitter client
|
||||
var client = new twitter(config);
|
||||
|
||||
/*------------------------
|
||||
TWITTER FUNCTIONS
|
||||
--------------------------*/
|
||||
|
||||
function search_twitter(keyword_value, nbr_hits) {
|
||||
|
||||
|
||||
//var twitter_search_params = {q: keyword_value, geocode: "37.781157,-122.398720,100mi"}; // <-- this is centered around a given region
|
||||
var twitter_search_params = {q: keyword_value, count: nbr_hits};
|
||||
|
||||
client.get('search/tweets', twitter_search_params, function(error, tweets, response) {
|
||||
|
||||
var results = [];
|
||||
|
||||
//console.log(tweets);
|
||||
|
||||
if(!error) {
|
||||
for(tweet of tweets.statuses) {
|
||||
|
||||
if(tweet.user.location != null && tweet.user.location.trim().length > 0) {
|
||||
|
||||
//console.log('-->' + tweet.user.location + '<--');
|
||||
|
||||
results.push({tweet_text: tweet.text, location: tweet.user.location});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log('* ERROR *: ' + error);
|
||||
}
|
||||
|
||||
// send results to client
|
||||
io.emit('search_twitter_results', results);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/* ----------------------------------
|
||||
Server and Socket Configuration
|
||||
--------------------------------------*/
|
||||
|
||||
// tell express to server our index.html file
|
||||
app.get('/', function (req, res) {
|
||||
res.sendFile(__dirname + '/index.html');
|
||||
});
|
||||
|
||||
// configure socket.io
|
||||
// (1) when there is a connection
|
||||
io.on('connection', function(socket) {
|
||||
|
||||
console.log('got a connection');
|
||||
|
||||
// (2) configure the connected socket to receive custom messages ('message from human')
|
||||
socket.on('search_twitter', function(msg) {
|
||||
|
||||
console.log('searching twitter with: ' + msg.toString());
|
||||
|
||||
search_twitter(msg.keyword_value, msg.nbr_hits);
|
||||
|
||||
});
|
||||
|
||||
socket.on('disconnet', function() {
|
||||
|
||||
console.log('got a disconnection');
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
/* -------------------
|
||||
Start the server
|
||||
----------------------*/
|
||||
|
||||
// listen to connection on port 8088 --> http://localhost:8088
|
||||
server.listen(8088, function () {
|
||||
console.log('listening on port: ' + 8088);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"consumer_key": "jKMz31iuQMImQpDZOEKlFelb2",
|
||||
"consumer_secret": "IAfen9GcPSKsmiFZcXv23qVLDdI6PhHAFfJidc8UWbg8qIJnmH",
|
||||
"access_token_key": "801125183360745472-eMpgT1Cscw86Mm31KG8DTq336L5g4RB",
|
||||
"access_token_secret": "xZEuPLawdBwOgPCSWHaWsUAgbYlobAztYmwMMGeJvtsli"
|
||||
}
|
||||
Reference in New Issue
Block a user