Last active
May 20, 2024 18:46
-
-
Save pdaug/e668a06f10402592f88ddb4858db9273 to your computer and use it in GitHub Desktop.
The script web scrapper runner in console browser to get all device trips at tracker platform
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
// webscrapper | |
(async function () { | |
const ConvertDateTime = function (dateTime) { | |
const dateDay = dateTime.slice(0, 2); | |
const dateMonth = dateTime.slice(3, 5); | |
const dateYear = dateTime.slice(6, 10); | |
const timeHour = dateTime.slice(11, 13); | |
const timeMinute = dateTime.slice(14, 16); | |
const timeSeconds = dateTime.slice(17, 19); | |
const dateString = `${dateDay}/${dateMonth}/${dateYear}`; | |
const timeString = `${timeHour}:${timeMinute}:${timeSeconds}`; | |
const dateFormatted = `${dateString} ${timeString}`; | |
return dateFormatted; | |
}; | |
const delay = 2000; | |
const table = document.querySelector("table.history_table tbody"); | |
const tableSecondChildren = table.children[1]; | |
tableSecondChildren.click(); | |
await new Promise(function (resolve) { | |
setTimeout(function() { | |
resolve(); | |
}, delay); | |
}); | |
const fileJson = new Array(); | |
let lastLatitude = 0; | |
let lastLongitude = 0; | |
for await (const row of table.children) { | |
const isEventAction = row.classList.contains("event-action"); | |
if (!row.className || isEventAction) { | |
continue; | |
} | |
row.click(); | |
const isParkAction = row.classList.contains("park-action"); | |
if (isParkAction) { | |
await new Promise(function (resolve) { | |
setTimeout(function() { | |
const popup = document.querySelector("div.popup-body table.table-condensed tbody"); | |
const latitudeRaw = popup.children[3].children[1].innerText.trim(); | |
const longitudeRaw = popup.children[4].children[1].innerText.trim(); | |
const latitudeParsed = parseFloat(latitudeRaw.slice(0, latitudeRaw.length - 2)); | |
const longitudeParse = parseFloat(longitudeRaw.slice(0, longitudeRaw.length - 2)); | |
lastLatitude = latitudeParsed; | |
lastLongitude = longitudeParse; | |
const duration = row.children[2].innerText.trim(); | |
const stop = { | |
type: "park", | |
diff_dt_parsed: duration, | |
}; | |
console.log(stop); | |
fileJson.push(stop); | |
resolve(); | |
}, delay); | |
}); | |
continue; | |
} | |
await new Promise(function (resolve) { | |
setTimeout(function() { | |
const popup = document.querySelector("div.popup-body table.table-condensed tbody"); | |
const startDateTime = ConvertDateTime(popup.children[1].children[1].innerText.trim()); | |
const endDateTime = ConvertDateTime(popup.children[2].children[1].innerText.trim()); | |
const distance = popup.children[4].children[1].innerText.trim(); | |
const duration = popup.children[5].children[1].innerText.trim(); | |
const route = { | |
type: "route", | |
first: { | |
lat: lastLatitude, | |
lng: lastLongitude, | |
}, | |
first_time_parsed: startDateTime, | |
last_time_parsed: endDateTime, | |
dis_km: parseFloat(distance), | |
diff_dt_parsed: duration, | |
}; | |
console.log(route); | |
fileJson.push(route); | |
resolve(); | |
}, delay); | |
}); | |
continue; | |
} | |
for (let index = fileJson.length - 1; index > 0; index--) { | |
const isRoute = (fileJson[index].type === "route"); | |
if (isRoute) { | |
fileJson[index].last = { | |
lat: lastLatitude, | |
lng: lastLongitude, | |
} | |
lastLatitude = fileJson[index].first.lat; | |
lastLongitude = fileJson[index].first.lng; | |
continue; | |
} | |
} | |
const jsonTabs = 4; | |
const jsonReplacer = null; | |
const blobSource = JSON.stringify(fileJson, jsonReplacer, jsonTabs); | |
const blobOptions = { type: "application/json" }; | |
const blob = new Blob([ blobSource ], blobOptions); | |
const blobUrl = URL.createObjectURL(blob); | |
window.open(blobUrl, "_blank"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bring the KM back!