SeriesPlugin: 1.5.8 Fixed encoding, popups, channel list integration
[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):
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                 # We have to compare the length,
52                 # because of the E2 special chars handling for creating the filenames
53                 #if timer.name == name:
54                 # Mad Men != Mad_Men
55                 
56                 epgcache = eEPGCache.getInstance()
57                 
58                 event = None
59                 
60                 if timer.eit:
61                         #splog("SPT: Timer Eit is set", timer.service_ref.ref, timer.eit)
62                         event = epgcache.lookupEventId(timer.service_ref.ref, timer.eit)
63                         splog("SPT: LookupEventId", timer.eit, event)
64                 if not(event):
65                         #splog("Lookup EventTime", timer.service_ref.ref, end, begin)
66                         event = epgcache.lookupEventTime( timer.service_ref.ref, begin + ((end - begin) /2) );
67                         splog("SPT: lookupEventTime", event )
68                 #if not(event):
69                 #       splog("Lookup Event", timer.service_ref.ref, end, begin)
70                 #       events = epgcache.lookupEvent( [ "T" , ( timer.service_ref.ref, 0, begin + ((end - begin) /2) ) ] );
71                 #       splog("LookupEvent event(s) found", len(events), events )
72                 #       event = events and events[0]
73                 
74                 if event:
75                         #splog("EPG event found")
76                         if not ( len(timer.name) == len(name) == len(event.getEventName()) ):
77                                 splog("SPT: Skip timer because it is already modified", timer.name, name, event and event.getEventName(), len(timer.name), len(name), len(event.getEventName()) )
78                                 timer.log(601, "[SeriesPlugin] Skip timer because it is already modified")
79                                 return
80                 else:
81                         if ( len(timer.name) == len(name) ):
82                                 splog("SPT: Skip timer because no event was found", timer.name, name, len(timer.name), len(name))
83                                 timer.log(602, "[SeriesPlugin] Skip timer because no event was found")
84                                 return
85                 
86                 if timer.begin < time() + 60:
87                         splog("SPT: Skipping an event because it starts in less than 60 seconds", timer.name )
88                         timer.log(603, "[SeriesPlugin] Skip timer because it starts in less than 60 seconds")
89                         return
90                 
91                 if timer.isRunning():
92                         splog("SPT: Skipping timer because it is already running", timer.name )
93                         timer.log(604, "[SeriesPlugin] Skip timer because it is already running")
94                         return
95                 
96                 if timer.justplay:
97                         splog("SPT: Skipping justplay timer", timer.name )
98                         timer.log(605, "[SeriesPlugin] Skip justplay timer")
99                         return
100                 
101                 
102                 seriesPlugin = getInstance()
103                 
104                 if timer.service_ref:
105                         #channel = timer.service_ref.getServiceName()
106                         #splog(channel)
107                         
108                         splog("SPT: getEpisode:", name, begin, end)
109                         seriesPlugin.getEpisode(
110                                         boundFunction(self.timerCallback, timer),
111                                         #name, begin, end, channel, future=True
112                                         name, begin, end, str(timer.service_ref), future=True
113                                 )
114                 else:
115                         splog("SPT: SeriesPluginTimer: No channel specified")
116                         self.timerCallback("No channel specified")
117
118         def timerCallback(self, timer, data=None):
119                 splog("SPT: timerCallback", data)
120                 splog(data)
121                 
122                 if data and len(data) == 4 and timer:
123                         
124                         # Episode data available, refactor name and description
125                         from SeriesPluginRenamer import newLegacyEncode
126                         timer.name = refactorTitle(timer.name, data)
127                         #timer.name = newLegacyEncode(refactorTitle(timer.name, data))
128                         timer.description = refactorDescription(timer.description, data)
129                         
130                         timer.log(610, "[SeriesPlugin] Success: Changed name: %s." % (timer.name))
131                 
132                 elif data:
133                         timer.log(611, "[SeriesPlugin] Failed: %s." % ( str( data ) ))
134                         SeriesPluginTimer.data.append(
135                                 str(timer.name) + " " + str( data )
136                         )
137                 
138                 else:
139                         timer.log(612, "[SeriesPlugin] Failed." )
140                         SeriesPluginTimer.data.append(
141                                 str(timer.name) + " " + _("No data available")
142                         )
143                 
144                 if config.plugins.seriesplugin.timer_popups.value or config.plugins.seriesplugin.timer_popups_success.value:
145                         
146                         SeriesPluginTimer.counter = SeriesPluginTimer.counter +1
147                         
148                         if SeriesPluginTimer.data or config.plugins.seriesplugin.timer_popups_success.value:
149                                 
150                                 # Maybe there is a better way to avoid multiple Popups
151                                 from SeriesPlugin import seriespluginworker
152                                 
153                                 splog("SPT: timerCallback getListLength", not seriespluginworker or seriespluginworker.getListLength(), not seriespluginworker or seriespluginworker.isListEmpty() )
154                                 
155                                 if not seriespluginworker or seriespluginworker.isListEmpty():
156                                         if SeriesPluginTimer.data:
157                                                 AddPopup(
158                                                         "SeriesPlugin:\n" + _("Timer rename has been finished with %d errors:\n") % (len(SeriesPluginTimer.data)) +"\n" +"\n".join(SeriesPluginTimer.data),
159                                                         MessageBox.TYPE_ERROR,
160                                                         int(config.plugins.seriesplugin.timer_popups_timeout.value),
161                                                         'SP_PopUp_ID_TimerFinished'
162                                                 )
163                                         else:
164                                                 AddPopup(
165                                                         "SeriesPlugin:\n" + _("%d timer renamed successfully") % (SeriesPluginTimer.counter),
166                                                         MessageBox.TYPE_INFO,
167                                                         int(config.plugins.seriesplugin.timer_popups_timeout.value),
168                                                         'SP_PopUp_ID_TimerFinished'
169                                                 )
170                                         SeriesPluginTimer.data = []
171                                         SeriesPluginTimer.counter = 0