Skin-Components support for automaticvolumeadjustment, merlinmusicplayer, partnerbox...
[enigma2-plugins.git] / yttrailer / src / plugin.py
1
2 #  YTTrailer
3 #
4 #  Coded by Dr.Best (c) 2011
5 #  Support: www.dreambox-tools.info
6 #
7 #  All Files of this Software are licensed under the Creative Commons
8 #  Attribution-NonCommercial-ShareAlike 3.0 Unported
9 #  License if not stated otherwise in a Files Head. To view a copy of this license, visit
10 #  http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative
11 #  Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
12
13 #  Additionally, this plugin may only be distributed and executed on hardware which
14 #  is licensed by Dream Multimedia GmbH.
15
16 #  This plugin is NOT free software. It is open source, you are allowed to
17 #  modify it (if you keep the license), but it may not be commercially
18 #  distributed other than under the conditions noted above.
19 #  This applies to the source code as a whole as well as to parts of it, unless
20 #  explicitely stated otherwise.
21
22 from __init__ import decrypt_block, validate_cert, read_random, rootkey, l2key
23 from Screens.Screen import Screen
24 from Plugins.Plugin import PluginDescriptor
25 from Components.ActionMap import ActionMap, HelpableActionMap
26 from Components.PluginComponent import plugins
27 from Plugins.Plugin import PluginDescriptor
28 from Components.Sources.StaticText import StaticText
29 from Components.GUIComponent import GUIComponent
30 from enigma import eServiceReference,  RT_WRAP, RT_VALIGN_CENTER, RT_HALIGN_LEFT, gFont, eListbox, eListboxPythonMultiContent, eTPM
31
32 from Components.config import config, ConfigSubsection, ConfigSelection, getConfigListEntry, configfile, ConfigText, ConfigInteger, ConfigYesNo
33 from Components.ConfigList import ConfigListScreen
34
35 from Screens.InfoBarGenerics import InfoBarShowHide, InfoBarSeek, InfoBarAudioSelection, InfoBarNotifications, InfoBarServiceNotifications, InfoBarPVRState, InfoBarMoviePlayerSummarySupport
36 from Components.ServiceEventTracker import InfoBarBase
37 from skin import TemplatedListFonts, componentSizes
38
39 # for localized messages
40 from . import _
41
42 config.plugins.yttrailer = ConfigSubsection()
43 config.plugins.yttrailer.show_in_extensionsmenu = ConfigYesNo(default = False)
44 config.plugins.yttrailer.best_resolution = ConfigSelection(default="2", choices = [("0", _("1080p")),("1", _("720p")), ("2", _("No HD streaming"))])
45 config.plugins.yttrailer.ext_descr = ConfigText(default="german", fixed_size = False)
46 config.plugins.yttrailer.max_results =  ConfigInteger(5,limits = (1, 10))
47 config.plugins.yttrailer.close_player_with_exit =  ConfigYesNo(default = False)
48
49 from Screens.EventView import EventViewBase
50 baseEventViewBase__init__ = None
51
52 from Screens.EpgSelection import EPGSelection
53 baseEPGSelection__init__ = None
54 etpm = eTPM()
55
56 from Plugins.SystemPlugins.TubeLib.youtube.Base import buildYoutube
57 from Plugins.SystemPlugins.TubeLib.youtube.Search import Search
58 from Plugins.SystemPlugins.TubeLib.youtube.Videos import Videos
59
60 def autostart(reason, **kwargs):
61         global l2key
62         l2cert = etpm.getData(eTPM.DT_LEVEL2_CERT)
63         if l2cert:
64                 l2key = validate_cert(l2cert, rootkey)
65                 if l2key:
66                         global baseEventViewBase__init__, baseEPGSelection__init__
67                         if baseEventViewBase__init__ is None:
68                                 baseEventViewBase__init__ = EventViewBase.__init__
69                         EventViewBase.__init__ = EventViewBase__init__
70                         EventViewBase.showTrailer = showTrailer
71                         EventViewBase.showTrailerList = showTrailerList
72                         EventViewBase.showConfig = showConfig
73
74                         if baseEPGSelection__init__ is None:
75                                 baseEPGSelection__init__ = EPGSelection.__init__
76                         EPGSelection.__init__ = EPGSelection__init__
77                         EPGSelection.showTrailer = showTrailer
78                         EPGSelection.showConfig = showConfig
79                         EPGSelection.showTrailerList = showTrailerList
80
81
82 def setup(session,**kwargs):
83         session.open(YTTrailerSetup)
84
85 def Plugins(**kwargs):
86
87         list = [PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART, fnc = autostart)]
88         list.append(PluginDescriptor(name="YTTrailer Setup", description=_("YouTube-Trailer Setup"), where = PluginDescriptor.WHERE_PLUGINMENU, fnc=setup, icon="YTtrailer.png"))
89         if config.plugins.yttrailer.show_in_extensionsmenu.value:
90                 list.append(PluginDescriptor(name="YTTrailer Setup", description=_("YouTube-Trailer Setup"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=setup, icon="YTtrailer.png"))
91         return list
92
93 def EventViewBase__init__(self, Event, Ref, callback=None, similarEPGCB=None):
94         baseEventViewBase__init__(self, Event, Ref, callback, similarEPGCB)
95         self["trailerActions"] = ActionMap(["InfobarActions", "InfobarTeletextActions"],
96         {
97                 "showTv": self.showTrailer,
98                 "showRadio": self.showTrailerList,
99                 "startTeletext": self.showConfig
100         })
101
102
103 def EPGSelection__init__(self, session, service, zapFunc=None, eventid=None, bouquetChangeCB=None, serviceChangeCB=None):
104         baseEPGSelection__init__(self, session, service, zapFunc, eventid, bouquetChangeCB, serviceChangeCB)
105         self["trailerActions"] = ActionMap(["InfobarActions", "InfobarTeletextActions"],
106         {
107                 "showTv": self.showTrailer,
108                 "showRadio": self.showTrailerList,
109                 "startTeletext": self.showConfig
110         })
111
112 def showConfig(self):
113         self.session.open(YTTrailerSetup)
114
115 def showTrailer(self):
116         eventname = ""
117         if isinstance(self, EventViewBase):
118                 if self.event:
119                         eventname = self.event.getEventName()
120         else:
121                 cur = self["list"].getCurrent()
122                 if cur and cur[0]:
123                         event = cur[0]
124                         eventname = event.getEventName()
125
126         ytTrailer = YTTrailer(self.session)
127         ytTrailer.showTrailer(eventname)
128
129 def showTrailerList(self):
130         eventname = ""
131         if isinstance(self, EventViewBase):
132                 if self.event:
133                         eventname = self.event.getEventName()
134         else:
135                 cur = self["list"].getCurrent()
136                 if cur and cur[0]:
137                         event = cur[0]
138                         eventname = event.getEventName()
139
140         self.session.open(YTTrailerList, eventname)
141
142 class YTTrailer:
143         def __init__(self, session):
144                 self.session = session
145                 self._youtube = None
146                 self._query = None
147                 self.l3cert = etpm.getData(eTPM.DT_LEVEL3_CERT)
148
149         def showTrailer(self, eventname):
150                 if eventname:
151                         self.getYTFeeds(eventname, 1)
152
153         def _gotYTFeeds(self, success, videos, data):
154                 if videos:
155                         ref = self.setServiceReference(videos[0])
156                         if ref:
157                                 from twisted.internet import reactor
158                                 reactor.callLater(0, self.session.open, TrailerPlayer, ref)
159
160         def getYTFeeds(self, eventname, max_results):
161                 if not self._youtube:
162                         self._youtube = buildYoutube()
163                 if int(config.plugins.yttrailer.best_resolution.value) <= 1:
164                         shd = "HD"
165                 else:
166                         shd = ""
167                 q = "%s %s Trailer %s" % (eventname, shd, config.plugins.yttrailer.ext_descr.value)
168                 search = Search(self._youtube)
169                 self._query = search.list(self._gotYTFeeds, searchTerm=q, maxResults=max_results)
170
171         def setServiceReference(self, entry):
172                 url = entry.url
173                 if url:
174                         ref = eServiceReference(4097,0,url)
175                         ref.setName(entry.title)
176                 else:
177                         ref = None
178                 return ref
179
180 class YTTrailerList(Screen, YTTrailer):
181
182         skin = """
183                 <screen name="YTTrailerList" position="center,center" size="620,391" title="YT Trailer-List">
184                         <widget name="list" position="0,2" size="620,385" />
185                 </screen>"""
186
187         def __init__(self, session, eventname):
188                 Screen.__init__(self, session)
189                 YTTrailer.__init__(self, session)
190
191                 self["actions"] = ActionMap(["WizardActions"],
192                 {
193                         "ok": self.okPressed,
194                         "back": self.close
195                 }, -1)
196
197                 self.eventName = eventname
198                 self["list"] = TrailerList()
199                 self.onFirstExecBegin.append(self.startRun)
200
201         def _gotYTFeeds(self, success, videos, data):
202                 if videos:
203                         entryList = []
204                         for entry in videos:
205                                 entryList.append(((entry),))
206                         self["list"].setList(entryList)
207
208         def startRun(self):
209                 self.getYTFeeds(self.eventName, config.plugins.yttrailer.max_results.value)
210
211         def okPressed(self):
212                 entry = self["list"].getCurrent()
213                 if entry:
214                         ref = self.setServiceReference(entry)
215                         if ref:
216                                 self.session.open(TrailerPlayer, ref)
217
218 class TrailerList(GUIComponent, object):
219         SKIN_COMPONENT_KEY = "YTTrailerList"
220         SKIN_COMPONENT_TITLE_HEIGHT = "titleHeight"
221         SKIN_COMPONENT_DESCRIPTION_HEIGHT = "descriptionHeight"
222         SKIN_COMPONENT_LINE_SPACING = "lineSpacing"
223
224
225         GUI_WIDGET = eListbox
226
227         def __init__(self):
228                 GUIComponent.__init__(self)
229                 self.l = eListboxPythonMultiContent()
230                 self.l.setBuildFunc(self.buildList)
231                 tlf = TemplatedListFonts()
232                 self.l.setFont(0, gFont(tlf.face(tlf.BIG), tlf.size(tlf.BIG)))
233                 self.l.setFont(1, gFont(tlf.face(tlf.SMALL), tlf.size(tlf.SMALL)))
234                 self.l.setItemHeight(componentSizes.itemHeight(self.SKIN_COMPONENT_KEY, 77))
235
236         def buildList(self, entry):
237                 sizes = componentSizes[TrailerList.SKIN_COMPONENT_KEY]
238                 configEntryWidth = sizes.get(componentSizes.ITEM_WIDTH, 620)
239                 titleHeight = sizes.get(TrailerList.SKIN_COMPONENT_TITLE_HEIGHT, 25)
240                 descriptionHeight = sizes.get(TrailerList.SKIN_COMPONENT_DESCRIPTION_HEIGHT, 40)
241                 lineSpacing = sizes.get(TrailerList.SKIN_COMPONENT_LINE_SPACING, 3)             
242                 res = [ entry ]
243                 res.append((eListboxPythonMultiContent.TYPE_TEXT, 0, 2, configEntryWidth , titleHeight, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, entry.title))
244                 res.append((eListboxPythonMultiContent.TYPE_TEXT, 0, titleHeight+lineSpacing, configEntryWidth , descriptionHeight, 1, RT_WRAP, entry.description))
245                 return res
246
247         def getCurrent(self):
248                 cur = self.l.getCurrentSelection()
249                 return cur and cur[0]
250
251         def postWidgetCreate(self, instance):
252                 instance.setContent(self.l)
253                 self.instance.setWrapAround(True)
254
255         def preWidgetRemove(self, instance):
256                 instance.setContent(None)
257
258         def setList(self, list):
259                 self.l.setList(list)
260
261 class TrailerPlayer(InfoBarBase, InfoBarShowHide, InfoBarSeek, InfoBarAudioSelection, InfoBarNotifications, InfoBarServiceNotifications, InfoBarPVRState, InfoBarMoviePlayerSummarySupport, Screen):
262
263         ENABLE_RESUME_SUPPORT = True
264         ALLOW_SUSPEND = True
265
266         def __init__(self, session, ref):
267                 Screen.__init__(self, session)
268                 self.session = session
269                 self["actions"] = HelpableActionMap(self, "MoviePlayerActions",
270                         {
271                                 "leavePlayer": (self.leavePlayer, _("leave movie player..."))
272                         })
273
274                 if config.plugins.yttrailer.close_player_with_exit.value:
275                         self["closeactions"] = HelpableActionMap(self, "WizardActions",
276                                 {
277                                         "back": (self.close, _("leave movie player..."))
278                                 })
279
280
281                 self.allowPiP = False
282                 for x in InfoBarShowHide, InfoBarBase, InfoBarSeek, \
283                                 InfoBarAudioSelection, InfoBarNotifications, \
284                                 InfoBarServiceNotifications, InfoBarPVRState,  \
285                                 InfoBarMoviePlayerSummarySupport:
286                         x.__init__(self)
287
288                 self.returning = False
289                 self.skinName = "MoviePlayer"
290                 self.lastservice = session.nav.getCurrentlyPlayingServiceReference()
291                 self.session.nav.playService(ref)
292                 self.onClose.append(self.__onClose)
293
294         def leavePlayer(self):
295                 self.close()
296
297         def doEofInternal(self, playing):
298                 self.close()
299
300         def __onClose(self):
301                 self.session.nav.playService(self.lastservice)
302
303 class YTTrailerSetup(ConfigListScreen, Screen):
304         skin = """
305                 <screen position="center,center" size="560,400" title="YT-Trailer Setup">
306                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
307                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
308                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
309                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
310                         <widget render="Label" source="key_red" position="0,0" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
311                         <widget render="Label" source="key_green" position="140,0" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
312                         <widget name="config" position="20,50" size="520,330" scrollbarMode="showOnDemand" />
313                 </screen>"""
314
315         def __init__(self, session, args = None):
316                 Screen.__init__(self, session)
317                 self["key_red"] = StaticText(_("Cancel"))
318                 self["key_green"] = StaticText(_("Save"))
319
320                 cfglist = [ ]
321                 cfglist.append(getConfigListEntry(_("Show Setup in Extensions menu"), config.plugins.yttrailer.show_in_extensionsmenu))
322                 cfglist.append(getConfigListEntry(_("Extended search filter"), config.plugins.yttrailer.ext_descr))
323                 cfglist.append(getConfigListEntry(_("Best resolution"), config.plugins.yttrailer.best_resolution))
324                 cfglist.append(getConfigListEntry(_("Max. results in list-mode"), config.plugins.yttrailer.max_results))
325                 cfglist.append(getConfigListEntry(_("Close Player with exit-key"), config.plugins.yttrailer.close_player_with_exit))
326
327
328                 ConfigListScreen.__init__(self, cfglist, session)
329                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
330                 {
331                         "green": self.keySave,
332                         "cancel": self.keyClose
333                 }, -2)
334
335         def keySave(self):
336                 for x in self["config"].list:
337                         x[1].save()
338                 configfile.save()
339                 self.close()
340
341         def keyClose(self):
342                 for x in self["config"].list:
343                         x[1].cancel()
344                 self.close()
345