Skip to content

Instantly share code, notes, and snippets.

@valoricDe
Last active April 2, 2023 17:37
Show Gist options
  • Save valoricDe/1ef621a38b2557d63f3c22b108b7f044 to your computer and use it in GitHub Desktop.
Save valoricDe/1ef621a38b2557d63f3c22b108b7f044 to your computer and use it in GitHub Desktop.
fraabe
async function getHtmlDocumentById(id) {
const baseUrl = 'http://portal.fotoraabe.de'
const response = await fetch(
`${baseUrl}/EditCustomer?id=${id}`,
{
credentials: 'omit',
headers: {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0',
Accept: '*/*',
'Accept-Language': 'en-US,en;q=0.5',
},
referrer: `${baseUrl}`,
method: 'GET',
mode: 'cors',
}
)
if (response.status >= 400) throw Error(await response.text())
const html = await response.text()
const {
window: { document },
} = new JSDOM(html)
return document
}
function JSDOM(html) {
var template = document.createElement('template');
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
return { window: { document: template.content.firstChild }};
}
function getInputValue(document, selector) {
const el = document.querySelector(selector)
return el && el.value || null
}
function getSelectOptionLabel(document, selector) {
const el = document.querySelector(selector)
return el && el.selectedOptions[0].label || null
}
function extractData(document) {
return {
Name: getInputValue(document, '[name=Name]'),
Art: getSelectOptionLabel(document, '[name=CustomerType]'),
Groesse: getInputValue(document, '[name=Dimension]'),
Nation: getInputValue(document, '[name=Nation]'),
Bundesland: getInputValue(document, '[name=State]'),
Strasse: getInputValue(document, '[name=Street]'),
PLZ: getInputValue(document, '[name=ZipCode]'),
Stadt: getInputValue(document, '[name=City]'),
Stadtteil: getInputValue(document, '[name=District]'),
Ansprechpartner: getInputValue(document, '[name=MainContactName]'),
Vorwahl: getInputValue(document, '[name=DialingCode]'),
Telefon: getInputValue(document, '[name=PhoneNumber]'),
Email: getInputValue(document, '[name=EmailAddress]'),
Website: getInputValue(document, '[name=Website]'),
}
}
async function exportDatabase(max = 50, offset = 0) {
for(let id = offset; id < max; id++) {
const doc = await getHtmlDocumentById(id)
const data = extractData(doc)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment