-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.py
92 lines (77 loc) · 3.4 KB
/
analyzer.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
import odesk
import config
from PIL import Image
import io
import argparse
from datetime import timedelta, datetime
# Detect blackness of the picture to determine
# if we have a black screenshot
# 0 to 1, black pictures are those with > 0.9
def blackness(image_file):
image = Image.open(image_file)
image = image.convert('L')
size = image.size[0] * image.size[1]
hist = image.histogram()
dark = sum(hist[i] for i in range(10))
return dark / float(size)
# Generate the date range list
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
parser = argparse.ArgumentParser(
description='This app will fetch all snapshot data in the given date range\
and will output how much time is \'unbillable\'')
parser.add_argument('--start_date',
help='The start date to use for workdiary analysis',
required=True)
parser.add_argument('--end_date',
help='The end date to use for workdiary analysis',
required=True)
args = vars(parser.parse_args())
(start_date, end_date) = (datetime.strptime(args['start_date'], "%m%d%Y"),
datetime.strptime(args['end_date'], "%m%d%Y"))
PUBLIC_KEY = config.public_key or raw_input('Enter public key: ')
SECRET_KEY = config.secret_key or raw_input('Enter secret key: ')
USERNAME = config.username or raw_input('Enter your oDesk login: ')
PASSWORD = config.password or raw_input('Enter your oDesk password: ')
auth_token = config.auth_token or None
# Add login error handling
sessionClient = odesk.SessionClient(USERNAME, PASSWORD)
sessionClient.login()
if auth_token is None:
client = odesk.Client(PUBLIC_KEY, SECRET_KEY)
auth_url = client.auth.auth_url()
print auth_url
# TODO: Auto follow the auth link and get the frob
#frob_detected = sessionClient.urlopen(auth_url).geturl()
#frob_detected = frob_detected.split('?frob=')[1]
#print frob_detected
frob = raw_input("Input frob: ")
auth_token, user = client.auth.get_token(frob)
client = odesk.Client(PUBLIC_KEY, SECRET_KEY, auth_token)
wrong_screenshots = {}
screenshots_list = []
for tDate in daterange(start_date, end_date):
workdiary = client.team.get_workdiaries(config.team_name,
config.user_id,
date=tDate.strftime("%Y%m%d"))
for snapshot in workdiary[1]:
if int(snapshot[u'activity']) == 1:
# low activity detection
screenshots_list.append(snapshot[u'screenshot_img_lrg'])
wrong_screenshots[snapshot[u'screenshot_img_lrg']] =\
int(snapshot[u'time']) - int(snapshot[u'cell_time'])
else:
# black screenshots detection
responce = sessionClient.urlopen(snapshot[u'screenshot_img_lrg'])
image = io.BytesIO(responce.read())
blackness_level = blackness(image)
if blackness_level > 0.9:
screenshots_list.append(snapshot[u'screenshot_img_lrg'])
wrong_screenshots[snapshot[u'screenshot_img_lrg']] =\
int(snapshot[u'time']) - int(snapshot[u'cell_time'])
# Recap
print "Total time taken by wrong screenshots: %f " %\
(float(sum(wrong_screenshots.values())) / 3600)
print "Total number of wrong screenshots: %d" % (len(wrong_screenshots.keys()))
sessionClient.logout()