1
2
3
4
5 import time
6 import logging
7 import StringIO
8 from threading import Thread
9
10 from lib.common.abstracts import Auxiliary
11 from lib.common.results import NetlogFile
12 from lib.api.screenshot import Screenshot
13
14 log = logging.getLogger(__name__)
15 SHOT_DELAY = 1
16
18 """Take screenshots."""
19
21 Thread.__init__(self)
22 self.do_run = True
23
25 """Stop screenshotting."""
26 self.do_run = False
27
29 """Run screenshotting.
30 @return: operation status.
31 """
32 if not Screenshot().have_pil():
33 log.warning("Python Image Library is not installed, "
34 "screenshots are disabled")
35 return False
36
37 img_counter = 0
38 img_last = None
39
40 while self.do_run:
41 time.sleep(SHOT_DELAY)
42
43 try:
44 img_current = Screenshot().take()
45 except IOError as e:
46 log.error("Cannot take screenshot: %s", e)
47 continue
48
49 if img_last:
50 if Screenshot().equal(img_last, img_current):
51 continue
52
53 img_counter += 1
54
55
56 tmpio = StringIO.StringIO()
57 img_current.save(tmpio, format="JPEG")
58 tmpio.seek(0)
59
60
61 nf = NetlogFile("shots/%s.jpg" % str(img_counter).rjust(4, "0"))
62
63 for chunk in tmpio:
64 nf.sock.sendall(chunk)
65
66 nf.close()
67
68 img_last = img_current
69
70 return True
71