proofread
[vuplus_dvbapp-plugin] / googlemaps / src / WebPixmap.py
1 from enigma import loadPic,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:
14         r = 3
15     elif val == 1:
16         r = 3
17     elif val == 2:
18         r = 1
19     elif val == 3:
20         r = 1
21     elif val == 4:
22         r = 2
23     elif val == 5:
24         r = 2
25     elif val == 6:
26         r = 1
27     return r
28
29 class WebPixmap(Pixmap):
30     def __init__(self,default=None):
31         self.default = default
32         self.cachedir = "/tmp/googlemaps/"
33         Pixmap.__init__(self)
34
35     def load(self,url=None):
36         self.url = url
37         tmpfile = 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         else:
59             if self.default is not None:
60                 self.setPixmapFromFile(self.default)
61
62     def onLoadFinished(self,result):
63         self.setPixmapFromFile(self.tmpfile)
64         if os_isfile(self.tmpfile):
65             if config.plugins.GoogleMaps.cache_enabled.value is not True:
66                 os_remove(self.tmpfile)
67
68     def onLoadFailed(self,error):
69         print "WebPixmap:onLoadFAILED",error
70         if self.default is not None:
71             print "showing 404",self.default
72             self.setPixmapFromFile(self.default)
73         if os_isfile(self.tmpfile):
74             os_remove(self.tmpfile)
75
76     def setPixmapFromFile(self,file):
77         if self.instance is not None:
78             h = self.instance.size().height()
79             w = self.instance.size().width()
80             aspect = getAspect()
81             resize = 1
82             rotate = 0
83             background = 1
84             self.pixmap = loadPic(file, w,h,aspect,resize, rotate,background)
85             if self.pixmap is not None:
86                 self.instance.setPixmap(self.pixmap.__deref__())
87