week6
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>SEARCH INSTAGRAM</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 INSTAGRAM</label>
|
||||
<div id="frame">
|
||||
<!-- inputs for the search to send to server -->
|
||||
<div id="inputs">
|
||||
<label>user-id: </label>
|
||||
<input id="user_id_field" type="text"/>
|
||||
<label>keyword: </label>
|
||||
<input id="keyword_field" type="text"/>
|
||||
<input id="search_button" type="button" value="Search" onclick="search();">
|
||||
</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_instagram_results')
|
||||
socket.on('search_instagram_results', function(msg) {
|
||||
console.log('search_instagram_results: ' + JSON.stringify(msg));
|
||||
});
|
||||
|
||||
// when search button is pressed
|
||||
function search() {
|
||||
|
||||
var input_keyword = document.getElementById('keyword_field').value;
|
||||
var input_user_id = document.getElementById('user_id_field').value;
|
||||
|
||||
socket.emit('search_instagram', {keyword: input_keyword, user_id: input_user_id});
|
||||
|
||||
}
|
||||
|
||||
|
||||
</script>
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"client_id": "c566ffdba5224164a7ac0bbf3920cf36",
|
||||
"client_secret": "c186b57d41a74ceb9432dc2cc2a44fa5"
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
Reference in New Issue
Block a user