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