Comparing version 0.0.4 to 0.0.5
@@ -1,1 +0,1 @@ | ||
'use strict';var _Promise='undefined'==typeof Promise?require('es6-promise').Promise:Promise,fetch=require('axios'),weekdays=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];function busy_hours(a,b){var c=require('@google/maps').createClient({key:b,Promise:_Promise}),d=function(a){return{hour:a[0],percentage:a[1]}},e=function(a){var b=['APP_INITIALIZATION_STATE=','window.APP_FLAGS'],c=a.substring(a.lastIndexOf(b[0])+b[0].length,a.lastIndexOf(b[1])),d=eval(c),e=eval(d[3][6].replace(')]}\'',''));return e[0][1][0][14][84]},f=function(a){if(a.data){var b=e(a.data);if(null===b)return{status:'error',message:'Place has no popular hours'};var c={status:'ok',week:Array.from(Array(7).keys()).map(function(a){var c=[];return b[0][a]&&b[0][a][1]&&(c=Array.from(b[0][a][1]).map(function(a){return d(a)})),{day:weekdays[a],hours:c}})},f=b[7];return void 0!==f&&(c.now=d(f)),c}return{status:'error'}},g=function(a){var b=a.json.result.url;return b?fetch({url:b,headers:{"User-Agent":'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'}}):{status:'error',message:'Invalid url'}},h=function(a){return{status:'error',message:a}},i=c.place({placeid:a},h).asPromise().then(g).then(f);return i}module.exports=busy_hours; | ||
'use strict';var _Promise='undefined'==typeof Promise?require('es6-promise').Promise:Promise,fetch=require('axios');async function busy_hours(a,b){if(!(a&&b))return{status:'error',message:'Place ID / API key missing'};var c=require('@google/maps').createClient({key:b,Promise:_Promise}),d=function(a){return{hour:a[0],percentage:a[1]}},e=function(a){var b=['APP_INITIALIZATION_STATE=','window.APP_FLAGS'],c=a.substring(a.lastIndexOf(b[0])+b[0].length,a.lastIndexOf(b[1])),d=eval(c),e=eval(d[3][6].replace(')]}\'',''));return e[0][1][0][14][84]},f=function(a){var b=e(a);if(!b)return{status:'error',message:'Place has no popular hours'};var c={status:'ok'},f=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];c.week=Array.from(Array(7).keys()).map(function(a){var c=[];return b[0][a]&&b[0][a][1]&&(c=Array.from(b[0][a][1]).map(function(a){return d(a)})),{day:f[a],hours:c}});var g=b[7];return void 0!==g&&(c.now=d(g)),c},g=async function(a){try{var b=await fetch({url:a,headers:{"User-Agent":'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'}});return b.data}catch(a){return{status:'error',message:'Invalid url'}}};try{var h=await c.place({placeid:a}).asPromise(),i=h.json.result,j=i.name,k=i.formatted_address,l=i.geometry.location,m=await g(i.url);return Object.assign({name:j,formatted_address:k,location:l},f(m))}catch(a){return{status:'error',message:'Error: '+a.json.status||a}}}module.exports=busy_hours; |
{ | ||
"name": "busy-hours", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Hacky web scraper for retrieving Google Places popular times data", | ||
"license": "WTFPL", | ||
"main": "./dist/index.js", | ||
@@ -6,0 +7,0 @@ "size-limit": [ |
@@ -23,2 +23,8 @@ ## busy-hours | ||
{ | ||
name: '...', | ||
formatted_address: '...', | ||
location: { | ||
lat: 52.225232, | ||
lng 21.0122137 | ||
}, | ||
week: [ | ||
@@ -25,0 +31,0 @@ { |
const fetch = require('axios'); | ||
async function busy_hours(place_id, key) { | ||
const weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; | ||
if (!(place_id && key)) { | ||
return {status: 'error', message: 'Place ID / API key missing'}; | ||
} | ||
function busy_hours(place_id, key) { | ||
let gmaps = require('@google/maps').createClient({ | ||
const gmaps = require('@google/maps').createClient({ | ||
key: key, | ||
@@ -12,3 +13,3 @@ Promise: Promise | ||
let format_output = array => { | ||
const format_output = array => { | ||
return { | ||
@@ -20,3 +21,3 @@ hour: array[0], | ||
let extract_data = html => { | ||
const extract_data = html => { | ||
// ACHTUNG! HACKY AF | ||
@@ -32,40 +33,35 @@ let str = ['APP_INITIALIZATION_STATE=', 'window.APP_FLAGS'], | ||
let process_html = resp => { | ||
const process_html = html => { | ||
const popular_times = extract_data(html); | ||
if (resp.data) { | ||
if (!popular_times) { | ||
return {status: 'error', message: 'Place has no popular hours'}; | ||
} | ||
let popular_times = extract_data(resp.data); | ||
const data = {status: 'ok'}; | ||
const weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; | ||
if (popular_times === null) { | ||
return {status: 'error', message: 'Place has no popular hours'}; | ||
data.week = Array.from(Array(7).keys()).map(index => { | ||
let hours = []; | ||
if (popular_times[0][index] && popular_times[0][index][1]) { | ||
hours = Array.from(popular_times[0][index][1]).map(array => format_output(array)); | ||
} | ||
return { | ||
day: weekdays[index], | ||
hours: hours | ||
}; | ||
let data = {status: 'ok'}; | ||
}); | ||
const crowded_now = popular_times[7]; | ||
data.week = Array.from(Array(7).keys()).map(index => { | ||
let hours = []; | ||
if (popular_times[0][index] && popular_times[0][index][1]) { | ||
hours = Array.from(popular_times[0][index][1]).map(array => format_output(array)); | ||
} | ||
return { | ||
day: weekdays[index], | ||
hours: hours | ||
}; | ||
if (crowded_now !== undefined) { | ||
data.now = format_output(crowded_now); | ||
} | ||
return data; | ||
}); | ||
let crowded_now = popular_times[7]; | ||
if (crowded_now !== undefined) { | ||
data.now = format_output(crowded_now); | ||
} | ||
return data; | ||
} else { | ||
return {status: 'error'}; | ||
} | ||
}; | ||
let fetch_html = resp => { | ||
let url = resp.json.result.url; | ||
if (url) { | ||
return fetch({ | ||
const fetch_html = async(url) => { | ||
try { | ||
const html = await fetch({ | ||
url: url, | ||
@@ -76,3 +72,5 @@ headers: { | ||
}); | ||
} else { | ||
return html.data; | ||
} | ||
catch (err) { | ||
return {status: 'error', message: 'Invalid url'}; | ||
@@ -82,14 +80,15 @@ } | ||
let handle_err = err => { | ||
return {status: 'error', message: err} | ||
}; | ||
try { | ||
const place = await gmaps.place({placeid: place_id}).asPromise(); | ||
const result = place.json.result; | ||
const {name, formatted_address, geometry:{location}} = result; | ||
const html = await fetch_html(result.url); | ||
return Object.assign({name, formatted_address, location}, process_html(html)); | ||
} catch (err) { | ||
return {status: 'error', message: 'Error: ' + err.json.status || err}; | ||
} | ||
let new_promise = gmaps.place({placeid: place_id}, handle_err) | ||
.asPromise() | ||
.then(fetch_html) | ||
.then(process_html); | ||
return new_promise; | ||
} | ||
module.exports = busy_hours; |
6542
72
60