[MerlinSkinThemes] - add support for layouts
[enigma2-plugins.git] / skipintro / src / plugin.py
1 # -*- coding: utf-8 -*-
2 from Plugins.Plugin import PluginDescriptor
3 from Components.ActionMap import HelpableActionMap
4 from Components.config import config
5 from Screens.MessageBox import MessageBox
6 from Screens.InfoBarGenerics import InfoBarSeek
7 from SkipIntroDatabase import SIDatabase
8 from SkipIntroSetup import SISetupScreen
9 import re
10
11 baseInfoBarSeek__init__ = None
12 baseInfoBarSeek__serviceStarted = None
13 baseInfoBarSeek__seekableStatusChanged = None
14
15 def getServiceName(self):
16                 service = self.session.nav.getCurrentService()
17                 info = service and service.info()
18                 serviceName = info and info.getName()
19
20                 season=""
21                 if config.plugins.skipintro.title_pattern.value != "Off":
22                         serviceNameRegex = re.compile(config.plugins.skipintro.title_pattern.value)
23                         serviceNameRegexResult = serviceNameRegex.search(serviceName)
24
25                         if serviceNameRegexResult and serviceNameRegexResult.group(2):
26                                 if int(serviceNameRegexResult.group(2).replace("S","")) > 0:
27                                         season = " " + serviceNameRegexResult.group(2)
28                         #if config.plugins.skipintro.save_season.value:
29                         return (serviceNameRegexResult.group(1), season) if serviceNameRegexResult else (serviceName, season)
30                 else:
31                         return (serviceName, season)
32
33 def InfoBarSeek__serviceStarted(self):
34         print "=== InfoBarSeek__serviceStarted SkipIntro"
35         baseInfoBarSeek__serviceStarted(self)
36
37         seek = self.getSeek()
38         if seek is None:
39                 return
40         #make here your own code at starting a video
41         if config.plugins.skipintro.show_videostartmsg.value:
42                 from Tools import Notifications
43
44                 # get the video title
45                 title = getServiceName(self)
46                 # read seek time from database
47                 self.skipSeekTime, self.seriesName = self.database.getSkipTime(title[0], title[1])
48
49                 if self.skipSeekTime == 0:
50                         msgtxt = _("No skip time saved for\n%s") % title[0]
51                 else:
52                         msgtxt = _("Saved skip time found for\n%s:\n%s seconds") %(self.seriesName, str(self.skipSeekTime / 90000))
53                 Notifications.AddNotification(MessageBox, msgtxt, MessageBox.TYPE_INFO, timeout=3)
54
55 def InfoBarSeek__seekableStatusChanged(self):
56                 print "=== InfoBarSeek__seekableStatusChanged SkipIntro"
57                 baseInfoBarSeek__seekableStatusChanged(self)
58                 if self["SkipIntroSeekActions"] is not None:
59                         #do not use this actionmap in tv-mode or on active timeshift
60                         if not self.isSeekable() or (hasattr(self, "timeshift_enabled") and self.timeshift_enabled):
61                                 self["SkipIntroSeekActions"].setEnabled(False)
62                         else:
63                                 self["SkipIntroSeekActions"].setEnabled(True)
64
65 def InfoBarSeek__init__(self, actionmap = "InfobarSeekActions"):
66         print "== InfoBarSeek__init__ SkipIntro"
67         self["SkipIntroSeekActions"] = None
68         baseInfoBarSeek__init__(self, actionmap)
69
70         self.setSkipTimeStart = False
71         self.setSeasonIntroTime = False
72         self.skipTimeStartPos = 0
73         self.skipTimeStopPos = 0
74         self.skipSeekTime = 0
75         self.seriesName = ""
76         self.database = SIDatabase()
77         self.database.initialize()
78
79         def skipIntro():
80                 print "== skip intro"
81                 title = getServiceName(self)
82
83                 if self.setSkipTimeStart:
84                         setIntroTime()
85                         return
86
87                 # read seek time from database
88                 self.skipSeekTime, self.seriesName = self.database.getSkipTime(title[0],title[1])
89
90                 if self.skipSeekTime == 0:
91                         #self.setSkipTimeStart = False
92                         setIntroTime()
93                 else:
94                         if config.plugins.skipintro.show_skipmsg.value:
95                                 self.session.open(MessageBox, _("SkipIntro found seek time for\n%s:\n%s seconds") %(self.seriesName,str(self.skipSeekTime / 90000)), MessageBox.TYPE_INFO, timeout=3)
96                         self.doSeekRelative(self.skipSeekTime)
97
98         def skipIntro_long():
99                 if self.setSkipTimeStart:
100                         self.setSeasonIntroTime=True
101                         setIntroTime()
102                 else:
103                         setIntroTime()
104
105         def setIntroTime_long():
106                 if self.setSkipTimeStart:
107                         self.setSeasonIntroTime=True
108                 setIntroTime()
109
110         def setIntroTime():
111                 if self.setSkipTimeStart:
112                         seekable = self.getSeek()
113                         if seekable:
114                                 self.skipTimeStopPos = seekable.getPlayPosition()
115                                 if self.skipTimeStopPos[0]:
116                                         self.skipTimeStopPos = self.skipTimeStopPos[0]
117                                 else:
118                                         self.skipTimeStopPos = self.skipTimeStopPos[1]
119                         self.skipSeekTime = int(self.skipTimeStopPos) - int(self.skipTimeStartPos) 
120
121                         title = getServiceName(self)
122
123                         db_title = title[0]
124                         if self.setSeasonIntroTime:
125                                 db_title = title[0] + title[1]
126
127                         msgtxt = _("Stopping time measurment for intro duration for\n%s.\nIntro duration: %s seconds") %(db_title, str(self.skipSeekTime / 90000))
128                         msgtxt += _(" (-%s seconds).") %(config.plugins.skipintro.skiptime_decrease.value)
129                         if self.setSeasonIntroTime:
130                                 msgtxt += _("\nIntro duration will be saved with season.")
131                         self.session.open(MessageBox, msgtxt, MessageBox.TYPE_INFO, timeout=3)
132
133                         #reduce the calculated skiptime
134                         self.skipSeekTime = int(self.skipSeekTime) - (int(config.plugins.skipintro.skiptime_decrease.value) * 90000)
135
136                         if self.skipSeekTime<0:
137                                 self.skipSeekTime=0
138
139                         # save seek time into database
140                         self.database.setSkipTime(db_title, self.skipSeekTime)
141                         self.setSkipTimeStart = False
142                         self.setSeasonIntroTime = False
143
144                 else:
145                         self.setSeasonIntroTime=False
146                         seekable = self.getSeek()
147                         if seekable:
148                                 self.skipTimeStartPos = seekable.getPlayPosition()
149                                 if self.skipTimeStartPos[0]:
150                                         self.skipTimeStartPos = self.skipTimeStartPos[0]
151                                 else:
152                                         self.skipTimeStartPos = self.skipTimeStartPos[1]
153                         title = getServiceName(self)
154                         self.session.open(MessageBox, _("Starting time measurement for intro duration for:\n%s") %(title[0]), MessageBox.TYPE_INFO, timeout=3)
155                         self.setSkipTimeStart = True
156
157         self["SkipIntroSeekActions"] = HelpableActionMap(self, "SkipIntroSeekActions",
158                         {
159                                 "2": (skipIntro, _("skip intro")),
160                                 "8": (skipIntro_long, _("set intro time")),
161                                 "2_long": (skipIntro_long, _("skip intro")),
162                         }, prio=-5)
163                         # give them a more priority to win over SeekActions in VTI-Image
164         self["SkipIntroSeekActions"].setEnabled(False)
165
166 ##############################################
167
168 def autostart(reason, **kwargs):
169         if reason == 0 and "session" in kwargs:
170                 session = kwargs["session"]
171
172                 global baseInfoBarSeek__init__, baseInfoBarServiceErrorPopupSupport__serviceStarted
173                 if baseInfoBarSeek__init__ is None:
174                         baseInfoBarSeek__init__ = InfoBarSeek.__init__
175                 InfoBarSeek.__init__ = InfoBarSeek__init__
176
177                 global baseInfoBarSeek__serviceStarted
178                 if baseInfoBarSeek__serviceStarted is None:
179                         baseInfoBarSeek__serviceStarted = InfoBarSeek._InfoBarSeek__serviceStarted
180                 InfoBarSeek._InfoBarSeek__serviceStarted = InfoBarSeek__serviceStarted
181
182                 global baseInfoBarSeek__seekableStatusChanged
183                 if baseInfoBarSeek__seekableStatusChanged is None:
184                         baseInfoBarSeek__seekableStatusChanged = InfoBarSeek._InfoBarSeek__seekableStatusChanged
185                 InfoBarSeek._InfoBarSeek__seekableStatusChanged = InfoBarSeek__seekableStatusChanged
186
187 def setup(session, **kwargs):
188         session.open(SISetupScreen)
189
190 ##############################################
191
192 def Plugins(**kwargs):
193         from SkipIntroSetup import version
194         return [
195                 PluginDescriptor(where=[PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc=autostart),
196                 PluginDescriptor(where=[PluginDescriptor.WHERE_PLUGINMENU], fnc=setup, name="SkipIntro", description=_("Skip intro by predefined skip time"), icon="plugin.png"),
197         ]