swisstennis.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import fs from 'fs'
  2. import express from 'express'
  3. import bhttp from 'bhttp'
  4. import excel from '../../excel'
  5. // Create the router
  6. const swisstennis = express.Router()
  7. // Use this variable for the session
  8. const session = bhttp.session()
  9. function fileDate(date) {
  10. return `${ date.getFullYear()*10000 + (date.getMonth()+1)*100 + date.getDate() }${ date.getHours()*10000 + date.getMinutes()*100 + date.getSeconds() }`
  11. }
  12. /*
  13. * Define the Routes
  14. */
  15. // Login
  16. swisstennis.post('/login', async (req, res) => {
  17. const { username, password } = req.body
  18. // return, if username or password are missing
  19. if (!username || !password) {
  20. res.status(400).json({
  21. success: false,
  22. msg: 'Parameters username and password are required'
  23. })
  24. return
  25. }
  26. // assemble the login data
  27. const loginData = {
  28. Lang: 'D',
  29. id: username,
  30. pwd: password,
  31. Tournament: ''
  32. }
  33. try {
  34. const myTournamentsPage = await session.get('https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D')
  35. const loginPage = await session.post('https://comp.swisstennis.ch/advantage/servlet/Login', loginData)
  36. const strLoginPage = loginPage.body.toString()
  37. if (strLoginPage.includes('Zugriff verweigert')) {
  38. res.status(400).json({
  39. success: false,
  40. msg: 'Access denied!'
  41. })
  42. return
  43. }
  44. } catch (error) {
  45. res.status(400).json({
  46. success: false,
  47. msg: error
  48. })
  49. return
  50. }
  51. res.json({
  52. success: true,
  53. msg: 'Logged in successfully.'
  54. })
  55. })
  56. swisstennis.get('/tournaments', async (req, res) => {
  57. const tournaments = {}
  58. try {
  59. let match
  60. const myTournamentsPage = await session.get('https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D')
  61. const strMyTournamentsPage = myTournamentsPage.body.toString()
  62. if (strMyTournamentsPage.includes('Login-Zone')) {
  63. res.status(400).json({
  64. success: false,
  65. message: 'Not logged in.'
  66. })
  67. return
  68. }
  69. const tournamentRegexp = /<a href=".*ProtectedDisplayTournament.*tournament=Id(\d+)">([^<]+)<\/a>/gm
  70. do {
  71. match = tournamentRegexp.exec(strMyTournamentsPage)
  72. if (match) {
  73. tournaments[match[1]] = match[2]
  74. }
  75. } while (match)
  76. } catch (error) {
  77. res.status(400).json({
  78. success: false,
  79. msg: error.toString()
  80. })
  81. return
  82. }
  83. res.json({
  84. success: true,
  85. tournaments
  86. })
  87. })
  88. swisstennis.get('/draws/:tournament', async (req, res) => {
  89. const { tournament } = req.params
  90. if (!tournament) {
  91. res.json({
  92. success: false,
  93. msg: 'No tournament given.'
  94. })
  95. return
  96. }
  97. try {
  98. let match
  99. const draws = {}
  100. const tournamentPage = await session.get(`https://comp.swisstennis.ch/advantage/servlet/ProtectedDisplayTournament?Lang=D&tournament=Id${tournament}`)
  101. const strTournamentPage = tournamentPage.body.toString()
  102. if (strTournamentPage.includes('Login-Zone')) {
  103. res.status(400).json({
  104. success: false,
  105. message: 'Not logged in.'
  106. })
  107. return
  108. }
  109. const drawRegexp = /<a (?:class="text" )?href=".*DisplayEvent.*eventId=(\d+).*">([^<]+)<\/a>/gm
  110. do {
  111. match = drawRegexp.exec(strTournamentPage)
  112. if (match) {
  113. draws[match[1]] = match[2]
  114. }
  115. } while (match)
  116. res.json({
  117. success: true,
  118. draws
  119. })
  120. } catch (error) {
  121. res.status(400).json({
  122. success: false,
  123. msg: error.toString()
  124. })
  125. return
  126. }
  127. })
  128. swisstennis.get('/download/playerlist/:tournament', async (req, res) => {
  129. const { tournament } = req.params
  130. if (!tournament) {
  131. res.json({
  132. success: false,
  133. msg: 'No tournament given.'
  134. })
  135. return
  136. }
  137. try {
  138. const playerListFile = fs.createWriteStream(`swisstennis_files/PlayerList-${fileDate(new Date())}.xls`)
  139. const playerList = await session.get(`https://comp.swisstennis.ch/advantage/servlet/PlayerList.xls?tournament=Id${tournament}&lang=D`, {stream: true})
  140. playerList.pipe(playerListFile)
  141. res.json({
  142. success: true,
  143. msg: 'Download successful.'
  144. })
  145. } catch (error) {
  146. res.status(400).json({
  147. success: false,
  148. msg: error.toString()
  149. })
  150. return
  151. }
  152. })
  153. swisstennis.get('/download/calendar/:tournament', async (req, res) => {
  154. const { tournament } = req.params
  155. if (!tournament) {
  156. res.json({
  157. success: false,
  158. msg: 'No tournament given.'
  159. })
  160. return
  161. }
  162. try {
  163. const calendarFile = fs.createWriteStream(`swisstennis_files/Calendar-${fileDate(new Date())}.xls`)
  164. 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})
  165. calendar.pipe(calendarFile)
  166. res.json({
  167. success: true,
  168. msg: 'Download successful.'
  169. })
  170. } catch (error) {
  171. res.status(400).json({
  172. success: false,
  173. msg: error.toString()
  174. })
  175. return
  176. }
  177. })
  178. swisstennis.get('/download/draw/:draw', async (req, res) => {
  179. const { draw } = req.params
  180. if (!draw) {
  181. res.json({
  182. success: false,
  183. msg: 'No draw given.'
  184. })
  185. return
  186. }
  187. try {
  188. const drawFile = fs.createWriteStream(`swisstennis_files/DisplayDraw${draw}-${fileDate(new Date())}.xls`)
  189. const drawDisplay = await session.get(`https://comp.swisstennis.ch/advantage/servlet/DisplayDraw.xls?eventId=${draw}&lang=D`, {stream: true})
  190. drawDisplay.pipe(drawFile)
  191. res.json({
  192. success: true,
  193. msg: 'Download successful.'
  194. })
  195. } catch (error) {
  196. res.status(400).json({
  197. success: false,
  198. msg: error.toString()
  199. })
  200. return
  201. }
  202. })
  203. function generatePlayerList (file) {
  204. return new Promise((resolve, reject) => {
  205. console.log('About to read the player list.')
  206. Excel.readWorkbook(file).then(worksheets => {
  207. console.log('got worksheets', worksheets)
  208. const worksheet = worksheets.Players
  209. if (worksheet[4].length !== 32 & worksheet[3][0] !== 'Konkurrenz' & worksheet[3][31] !== 'bezahlt') {
  210. reject(Error(`Wrong file structure. Length: ${worksheet[4].length} (expected 32), Column A name: ${worksheet[3][0]} (expected Konkurrenz)`))
  211. }
  212. const headers = worksheet.slice(3, 1)
  213. const allPlayers = worksheet.slice(4, worksheet.length).map(playerData => new Player.Player(playerData))
  214. resolve({ headers, allPlayers })
  215. }).catch(error => {
  216. reject(Error(`Error reading workbook ${error.toString()}`))
  217. })
  218. })
  219. }
  220. export default swisstennis