SeriesPlugin 1.2.8: Fixed multiple timer / movie handling
[enigma2-plugins.git] / seriesplugin / src / SeriesPluginTimer.py
1 #######################################################################
2 #
3 #    Series Plugin for Enigma-2
4 #    Coded by betonme (c) 2012 <glaserfrank(at)gmail.com>
5 #    Support: http://www.i-have-a-dreambox.com/wbb2/thread.php?threadid=TBD
6 #
7 #    This program is free software; you can redistribute it and/or
8 #    modify it under the terms of the GNU General Public License
9 #    as published by the Free Software Foundation; either version 2
10 #    of the License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #######################################################################
18
19 # for localized messages
20 from . import _
21
22 from time import time
23 from enigma import eEPGCache
24 from ServiceReference import ServiceReference
25
26 # Config
27 from Components.config import *
28
29 from Screens.MessageBox import MessageBox
30 from Tools.Notifications import AddPopup
31 from Tools.BoundFunction import boundFunction
32
33 # Plugin internal
34 from SeriesPlugin import getInstance, refactorTitle, refactorDescription
35 from Logger import splog
36
37
38 #######################################################
39 # Label timer
40 class SeriesPluginTimer(object):
41
42         data = []
43         counter = 0;
44         
45         def __init__(self, timer, name, begin, end):
46                 
47                 splog("SPT: SeriesPluginTimer: name, timername, begin, end:", name, timer.name, begin, end)
48                 timer.log(600, "[SeriesPlugin] Try to find infos for %s" % (timer.name) )
49                 
50                 # We have to compare the length,
51                 # because of the E2 special chars handling for creating the filenames
52                 #if timer.name == name:
53                 # Mad Men != Mad_Men
54                 
55                 epgcache = eEPGCache.getInstance()
56                 
57                 event = None
58                 
59                 if timer.eit:
60                         #splog("SPT: Timer Eit is set", timer.service_ref.ref, timer.eit)
61                         event = epgcache.lookupEventId(timer.service_ref.ref, timer.eit)
62                         splog("SPT: LookupEventId", timer.eit, event)
63                 if not(event):
64                         #splog("Lookup EventTime", timer.service_ref.ref, end, begin)
65                         event = epgcache.lookupEventTime( timer.service_ref.ref, begin + ((end - begin) /2) );
66                         splog("SPT: lookupEventTime", event )
67                 #if not(event):
68                 #       splog("Lookup Event", timer.service_ref.ref, end, begin)
69                 #       events = epgcache.lookupEvent( [ "T" , ( timer.service_ref.ref, 0, begin + ((end - begin) /2) ) ] );
70                 #       splog("LookupEvent event(s) found", len(events), events )
71                 #       event = events and events[0]
72                 
73                 if event:
74                         #splog("EPG event found")
75                         if not ( len(timer.name) == len(name) == len(event.getEventName()) ):
76                                 splog("SPT: Skip timer because it is already modified", timer.name, name, event and event.getEventName(), len(timer.name), len(name), len(event.getEventName()) )
77                                 timer.log(601, "[SeriesPlugin] Skip timer because it is already modified")
78                                 return
79                 else:
80                         if ( len(timer.name) == len(name) ):
81                                 splog("SPT: Skip timer because no event was found", timer.name, name, len(timer.name), len(name))
82                                 timer.log(602, "[SeriesPlugin] Skip timer because no event was found")
83                                 return
84                 
85                 if timer.begin < time() + 60:
86                         splog("SPT: Skipping an event because it starts in less than 60 seconds", timer.name )
87                         timer.log(603, "[SeriesPlugin] Skip timer because it starts in less than 60 seconds")
88                         return
89                 
90                 if timer.isRunning():
91                         splog("SPT: Skipping timer because it is already running", timer.name )
92                         timer.log(604, "[SeriesPlugin] Skip timer because it is already running")
93                         return
94                 
95                 if timer.justplay:
96                         splog("SPT: Skipping justplay timer", timer.name )
97                         timer.log(605, "[SeriesPlugin] Skip justplay timer")
98                         return
99                 
100                 
101                 seriesPlugin = getInstance()
102                 
103                 if timer.service_ref:
104                         #channel = timer.service_ref.getServiceName()
105                         #splog(channel)
106                         
107                         splog("SPT: getEpisode:", name, begin, end)
108                         seriesPlugin.getEpisode(
109                                         boundFunction(self.timerCallback, timer),
110                                         #name, begin, end, channel, future=True
111                                         name, begin, end, str(timer.service_ref), future=True
112                                 )
113                 else:
114                         splog("SPT: SeriesPluginTimer: No channel specified")
115                         self.timerCallback("No channel specified")
116
117         def timerCallback(self, timer, data=None):
118                 splog("SPT: timerCallback", data)
119                 splog(data)
120                 
121                 if data and len(data) == 4 and timer:
122                         
123                         # Episode data available, refactor name and description
124                         from SeriesPluginRenamer import newLegacyEncode
125                         timer.name = refactorTitle(timer.name, data)
126                         #timer.name = newLegacyEncode(refactorTitle(timer.name, data))
127                         timer.description = refactorDescription(timer.description, data)
128                         
129                         timer.log(610, "[SeriesPlugin] Success: Changed name: %s." % (timer.name))
130                 
131                 elif data:
132                         timer.log(611, "[SeriesPlugin] Failed: %s." % ( str( data ) ))
133                         SeriesPluginTimer.data.append(
134                                 str(timer.name) + " " + str( data )
135                         )
136                 
137                 else:
138                         timer.log(612, "[SeriesPlugin] Failed." )
139                         SeriesPluginTimer.data.append(
140                                 str(timer.name) + " " + _("No data available")
141                         )
142                 
143                 if config.plugins.seriesplugin.timer_popups.value and config.plugins.seriesplugin.timer_popups_success.value:
144                         
145                         SeriesPluginTimer.counter = SeriesPluginTimer.counter +1
146                         
147                         if SeriesPluginTimer.data or config.plugins.seriesplugin.timer_popups_success.value:
148                                 
149                                 # Maybe there is a better way to avoid multiple Popups
150                                 from SeriesPlugin import seriespluginworker
151                                 if not seriespluginworker or not seriespluginworker.__list:
152                                         if SeriesPluginTimer.data:
153                                                 AddPopup(
154                                                         "SeriesPlugin:\n" + _("Timer rename has been finished with %d errors:\n") % (len(SeriesPluginTimer.data)) +"\n" +"\n".join(SeriesPluginTimer.data),
155                                                         MessageBox.TYPE_ERROR,
156                                                         0,
157                                                         'SP_PopUp_ID_TimerFinished'
158                                                 )
159                                         else:
160                                                 AddPopup(
161                                                         "SeriesPlugin:\n" + _("%d timer renamed successfully") % (SeriesPluginTimer.counter),
162                                                         MessageBox.TYPE_INFO,
163                                                         0,
164                                                         'SP_PopUp_ID_TimerFinished'
165                                                 )
166                                         SeriesPluginTimer.data = []
167                                         SeriesPluginTimer.counter = 0