|  | @@ -1,43 +1,169 @@
 | 
	
		
			
				|  |  |  /** @module setting/state */
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +import rp from 'request-promise'
 | 
	
		
			
				|  |  | +import { takeLatest, all, call, put } from 'redux-saga/effects'
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * state.js
 | 
	
		
			
				|  |  |   *
 | 
	
		
			
				|  |  |   * Collection of everything which has to do with state changes.
 | 
	
		
			
				|  |  |   **/
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -const SCRAPE_FILES = [
 | 
	
		
			
				|  |  | -  'https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D',
 | 
	
		
			
				|  |  | -  'https://comp.swisstennis.ch/advantage/servlet/ProtectedDisplayTournament?Lang=D&tournament=Id104840',
 | 
	
		
			
				|  |  | -  'https://comp.swisstennis.ch/advantage/servlet/Calendar?tournament=Id104840&lang=D',
 | 
	
		
			
				|  |  | -  'https://comp.swisstennis.ch/advantage/servlet/Calendar.xls?Lang=D&tournament=Id104840&Type=Match&Inp_DateRangeFilter.fromDate=05.07.2017&Inp_DateRangeFilter.toDate=16.07.2017',
 | 
	
		
			
				|  |  | -  'https://comp.swisstennis.ch/advantage/servlet/PlayerList?tournament=Id104840&lang=D',
 | 
	
		
			
				|  |  | -  'https://comp.swisstennis.ch/advantage/servlet/PlayerList.xls?tournament=Id104840&lang=D',
 | 
	
		
			
				|  |  | -  'https://comp.swisstennis.ch/advantage/servlet/DisplayEvent?eventId=425828&lang=D',
 | 
	
		
			
				|  |  | -  'https://comp.swisstennis.ch/advantage/servlet/ModifyDraw?eventId=425828&lang=D',
 | 
	
		
			
				|  |  | -  'https://comp.swisstennis.ch/advantage/servlet/DisplayDraw.xls?eventId=425828&lang=D'
 | 
	
		
			
				|  |  | -]
 | 
	
		
			
				|  |  | +const SCRAPE_FILES = {
 | 
	
		
			
				|  |  | +  login: 'https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D',
 | 
	
		
			
				|  |  | +  tournament: 'https://comp.swisstennis.ch/advantage/servlet/ProtectedDisplayTournament?Lang=D&tournament=Id104840',
 | 
	
		
			
				|  |  | +  calendar: 'https://comp.swisstennis.ch/advantage/servlet/Calendar?tournament=Id104840&lang=D',
 | 
	
		
			
				|  |  | +  calendarXls: 'https://comp.swisstennis.ch/advantage/servlet/Calendar.xls?Lang=D&tournament=Id104840&Type=Match&Inp_DateRangeFilter.fromDate=05.07.2017&Inp_DateRangeFilter.toDate=16.07.2017',
 | 
	
		
			
				|  |  | +  playerList: 'https://comp.swisstennis.ch/advantage/servlet/PlayerList?tournament=Id104840&lang=D',
 | 
	
		
			
				|  |  | +  playerListXls: 'https://comp.swisstennis.ch/advantage/servlet/PlayerList.xls?tournament=Id104840&lang=D',
 | 
	
		
			
				|  |  | +  event: 'https://comp.swisstennis.ch/advantage/servlet/DisplayEvent?eventId=425828&lang=D',
 | 
	
		
			
				|  |  | +  draw: 'https://comp.swisstennis.ch/advantage/servlet/ModifyDraw?eventId=425828&lang=D',
 | 
	
		
			
				|  |  | +  drawXls: 'https://comp.swisstennis.ch/advantage/servlet/DisplayDraw.xls?eventId=425828&lang=D'
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  const DRAW_STATE = /Matches bereit zum spielen/
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /** actionTypes define what actions are handeled by the reducer. */
 | 
	
		
			
				|  |  |  export const actions = {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +  loginStart: form => {
 | 
	
		
			
				|  |  | +    return {
 | 
	
		
			
				|  |  | +      type: 'SCRAPE_LOGIN_START',
 | 
	
		
			
				|  |  | +      form
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  },
 | 
	
		
			
				|  |  | +  loginSuccess: data => {
 | 
	
		
			
				|  |  | +    return {
 | 
	
		
			
				|  |  | +      type: 'SCRAPE_LOGIN_SUCCESS',
 | 
	
		
			
				|  |  | +      data
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  },
 | 
	
		
			
				|  |  | +  loginFailure: error => {
 | 
	
		
			
				|  |  | +    return {
 | 
	
		
			
				|  |  | +      type: 'SCRAPE_LOGIN_FAILURE',
 | 
	
		
			
				|  |  | +      error
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  console.log('State actions', actions)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /** state definition */
 | 
	
		
			
				|  |  |  export const state = {
 | 
	
		
			
				|  |  | +  loginState: 'not_logged_in',
 | 
	
		
			
				|  |  | +  jar: null,
 | 
	
		
			
				|  |  | +  pages: {},
 | 
	
		
			
				|  |  | +  files: {},
 | 
	
		
			
				|  |  | +  tournaments: {},
 | 
	
		
			
				|  |  | +  selectedTournament: {},
 | 
	
		
			
				|  |  | +  categories: {}
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  console.log('State state', state)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /** reducer is called by the redux dispatcher and handles all component actions */
 | 
	
		
			
				|  |  |  export function reducer (state = [], action) {
 | 
	
		
			
				|  |  |    switch (action.type) {
 | 
	
		
			
				|  |  | +    case 'SCRAPE_LOGIN_START':
 | 
	
		
			
				|  |  | +      return { ...state, loginState: 'logging_in' }
 | 
	
		
			
				|  |  | +    case 'SCRAPE_LOGIN_SUCCESS':
 | 
	
		
			
				|  |  | +      const { newPages } = { ...state.pages }
 | 
	
		
			
				|  |  | +      newPages.myTournaments = action.data.myTournamentsPage
 | 
	
		
			
				|  |  | +      return { ...state, loginState: 'logged_in', jar: action.data.jar, pages: newPages }
 | 
	
		
			
				|  |  | +    case 'SCRAPE_LOGIN_FAILURE':
 | 
	
		
			
				|  |  | +      return { ...state, loginState: 'login_failure' }
 | 
	
		
			
				|  |  |      default:
 | 
	
		
			
				|  |  |        return state
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +function * swisstennisLogin (action) {
 | 
	
		
			
				|  |  | +  console.log('swisstennisLogin', action)
 | 
	
		
			
				|  |  | +  const jar = rp.jar()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  const requestOptions = {
 | 
	
		
			
				|  |  | +    uri: 'https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D',
 | 
	
		
			
				|  |  | +    // method: 'GET',
 | 
	
		
			
				|  |  | +    jar,
 | 
	
		
			
				|  |  | +    headers: {
 | 
	
		
			
				|  |  | +      Host: 'comp.swisstennis.ch',
 | 
	
		
			
				|  |  | +      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0',
 | 
	
		
			
				|  |  | +      Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
 | 
	
		
			
				|  |  | +      'Accept-Language': 'de-CH,de;q=0.8,en-US;q=0.5,en;q=0.3',
 | 
	
		
			
				|  |  | +      Connection: 'keep-alive',
 | 
	
		
			
				|  |  | +      'Upgrade-Insecure-Requests': '1',
 | 
	
		
			
				|  |  | +      'Cache-Control': 'max-age=0'
 | 
	
		
			
				|  |  | +    },
 | 
	
		
			
				|  |  | +    resolveWithFullResponse: true
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  const loginOptions = {
 | 
	
		
			
				|  |  | +    uri: 'https://comp.swisstennis.ch/advantage/servlet/Login',
 | 
	
		
			
				|  |  | +    // method: 'POST',
 | 
	
		
			
				|  |  | +    jar,
 | 
	
		
			
				|  |  | +    form: {
 | 
	
		
			
				|  |  | +      Lang: 'D',
 | 
	
		
			
				|  |  | +      id: action.form.id,
 | 
	
		
			
				|  |  | +      pwd: action.form.pwd,
 | 
	
		
			
				|  |  | +      Tournament: ''
 | 
	
		
			
				|  |  | +    },
 | 
	
		
			
				|  |  | +    headers: {
 | 
	
		
			
				|  |  | +      Host: 'comp.swisstennis.ch',
 | 
	
		
			
				|  |  | +      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0',
 | 
	
		
			
				|  |  | +      Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
 | 
	
		
			
				|  |  | +      'Accept-Language': 'de-CH,de;q=0.8,en-US;q=0.5,en;q=0.3',
 | 
	
		
			
				|  |  | +      Referer: 'https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D',
 | 
	
		
			
				|  |  | +      'Upgrade-Insecure-Requests': '1',
 | 
	
		
			
				|  |  | +      Connection: 'keep-alive'
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  const downloadOptions = {
 | 
	
		
			
				|  |  | +    uri: 'https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D',
 | 
	
		
			
				|  |  | +    // method: 'GET',
 | 
	
		
			
				|  |  | +    jar,
 | 
	
		
			
				|  |  | +    headers: {
 | 
	
		
			
				|  |  | +      Host: 'comp.swisstennis.ch',
 | 
	
		
			
				|  |  | +      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0',
 | 
	
		
			
				|  |  | +      Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
 | 
	
		
			
				|  |  | +      'Accept-Language': 'de-CH,de;q=0.8,en-US;q=0.5,en;q=0.3',
 | 
	
		
			
				|  |  | +      Referer: 'https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D',
 | 
	
		
			
				|  |  | +      'Upgrade-Insecure-Requests': '1',
 | 
	
		
			
				|  |  | +      Connection: 'keep-alive'
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  console.log(jar)
 | 
	
		
			
				|  |  | +  try {
 | 
	
		
			
				|  |  | +    console.log('attempting to fetch login page.')
 | 
	
		
			
				|  |  | +    const reqPage = yield call(rp.get, requestOptions)
 | 
	
		
			
				|  |  | +    console.log('successfully fetched login page.', reqPage, jar)
 | 
	
		
			
				|  |  | +  } catch (error) {
 | 
	
		
			
				|  |  | +    console.log('Error fetching login page.', jar)
 | 
	
		
			
				|  |  | +    yield put(actions.loginFailure(error))
 | 
	
		
			
				|  |  | +    return
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  try {
 | 
	
		
			
				|  |  | +    console.log('attempting to login.', loginOptions)
 | 
	
		
			
				|  |  | +    const loginPage = yield call(rp.post, loginOptions)
 | 
	
		
			
				|  |  | +    console.log('received a page.', loginPage, jar)
 | 
	
		
			
				|  |  | +    if (loginPage.includes('Zugriff verweigert')) {
 | 
	
		
			
				|  |  | +      yield put(actions.loginFailure(Error('Login rejected')))
 | 
	
		
			
				|  |  | +    } else {
 | 
	
		
			
				|  |  | +      yield put(actions.loginFailure(Error('Other login problem')))
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    return
 | 
	
		
			
				|  |  | +  } catch (error) {
 | 
	
		
			
				|  |  | +    console.log('successfully logged in.', error)
 | 
	
		
			
				|  |  | +    return
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  try {
 | 
	
		
			
				|  |  | +    console.log('attempting to fetch my tournaments.')
 | 
	
		
			
				|  |  | +    const myTournamentsPage = yield call(rp.get, downloadOptions)
 | 
	
		
			
				|  |  | +    const match = myTournamentsPage.match(/<a href=".*tournament=Id(\d+)">([^<]+)<\/a>/gm)
 | 
	
		
			
				|  |  | +    console.log('tournament page', match)
 | 
	
		
			
				|  |  | +    yield put(actions.loginSuccess({ myTournamentsPage, jar }))
 | 
	
		
			
				|  |  | +  } catch (error) {
 | 
	
		
			
				|  |  | +    console.log('Error fetching tournaments.')
 | 
	
		
			
				|  |  | +    yield put(actions.loginFailure(Error('Error fetching tournaments')))
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  /** sagas are asynchronous workers (JS generators) to handle the state. */
 | 
	
		
			
				|  |  | -export function * saga () {}
 | 
	
		
			
				|  |  | +export function * saga () {
 | 
	
		
			
				|  |  | +  yield all([
 | 
	
		
			
				|  |  | +    takeLatest('SCRAPE_LOGIN_START', swisstennisLogin)
 | 
	
		
			
				|  |  | +  ])
 | 
	
		
			
				|  |  | +}
 |