follow eConsoleAppContainer changes
[enigma2-plugins.git] / webinterface / src / WebChilds / Screengrab.py
1 from enigma import eConsoleAppContainer
2
3 from twisted.web2 import resource, stream, responsecode, http, http_headers
4
5 from os import path as os_path, remove as os_remove
6
7 class GrabResource(resource.Resource):
8     """
9         this is a interface to Seddis AiO Dreambox Screengrabber 
10         version 0.8 or lower
11     """
12     grab_bin = "/usr/bin/grab"
13     
14     def render(self, req):
15         grab_command = self.grab_bin+" "
16         
17         #choosing what to grab
18         if req.args.has_key("command"):
19             # using 'command' for older versions
20             cmd = req.args['command'][0].replace("-","")
21             if cmd == "o":
22                 #-o only grab osd (framebuffer)
23                 grab_command += " -o "
24             elif cmd == "v":
25                 #-v only grab video
26                 grab_command += " -v "
27         elif req.args.has_key("o"):
28             grab_command += " -o "
29         elif req.args.has_key("v"):
30             grab_command += " -v " 
31                   
32         #chossing the imageformat
33         if req.args.has_key("format"):
34             formatraw = req.args["format"][0]
35             if formatraw == "png":
36                 #-p produce png files instead of bmp
37                 imageformat = "png"
38                 grab_command += " -p "
39             elif formatraw == "jpg":
40                 #-j (quality) produce jpg files instead of bmp (quality 0-100)
41                 imageformat = "jpg"
42                 if req.args.has_key("jpgquali"):
43                     grab_command += " -j %s "%req.args["jpgquali"][0]
44                 else:    
45                     grab_command += " -j 80 "
46             else:
47                 imageformat = "bmp"
48         else:
49             imageformat = "bmp"
50
51         
52         #-d always use osd resolution (good for skinshots)
53         if req.args.has_key("d"):
54             grab_command += " -d " 
55
56         #-n dont correct 16:9 aspect ratio
57         if req.args.has_key("n"):
58             grab_command += " -n " 
59             
60         #-r (size) resize to to a fixed width, maximum: 1920
61         if req.args.has_key("r"):
62             grab_command += " -r %s " % req.args['r'][0]
63
64         #-l always 4:3, create letterbox if 16:9
65         if req.args.has_key("l"):
66             grab_command += " -l "
67
68         #target filename
69         if req.args.has_key("filename"):
70             filetarget = req.args['filename'][0]
71         else:
72             filetarget = "/tmp/screenshot."+imageformat
73
74         # choose if we leave the file in /tmp
75         if req.args.has_key("save"):
76             save_image = True
77         else:
78             save_image = False
79
80         if os_path.exists(self.grab_bin) is not True:
81             return http.Response(responsecode.OK,stream="grab is not installed at '%s'. go and fix it."%self.grab_bin)
82         else:
83             headers = http_headers.Headers()
84             headers.addRawHeader('Content-Disposition', 'inline; filename=screenshot.%s;'%imageformat)
85             headers.addRawHeader('Content-Type','image/%s'%imageformat)
86             return http.Response(responsecode.OK,headers,stream=GrabStream(grab_command+filetarget,target=filetarget,save=save_image))
87        
88 class GrabStream(stream.ProducerStream):
89     """
90         used to start the grab-bin in the console in the background
91         while this takes some time, the browser must wait until the grabis finished
92     """
93     def __init__(self,cmd,target=None,save=False):
94         self.cmd = cmd
95         self.target = target
96         self.save = save
97         self.output = ""
98         stream.ProducerStream.__init__(self)
99
100         self.container = eConsoleAppContainer()
101         self.container.appClosed.append(self.cmdFinished)
102         self.container.dataAvail.append(self.dataAvail)
103         print "AiO grab starting aio grab with cmdline:",self.cmd
104         self.container.execute(cmd)
105
106     def cmdFinished(self,data):
107         if int(data) is 0 and self.target is not None:
108             try:
109                 fp = open(self.target)
110                 self.write(fp.read())
111                 fp.close()
112                 if self.save is False:
113                     os_remove(self.target)
114             except Exception,e:
115                 self.write("internal error while reading target file")
116         elif int(data) is 0 and self.target is None:
117             self.write(self.output)
118         elif int(data) is 1:
119             self.write(self.output)
120         else:
121             self.write("internal error")
122         self.finish()
123
124     def dataAvail(self,data):
125         print "AiO grab:",data