Create bahan_gabut/avtr/usage_openai.js
Browse files
bahan_gabut/avtr/usage_openai.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.querySelectorAll('.usage-table .expn .expn-title[aria-expanded="false"]').forEach(element => {
|
| 2 |
+
element.setAttribute('aria-expanded', 'true');
|
| 3 |
+
element.click();
|
| 4 |
+
});
|
| 5 |
+
|
| 6 |
+
document.querySelectorAll('div.expn div.expn-title[aria-expanded="false"]').forEach(element => {
|
| 7 |
+
element.setAttribute('aria-expanded', 'true');
|
| 8 |
+
element.click();
|
| 9 |
+
});
|
| 10 |
+
|
| 11 |
+
function delay(time) {
|
| 12 |
+
return new Promise(resolve => setTimeout(resolve, time));
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
delay(1000).then(() => {
|
| 16 |
+
console.log('Scrap Data Complete');
|
| 17 |
+
|
| 18 |
+
var usageTableContent = document.querySelector('.usage-table').innerHTML;
|
| 19 |
+
convertToJSON(usageTableContent);
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
function convertToJSON(usageTableContent) {
|
| 23 |
+
const parser = new DOMParser();
|
| 24 |
+
const doc = parser.parseFromString(usageTableContent, 'text/html');
|
| 25 |
+
|
| 26 |
+
const usageDatesElements = doc.querySelectorAll('.usage-dates');
|
| 27 |
+
const numRequestsElements = doc.querySelectorAll('.num-requests');
|
| 28 |
+
const tabularNumsElements = doc.querySelectorAll('.body-small.bold.tabular-nums');
|
| 29 |
+
|
| 30 |
+
const data = Array.from(usageDatesElements).map((elem, index) => {
|
| 31 |
+
const usageDateElem = elem.querySelector('.usage-date-local');
|
| 32 |
+
const usageDate = usageDateElem ? usageDateElem.innerText.replace('Local time: ', '').trim() : null;
|
| 33 |
+
|
| 34 |
+
const numRequest = numRequestsElements[index].innerText.split(',')[0];
|
| 35 |
+
|
| 36 |
+
const tabularNumsText = tabularNumsElements[index].innerText;
|
| 37 |
+
const tabularNumsMatches = tabularNumsText.match(/([\d,]+)\s*prompt.*?([\d,]+)\s*completion.*?([\d,]+)\s*token/);
|
| 38 |
+
|
| 39 |
+
return {
|
| 40 |
+
ID: "ID" + (index + 1),
|
| 41 |
+
Date: usageDate,
|
| 42 |
+
Model: numRequest,
|
| 43 |
+
Prompt: tabularNumsMatches ? tabularNumsMatches[1].replace(/,/g, '') : null,
|
| 44 |
+
Completion: tabularNumsMatches ? tabularNumsMatches[2].replace(/,/g, '') : null,
|
| 45 |
+
Total: tabularNumsMatches ? tabularNumsMatches[3].replace(/,/g, '') : null,
|
| 46 |
+
};
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
//ToDO Output JSON to Google Spreadsheet (Cancelled (?))
|
| 50 |
+
//const jsonData = JSON.stringify(data, null, 2);
|
| 51 |
+
//const jsonBlob = new Blob([jsonData], { type: 'application/json' });
|
| 52 |
+
//const jsonLink = document.createElement('a');
|
| 53 |
+
//jsonLink.href = URL.createObjectURL(jsonBlob);
|
| 54 |
+
//jsonLink.download = 'data.json';
|
| 55 |
+
//jsonLink.click();
|
| 56 |
+
|
| 57 |
+
const csvHeaders = ["ID", "Date", "Model", "Prompt", "Completion", "Total"];
|
| 58 |
+
const csvData = data.map(row => [
|
| 59 |
+
row.ID,
|
| 60 |
+
row.Date,
|
| 61 |
+
row.Model,
|
| 62 |
+
row.Prompt,
|
| 63 |
+
row.Completion,
|
| 64 |
+
row.Total,
|
| 65 |
+
].map(value => `"${value ? value.replace(/"/g, '""') : ''}"`).join(','));
|
| 66 |
+
csvData.unshift(csvHeaders.join(','));
|
| 67 |
+
const csvContent = csvData.join('\r\n');
|
| 68 |
+
const csvBlob = new Blob([csvContent], { type: 'text/csv' });
|
| 69 |
+
const csvLink = document.createElement('a');
|
| 70 |
+
csvLink.href = URL.createObjectURL(csvBlob);
|
| 71 |
+
csvLink.download = 'data_openai_usage_september_2023';
|
| 72 |
+
csvLink.click();
|
| 73 |
+
|
| 74 |
+
alert("Berhasil menscrap data usage openai");
|
| 75 |
+
}
|
| 76 |
+
|