Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function loadOrders() {
var pageNum = 0;
function loadPage() {
setTimeout(() => {
if (pageNum > 50) {
return;
} else {
pageNum++;
}
window.scrollTo(0, document.body.scrollHeight);
console.log(pageNum);
loadPage();
}, 1000);
}
loadPage()
}
function extractOrders() {
var result = [];
document.querySelectorAll('.order-item').forEach(item => {
var restaurant_score = parseFloat(item.querySelector('.order-restaurant .point').textContent.replace(',', '.').trim());
var order_info = item.querySelector('.order-details .order-info').textContent.trim();
var order_amount = parseFloat(item.querySelector('.order-details .amount-info strong').textContent.replace(',', '.').replace('TL', '').trim());
var order_id = parseInt(item.querySelector('.order-details .order-detail-link a').getAttribute('href').replace('/siparis/bilgi?trackingNumber=', '').replace('&oh=1', '').trim());
var order_date_raw = item.querySelector('.order-date').textContent.trim().replace('Sipariş Tarihi: ', '').trim();
var order_date_epoch = new Date(order_date_raw.replace(' - ', ' ')).getTime() / 1000;
var restaurant_title = item.querySelector('.order-restaurant a').textContent.trim();
var title_tokens = restaurant_title.split(',');
var restaurant_name = title_tokens[0].trim();
var restaurant_local = null;
if (title_tokens.length > 1) {
restaurant_local = title_tokens[1].trim();
}
var restaurant_slug = item.querySelector('.order-restaurant a').getAttribute('href');
result.push({
restaurant_slug: restaurant_slug,
restaurant_name: restaurant_name,
restaurant_local: restaurant_local,
restaurant_score: restaurant_score,
order_date_raw: order_date_raw,
order_date_epoch: order_date_epoch,
order_id: order_id,
order_info: order_info,
order_amount: order_amount,
});
});
return result;
}
function exportToCsv(filename, rows) {
var processRow = function(row) {
var finalVal = '';
for (var j = 0; j < row.length; j++) {
var innerValue = row[j] === null ? '' : row[j].toString();
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
};
var result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0)
result = '"' + result + '"';
if (j > 0)
finalVal += ',';
finalVal += result;
}
return finalVal + '\n';
};
var csvFile = '';
for (var i = 0; i < rows.length; i++) {
csvFile += processRow(rows[i]);
}
var blob = new Blob([csvFile], {
type: 'text/csv;charset=utf-8;'
});
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, filename);
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}
function transformToRows(objArr) {
var output = [];
var labels = Object.keys(objArr[0]);
output.push(labels);
objArr.forEach(obj => {
var row = labels.map(label => obj[label]);
output.push(row);
});
return output;
}
function main(forceLoad) {
if (forceLoad === true) {
loadOrders();
}
var orderList = extractOrders();
var orderRows = transformToRows(orderList);
exportToCsv('ys_orders', orderRows);
}
main(false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.