From de732246dd9de5cc5c4e17e38a0bb1ff00601248 Mon Sep 17 00:00:00 2001 From: gauthiier Date: Wed, 7 Dec 2016 12:27:41 +0100 Subject: [PATCH] week6 --- week6/instagram-search/index.html | 69 +++++++++ .../instagram_credentials.json | 4 + week6/instagram-search/package.json | 16 ++ week6/instagram-search/server.js | 134 +++++++++++++++++ week6/notes.md | 19 +++ week6/spotify-search/index.html | 113 ++++++++++++++ week6/spotify-search/package.json | 16 ++ week6/spotify-search/server.js | 109 ++++++++++++++ week6/spotify-search/spotify_credentials.json | 4 + week6/twitter-search-geo/index.html | 139 ++++++++++++++++++ week6/twitter-search-geo/package.json | 16 ++ week6/twitter-search-geo/server.js | 110 ++++++++++++++ .../twitter_credentials.json | 6 + 13 files changed, 755 insertions(+) create mode 100644 week6/instagram-search/index.html create mode 100644 week6/instagram-search/instagram_credentials.json create mode 100644 week6/instagram-search/package.json create mode 100644 week6/instagram-search/server.js create mode 100644 week6/notes.md create mode 100644 week6/spotify-search/index.html create mode 100644 week6/spotify-search/package.json create mode 100644 week6/spotify-search/server.js create mode 100644 week6/spotify-search/spotify_credentials.json create mode 100644 week6/twitter-search-geo/index.html create mode 100644 week6/twitter-search-geo/package.json create mode 100644 week6/twitter-search-geo/server.js create mode 100644 week6/twitter-search-geo/twitter_credentials.json diff --git a/week6/instagram-search/index.html b/week6/instagram-search/index.html new file mode 100644 index 0000000..fda94b9 --- /dev/null +++ b/week6/instagram-search/index.html @@ -0,0 +1,69 @@ + + + SEARCH INSTAGRAM + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + + + + + \ No newline at end of file diff --git a/week6/instagram-search/instagram_credentials.json b/week6/instagram-search/instagram_credentials.json new file mode 100644 index 0000000..0472610 --- /dev/null +++ b/week6/instagram-search/instagram_credentials.json @@ -0,0 +1,4 @@ +{ + "client_id": "c566ffdba5224164a7ac0bbf3920cf36", + "client_secret": "c186b57d41a74ceb9432dc2cc2a44fa5" +} \ No newline at end of file diff --git a/week6/instagram-search/package.json b/week6/instagram-search/package.json new file mode 100644 index 0000000..5d02c11 --- /dev/null +++ b/week6/instagram-search/package.json @@ -0,0 +1,16 @@ +{ + "name": "instagram-search", + "version": "1.0.0", + "description": "basic search with instagram api", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "gauthier", + "license": "ISC", + "dependencies": { + "express": "^4.14.0", + "nodegram": "^0.2.3", + "socket.io": "^1.7.1" + } +} diff --git a/week6/instagram-search/server.js b/week6/instagram-search/server.js new file mode 100644 index 0000000..27a0960 --- /dev/null +++ b/week6/instagram-search/server.js @@ -0,0 +1,134 @@ +// 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 nodegram +// https://github.com/zzarcon/nodegram +var Nodegram = require('nodegram'); // npm install --save nodegram + +/* ----------------------------------- + Authetication (OAuth) Configuration +--------------------------------------*/ + +// access token use to autheticate with Instagram +var access_token = null; + +// load configuration file with credentials, secrets, etc. +var config_file = "./instagram_credentials.json"; +var config = JSON.parse(fs.readFileSync(config_file, "utf8")); + +var options = { + clientId: config.client_id, + clientSecret: config.client_secret, + redirectUri: 'http://localhost:8088/callback' +} + +var gram = new Nodegram(options); + +app.get('/auth', function authorize_user(req, res) { + + var uri = gram.getAuthUrl(); + res.redirect(uri); +}); + +app.get('/callback', function authorize_user(req, res) { + + gram.getAccessToken(req.query.code).then(function(resp) { + var token = resp.access_token; + access_token = token; + console.log(resp.user); + res.redirect('/'); + }); + +}); + +/*------------------------ + INSTAGRAM FUNCTIONS +--------------------------*/ + +function search_for_username(username, callback) { + + // example taken from https://github.com/zzarcon/nodegram + + var instagram = new Nodegram({accessToken: access_token}); + + /* ---------------------------------- + This won't work unless you have + accounts are added to the app's + sandbox -- re: give you permissions + -------------------------------------*/ + + instagram.get('/users/search', {q: username}).then(function(res, pag) { + + callback(res); + + }).catch(function(err) { + + console.log(err); + + }); + +} + +/* ------------------- + Configure Socket.io +----------------------*/ + +// 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_instagram', function(msg) { + + console.log('searching instagram with: ' + JSON.stringify(msg)); + + search_for_username(msg.user_id, function(results) { + // send raw results + io.emit('search_instagram_results', results); + }); + + }); + + socket.on('disconnet', function() { + + console.log('got a disconnection'); + + }); + +}); + +/* ------------------------------------ + Main entry point for the server +----------------------------------------*/ + +app.get('/', function(req, res) { + // we the client is not authenticated yet, redirect to /auth (see line: 37) + if(access_token === null) { + res.redirect('/auth'); + } + // if already autheticated serve index.html + else { + res.sendFile(__dirname + '/index.html'); + } +}); + +/* ------------------- + Start the server +----------------------*/ + +// listen to connection on port 8088 --> http://localhost:8088 +server.listen(8088, function () { + console.log('listening on port: ' + 8088); +}); \ No newline at end of file diff --git a/week6/notes.md b/week6/notes.md new file mode 100644 index 0000000..b5917ea --- /dev/null +++ b/week6/notes.md @@ -0,0 +1,19 @@ +#### (working) notes + +## Instagram: + +Node: [Nodegram](https://github.com/zzarcon/nodegram) + +[Instagram API Documentation](https://www.instagram.com/developer/) + +## Twitter: + +Node: [twitter](https://github.com/desmondmorris/node-twitter) + +[Twitter API Documentation](https://dev.twitter.com/rest/public) + +## Spotify: + +Node: [Spotify Web API Node](https://github.com/thelinmichael/spotify-web-api-node) + +[Spotify API Documentation](https://developer.spotify.com/web-api/) diff --git a/week6/spotify-search/index.html b/week6/spotify-search/index.html new file mode 100644 index 0000000..c3aea18 --- /dev/null +++ b/week6/spotify-search/index.html @@ -0,0 +1,113 @@ + + + SEARCH SPOTIFY + + + + + + + + + + +
+ +
+ + + +
+ +
+ +
+ +
+ + + + + + + + \ No newline at end of file diff --git a/week6/spotify-search/package.json b/week6/spotify-search/package.json new file mode 100644 index 0000000..0e7bc17 --- /dev/null +++ b/week6/spotify-search/package.json @@ -0,0 +1,16 @@ +{ + "name": "spotify", + "version": "1.0.0", + "description": "spotify api sandbox", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "gauthier", + "license": "ISC", + "dependencies": { + "express": "^4.14.0", + "socket.io": "^1.7.1", + "spotify-web-api-node": "^2.3.6" + } +} diff --git a/week6/spotify-search/server.js b/week6/spotify-search/server.js new file mode 100644 index 0000000..ff7ac35 --- /dev/null +++ b/week6/spotify-search/server.js @@ -0,0 +1,109 @@ +// 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 spotify we api +var SpotifyWebApi = require('spotify-web-api-node'); + +/* ----------------------------------- + Spotify Authetication (OAuth) Configuration +--------------------------------------*/ + +// access token use to autheticate with Instagram +var access_token = null; + +// load configuration file with credentials, secrets, etc. +var config_file = "./spotify_credentials.json"; +var config = JSON.parse(fs.readFileSync(config_file, "utf8")); + +var spotify = new SpotifyWebApi({ + clientId : config.client_id, + clientSecret : config.client_secret +}); + + +spotify.clientCredentialsGrant() + .then(function(data) { + spotify.setAccessToken(data.body.access_token); + }, function(err) { + console.log('Something went wrong when retrieving an access token', err.message); + }); + +/*------------------------ + SPOTIFY FUNCTIONS +--------------------------*/ + +function search_for_tracks(keyword, callback) { + + console.log('search_for_tracks: ' + keyword); + + spotify.searchTracks(keyword, null, function(err, data) { + + if (err) { + console.error('Something went wrong', err.message); + return; + } + + callback(data.body.tracks.items); + + }); + +} + + +/* ----------------------------- + Configure Socket.io +--------------------------------*/ + +// 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_spotify', function(msg) { + + console.log('searching spotify with: ' + JSON.stringify(msg)); + + search_for_tracks(msg.keyword, function(results) { + // send raw results + io.emit('search_spotify_results', results); + }); + + + }); + + socket.on('disconnet', function() { + + console.log('got a disconnection'); + + }); + +}); + +/* ------------------------------------ + Main entry point for the server +----------------------------------------*/ + +app.get('/', function(req, res) { + res.sendFile(__dirname + '/index.html'); +}); + +/* ------------------- + 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/week6/spotify-search/spotify_credentials.json b/week6/spotify-search/spotify_credentials.json new file mode 100644 index 0000000..17c366f --- /dev/null +++ b/week6/spotify-search/spotify_credentials.json @@ -0,0 +1,4 @@ +{ + "client_id": "503a0d7805bf49e9ae1d3880c9b15af4", + "client_secret": "2bd7aebb3d5a4b7c96d4352659a5b045" +} \ No newline at end of file diff --git a/week6/twitter-search-geo/index.html b/week6/twitter-search-geo/index.html new file mode 100644 index 0000000..57f7911 --- /dev/null +++ b/week6/twitter-search-geo/index.html @@ -0,0 +1,139 @@ + + + SEARCH TWITTER + + + + + + + + +
+ +
+ + + + + +
+ +
+ +
+ + + + + + + + + \ No newline at end of file diff --git a/week6/twitter-search-geo/package.json b/week6/twitter-search-geo/package.json new file mode 100644 index 0000000..06ea70e --- /dev/null +++ b/week6/twitter-search-geo/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/week6/twitter-search-geo/server.js b/week6/twitter-search-geo/server.js new file mode 100644 index 0000000..257d852 --- /dev/null +++ b/week6/twitter-search-geo/server.js @@ -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); +}); + diff --git a/week6/twitter-search-geo/twitter_credentials.json b/week6/twitter-search-geo/twitter_credentials.json new file mode 100644 index 0000000..08fe993 --- /dev/null +++ b/week6/twitter-search-geo/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