Füge myTischtennis-Verbindung hinzu: Implementiere Authentifizierung, Import von TTR-Werten und speichere Verbindungsdaten sicher
This commit is contained in:
69
server/utils/mytischtennis-client.js
Normal file
69
server/utils/mytischtennis-client.js
Normal file
@@ -0,0 +1,69 @@
|
||||
import { chromium } from 'playwright'
|
||||
|
||||
const BASE_URL = 'https://www.mytischtennis.de'
|
||||
const HEADERS = { 'accept-language': 'de-DE,de;q=0.9', 'user-agent': 'Harheimer-TC-Vereinsverwaltung/1.0' }
|
||||
|
||||
function findAuthCookie(headers) {
|
||||
const raw = headers.getSetCookie?.() || (headers.get('set-cookie') ? [headers.get('set-cookie')] : [])
|
||||
return raw.find(value => value.startsWith('sb-10-auth-token='))?.split(';')[0] || null
|
||||
}
|
||||
|
||||
function findEntries(value) {
|
||||
if (!value || typeof value !== 'object') return null
|
||||
if (Array.isArray(value.entries)) return value.entries
|
||||
for (const child of Object.values(value)) {
|
||||
const result = findEntries(child)
|
||||
if (result) return result
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
async function loginDirect(email, password) {
|
||||
const page = await fetch(`${BASE_URL}/login?next=%2F`, { headers: HEADERS })
|
||||
const html = await page.text()
|
||||
if (/captcha/i.test(html)) throw new Error('CAPTCHA_REQUIRED')
|
||||
const xsrf = html.match(/name=["']xsrf["'][^>]*value=["']([^"']+)/i)?.[1]
|
||||
const form = new URLSearchParams({ email, password, intent: 'login' })
|
||||
if (xsrf) form.set('xsrf', xsrf)
|
||||
const result = await fetch(`${BASE_URL}/login?next=%2F&_data=routes%2F_auth%2B%2Flogin`, {
|
||||
method: 'POST', headers: { ...HEADERS, 'content-type': 'application/x-www-form-urlencoded' }, body: form, redirect: 'manual'
|
||||
})
|
||||
const cookie = findAuthCookie(result.headers)
|
||||
if (!cookie) throw new Error('Direkter myTischtennis-Login fehlgeschlagen.')
|
||||
return cookie
|
||||
}
|
||||
|
||||
async function loginWithBrowser(email, password) {
|
||||
const browser = await chromium.launch({ headless: true })
|
||||
try {
|
||||
const page = await browser.newPage()
|
||||
await page.goto(`${BASE_URL}/login?next=%2F`, { waitUntil: 'networkidle' })
|
||||
await page.locator('input[name="email"]').fill(email)
|
||||
await page.locator('input[name="password"]').fill(password)
|
||||
// Das CAPTCHA wird vom Anbieter im Browser erzeugt. Ohne erfolgreiches Token wird nicht abgesendet.
|
||||
await page.waitForFunction(() => {
|
||||
const input = document.querySelector('input[name="captcha"]')
|
||||
return Boolean(input?.value)
|
||||
}, { timeout: 25_000 })
|
||||
await page.locator('form').evaluate(form => form.requestSubmit())
|
||||
await page.waitForTimeout(1_500)
|
||||
const cookie = (await page.context().cookies(BASE_URL)).find(item => item.name === 'sb-10-auth-token')
|
||||
if (!cookie) throw new Error('CAPTCHA konnte nicht automatisch gelöst werden.')
|
||||
return `${cookie.name}=${cookie.value}`
|
||||
} finally { await browser.close() }
|
||||
}
|
||||
|
||||
export async function fetchClubRankings(connection) {
|
||||
let cookie
|
||||
try { cookie = await loginDirect(connection.email, connection.password) } catch (error) {
|
||||
if (error.message !== 'CAPTCHA_REQUIRED') throw error
|
||||
cookie = await loginWithBrowser(connection.email, connection.password)
|
||||
}
|
||||
const url = new URL('/rankings/andro-rangliste', BASE_URL)
|
||||
url.search = new URLSearchParams({ 'all-players': 'on', clubnr: connection.clubId, fednickname: connection.association, 'current-ranking': 'yes', 'results-per-page': '100', page: '0', _data: 'routes/$' })
|
||||
const response = await fetch(url, { headers: { ...HEADERS, cookie, accept: 'application/json', referer: `${BASE_URL}/` } })
|
||||
if (!response.ok) throw new Error(`Ranglisten-Abruf fehlgeschlagen (HTTP ${response.status}).`)
|
||||
const entries = findEntries(await response.json())
|
||||
if (!entries) throw new Error('myTischtennis hat keine Ranglisten-Daten geliefert.')
|
||||
return entries
|
||||
}
|
||||
Reference in New Issue
Block a user