after class commit

This commit is contained in:
gauthiier 2016-11-23 18:48:32 +01:00
parent 01c46ba645
commit eb7ad3687c
3 changed files with 57 additions and 23 deletions

View File

@ -9,6 +9,16 @@
#inputs {
width: 30em;
}
.book {
padding: 10px;
border-bottom: 1px solid red;
}
#books {
width: 30em;
}
</style>
<!-- SCRIPTS -->
<!-- import handlebars -->
@ -26,14 +36,20 @@
<input id="nbr_books" type="number" value="2" min="1" max="5">
</div>
<!--
<div id="books">
<script id="books-template" type="text/x-handlebars-template">
{{#each this}}
<div class="book">
<div class="title">Title: {{title}}</div>
<div class="author">Author: {{author}}</div>
<div class="price">Price: {{price}}</div>
<div class="kind">Kind: {{kind}}</div>
</div>
{{/each}}
</script>
</div>
-->
</div>
@ -51,11 +67,21 @@
// when receiving a custom message form the server
socket.on('all the books we have', function(msg) {
console.log('all the books: ' + msg);
console.log('all the books: ' + JSON.stringify(msg));
compile_results_and_display(msg);
});
/*
function compile_results_and_dispay(results) {
function get_books() {
socket.emit('give some books', "nothing");
}
function compile_results_and_display(results) {
var template_script = $("#books-template").html();
@ -66,7 +92,7 @@
$('#books').append(template(results));
}
*/
</script>
</body>

View File

@ -83,7 +83,9 @@
// when receiving a custom message form the server ('search_twitter_results')
socket.on('search_twitter_results', function(msg) {
console.log('search_twitter_results: ' + msg);
console.log('search_twitter_results: ' + JSON.stringify(msg));
compile_results_and_dispay(msg);
});
@ -93,8 +95,6 @@
var keyword = document.getElementById("text_field").value;
var hits = document.getElementById("nbr_results").value;
console.log(hits);
// radio buttons -- filter
var filter = null;
if(document.getElementById("radio_images").checked) {

View File

@ -28,33 +28,46 @@ var config = JSON.parse(fs.readFileSync(config_file, "utf8"));
// create the twitter client
var client = new twitter(config);
function search_twitter(keyword_value, nbr_hits, filter_value, callback) {
function search_twitter(keyword_value, nbr_hits, filter_value) {
var search_params = {q: keyword_value, count: nbr_hits};
//https://dev.twitter.com/rest/reference/get/search/tweets
var twitter_search_params = {q: keyword_value, count: nbr_hits};
// if there is a filter
if(filter_value) {
search_params.filter = filter_value;
twitter_search_params.filter = filter_value;
}
client.get('search/tweets', search_params, function(error, tweets, response) {
client.get('search/tweets', twitter_search_params, function(error, tweets, response) {
var results = [];
console.log(tweets);
if(!error) {
//console.log("got " + tweets.statuses.length + " hits")
for(tweet of tweets.statuses) {
// console.log(tweet);
let r = {};
var r = {};
r.text = tweet.text;
if(tweet.entities.media) {
r.images = [];
for(media of tweet.entities.media) {
if(media.type == 'photo')
r.images.push(media.media_url);
}
}
results.push(r);
}
} else {
@ -62,9 +75,10 @@ function search_twitter(keyword_value, nbr_hits, filter_value, callback) {
}
// send results to client
//io.emit('search_twitter_results', results);
io.emit('search_twitter_results', results);
//callback(results);
callback(results);
});
}
@ -90,13 +104,7 @@ io.on('connection', function(socket) {
console.log('searching twitter with: ' + msg.toString());
search_twitter(msg.keyword_value, msg.nbr_hits, msg.filter_value, function (results) {
console.log(JSON.stringify(results, null, '\t'));
io.emit('search_twitter_results', results);
});
search_twitter(msg.keyword_value, msg.nbr_hits, msg.filter_value);
});