This code provides functions to automate interactions with the Google Maps timeline, including navigating to the page, logging in, selecting a specific date, and handling different page states.
npm run import -- "Scrape google timeline"
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
function selectDate(date) {
return client
.then(() => fillForm({
'Year': date.getFullYear(),
'Month': months[date.getMonth()],
'Day': date.getDate(),
}))
}
function getGoogleTimeline(date) {
console.log('Google: Logging timeline history');
return client
.getUrl()
.then(url => {
return client
.isVisible('button[jsaction="header.select-today"]')
.then(is => url.indexOf('/timeline') === -1 || !is
? client.url('https://www.google.com/maps/timeline')
.loginGoogle()
.pause(3000)
: []
)
})
.catch(function (e) {
console.log(e);
})
.then(() => date
? selectDate(date)
: client.click('button[jsaction="header.select-today"]'))
.pause(3000)
.catch(e => console.log(e))
}
if (typeof client.getGoogleTimeline == 'undefined') {
client.addCommand('getGoogleTimeline', getGoogleTimeline);
}
module.exports = getGoogleTimeline;
// Define an array of month abbreviations for easy mapping
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/**
* Function to format a date object into a format suitable for filling a form
*
* @param {Date} date The date object to format
* @returns {Object} An object containing the formatted date
*/
function formatDate(date) {
return {
'Year': date.getFullYear(),
'Month': months[date.getMonth()],
'Day': date.getDate(),
};
}
/**
* Function to select a date in a form
*
* @param {Date} date The date object to select
* @returns {Promise} A promise resolving to the result of the select operation
*/
function selectDate(date) {
return client.fillForm(formatDate(date));
}
/**
* Function to get Google timeline data
*
* @param {Date} date The date object to get timeline data for (optional)
* @param {Boolean} [selectDate=false] Whether to select the date in the form
* @returns {Promise} A promise resolving to the result of the get operation
*/
function getGoogleTimeline(date, selectDate = false) {
console.log('Google: Logging timeline history');
// Check if the date is valid before proceeding
if (!date) {
throw new Error('Date is required');
}
// Navigate to the Google Maps timeline page if not already there
return client
.getUrl()
.then(url => {
return client
.isVisible('button[jsaction="header.select-today"]')
.then(is =>!is || url.indexOf('/timeline') === -1
? client.url('https://www.google.com/maps/timeline')
.loginGoogle()
.pause(3000)
: []
)
})
// If the date is valid, select it in the form
.then(() => selectDate(date))
.catch(e => console.log(e));
}
// Check if the client has a getGoogleTimeline command and add it if not
if (typeof client.getGoogleTimeline === 'undefined') {
client.addCommand('getGoogleTimeline', getGoogleTimeline);
}
// Export the getGoogleTimeline function
module.exports = getGoogleTimeline;
This code snippet defines two functions, selectDate
and getGoogleTimeline
, that interact with a Google Maps timeline interface.
Here's a breakdown:
months
Array:
months
containing abbreviated month names.selectDate
Function:
date
object as input.client
(presumably a web automation client) to fill a form with the year, month, and day from the provided date.getGoogleTimeline
Function:
date
object as input.client.getUrl()
to get the current URL.date
object is provided, it calls selectDate
to fill the form with the specified date.Command Registration:
getGoogleTimeline
already exists on the client
object.getGoogleTimeline
function as a command to the client
.Module Export:
getGoogleTimeline
function as the main module export.Purpose:
This code provides a way to programmatically interact with the Google Maps timeline, navigating to the timeline page, potentially logging in, selecting a specific date, and handling different scenarios based on the current URL and button visibility.