-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuk.ac.ucl.cs.study.multitasking.chrome
200 lines (169 loc) · 7.16 KB
/
uk.ac.ucl.cs.study.multitasking.chrome
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
# Original Chrome Messaging API example:
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Modifications:
# Copyright 2015 Steve Dodier-Lazaro <[email protected]>
# Modifications are under public domain
import math
import struct
import sys
import syslog
import json
import time
from zeitgeist.client import ZeitgeistClient
from zeitgeist.datamodel import *
zg = ZeitgeistClient()
ManifestationWebEvents = "activity://web-browser/chromium/WebEvent"
InterpretationWebAccess = "activity://web-browser/chromium/WebAccessEvent"
InterpretationWebLeave = "activity://web-browser/chromium/WebLeaveEvent"
InterpretationWebDownload = "activity://web-browser/chromium/WebDownloadEvent"
InterpretationWebActiveTabs = "activity://web-browser/chromium/OpenWindowsInterval"
MIN_DURATION_SEC = 5.0
def on_status_changed_callback(enabled):
""" This method will be called whenever someone enables or disables
the data-source. """
if enabled:
print('Data-source enabled and ready to send events!')
else:
print('Data-source disabled; sent events will be ignored.')
def register():
""" This method registers the Zeitgeist data-source. """
unique_id = 'uk.ac.ucl.cs.study.multitasking.chrome'
name = 'UCL Study Chromium Datasource'
description = 'Logs your access to websites, and how you spend time across browser tabs'
zg.register_data_source(unique_id, name, description, [], on_status_changed_callback)
def logAccess(documentInfo):
""" This method logs a website access event. """
subjects = []
subjects.append(Subject.new_for_values(
uri=documentInfo['url'].replace('&', '%26'),
origin=documentInfo['origin'],
mimetype=documentInfo['mimeType'],
text=documentInfo['title']))
activity = "activity://null///pid://%d///winid://%d///index://%d///tabid://%d///" % (documentInfo['pid'], documentInfo['windowId'], documentInfo['index'], documentInfo['id'])
subjects.append(Subject.new_for_values(
uri=activity,
interpretation='activity://web-browser/Actor',
manifestation=Manifestation.WORLD_ACTIVITY,
mimetype='application/octet-stream',
text='ucl-study-metadata'))
event = Event.new_for_values(
timestamp=int(math.floor(time.time()*1000)),
manifestation=Manifestation.USER_ACTIVITY,
interpretation=InterpretationWebAccess,
actor='application://chromium-browser.desktop',
subjects=subjects)
zg.insert_event(event)
def logLeave(tabid, documentInfo):
""" This method logs a website leave event. """
subjects = []
subjects.append(Subject.new_for_values(
uri=documentInfo['url'].replace('&', '%26'),
origin=documentInfo['origin'],
mimetype=documentInfo['mimeType'],
text=documentInfo['title']))
activity = "activity://null///pid://%d///winid://%d///index://%d///tabid://%d///" % (documentInfo['pid'], documentInfo['windowId'], documentInfo['index'], documentInfo['id'])
subjects.append(Subject.new_for_values(
uri=activity,
interpretation='activity://web-browser/Actor',
manifestation=Manifestation.WORLD_ACTIVITY,
mimetype='application/octet-stream',
text='ucl-study-metadata'))
event = Event.new_for_values(
timestamp=int(math.floor(time.time()*1000)),
manifestation=Manifestation.USER_ACTIVITY,
interpretation=InterpretationWebLeave,
actor='application://chromium-browser.desktop',
subjects=subjects)
zg.insert_event(event)
def logActiveTabs(activeEventInfo):
""" This method logs an active browser tabs event. """
subjects = []
for key in activeEventInfo:
if activeEventInfo[key] > MIN_DURATION_SEC:
subjects.append(Subject.new_for_values(
uri=key.replace('&', '%26'),
mimetype='text/url',
text=str(activeEventInfo[key])))
activity = "activity://n/a///pid://n/a///winid://n/a///"
subjects.append(Subject.new_for_values(
uri=activity,
interpretation='activity://web-browser/Actor',
manifestation=Manifestation.WORLD_ACTIVITY,
mimetype='application/octet-stream',
text='ucl-study-metadata'))
event = Event.new_for_values(
timestamp=int(math.floor(time.time()*1000)),
manifestation=ManifestationWebEvents,
interpretation=InterpretationWebActiveTabs,
actor='application://chromium-browser.desktop',
subjects=subjects)
zg.insert_event(event)
def logDownload(item):
""" This method logs a download event. """
subjects = []
text = "Download: %f bytes, started on %s" % (item['fileSize'], item['startTime'])
subjects.append(Subject.new_for_values(
uri=item['filename'],
origin=item['referrer'].replace('&', '%26'),
mimetype=item['mime'],
text=text))
activity = "activity://null///pid://n/a///winid://n/a///"
subjects.append(Subject.new_for_values(
uri=activity,
interpretation='activity://web-browser/Actor',
manifestation=Manifestation.WORLD_ACTIVITY,
mimetype='application/octet-stream',
text='ucl-study-metadata'))
event = Event.new_for_values(
timestamp=int(math.floor(time.time()*1000)),
manifestation=Manifestation.USER_ACTIVITY,
interpretation=InterpretationWebDownload,
actor='application://chromium-browser.desktop',
subjects=subjects)
zg.insert_event(event)
def messageDispatcher(text):
packet = json.loads(text)
try:
if packet['type'] == "Access":
logAccess(packet['documentInfo'])
elif packet['type'] == "Leave":
logLeave(packet['tabid'], packet['documentInfo'])
elif packet['type'] == "ActiveTabs":
logActiveTabs(packet['info'])
elif packet['type'] == "Download":
logDownload(packet['item'])
else:
syslog.syslog(syslog.LOG_ERR, 'Unknown message type received: %s' % packet['type'])
except NameError as e:
syslog.syslog(syslog.LOG_ERR, 'Name Error: %s (message was %s)' % (e, text.encode('utf-8')))
def send_message(message):
""" Helper function that sends a message to the webapp. """
# Write message size.
sys.stdout.write(struct.pack('I', len(message)))
# Write the message itself.
sys.stdout.write(message)
sys.stdout.flush()
#
def read_thread_func():
""" Thread that reads messages from the webapp. """
message_number = 0
while 1:
# Read the message length (first 4 bytes).
text_length_bytes = sys.stdin.read(4)
if len(text_length_bytes) != 0:
# Unpack message length as 4 byte integer.
text_length = struct.unpack('i', text_length_bytes)[0]
# Read the text (JSON object) of the message.
text = sys.stdin.read(text_length).decode('utf-8')
messageDispatcher(text)
else:
Exit()
def Exit():
syslog.closelog()
sys.exit(0)
if __name__ == '__main__':
register()
read_thread_func()