{"size": 1077, "ext": "js", "max_stars_repo_path": "sbahn.js", "max_stars_repo_name": "derhuerst/vbb-disruptions", "max_stars_repo_stars_event_min_datetime": "2016-07-18T09:14:30.000Z", "max_stars_repo_stars_event_max_datetime": "2016-07-18T09:14:30.000Z", "max_issues_repo_path": "sbahn.js", "max_issues_repo_name": "derhuerst/vbb-disruptions", "max_issues_repo_issues_event_min_datetime": "2017-03-21T13:01:53.000Z", "max_issues_repo_issues_event_max_datetime": "2018-01-06T22:48:59.000Z", "max_forks_repo_path": "sbahn.js", "max_forks_repo_name": "derhuerst/vbb-disruptions", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 23.9333333333, "max_line_length": 79, "alphanum_fraction": 0.5877437326}
'use strict'

const {fetch} = require('fetch-ponyfill')({Promise: require('pinkie-promise')})
const cheerio = require('cheerio')
const _ = require('./helpers')



const disruptions = () =>
	fetch(`http://mobil.s-bahn-berlin.de/constructions/overview`)
	.then((res) => res.text())
	.then((body) => {
		const $ = cheerio.load(body)

		const row = (disruption, row) => {
			let key = $('th', row).text()
			let value = $('td', row).text()
			if (key === 'Linien') {
				key = 'lines'
				value = _.clean(value).split('\n')
			}
			else if (key === 'Wann') key = 'when'
			else if (key === 'Infos') {
				key = 'description'
				value = _.clean(value)
			}
			disruption[key] = value
			return disruption
		}

		return Array.from($('#content .list-group li')).map((e) => {
			const disruption = {
				where: $('h4', e).text()
			}
			Array.from($('tr', e)).reduce(row, disruption)

			if ($('.badge .glyphicon-night', e)[0]) disruption.night = true
			if ($('.badge .glyphicon-weekend', e)[0]) disruption.weekend = true

			return disruption
		})
	})

module.exports = disruptions
