From 41ad6ee9014eaee6fa77df2c75ca3b6b99ae1ff3 Mon Sep 17 00:00:00 2001 From: gauthiier Date: Wed, 23 Nov 2016 12:48:37 +0100 Subject: [PATCH] week4 --- week4/notes.md | 32 +++++ week4/templating/index.html | 73 ++++++++++ week4/templating/package.json | 15 ++ week4/templating/server.js | 81 +++++++++++ week4/twitter-search/index.html | 133 ++++++++++++++++++ week4/twitter-search/package.json | 16 +++ week4/twitter-search/server.js | 119 ++++++++++++++++ week4/twitter-search/twitter_credentials.json | 6 + 8 files changed, 475 insertions(+) create mode 100644 week4/notes.md create mode 100644 week4/templating/index.html create mode 100644 week4/templating/package.json create mode 100644 week4/templating/server.js create mode 100644 week4/twitter-search/index.html create mode 100644 week4/twitter-search/package.json create mode 100644 week4/twitter-search/server.js create mode 100644 week4/twitter-search/twitter_credentials.json diff --git a/week4/notes.md b/week4/notes.md new file mode 100644 index 0000000..7c13e85 --- /dev/null +++ b/week4/notes.md @@ -0,0 +1,32 @@ +#### (working) notes + +APIs (major vendors): + +[Youtube](https://developers.google.com/youtube/v3/) + +[Soundcloud](https://developers.soundcloud.com/docs/api/sdks) + +[Soundcloud -- source](https://github.com/soundcloud/soundcloud-javascript) + +[Twitter](https://dev.twitter.com/rest/public) + + +Interesting Projects: + +Tactical Bots + Media jamming: [https://it-was-fun-at-first.net/](https://it-was-fun-at-first.net/) + +#NaBoMaMo + +[NaNoGenMo](https://github.com/NaNoGenMo/2016) + +[#NaNoGenMo](https://twitter.com/hashtag/NaBoMaMo) + +example: + +[Bot - rpgoptions](https://twitter.com/rpgoptions) + +[Source code - rpgoptions](https://github.com/Harrison-M/rpg-options) + +bot framework: + +[Hubot](https://hubot.github.com/docs/) diff --git a/week4/templating/index.html b/week4/templating/index.html new file mode 100644 index 0000000..71603d3 --- /dev/null +++ b/week4/templating/index.html @@ -0,0 +1,73 @@ + + + Bookstore (remember?!) + + + + + + + + + + +
+ +
+ + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/week4/templating/package.json b/week4/templating/package.json new file mode 100644 index 0000000..2ebae06 --- /dev/null +++ b/week4/templating/package.json @@ -0,0 +1,15 @@ +{ + "name": "templating", + "version": "1.0.0", + "description": "example of templating using handlebars.js", + "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" + } +} diff --git a/week4/templating/server.js b/week4/templating/server.js new file mode 100644 index 0000000..0221fb5 --- /dev/null +++ b/week4/templating/server.js @@ -0,0 +1,81 @@ +// 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 + +var books = [ + {'title': "Ways of Curating", + 'author': "Hans Ulrich Orbist", + 'price': 16.95, + 'kind': "Paperback" + }, + {'title': "Ardor", + 'author': "Roberto Calasso", + 'price': 39.50, + 'kind': "Hardcover" + }, + {'title': "Why Grow Up?", + 'author': "Susan Neiman", + 'price': 15.95, + 'kind': "Paperback" + }, + {'title': "The Complete Stories", + 'author': "Flannery O'connor", + 'price': 19.95, + 'kind': "Paperback" + }, + {'title': "The Hatred of Poetry", + 'author': "Ben Lerner", + 'price': 13.95, + 'kind': "Paperback" + } +]; + +/* ---------------------------------- + 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 + socket.on('give some books', function(msg) { + + console.log('got a give some books: ' + msg); + + io.emit('all the books we have', books); + + }); + + 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); +}); + diff --git a/week4/twitter-search/index.html b/week4/twitter-search/index.html new file mode 100644 index 0000000..48b5a0f --- /dev/null +++ b/week4/twitter-search/index.html @@ -0,0 +1,133 @@ + + + SEARCH TWITTER + + + + + + + + + + +
+ +
+ + + + + + + + + + + + + +
+ +
+ +
+ +
+ + + + + + + \ No newline at end of file diff --git a/week4/twitter-search/package.json b/week4/twitter-search/package.json new file mode 100644 index 0000000..06ea70e --- /dev/null +++ b/week4/twitter-search/package.json @@ -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" + } +} diff --git a/week4/twitter-search/server.js b/week4/twitter-search/server.js new file mode 100644 index 0000000..7a251c3 --- /dev/null +++ b/week4/twitter-search/server.js @@ -0,0 +1,119 @@ +// 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); + +function search_twitter(keyword_value, nbr_hits, filter_value, callback) { + + var search_params = {q: keyword_value, count: nbr_hits}; + + // if there is a filter + if(filter_value) { + search_params.filter = filter_value; + } + + client.get('search/tweets', search_params, function(error, tweets, response) { + + var results = []; + + if(!error) { + //console.log("got " + tweets.statuses.length + " hits") + for(tweet of tweets.statuses) { + // console.log(tweet); + let 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 { + console.log('* ERROR *: ' + error); + } + + // send results to client + //io.emit('search_twitter_results', results); + + callback(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'); + //io.emit('message from robot', 'Hi! my name is Reihtuag!'); // greetings + + // (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, msg.filter_value, function (results) { + + console.log(JSON.stringify(results, null, '\t')); + + io.emit('search_twitter_results', results); + + }); + + }); + + 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); +}); + diff --git a/week4/twitter-search/twitter_credentials.json b/week4/twitter-search/twitter_credentials.json new file mode 100644 index 0000000..08fe993 --- /dev/null +++ b/week4/twitter-search/twitter_credentials.json @@ -0,0 +1,6 @@ +{ + "consumer_key": "jKMz31iuQMImQpDZOEKlFelb2", + "consumer_secret": "IAfen9GcPSKsmiFZcXv23qVLDdI6PhHAFfJidc8UWbg8qIJnmH", + "access_token_key": "801125183360745472-eMpgT1Cscw86Mm31KG8DTq336L5g4RB", + "access_token_secret": "xZEuPLawdBwOgPCSWHaWsUAgbYlobAztYmwMMGeJvtsli" +} \ No newline at end of file