166 lines
4.9 KiB
JavaScript
166 lines
4.9 KiB
JavaScript
const logger = require('log4js').getLogger('FS 🔧');
|
|
|
|
const { get, isEmpty } = require('lodash');
|
|
|
|
// Bearer YlF_b6D149xr_xnrrYudlSnpn1A53b67vALlIK2HnD0ymBXQocRvPW3KjGN8jZNw0KnyAqxGaOzU7CLVPr84_KbnTxutNRXFVR9axmRqGN6ccda1xahoZo58KC2GWnYx'
|
|
|
|
|
|
// google api key AIzaSyBl7O9LHIthCagcqIaDkQ4um_hghYG5reE
|
|
|
|
logger.level = 'debug';
|
|
|
|
function reduceExplore(data) {
|
|
const cleaner = /\((.*?)\)/g;
|
|
const obj = {};
|
|
if (typeof data === 'undefined' || isEmpty(data)) return obj;
|
|
const { categories, location, contact } = data;
|
|
|
|
// console.log(contact);
|
|
// make copy of object;
|
|
|
|
const localObj = Object.assign({}, data);
|
|
|
|
const iconPrefix = get(categories[0], 'icon.prefix', '');
|
|
const iconSuffix = get(categories[0], 'icon.suffix', '');
|
|
|
|
obj.name = get(localObj, 'name', '');
|
|
obj.description = get(localObj, 'description', '');
|
|
obj.category = get(categories[0], 'shortName', '');
|
|
obj.icon = (iconPrefix !== '') ? `${iconPrefix}64${iconSuffix}` : '';
|
|
obj.id = get(localObj, 'id', '');
|
|
obj.provider = 'foursquare';
|
|
obj.address = get(location, 'formattedAddress', []).join(', ').replace(cleaner, '');
|
|
obj.city = get(location, 'city', '');
|
|
obj.state = get(location, 'state', '');
|
|
obj.postcode = get(location, 'postalCode', '');
|
|
obj.twitter = get(contact, 'twitter', '');
|
|
obj.facebook = get(contact, 'facebookName', '');
|
|
obj.url = get(localObj, 'canonicalUrl', '');
|
|
obj.latitude = get(location, 'lat', '');
|
|
obj.longitude = get(location, 'lng', '');
|
|
|
|
// logger.debug(JSON.stringify(obj));
|
|
|
|
return obj;
|
|
}
|
|
|
|
function reduceYelp(data) {
|
|
const obj = {};
|
|
|
|
if (typeof data === 'undefined' || isEmpty(data)) return obj;
|
|
const yelpUrlfixer = /([--:\w?@%&+~#=]*\.[a-z]{2,4}\/{0,2})((?:[?&](?:\w+)=(?:\w+))+|[--:\w?@%&+~#=]+)?/g;
|
|
|
|
const localObj = Object.assign({}, data);
|
|
|
|
obj.url = get(localObj, 'url', '');
|
|
obj.rating = get(localObj, 'rating', '');
|
|
obj.reviewCount = get(localObj, 'review_count', '');
|
|
|
|
if (obj.url !== '') {
|
|
const url = yelpUrlfixer.exec(obj.url);
|
|
const urlBit = url[2];
|
|
obj.viewIntent = `https://m.yelp.com/${urlBit}`;
|
|
}
|
|
else
|
|
obj.viewIntent = '';
|
|
|
|
return obj;
|
|
}
|
|
|
|
function reduceFullFS(data) {
|
|
const obj = {};
|
|
if (typeof data === 'undefined' || isEmpty(data)) return obj;
|
|
const localObj = Object.assign({}, data);
|
|
const photoBlob = get(localObj, 'photos.groups');
|
|
let photoItems;
|
|
|
|
for (const i of photoBlob)
|
|
if (i.type === 'venue')
|
|
photoItems = i.items;
|
|
|
|
const photosCount = (typeof(photoItems) !== 'undefined' && photoItems !== null) ? photoItems.length : 0;
|
|
const tipsCount = get(localObj, 'tips.count', 0);
|
|
|
|
if (photosCount > 0)
|
|
obj.images = photoItems.map(item => {
|
|
const prefix = get(item, 'prefix', '');
|
|
const suffix = get(item, 'suffix', '');
|
|
const width = get(item, 'width', 640);
|
|
const height = get(item, 'height', 480);
|
|
|
|
const ratio = width / 640;
|
|
let ratioHeight = ~~(height / ratio);
|
|
if (ratioHeight <= 0) ratioHeight = 640;
|
|
|
|
const fsImgPath = `${prefix}${width}x${height}${suffix}`;
|
|
|
|
console.log(`https://image.silvrtree.co.uk/${640}x${ratioHeight},fit,q80/${fsImgPath}`);
|
|
|
|
// return `${prefix}${640}x${ratioHeight}${suffix}`;
|
|
|
|
return `https://image.silvrtree.co.uk/${640}x${ratioHeight},fit,q80/${fsImgPath}`;
|
|
});
|
|
|
|
if (tipsCount > 0) {
|
|
const tipItems = get(localObj, 'tips.groups[0].items');
|
|
obj.tips = tipItems.map(item => {
|
|
return get(item, 'text', '');
|
|
});
|
|
}
|
|
|
|
obj.menuUrl = get(localObj, 'menu.mobileUrl');
|
|
|
|
return obj;
|
|
}
|
|
|
|
function reduceTwitter(data) {
|
|
const urlReg = /(http|ftp|https):\/\/([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?/;
|
|
let obj = [];
|
|
if (data.length > 0)
|
|
obj = data.map(item => {
|
|
let text = get(item, 'text');
|
|
const urlTest = urlReg.exec(text);
|
|
|
|
if (urlTest !== null) {
|
|
const newUrl = `<a hef='${urlTest[0]}'>${urlTest[0]}</a>`;
|
|
text = text.replace(urlTest[0], newUrl);
|
|
}
|
|
|
|
return text;
|
|
});
|
|
|
|
return obj;
|
|
}
|
|
|
|
function reducePhotos(data) {
|
|
const obj = {};
|
|
if (typeof data === 'undefined' || isEmpty(data)) return obj;
|
|
const localObj = Object.assign({}, data);
|
|
const photoBlob = get(localObj, 'response.photos');
|
|
let photoItems;
|
|
|
|
photoItems = photoBlob.items;
|
|
|
|
const photosCount = (typeof(photoItems) !== 'undefined' && photoItems !== null) ? photoItems.length : 0;
|
|
|
|
if (photosCount > 0)
|
|
|
|
obj.images = photoItems.map(item => {
|
|
const prefix = get(item, 'prefix', '');
|
|
const suffix = get(item, 'suffix', '');
|
|
const width = get(item, 'width', 640);
|
|
const height = get(item, 'height', 480);
|
|
|
|
const ratio = width / 640;
|
|
let ratioHeight = ~~(height / ratio);
|
|
if (ratioHeight <= 0) ratioHeight = 640;
|
|
console.log(`${width}, ${height} => ${640}, ${ratioHeight}`);
|
|
|
|
return `${prefix}${640}x${ratioHeight}${suffix}`;
|
|
});
|
|
|
|
return obj;
|
|
}
|
|
|
|
module.exports = { reduceExplore, reduceYelp, reduceFullFS, reduceTwitter, reducePhotos };
|