added porn-center plugin
[enigma2-plugins.git] / porncenter / src / Additions / Plugin.py
1 # PornCenter by AliAbdul\r
2 from Components.AVSwitch import AVSwitch
3 from enigma import ePicLoad, eTimer
4 from os import listdir, remove
5 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
6 from Tools.Import import my_import
7 from Tools.LoadPixmap import LoadPixmap
8 from twisted.web.client import downloadPage, getPage
9 import urllib2
10
11 ##################################################
12
13 class Cache:
14         def __init__(self):
15                 self.sc = AVSwitch().getFramebufferScale()
16                 self.idx = -1
17                 self.finishCallback = None
18                 self.finishCallbackTimer = eTimer()
19                 self.finishCallbackTimer.callback.append(self.callback)
20                 self.oldService = None
21                 self.session = None
22
23         def getIndex(self):
24                 self.idx += 1
25                 return self.idx
26
27         def callback(self):
28                 if self.finishCallback:
29                         self.finishCallback()
30
31         def startCallbackTimer(self):
32                 self.finishCallbackTimer.stop()
33                 self.finishCallbackTimer.start(5000, 1)
34 cache = Cache()
35
36 ##################################################
37
38 class Movie:
39         def __init__(self, name, url, thumb=None):
40                 self.type = "Movie"
41                 self.name = name
42                 self.url = url
43                 self.thumb = None
44                 if thumb:
45                         try:
46                                 req = urllib2.Request(thumb)
47                                 url_handle = urllib2.urlopen(req)\r
48                                 headers = url_handle.info()\r
49                                 contentType = headers.getheader("content-type")
50                         except:
51                                 contentType = None
52                         if contentType:
53                                 if 'image/jpeg' in contentType:
54                                         self.thumbnailFile = "/tmp/"+str(cache.getIndex())+".jpg"
55                                 elif 'image/png' in contentType:
56                                         self.thumbnailFile = "/tmp/"+str(cache.getIndex())+".png"
57                                 else:
58                                         self.thumbnailFile = None
59                         else:
60                                 self.thumbnailFile = None
61                         if self.thumbnailFile:
62                                 downloadPage(thumb, self.thumbnailFile).addCallback(self.decodeThumbnail).addErrback(self.error)
63
64         def error(self, error=None):
65                 if error: print error
66
67         def decodeThumbnail(self, str=None):
68                 self.picload = ePicLoad()
69                 self.picload.PictureData.get().append(self.decodeThumbnailFinished)
70                 self.picload.setPara((150, 75, cache.sc[0], cache.sc[1], False, 1, "#00000000"))
71                 self.picload.startDecode(self.thumbnailFile)
72
73         def decodeThumbnailFinished(self, picInfo=None):
74                 ptr = self.picload.getData()
75                 if ptr:
76                         self.thumb = ptr
77                 remove(self.thumbnailFile)
78                 del self.picload
79                 cache.startCallbackTimer()
80
81         def getVideoUrl(self):
82                 return self.url
83
84 ##################################################
85
86 class Plugin:
87         def __init__(self, name, thumb):
88                 self.type = "Plugin"
89                 self.name = name
90                 self.thumb = LoadPixmap(cached=True, path=resolveFilename(SCOPE_PLUGINS)+"/Extensions/PornCenter/Additions/"+thumb)
91                 self.callback = None
92
93         def getName(self):
94                 return self.name
95
96         def getEntries(self, callback):
97                 pass
98
99         def getMoreEntries(self):
100                 pass
101
102         def getPage(self, url):
103                 getPage(url).addCallback(self.getPageCallback).addErrback(self.getPageError)
104
105         def getPageCallback(self, page):
106                 pass
107
108         def getPageError(self, error=None):
109                 if error:
110                         print "[%s] Error: %s" % (self.name, error)
111
112 ##################################################
113
114 def getPlugins():
115         try:
116                 files = listdir(resolveFilename(SCOPE_PLUGINS)+"/Extensions/PornCenter/Additions")
117                 files.sort()
118         except Exception, exc:
119                 print "[PornCenter] failed to search for plugins:", exc
120                 files = []
121         plugins = []
122         for file in files:
123                 if file.endswith(".py") and not file in ["__init__.py", "Plugin.py", "Podcast.py"]:
124                         try:
125                                 plugin = my_import('.'.join(["Plugins", "Extensions", "PornCenter", "Additions", file[:-3]]))
126                                 if not plugin.__dict__.has_key("getPlugin"):
127                                         print "Plugin %s doesn't have 'getPlugin'-call." % file
128                                         continue
129                                 p = plugin.getPlugin()
130                                 if p:
131                                         plugins.append(p)
132                         except Exception, exc:
133                                 print "Plugin %s failed to load: %s" % (file, exc)
134                                 continue
135         return plugins