-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshots.py
68 lines (53 loc) · 2.16 KB
/
screenshots.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
import logging
import os
import cloudstorage as gcs
import webapp2
import json
import datetime
import time
my_default_retry_params = gcs.RetryParams(initial_delay=0.2,
max_delay=5.0,
backoff_factor=2,
max_retry_period=15)
gcs.set_default_retry_params(my_default_retry_params)
class Screenshots(webapp2.RequestHandler):
def post(self):
screenshot = self.request.POST["file"]
write_retry_params = gcs.RetryParams(backoff_factor=1.1)
gcs_file = gcs.open('/timer/' + screenshot.filename,
'w',
content_type='application/octet-stream',
retry_params=write_retry_params)
gcs_file.write(screenshot.value)
gcs_file.close()
self.response.status_int = 204
def get(self):
bucket_name = 'timer'
self.response.headers['Content-Type'] = 'application/json'
bucket = '/' + bucket_name
try:
stats = gcs.listbucket(bucket)
ten_minutes = datetime.timedelta(minutes = 10)
ten_minutes_ago = datetime.datetime.now()-ten_minutes
names = []
for x in stats:
create_time = datetime.datetime.fromtimestamp(x.st_ctime)
if ten_minutes_ago < create_time:
name = x.filename.replace('/timer/', '').split('.')
image = {'classroom':name[0],'student':name[1],'created':x.st_ctime}
names.append(image)
self.response.write(json.dumps(names))
except Exception as e:
logging.exception(e)
self.response.write('{"error": "oops"}')
class Screenshot(webapp2.RequestHandler):
def get(self,created,filename):
self.response.content_type = 'image/png'
gcs_file = gcs.open('/timer/' + filename)
self.response.write(gcs_file.read())
gcs_file.close()
app = webapp2.WSGIApplication([
(r'/screenshots/(\d+.?\d*)/(.+)', Screenshot),
(r'/screenshots', Screenshots)
],
debug=True)