Created
June 10, 2015 19:02
-
-
Save ismaelc/d7120b3422b7f16cb576 to your computer and use it in GitHub Desktop.
Rough concept for app recommendation engine powered by Concur Itinerary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
var async = require('async'); | |
var gplay = require('google-play-scraper'); | |
var querystring = require('querystring'); | |
var https = require('https'); | |
app.set('port', (process.env.PORT || 5000)); | |
app.use(express.static(__dirname + '/public')); | |
app.get('/', function(request, response) { | |
//response.send('Hello World!'); | |
async.waterfall([ | |
// | |
// 1. Call separate service that gets Concur user's trip segments | |
// Check README of https://github.com/ismaelc/Concur-QuickApp | |
// for a live sample data | |
// | |
function(callback) { | |
var host = "radiant-springs-1767.herokuapp.com"; | |
var endpoint = "/segments"; | |
doRequest(host, endpoint, 'GET', {}, | |
function(concurData) { | |
callback(null, concurData); | |
}); | |
}, | |
// | |
// 2. Pick out upcoming segment | |
// | |
function(concurItinArr, callback) { | |
var i = 0; | |
var err = null; | |
while(i < concurItinArr.length) { | |
if(concurItinArr[i].IsNext == "true") break; | |
i++; | |
} | |
if(i >= concurItinArr.length) err = "No upcoming segment."; | |
else result = concurItinArr[i].Location; | |
callback(err, result); | |
}, | |
// | |
// 3. Call google-play-scraper 'search' using segment's city | |
// | |
function(concurLocation, callback) { | |
gplay.search({ | |
term: concurLocation.city, | |
num: 15 | |
}).then(function(value) { | |
callback(null, value); | |
}, function(reason) { | |
callback(reason, value); | |
}); | |
}, | |
// | |
// 4. Display app icons using good 'ol <li> tags | |
// | |
function(appsArray, callback) { | |
callback(null, returnHTMList(appsArray)); | |
} | |
], function(err, result) { | |
response.send(result); | |
} | |
); | |
}); | |
app.listen(app.get('port'), function() { | |
console.log('Node app is running on port', app.get('port')); | |
}); | |
// UTILS | |
function doRequest(host, endpoint, method, data, success) { | |
var dataString = JSON.stringify(data); | |
var headers = {}; | |
if (method == 'GET') { | |
endpoint += '?' + querystring.stringify(data); | |
} | |
else { | |
headers = { | |
'Content-Type': 'application/json', | |
'Content-Length': dataString.length | |
}; | |
} | |
var options = { | |
host: host, | |
path: endpoint, | |
method: method, | |
headers: headers | |
}; | |
var req = https.request(options, function(res) { | |
res.setEncoding('utf-8'); | |
var responseString = ''; | |
res.on('data', function(data) { | |
responseString += data; | |
}); | |
res.on('end', function() { | |
console.log(responseString); | |
var responseObject = JSON.parse(responseString); | |
success(responseObject); | |
}); | |
}); | |
req.write(dataString); | |
req.end(); | |
} | |
function returnHTMList(appsArray) { | |
var html = "<ul>"; | |
for(var i = 0; i < appsArray.length; i++) { | |
html += "<li style='display:inline'>" + "<img width='150px' src='" + appsArray[i].icon + "' />" + "</li>" | |
} | |
html = html + "</ul>"; | |
return html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment