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
17
18
19 SKIP_AREA = None
20
22 """Take screenshots."""
23
25 Thread.__init__(self)
26 self.do_run = True
27
29 """Stop screenshotting."""
30 self.do_run = False
31
33 """Run screenshotting.
34 @return: operation status.
35 """
36 if not Screenshot().have_pil():
37 log.warning("Python Image Library is not installed, "
38 "screenshots are disabled")
39 return False
40
41 img_counter = 0
42 img_last = None
43
44 while self.do_run:
45 time.sleep(SHOT_DELAY)
46
47 try:
48 img_current = Screenshot().take()
49 except IOError as e:
50 log.error("Cannot take screenshot: %s", e)
51 continue
52
53 if img_last:
54 if Screenshot().equal(img_last, img_current, SKIP_AREA):
55 continue
56
57 img_counter += 1
58
59
60 tmpio = StringIO.StringIO()
61 img_current.save(tmpio, format="JPEG")
62 tmpio.seek(0)
63
64
65 nf = NetlogFile("shots/%s.jpg" % str(img_counter).rjust(4, "0"))
66
67 for chunk in tmpio:
68 nf.sock.sendall(chunk)
69
70 nf.close()
71
72 img_last = img_current
73
74 return True
75