week4
This commit is contained in:
parent
782bd00d11
commit
41ad6ee901
32
week4/notes.md
Normal file
32
week4/notes.md
Normal file
@ -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/)
|
||||
73
week4/templating/index.html
Normal file
73
week4/templating/index.html
Normal file
@ -0,0 +1,73 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookstore (remember?!)</title>
|
||||
<style>
|
||||
/* CSS Styling */
|
||||
#frame {
|
||||
width: 100%;
|
||||
}
|
||||
#inputs {
|
||||
width: 30em;
|
||||
}
|
||||
</style>
|
||||
<!-- SCRIPTS -->
|
||||
<!-- import handlebars -->
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
|
||||
<!-- import jquery -->
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<!-- HTML -->
|
||||
<label>Bookstore (remember?!)</label>
|
||||
<div id="frame">
|
||||
|
||||
<div id="inputs">
|
||||
<input type="button" value="Get some books" onclick="get_books();"/>
|
||||
<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}}
|
||||
{{/each}}
|
||||
</script>
|
||||
</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');
|
||||
|
||||
// when connecting
|
||||
socket.on('connect', function (data) {
|
||||
console.log('connected');
|
||||
});
|
||||
|
||||
// when receiving a custom message form the server
|
||||
socket.on('all the books we have', function(msg) {
|
||||
console.log('all the books: ' + msg);
|
||||
});
|
||||
|
||||
/*
|
||||
function compile_results_and_dispay(results) {
|
||||
|
||||
var template_script = $("#books-template").html();
|
||||
|
||||
var template = Handlebars.compile(template_script);
|
||||
|
||||
//$('.book').remove();
|
||||
|
||||
$('#books').append(template(results));
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
15
week4/templating/package.json
Normal file
15
week4/templating/package.json
Normal file
@ -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"
|
||||
}
|
||||
}
|
||||
81
week4/templating/server.js
Normal file
81
week4/templating/server.js
Normal file
@ -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);
|
||||
});
|
||||
|
||||
133
week4/twitter-search/index.html
Normal file
133
week4/twitter-search/index.html
Normal file
@ -0,0 +1,133 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>SEARCH TWITTER</title>
|
||||
<style>
|
||||
/* CSS Styling */
|
||||
#frame {
|
||||
width: 100%;
|
||||
}
|
||||
#inputs{
|
||||
width: 50em;
|
||||
bborder: 1px solid black;
|
||||
}
|
||||
.hit {
|
||||
bborder: 1px solid green;
|
||||
display: inline-block;
|
||||
padding: 1em;
|
||||
max-width: 400px;
|
||||
vertical-align: top;
|
||||
}
|
||||
.tweet_image img {
|
||||
max-width: 400px;
|
||||
margin: 0.3em;
|
||||
}
|
||||
|
||||
</style>
|
||||
<!-- SCRIPTS -->
|
||||
<!-- import handlebars -->
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
|
||||
<!-- 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">
|
||||
<label>filters: </label>
|
||||
<label>images </label>
|
||||
<input id="radio_images" type="radio" checked/>
|
||||
<label>links </label>
|
||||
<input id="radio_links" type="radio"/>
|
||||
<label>videos </label>
|
||||
<input id="radio_videos" type="radio"/>
|
||||
<!-- search button -->
|
||||
<input type="button" value="Search" onclick="search();"/>
|
||||
</div>
|
||||
|
||||
<div id="results">
|
||||
<script id="results-template" type="text/x-handlebars-template">
|
||||
{{#each this}}
|
||||
<div class="hit">
|
||||
<div class='tweet_text'>{{text}}</div>
|
||||
{{#if images}}
|
||||
{{#each images}}
|
||||
<div class='tweet_image'>
|
||||
<img src={{this}} />
|
||||
</div>
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
</div>
|
||||
{{/each}}
|
||||
</script>
|
||||
</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');
|
||||
|
||||
// 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: ' + msg);
|
||||
compile_results_and_dispay(msg);
|
||||
});
|
||||
|
||||
// when search button is pressed
|
||||
function search() {
|
||||
|
||||
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) {
|
||||
filter = "images";
|
||||
document.getElementById("radio_images").checked = false;
|
||||
} else if (document.getElementById("radio_links").checked) {
|
||||
filter = "links";
|
||||
document.getElementById("radio_links").checked = false;
|
||||
} else if (document.getElementById("radio_videos").checked) {
|
||||
filter = "videos";
|
||||
document.getElementById("radio_videos").checked = false;
|
||||
}
|
||||
|
||||
// create search parameters that will be sent to the server
|
||||
var search_params = {keyword_value: keyword, nbr_hits: hits, filter_value: filter}
|
||||
|
||||
// send request to the server
|
||||
socket.emit('search_twitter', search_params);
|
||||
|
||||
}
|
||||
|
||||
function compile_results_and_dispay(results) {
|
||||
|
||||
var template_script = $("#results-template").html();
|
||||
|
||||
var template = Handlebars.compile(template_script);
|
||||
|
||||
$('.hit').remove();
|
||||
|
||||
$('#results').append(template(results));
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
16
week4/twitter-search/package.json
Normal file
16
week4/twitter-search/package.json
Normal file
@ -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"
|
||||
}
|
||||
}
|
||||
119
week4/twitter-search/server.js
Normal file
119
week4/twitter-search/server.js
Normal file
@ -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);
|
||||
});
|
||||
|
||||
6
week4/twitter-search/twitter_credentials.json
Normal file
6
week4/twitter-search/twitter_credentials.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"consumer_key": "jKMz31iuQMImQpDZOEKlFelb2",
|
||||
"consumer_secret": "IAfen9GcPSKsmiFZcXv23qVLDdI6PhHAFfJidc8UWbg8qIJnmH",
|
||||
"access_token_key": "801125183360745472-eMpgT1Cscw86Mm31KG8DTq336L5g4RB",
|
||||
"access_token_secret": "xZEuPLawdBwOgPCSWHaWsUAgbYlobAztYmwMMGeJvtsli"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user