add homeys TVCharts plugin
[vuplus_dvbapp-plugin] / googlemaps / src / WebPixmap.py
1 from enigma import ePicLoad, ePixmap, getDesktop
2 from Components.Pixmap import Pixmap
3 from twisted.web.client import downloadPage
4 from urllib import quote_plus
5 from os import remove as os_remove, mkdir as os_mkdir
6 from os.path import isdir as os_path_isdir, isfile as os_isfile
7
8 from Components.AVSwitch import AVSwitch
9 from Components.config import config
10
11 def getAspect():
12         val = AVSwitch().getAspectRatioSetting()
13         if val == 0 or val == 1:
14                 r = (5*576, 4*720)
15         elif val == 2 or val == 3 or val == 6:
16                 r = (16*720, 9*1280)
17         elif val == 4 or val == 5:
18                 r = (16*576, 10*720)
19         return r
20
21 class WebPixmap(Pixmap):
22         def __init__(self, default = None):
23                 Pixmap.__init__(self)
24                 self.picload = ePicLoad()
25                 self.picload.PictureData.get().append(self.setPixmapCB)
26                 self.cachedir = "/tmp/googlemaps/"
27                 self.default = default
28
29         def onShow(self):
30                 Pixmap.onShow(self)
31                 sc = getAspect()
32                 resize = 1
33                 background = '#ff000000'
34                 self.picload.setPara((self.instance.size().width(), self.instance.size().height(), sc[0], sc[1], False, resize, background))
35
36         def load(self, url = None):
37                 tmpfile = ''.join((self.cachedir, quote_plus(url), '.jpg'))
38                 if os_path_isdir(self.cachedir) is False:
39                         print "cachedir not existing, creating it"
40                         os_mkdir(self.cachedir)
41                 if os_isfile(tmpfile):
42                         self.tmpfile = tmpfile
43                         self.onLoadFinished(None)
44                 elif url is not None:
45                         self.tmpfile = tmpfile
46                         head = {
47                                 "Accept":"image/png,image/*;q=0.8,*/*;q=0.5",
48                                 "Accept-Language":"de",
49                                 "Accept-Encoding":"gzip,deflate",
50                                 "Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.7",
51                                 "Keep-Alive":"300",
52                                 "Referer":"http://maps.google.de/",
53                                 "Cookie:": "khcookie=fzwq1BaIQeBvxLjHsDGOezbBcCBU1T_t0oZKpA; PREF=ID=a9eb9d6fbca69f5f:TM=1219251671:LM=1219251671:S=daYFLkncM3cSOKsF; NID=15=ADVC1mqIWQWyJ0Wz655SirSOMG6pXP2ocdXwdfBZX56SgYaDXNNySnaOav-6_lE8G37iWaD7aBFza-gsX-kujQeH_8WTelqP9PpaEg0A_vZ9G7r50tzRBAZ-8GUwnEfl",
54                                 "Connection":"keep-alive"
55                         }
56                         agt = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.2) Gecko/2008091620 Firefox/3.0.2"
57                         downloadPage(url,self.tmpfile,headers=head,agent=agt).addCallback(self.onLoadFinished).addErrback(self.onLoadFailed)
58                 elif self.default:
59                         self.picload.startDecode(self.default)
60
61         def onLoadFinished(self,result):
62                 self.picload.startDecode(self.tmpfile)
63
64         def onLoadFailed(self,error):
65                 print "WebPixmap:onLoadFAILED", error
66                 if self.default and self.instance:
67                         print "showing 404", self.default
68                         self.picload.startDecode(self.default)
69                 if os_isfile(self.tmpfile):
70                         os_remove(self.tmpfile)
71
72         def setPixmapCB(self, picInfo = None):
73                 if os_isfile(self.tmpfile):
74                         if config.plugins.GoogleMaps.cache_enabled.value is not True:
75                                 os_remove(self.tmpfile)
76                 ptr = self.picload.getData()
77                 if ptr and self.instance:
78                         self.instance.setPixmap(ptr.__deref__())
79