408 lines
16 KiB
JavaScript
408 lines
16 KiB
JavaScript
const logger = require('log4js').getLogger('RightByMe');
|
|
const foursquare = require('node-foursquare-venues')('IXXFUGW3NC3DEVS2V5EU4NV4CL5E12AYGUPIR2D3U3B5DX4B', 'MZRIJDCEKUMVERA1OKVAIZI0TYAEBD3W2A2AGPTPI5TOLL1D', '20181111');
|
|
const Twitter = require('twitter');
|
|
const yelp = require('yelp-fusion');
|
|
const jsonfile = require('jsonfile');
|
|
const dateformat = require('dateformat');
|
|
|
|
const client = yelp.client('YlF_b6D149xr_xnrrYudlSnpn1A53b67vALlIK2HnD0ymBXQocRvPW3KjGN8jZNw0KnyAqxGaOzU7CLVPr84_KbnTxutNRXFVR9axmRqGN6ccda1xahoZo58KC2GWnYx');
|
|
|
|
const { get, isEmpty, has } = require('lodash');
|
|
|
|
const { reduceExplore, reduceYelp, reduceFullFS, reduceTwitter, reducePhotos } = require('./reducers/rightbyme');
|
|
|
|
const twitterClient = new Twitter({
|
|
'consumer_key': 'bJvwgjA9O52j7rC6mqoeefPLO',
|
|
'consumer_secret': 'NB6ASJxxMI9yaOgTAlWEpd18J1BdtIOYb4iz1HivIVpPqSLBY5',
|
|
'access_token_key': '4773651-Ix1Qemdg4k02UV6A4ZRmwgD94eEPmYb6Y77QkzpWZA',
|
|
'access_token_secret': 'RweUNv147YCAglOqk8myOVNuoVBvo6F9HPT3kM5ERQtR2'
|
|
});
|
|
|
|
logger.level = 'debug';
|
|
|
|
// google api key AIzaSyBl7O9LHIthCagcqIaDkQ4um_hghYG5reE
|
|
|
|
function nowTS() {
|
|
const now = new Date();
|
|
|
|
// return dateformat(now, 'yymmddMMHH');
|
|
|
|
return '';
|
|
}
|
|
|
|
function doFSVenueSearch(ll, data = {}) {
|
|
let payLoad = Object.assign({}, data);
|
|
logger.debug('>> doFSVenueSearch');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const fsObj = {
|
|
'll': ll,
|
|
'radius': 15,
|
|
'v': '20170801',
|
|
'limit': 1
|
|
};
|
|
|
|
if (isEmpty(payLoad))
|
|
foursquare.venues.search(fsObj, function(err, fsData) {
|
|
if (err) {
|
|
logger.debug('doFSVenueSearch', err);
|
|
|
|
return reject(err);
|
|
}
|
|
else {
|
|
const venues = get(fsData, 'response.venues');
|
|
if (venues.length > 0) {
|
|
fsP1 = venues[0];
|
|
|
|
payLoad = reduceExplore(fsP1);
|
|
|
|
jsonfile.writeFileSync(`output/${payLoad.id}-FSVenueSearch.json`, fsData);
|
|
|
|
return resolve(payLoad);
|
|
}
|
|
}
|
|
});
|
|
|
|
else
|
|
|
|
return resolve(payLoad);
|
|
});
|
|
}
|
|
|
|
function doFSVenueExplore(ll) {
|
|
let payLoad = {};
|
|
logger.debug('>> doFSVenueExplore', ll);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const fsObj = {
|
|
'll': ll,
|
|
'radius': 35,
|
|
'v': '20170801',
|
|
'limit': 1
|
|
};
|
|
|
|
foursquare.venues.explore(fsObj, function(err, fsData) {
|
|
if (err) {
|
|
console.log(err);
|
|
|
|
return reject(err);
|
|
}
|
|
else {
|
|
const groups = get(fsData, 'response.groups');
|
|
const items = groups[0];
|
|
const venues = items.items[0];
|
|
|
|
if (venues) {
|
|
fsP1 = venues.venue;
|
|
|
|
payLoad = reduceExplore(fsP1);
|
|
}
|
|
|
|
logger.debug(payLoad);
|
|
// jsonfile.writeFileSync(`output/${payLoad.id}-FSVenueExplore.json`, fsData);
|
|
|
|
return resolve(payLoad);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function doYelpSearch(data = {}) {
|
|
const payLoad = Object.assign({}, data);
|
|
logger.debug('>> doYelpSearch');
|
|
|
|
const yelpSearch = {};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
if (!isEmpty(payLoad)) {
|
|
yelpSearch.term = payLoad.name;
|
|
yelpSearch.location = payLoad.address;
|
|
yelpSearch.latitude = payLoad.latitude;
|
|
yelpSearch.longitude = payLoad.longitude;
|
|
|
|
yelpSearch.radius = 250;
|
|
yelpSearch.sort_by = 'distance';
|
|
logger.debug(yelpSearch);
|
|
client.search(yelpSearch).then(response => {
|
|
const respArray = get(response, 'jsonBody.businesses');
|
|
yelpReply = (respArray.length > 0) ? respArray[0] : {};
|
|
|
|
reduceYelp(yelpReply);
|
|
|
|
payLoad.yelp = reduceYelp(yelpReply);
|
|
|
|
jsonfile.writeFileSync(`output/${payLoad.id}-yelp.json`, response);
|
|
|
|
return resolve(payLoad);
|
|
}).catch(e => {
|
|
console.error('doYelpSearch', e);
|
|
|
|
return reject(e);
|
|
});
|
|
}
|
|
else
|
|
|
|
return resolve(payLoad);
|
|
});
|
|
}
|
|
|
|
function doFSGetFullVenue(data = {}) {
|
|
const payLoad = Object.assign({}, data);
|
|
logger.debug('>> doFSGetFullVenue');
|
|
|
|
// https://api.foursquare.com/v2/venues/4c5ff51213791b8d0d5c4eaf?client_id=IXXFUGW3NC3DEVS2V5EU4NV4CL5E12AYGUPIR2D3U3B5DX4B&client_secret=MZRIJDCEKUMVERA1OKVAIZI0TYAEBD3W2A2AGPTPI5TOLL1D&v=20180224
|
|
return new Promise((resolve, reject) => {
|
|
if (!isEmpty(payLoad)) {
|
|
// stuff
|
|
// more
|
|
const id = payLoad.id;
|
|
foursquare.venues.venue(id, {}, function(err, fsData) {
|
|
if (err)
|
|
return reject(err);
|
|
else {
|
|
// console.log(JSON.stringify(fsData));
|
|
const initPayload = (has(payLoad, 'name')) ? {} : reduceExplore(get(fsData, 'response.venue'));
|
|
const partPayload = reduceFullFS(get(fsData, 'response.venue'));
|
|
const newPayload = Object.assign(payLoad, initPayload, partPayload);
|
|
|
|
jsonfile.writeFileSync(`output/${payLoad.id}-FSGetFullVenue.json`, fsData);
|
|
|
|
return resolve(newPayload);
|
|
}
|
|
});
|
|
}
|
|
else
|
|
|
|
return resolve(payLoad);
|
|
});
|
|
}
|
|
|
|
function doFSGetPhotos(data = {}) {
|
|
const payLoad = Object.assign({}, data);
|
|
logger.debug('>> doFSGetPhotos');
|
|
|
|
// https://api.foursquare.com/v2/venues/4c5ff51213791b8d0d5c4eaf?client_id=IXXFUGW3NC3DEVS2V5EU4NV4CL5E12AYGUPIR2D3U3B5DX4B&client_secret=MZRIJDCEKUMVERA1OKVAIZI0TYAEBD3W2A2AGPTPI5TOLL1D&v=20180224
|
|
return new Promise((resolve, reject) => {
|
|
if (!isEmpty(payLoad)) {
|
|
// stuff
|
|
// more
|
|
const id = payLoad.id;
|
|
foursquare.venues.photos(id, { 'limit':16 }, function(err, fsData) {
|
|
if (err)
|
|
return reject(err);
|
|
else {
|
|
// console.log(JSON.stringify(fsData));
|
|
jsonfile.writeFileSync('output/FSGetPhotos.json', fsData);
|
|
logger.debug('fsData', JSON.stringify(fsData));
|
|
const initPayload = (has(payLoad, 'name')) ? {} : reduceExplore(get(fsData, 'response.venue'));
|
|
const partPayload = reducePhotos(fsData);
|
|
const newPayload = Object.assign(payLoad, initPayload, partPayload);
|
|
|
|
// payLoad.images = partPayload;
|
|
|
|
jsonfile.writeFileSync(`output/${payLoad.id}-FSGetPhotos.json`, fsData);
|
|
|
|
return resolve(newPayload);
|
|
}
|
|
});
|
|
}
|
|
else
|
|
|
|
return resolve(payLoad);
|
|
});
|
|
}
|
|
|
|
function doTweetSearch(data = {}) {
|
|
const payLoad = Object.assign({}, data);
|
|
logger.debug('>> doTweetSearch');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
if (!isEmpty(payLoad)) {
|
|
console.log('payLoad.twitter', payLoad.twitter);
|
|
if (payLoad.twitter !== '') {
|
|
const params = { 'screen_name': payLoad.twitter };
|
|
|
|
twitterClient.get('statuses/user_timeline', params, function(error, tweets, response) {
|
|
if (error) {
|
|
console.error('doTweetSearch', error);
|
|
|
|
return reject(error);
|
|
}
|
|
else {
|
|
// const partPayload = reduceFullFS(get(fsData, 'response.venue'));
|
|
// const newPayload = Object.assign(payLoad, partPayload);
|
|
|
|
// return resolve(newPayload);
|
|
const reducedTweets = reduceTwitter(tweets);
|
|
|
|
payLoad.tweets = reducedTweets;
|
|
|
|
jsonfile.writeFileSync(`output/${payLoad.id}-Tweets.json`, tweets);
|
|
|
|
return resolve(payLoad);
|
|
}
|
|
});
|
|
}
|
|
else
|
|
return resolve(payLoad);
|
|
}
|
|
else
|
|
|
|
return resolve(payLoad);
|
|
});
|
|
}
|
|
|
|
function doGetRightByMe(ll) {
|
|
return new Promise((resolve, reject) => {
|
|
doFSVenueExplore(ll)
|
|
.then((d) => {
|
|
return doFSVenueSearch(ll, d);
|
|
})
|
|
.catch((e) => {
|
|
logger.error(e);
|
|
|
|
return reject(e);
|
|
// res.status(500).send('There was an error!');
|
|
})
|
|
.then((d) => {
|
|
return doYelpSearch(d);
|
|
})
|
|
.catch((e) => {
|
|
logger.error(e);
|
|
|
|
return reject(e);
|
|
// res.status(500).send('There was an error!');
|
|
})
|
|
.then((d) => {
|
|
// return doYelpSearch(d)
|
|
return doTweetSearch(d);
|
|
})
|
|
.then((d) => {
|
|
// return doYelpSearch(d)
|
|
return doFSGetFullVenue(d);
|
|
})
|
|
.catch((e) => {
|
|
logger.error(e);
|
|
|
|
return reject(e);
|
|
// res.status(500).send('There was an error!');
|
|
})
|
|
.then((d) => {
|
|
logger.info('Final', d.name, d.id);
|
|
jsonfile.writeFileSync(`output/${d.id}-doGetRightByMe.json`, d);
|
|
|
|
return resolve(d);
|
|
});
|
|
});
|
|
}
|
|
|
|
function doGetMoreDetail(id) {
|
|
return new Promise((resolve, reject) => {
|
|
doFSGetFullVenue({ 'id':id })
|
|
.catch((e) => {
|
|
logger.error(e);
|
|
|
|
return reject(e);
|
|
// res.status(500).send('There was an error!');
|
|
})
|
|
.then((d) => {
|
|
return doYelpSearch(d);
|
|
})
|
|
.catch((e) => {
|
|
logger.error(e);
|
|
|
|
return reject(e);
|
|
// res.status(500).send('There was an error!');
|
|
})
|
|
.then((d) => {
|
|
// return doYelpSearch(d)
|
|
return doTweetSearch(d);
|
|
})
|
|
.catch((e) => {
|
|
logger.error(e);
|
|
|
|
return reject(e);
|
|
// res.status(500).send('There was an error!');
|
|
})
|
|
.then((d) => {
|
|
logger.info('Final', (d.name || '***** Name Missing'), d.id);
|
|
jsonfile.writeFileSync(`output/${d.id}-doGetMoreDetail.json`, d);
|
|
|
|
return resolve(d);
|
|
});
|
|
});
|
|
}
|
|
|
|
async function test(ll, near) {
|
|
return new Promise((resolve, reject) => {
|
|
doFSVenueExplore(ll)
|
|
.then((d) => {
|
|
return doFSVenueSearch(ll, d);
|
|
})
|
|
.catch((e) => {
|
|
logger.error(e);
|
|
|
|
return reject(e);
|
|
// res.status(500).send('There was an error!');
|
|
})
|
|
.then((d) => {
|
|
return doYelpSearch(d);
|
|
})
|
|
.catch((e) => {
|
|
logger.error(e);
|
|
|
|
return reject(e);
|
|
// res.status(500).send('There was an error!');
|
|
})
|
|
.then((d) => {
|
|
// return doYelpSearch(d)
|
|
return doTweetSearch(d);
|
|
})
|
|
.then((d) => {
|
|
// return doYelpSearch(d)
|
|
return doFSGetFullVenue(d);
|
|
})
|
|
.catch((e) => {
|
|
logger.error(e);
|
|
|
|
return reject(e);
|
|
// res.status(500).send('There was an error!');
|
|
})
|
|
.then((d) => {
|
|
logger.info('Final', d.name, d.id);;
|
|
|
|
return resolve(d);
|
|
});
|
|
});
|
|
}
|
|
module.exports = { doGetRightByMe, doGetMoreDetail };
|
|
|
|
const tests = {
|
|
'cruachan' : '56.3946,-5.1166',
|
|
'morrisonsGarage' : '55.9429,-4.5622',
|
|
'tescohelensburgh' : '56.003466,-4.733689',
|
|
'howlinwolf' : '55.863991,-4.257788'
|
|
};
|
|
|
|
// doGetRightByMe('56.3946,-5.1166'); // cruachan
|
|
|
|
// doGetRightByMe('55.9429,-4.5622'); // morrisons garage
|
|
|
|
// 56.3890134, "lng" : -5.0939317
|
|
|
|
// cruachan 56.3946,-5.1166
|
|
|
|
// tesco helensburgh
|
|
// const location = { 'homeDistance':12.941340256604686, 'workDistance':33.47577415510536, 'latitude':56.003466, 'longitude':-4.733689, 'atHome':false, 'atWork':false, 'timestamp':1519638618383, 'll':'56.003466,-4.733689', 'llFixed':'56.003,-4.734', 'city':'Helensburgh', 'cityCC':'Helensburgh,GB', 'address':'9-13 Sinclair St, Helensburgh G84, UK' };
|
|
|
|
// test('55.863991,-4.257788');
|
|
|
|
// howling wolf http://localhost:8110/rightbyme?ll=55.863991,-4.257788
|
|
|
|
/*
|
|
{"response":{"result":[{"cards":[{"card_id":"83a014c3-c0a4-3242-b5a8-79d011b677d6","rendering_engine":"custom","type":"WEATHER","type_display_name":"Weather","ttl":1519810720,"layout":{"template":"weather"},"data":{},"modules":{},"reason":"","notify":false,"ranking_arguments":{"STREAM_TYPE:MAIN":1.0,"rule_score":3.0,"TIER":3.0,"TIME_OF_DAY_RANGE:MORNING":1.0,"score":0.6},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"c81eac4b-4553-3d60-85a6-178968bd5065","rendering_engine":"custom","type":"NEWS_DIGEST","type_display_name":"News Digest","ttl":1519810720,"layout":{"template":"news_digest"},"data":{},"modules":{},"reason":"","notify":false,"ranking_arguments":{"STREAM_TYPE:MAIN":1.0,"rule_score":2.0,"TIER":2.0,"TIME_OF_DAY_RANGE:MORNING":1.0,"score":0.55},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"4b5b8c71-58d2-3f26-abce-318e3d3a01d6","rendering_engine":"custom","type":"MORNING_NIGHT","type_display_name":"Set Alarm","ttl":1519810720,"layout":{"template":"morning_night"},"data":{},"modules":{},"reason":"","notify":false,"ranking_arguments":{"USER_LOCATION:OTHER":1.0,"USER_SPEED:STILL":1.0,"STREAM_TYPE:MAIN":1.0,"rule_score":3.0,"TIER":3.0,"TIME_OF_DAY_RANGE:MORNING":1.0,"score":0.5},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"bcacee7a-d79a-3e4e-a5b8-77c5b6480a8c","rendering_engine":"custom","type":"CALENDAR","type_display_name":"Calendar","ttl":1519810720,"layout":{"template":"calendar"},"data":{},"modules":{},"reason":"","notify":false,"ranking_arguments":{"USER_LOCATION:OTHER":1.0,"STREAM_TYPE:MAIN":1.0,"rule_score":2.0,"TIER":2.0,"TIME_OF_DAY_RANGE:MORNING":1.0,"score":0.25},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"9953c0e9-5b77-3162-bcf4-e19fcd1f541f","rendering_engine":"custom","type":"VENUE_INFO","type_display_name":"Venues","ttl":1519641520,"layout":{"template":"venue_info"},"data":{"name":"Tesco","category":"Grocery Store","iconUrl":"https://ss3.4sqi.net/img/categories_v2/shops/food_grocery_64.png","id":"4c5ff51213791b8d0d5c4eaf","provider":"foursquare","tips":["Prepare to be deafened by relief calls over the loudspeaker"],"images":[],"address":"23-25 Sinclair Street","city":"Helensburgh","state":"Argyll and Bute","zip":"G84 8SR","twitter":{"handle":"uktescooffers","viewIntent":"https://twitter.com/uktescooffers#Intent;action=android.intent.action.VIEW;package=com.twitter.android;end","tweetIntent":"#Intent;action=android.intent.action.SEND;component=com.twitter.android/.PostActivity;S.android.intent.extra.TEXT=@uktescooffers;end"},"yelp":{"url":"https://m.yelp.com/biz/tesco-stores-helensburgh?adjust_creative=ogmBMO91tbdmscbTIaQEdA&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ogmBMO91tbdmscbTIaQEdA","rating":3.5,"reviewCount":4,"viewIntent":"https://m.yelp.com/biz/tesco-stores-helensburgh?adjust_creative=ogmBMO91tbdmscbTIaQEdA&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ogmBMO91tbdmscbTIaQEdA#Intent;action=android.intent.action.VIEW;end"}},"modules":{},"reason":"","notify":false,"ranking_arguments":{"poi_latitude":56.00348039818216,"poi_longitude":-4.733884334564209,"USER_LOCATION:OTHER":1.0,"req_longitude":-4.7336884,"req_latitude":56.003464,"STREAM_TYPE:MAIN":1.0,"USER_LOCATION_POI_CONFIDENCE:HIGH":1.0,"rule_score":0.0,"TIER":0.0,"USER_DIST_FROM_REQ_LOC:WITHIN_100M":1.0,"score":0.25},"instrumentation":{"rid":"13kfji9d97ld0","bucket":"ga"}},{"card_id":"eea4f59a-e8a2-3e12-a917-96e254c416d5","rendering_engine":"custom","type":"VENUE_CHOOSER","type_display_name":"Around Me","ttl":1519641520,"layout":{"template":"venue_chooser"},"data":{"venues":[{"name":"Lido's Fish & Chips","category":"Fish & Chips Shop","iconUrl":"https://ss3.4sqi.net/img/categories_v2/food/fishandchips_64.png","id":"4b9a9a77f964a5207ec535e3","provider":"foursquare","eid":"{\"card\":\"venue\",\"id\":\"4b9a9a77f964a5207ec535e3\"}"},{"name":"Mr Kebab","category":"Fast Food Restaurant","iconUrl":"https://ss3.4sqi.net/img/categories_v2/food/fastfo
|
|
|
|
{"response":{"result":[{"cards":[{"card_id":"9953c0e9-5b77-3162-bcf4-e19fcd1f541f","rendering_engine":"custom","type":"VENUE_INFO","type_display_name":"Venues","ttl":1519654292,"layout":{"template":"venue_info"},"data":{"name":"Speirs Wharf","category":"Pool","iconUrl":"https://ss3.4sqi.net/img/categories_v2/parks_outdoors/pool_64.png","id":"5211c15a11d2c83298e7f8c2","provider":"foursquare","tips":[],"images":["https://igx.4sqi.net/img/general/640x640/72939115_8s_CraZskGLT5uJz5DzRiyBvKXgy6BobQBH5u0Vcsy4.jpg","https://igx.4sqi.net/img/general/960x364/62519220_Z_xAdlllLg7Mb1BUt1b9G18IKTpF1jyeNqGCMo0WhRs.jpg","https://igx.4sqi.net/img/general/960x541/24390977_Ws2HHTyM1zKBlvejccG1BxGTt0hKBURtWQOPPxYc7IM.jpg","https://igx.4sqi.net/img/general/960x960/149524__j5bRwPX5zy5RmOu-uleVtQ-KekAS-rXAJLgc_JjipY.jpg","https://igx.4sqi.net/img/general/640x640/4894760_ZOelH_F7ljq0Mjk6Zzs7KHH8-Dn470lbq4IfFg7WGiM.jpg","https://igx.4sqi.net/img/general/640x640/4894760_kW7NmETQYJxkGfBBnhHhQdDHcVouBj7OWpZBbyy0OTM.jpg"],"telephone":"","address":"","city":"","state":"","zip":"","latitude":55.872669822119796,"longitude":-4.257480441815151,"twitter":{"handle":null,"viewIntent":null,"tweetIntent":null},"yelp":{"url":null,"rating":0.0,"reviewCount":0,"viewIntent":null}},"modules":{},"reason":"","notify":false,"ranking_arguments":{"poi_latitude":55.872669822119796,"poi_longitude":-4.257480441815151,"USER_LOCATION:OTHER":1.0,"req_longitude":-4.2577868,"req_latitude":55.86399,"USER_LOCATION_POI_CONFIDENCE:HIGH":1.0,"STREAM_TYPE":1.0,"rule_score":0.0,"TIER":0.0,"USER_DIST_FROM_REQ_LOC:WITHIN_100M":1.0,"score":0.0},"instrumentation":{"rid":"fn62krdd981s4","bucket":"ga"}}]}],"error":null}}
|
|
*/
|
|
|