123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- import fs from 'fs'
- import express from 'express'
- import bhttp from 'bhttp'
- import excel from '../../excel'
- // Create the router
- const swisstennis = express.Router()
- // Use this variable for the session
- const session = bhttp.session()
- function fileDate(date) {
- return `${ date.getFullYear()*10000 + (date.getMonth()+1)*100 + date.getDate() }${ date.getHours()*10000 + date.getMinutes()*100 + date.getSeconds() }`
- }
- /*
- * Define the Routes
- */
- // Login
- swisstennis.post('/login', async (req, res) => {
- const { username, password } = req.body
- // return, if username or password are missing
- if (!username || !password) {
- res.status(400).json({
- success: false,
- msg: 'Parameters username and password are required'
- })
- return
- }
- // assemble the login data
- const loginData = {
- Lang: 'D',
- id: username,
- pwd: password,
- Tournament: ''
- }
- try {
- const myTournamentsPage = await session.get('https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D')
- const loginPage = await session.post('https://comp.swisstennis.ch/advantage/servlet/Login', loginData)
- const strLoginPage = loginPage.body.toString()
- if (strLoginPage.includes('Zugriff verweigert')) {
- res.status(400).json({
- success: false,
- msg: 'Access denied!'
- })
- return
- }
- } catch (error) {
- res.status(400).json({
- success: false,
- msg: error
- })
- return
- }
- res.json({
- success: true,
- msg: 'Logged in successfully.'
- })
- })
- swisstennis.get('/tournaments', async (req, res) => {
- const tournaments = {}
- try {
- let match
- const myTournamentsPage = await session.get('https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D')
- const strMyTournamentsPage = myTournamentsPage.body.toString()
- if (strMyTournamentsPage.includes('Login-Zone')) {
- res.status(400).json({
- success: false,
- message: 'Not logged in.'
- })
- return
- }
- const tournamentRegexp = /<a href=".*ProtectedDisplayTournament.*tournament=Id(\d+)">([^<]+)<\/a>/gm
-
- do {
- match = tournamentRegexp.exec(strMyTournamentsPage)
- if (match) {
- tournaments[match[1]] = match[2]
- }
- } while (match)
- } catch (error) {
- res.status(400).json({
- success: false,
- msg: error.toString()
- })
- return
- }
- res.json({
- success: true,
- tournaments
- })
- })
- swisstennis.get('/draws/:tournament', async (req, res) => {
- const { tournament } = req.params
- if (!tournament) {
- res.json({
- success: false,
- msg: 'No tournament given.'
- })
- return
- }
- try {
- let match
- const draws = {}
- const tournamentPage = await session.get(`https://comp.swisstennis.ch/advantage/servlet/ProtectedDisplayTournament?Lang=D&tournament=Id${tournament}`)
- const strTournamentPage = tournamentPage.body.toString()
- if (strTournamentPage.includes('Login-Zone')) {
- res.status(400).json({
- success: false,
- message: 'Not logged in.'
- })
- return
- }
- const drawRegexp = /<a (?:class="text" )?href=".*DisplayEvent.*eventId=(\d+).*">([^<]+)<\/a>/gm
- do {
- match = drawRegexp.exec(strTournamentPage)
- if (match) {
- draws[match[1]] = match[2]
- }
- } while (match)
- res.json({
- success: true,
- draws
- })
- } catch (error) {
- res.status(400).json({
- success: false,
- msg: error.toString()
- })
- return
- }
- })
- swisstennis.get('/download/playerlist/:tournament', async (req, res) => {
- const { tournament } = req.params
- if (!tournament) {
- res.json({
- success: false,
- msg: 'No tournament given.'
- })
- return
- }
- try {
- const playerListFile = fs.createWriteStream(`swisstennis_files/PlayerList-${fileDate(new Date())}.xls`)
- const playerList = await session.get(`https://comp.swisstennis.ch/advantage/servlet/PlayerList.xls?tournament=Id${tournament}&lang=D`, {stream: true})
- playerList.pipe(playerListFile)
- res.json({
- success: true,
- msg: 'Download successful.'
- })
- } catch (error) {
- res.status(400).json({
- success: false,
- msg: error.toString()
- })
- return
- }
- })
- swisstennis.get('/download/calendar/:tournament', async (req, res) => {
- const { tournament } = req.params
- if (!tournament) {
- res.json({
- success: false,
- msg: 'No tournament given.'
- })
- return
- }
- try {
- const calendarFile = fs.createWriteStream(`swisstennis_files/Calendar-${fileDate(new Date())}.xls`)
- const calendar = await session.get(`https://comp.swisstennis.ch/advantage/servlet/Calendar.xls?Lang=D&tournament=Id${tournament}&Type=Match&Inp_DateRangeFilter.fromDate=04.06.2018&Inp_DateRangeFilter.toDate=16.09.2018`, {stream: true})
- calendar.pipe(calendarFile)
- res.json({
- success: true,
- msg: 'Download successful.'
- })
- } catch (error) {
- res.status(400).json({
- success: false,
- msg: error.toString()
- })
- return
- }
- })
- swisstennis.get('/download/draw/:draw', async (req, res) => {
- const { draw } = req.params
- if (!draw) {
- res.json({
- success: false,
- msg: 'No draw given.'
- })
- return
- }
- try {
- const drawFile = fs.createWriteStream(`swisstennis_files/DisplayDraw${draw}-${fileDate(new Date())}.xls`)
- const drawDisplay = await session.get(`https://comp.swisstennis.ch/advantage/servlet/DisplayDraw.xls?eventId=${draw}&lang=D`, {stream: true})
- drawDisplay.pipe(drawFile)
- res.json({
- success: true,
- msg: 'Download successful.'
- })
- } catch (error) {
- res.status(400).json({
- success: false,
- msg: error.toString()
- })
- return
- }
- })
- function generatePlayerList (file) {
- return new Promise((resolve, reject) => {
- console.log('About to read the player list.')
- Excel.readWorkbook(file).then(worksheets => {
- console.log('got worksheets', worksheets)
- const worksheet = worksheets.Players
- if (worksheet[4].length !== 32 & worksheet[3][0] !== 'Konkurrenz' & worksheet[3][31] !== 'bezahlt') {
- reject(Error(`Wrong file structure. Length: ${worksheet[4].length} (expected 32), Column A name: ${worksheet[3][0]} (expected Konkurrenz)`))
- }
- const headers = worksheet.slice(3, 1)
- const allPlayers = worksheet.slice(4, worksheet.length).map(playerData => new Player.Player(playerData))
- resolve({ headers, allPlayers })
- }).catch(error => {
- reject(Error(`Error reading workbook ${error.toString()}`))
- })
- })
- }
- export default swisstennis
|