Skip to content

Instantly share code, notes, and snippets.

@sebfischer83
Created February 3, 2021 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sebfischer83/1e44ec04d977366004c4994b48277186 to your computer and use it in GitHub Desktop.
Save sebfischer83/1e44ec04d977366004c4994b48277186 to your computer and use it in GitHub Desktop.
Download Tresor One data
function saveFile(content) {
if(!content) {
console.error('Console.save: No data');
return;
}
let filename = 'console.json';
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4);
}
let blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
}
let table = document.getElementsByClassName('holdings-table')[0];
let header = table.querySelector('thead');
let mapHeader = new Map();
let data = [];
header.querySelectorAll('tr th').forEach((a, i) => {
let span = a.querySelector('span.sorting-link');
if (span) {
mapHeader.set(i, span.innerText.trim());
} else {
if (a.innerText === 'Anteile') {
mapHeader.set(i, a.innerText.trim());
}
}
});
let dataRows = table.querySelectorAll('tbody tr');
dataRows.forEach((row, i) => {
let obj = {};
obj.stock = {};
row.querySelectorAll('td').forEach((col, t) => {
if (!mapHeader.has(t)) {
return;
}
if (mapHeader.get(t).toUpperCase() === 'Wertpapier'.toUpperCase()) {
obj.stock.name = col.querySelector('strong').innerText;
let meta = col.querySelectorAll('small span');
obj.stock.isin = meta[0].innerText;
obj.stock.wkn = meta[1].innerText;
obj.stock.type = meta[2].innerText;
} else if (mapHeader.get(t).toUpperCase() === 'Einstieg'.toUpperCase()) {
let prices = col.querySelectorAll('span');
obj.currentValue = parseFloat(prices[0].innerText.replace('.', '').replace(',', '.'));
obj.currentQuote = parseFloat(prices[1].innerText.replace('.', '').replace(',', '.'));
} else if (mapHeader.get(t).toUpperCase() === 'Anteile'.toUpperCase()) {
obj.quantity = parseFloat(col.innerText.replace('.', '').replace(',', '.'));
}
});
data.push(obj);
});
saveFile(data);
@sebfischer83
Copy link
Author

sebfischer83 commented Feb 3, 2021

function saveCsvFile(content) {
    if(!content) {
        console.error('Console.save: No data');
        return;
    }

let delimiter = ";";
    let filename = 'console.csv';
let csv = ["Name", "isin", "wkn", "type", "curentVal", "currentQuote", "quantity"].join(delimiter);
    
if(typeof data === "object"){
data.forEach((obj, i) => {
csv += "\n";
let values = [obj.stock.name, obj.stock.isin, obj.stock.wkn, obj.stock.type, obj.currentValue, obj.currentQuote, obj.quantity];
csv += values.join(delimiter);
});
    }

    let blob = new Blob([csv], {type: 'text/csv'}),
        e    = document.createEvent('MouseEvents'),
        a    = document.createElement('a');

    a.download = filename;
    a.href = window.URL.createObjectURL(blob);
    a.dataset.downloadurl =  ['text/csv', a.download, a.href].join(':');
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    a.dispatchEvent(e);
}
function saveFile(content) {
    if(!content) {
        console.error('Console.save: No data');
        return;
    }

    let filename = 'console.json';

    if(typeof data === "object"){
        data = JSON.stringify(data, undefined, 4);
    }

    let blob = new Blob([data], {type: 'text/json'}),
        e    = document.createEvent('MouseEvents'),
        a    = document.createElement('a');

    a.download = filename;
    a.href = window.URL.createObjectURL(blob);
    a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':');
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    a.dispatchEvent(e);
}

let table = document.getElementsByClassName('holdings-table')[0];
let header = table.querySelector('thead');
let mapHeader = new Map();
let data = [];

header.querySelectorAll('tr th').forEach((a, i) => {
  let span = a.querySelector('span.sorting-link');
  if (span) {
    mapHeader.set(i, span.innerText.trim());
  } else {
   if (a.innerText === 'Anteile') {
    mapHeader.set(i, a.innerText.trim());
   } 
  }
});
let dataRows = table.querySelectorAll('tbody tr');
dataRows.forEach((row, i) => {
  let obj = {};
  obj.stock = {};
  row.querySelectorAll('td').forEach((col, t) => {
    if (!mapHeader.has(t)) {
      return;
    }
    if (mapHeader.get(t).toUpperCase() === 'Wertpapier'.toUpperCase()) {
      obj.stock.name = col.querySelector('strong').innerText;
      let meta = col.querySelectorAll('small span');
      obj.stock.isin = meta[0].innerText;
      obj.stock.wkn = meta[1].innerText;
      obj.stock.type = meta[2].innerText;
    } else if (mapHeader.get(t).toUpperCase() === 'Einstieg'.toUpperCase()) {
      let prices = col.querySelectorAll('span');
      obj.currentValue = parseFloat(prices[0].innerText.replace('.', '').replace(',', '.')); 
      obj.currentQuote = parseFloat(prices[1].innerText.replace('.', '').replace(',', '.')); 
    } else if (mapHeader.get(t).toUpperCase() === 'Anteile'.toUpperCase()) {
      obj.quantity = parseFloat(col.innerText.replace('.', '').replace(',', '.'));
    }
  });
  data.push(obj);
});
saveFile(data);



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment