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