-
Notifications
You must be signed in to change notification settings - Fork 1
/
getrest.py
132 lines (118 loc) · 5.23 KB
/
getrest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#
# The BSD License, because it's the 21st century and we have to
# worry about this sort of nonsense
#
#
# Copyright (c) 2014, Melinda Shore
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# TODO : add run calculations
import sys
import argparse
from bs4 import BeautifulSoup
import re
import urllib2
def cleanup(datum):
return float(datum.rstrip(','))
def pull_data(script):
data = []
for item in script.split(';'):
scanned = re.search('d2.push\(\[(.+?)\]\)', item)
if scanned:
data.append(map(cleanup, scanned.group(1).split()))
return data
def print_summary(data):
print 'Number of rests: {0}'.format(sum(map(lambda x: data[x][1] is 'REST', data)))
print 'Number of runs: {0}'.format(sum(map(lambda x: data[x][1] is 'RUN', data)))
print 'Total rest time: {0}'.format(sum( { x[1][0] for x in data.iteritems() if x[1][1] is 'REST' } ))
print 'Total run time: {0}'.format(sum( { x[1][0] for x in data.iteritems() if x[1][1] is 'RUN' } ))
def main():
soup = ""
speed_thresh = .5 # the speed below which we assume we're not really moving
time_thresh = 1.0 # have to be stopped at least this long to be
# assumed to be resting
start_time = 0.0 # holds the start hour for the rest
in_rest = True # state variable
run_start = 0.0 # track the time a "run" starts
duration = 0.0
total_rest = 0.0
total_run = 0.0
csv = False # state variable for whether or not we're writing csv
data = {}
parser = argparse.ArgumentParser()
parser.add_argument('-s', dest='speed', type=float,
help='Speed below which the team will be considered at rest')
parser.add_argument('-t', dest='time', type=float,
help='Minimum stopped time for which the time will be considered at rest')
parser.add_argument('-c', help='Format output as csv', action='store_true')
parser.add_argument('times_uri')
args = parser.parse_args()
if args.speed:
speed_thresh = args.speed
if args.time:
time_thresh = args.time
if args.c:
csv = True
try:
soup = BeautifulSoup(open(args.times_uri), "lxml")
except Exception as e:
try:
soup = BeautifulSoup(urllib2.urlopen(args.times_uri), "lxml")
except Exception as e:
print '{0}: {1}'.format(sys.argv[0], e)
sys.exit(1)
for script in soup.find_all(id='source'):
if 'd2' in str(script):
speed_list = pull_data(str(script))
for point in speed_list:
if point[1] < speed_thresh: # point[0] holds hours since start, point[1] holds speed
if not in_rest:
in_rest = True
duration = point[0] - run_start
data[run_start] = (round(duration,1), 'RUN')
total_run += duration
# nruns += 1
start_time = point[0]
else:
if in_rest:
duration = point[0] - start_time
if duration > time_thresh:
# print out_rest_format.format(start_time, duration)
data[start_time] = (round(duration, 1), 'REST')
total_rest = total_rest + duration
# nrests = nrests + 1
run_start = point[0]
in_rest = False
start_time = 0.0
if csv:
for row in sorted(data.keys()):
print('{0},{1},{2}'.format(row, data[row][0], data[row][1]))
else:
for row in sorted(data.keys()):
print('Time: {0}, duration: {1}, {2}'.format(row, data[row][0], data[row][1]))
print_summary(data)
if __name__ == '__main__':
main()