#!/usr/bin/env python3
import requests
from bs4 import BeautifulSoup
from dateutil import parser


class JumprunProApi(object):
	BASE_URL = 'https://jumprunpro.com/dropzones'

	def __init__(self, dropzone):
		self._dropzone = dropzone
		self._url = JumprunProApi.BASE_URL + '/' + dropzone

		status = requests.get(self._url).status_code
		if status != 200:
			raise ValueError('{} is not a valid dropzone.')

	def _request(self, service):
		return requests.get(self._url + '/' + service)

	def _soup(self, service):
		body = self._request(service).text
		soup = BeautifulSoup(body)

		return soup

	def departing_loads(self):
		soup = self._soup('departing_loads')
		return self._parse_departing_loads(soup)

	def _parse_departing_loads(self, soup):
		loads = []

		for load in soup.find_all('div', class_='departing-load'):
			load_name = load.find('h3', class_='load-name').text.strip()
			state = load.find('h5', class_='state')
			if state:
				state = state.text.strip()

			call = load.find('h5', class_='call')
			call_time = 0
			if call:
				call = call.text.strip()
				try:
					call_time_obj = parser.parse(call)

					call_time = call_time_obj.minute
					call_time += call_time_obj.hour * 60
				except ValueError:
					call_time = 0

			slots = []
			for slot in load.find_all('tr', class_='slot'):
				slot_name = slot.find('td', class_='slot-name').text.strip()
				slot_activity = slot.find('td', class_='slot-activity').text.strip()

				slots.append({
					'name': slot_name,
					'activity': slot_activity,
				})

			loads.append({
				'state': state,
				'name': load_name,
				'call': call,
				'call_time': call_time,
				'slots': slots,
			})

		return loads

@@ -702,16 +702,589 @@
 (soup)%0A%0A
+%09def _time_to_minutes(self, time_str):%0A%09%09%22%22%22%0A%09%09Convert a time string to the equivalent number in minutes as an int.%0A%09%09Return 0 if the time_str is not a valid amount of time.%0A%0A%09%09%3E%3E%3E jump = JumprunProApi('skydive-warren-county')%0A%09%09%3E%3E%3E jump._time_to_minutes('34 minutes')%0A%09%0934%0A%09%09%3E%3E%3E jump._time_to_minutes('1 hour, 30 minutes')%0A%09%0990%0A%09%09%3E%3E%3E jump._time_to_minutes('jfksadjfkas')%0A%09%090%0A%09%09%22%22%22%0A%09%09minutes = 0%0A%09%09try:%0A%09%09%09call_time_obj = parser.parse(time_str)%0A%0A%09%09%09minutes = call_time_obj.minute%0A%09%09%09minutes += call_time_obj.hour * 60%0A%09%09except ValueError:%0A%09%09%09minutes = 0%0A%0A%09%09return minutes%0A%0A
 %09def _pa
@@ -1316,16 +1316,16 @@
  soup):%0A
-
 %09%09loads 
@@ -1646,18 +1646,8 @@
 p()%0A
-%09%09%09%09try:%0A%09
 %09%09%09%09
@@ -1659,156 +1659,38 @@
 time
-_obj = parser.parse(call)%0A%0A%09%09%09%09%09call_time = call_time_obj.minute%0A%09%09%09%09%09call_time += call_time_obj.hour * 60%0A%09%09%09%09except ValueError:%0A%09%09%09%09%09call_time = 0
+ = self._time_to_minutes(call)
 %0A%0A%09%09

move minutes conversion to own method