remove outdated kwargs, ading default image f 404 rece ived
[vuplus_dvbapp-plugin] / googlemaps / src / WebPixmap.py
1 ###############################################################################
2 # Copyright (c) 2008 Rico Schulte, 3c5x9. All rights reserved.
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the "Software"),
6 # to deal in the Software without restriction, including without limitation
7 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be included
12 # in all copies or substantial portions of the Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 # DEALINGS IN THE SOFTWARE.
21 ###############################################################################
22
23 from enigma import loadPic,ePixmap, getDesktop 
24 from Components.Pixmap import Pixmap
25 from twisted.web.client import downloadPage
26 from urllib import quote_plus
27 from os import remove as os_remove, mkdir as os_mkdir
28 from os.path import isdir as os_path_isdir, isfile as os_isfile
29
30 from Components.AVSwitch import AVSwitch
31 from Components.config import config
32
33 def getAspect():
34     val = AVSwitch().getAspectRatioSetting()
35     if val == 0:
36         r = 3
37     elif val == 1:
38         r = 3
39     elif val == 2:
40         r = 1
41     elif val == 3:
42         r = 1
43     elif val == 4:
44         r = 2
45     elif val == 5:
46         r = 2
47     elif val == 6:
48         r = 1
49     return r
50
51 class WebPixmap(Pixmap):
52     def __init__(self,default=None):
53         self.default = default
54         self.cachedir = "/tmp/googlemaps/"
55         Pixmap.__init__(self)
56
57     def load(self,url=None):
58         self.url = url
59         tmpfile = self.cachedir+quote_plus(url)+".jpg"
60         if os_path_isdir(self.cachedir) is False:
61             print "cachedir not existing, creating it"
62             os_mkdir(self.cachedir)
63         if os_isfile(tmpfile):
64             self.tmpfile = tmpfile
65             self.onLoadFinished(None)
66         elif url is not None:
67             self.tmpfile = tmpfile
68             head = {
69                        "Accept":"image/png,image/*;q=0.8,*/*;q=0.5",
70                        "Accept-Language":"de",
71                        "Accept-Encoding":"gzip,deflate",
72                        "Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.7",
73                        "Keep-Alive":"300",
74                        "Referer":"http://maps.google.de/",
75                        "Cookie:": "khcookie=fzwq1BaIQeBvxLjHsDGOezbBcCBU1T_t0oZKpA; PREF=ID=a9eb9d6fbca69f5f:TM=1219251671:LM=1219251671:S=daYFLkncM3cSOKsF; NID=15=ADVC1mqIWQWyJ0Wz655SirSOMG6pXP2ocdXwdfBZX56SgYaDXNNySnaOav-6_lE8G37iWaD7aBFza-gsX-kujQeH_8WTelqP9PpaEg0A_vZ9G7r50tzRBAZ-8GUwnEfl",
76                        "Connection":"keep-alive"
77                        }
78             agt = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.2) Gecko/2008091620 Firefox/3.0.2"
79             downloadPage(url,self.tmpfile,headers=head,agent=agt).addCallback(self.onLoadFinished).addErrback(self.onLoadFailed)
80         else:
81             if self.default is not None:
82                 self.setPixmapFromFile(self.default)
83
84     def onLoadFinished(self,result):
85         self.setPixmapFromFile(self.tmpfile)
86         if os_isfile(self.tmpfile):
87             if config.plugins.GoogleMaps.cache_enabled.value is not True:
88                 os_remove(self.tmpfile)
89         
90     def onLoadFailed(self,error):
91         print "WebPixmap:onLoadFAILED",error
92         if self.default is not None:
93             print "showing 404",self.default
94             self.setPixmapFromFile(self.default)
95         if os_isfile(self.tmpfile):
96             os_remove(self.tmpfile)
97     
98     def setPixmapFromFile(self,file):
99         if self.instance is not None:
100             h = self.instance.size().height()
101             w = self.instance.size().width()
102             aspect = getAspect()
103             resize = 1
104             rotate = 0
105             background = 1
106             self.pixmap = loadPic(file, w,h,aspect,resize, rotate,background)
107             if self.pixmap is not None:
108                 self.instance.setPixmap(self.pixmap.__deref__())
109                 
110