global: 'Dream Multimedia' -> 'Dream Property'
[enigma2-plugins.git] / epgcopy / src / plugin.py
1 # -*- coding: utf-8 -*-
2 #=========================================================================================
3 #
4 # All Files of this Software are licensed under the Creative Commons 
5 # Attribution-NonCommercial-ShareAlike 3.0 Unported 
6 # License if not stated otherwise in a Files Head. To view a copy of this license, visit
7 # http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative
8 # Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
9 #
10 # Alternatively, this plugin may be distributed and executed on hardware which
11 # is licensed by Dream Property GmbH.
12 #
13 # This plugin is NOT free software. It is open source, you are allowed to
14 # modify it (if you keep the license), but it may not be commercially 
15 # distributed other than under the conditions noted above.
16 #
17 # Copyright (C) 2015 by nixkoenner@newnigma2.to
18 # http://newnigma2.to
19 #
20 # License: GPL
21 #
22 #=========================================================================================
23 from Screens.Screen import Screen
24 from Screens.MessageBox import MessageBox
25 from Components.Button import Button
26 from Components.ActionMap import ActionMap
27 from Components.ConfigList import ConfigList, ConfigListScreen
28
29 from enigma import eEPGCache, getDesktop, eTimer
30 from Plugins.Plugin import PluginDescriptor
31
32 import ftplib
33 from os import path, remove
34 from shutil import move
35 from Components.config import getConfigListEntry, config, \
36     ConfigSubsection, ConfigText, ConfigIP, ConfigYesNo, \
37     ConfigPassword, ConfigNumber, KEY_LEFT, KEY_RIGHT, KEY_0, ConfigClock
38     
39
40 from time import time, strftime, mktime, localtime
41 now = localtime()
42 autoCopy = mktime((
43         now.tm_year, now.tm_mon, now.tm_mday, 06, 00, \
44         0, now.tm_wday, now.tm_yday, now.tm_isdst)
45 )
46
47 config.plugins.epgCopy = ConfigSubsection()
48 config.plugins.epgCopy.username = ConfigText(default = "root", fixed_size = False)
49 config.plugins.epgCopy.password = ConfigPassword(default = "", fixed_size = False)
50 config.plugins.epgCopy.ip = ConfigIP(default = [0, 0, 0, 0])
51 config.plugins.epgCopy.copytime = ConfigClock(default = int(autoCopy))
52
53 def myPrint(txt, prefix = None):
54     print ("\033[91m%s\033[m " % txt)
55     
56 def myFtp(): 
57     directory_local='/tmp/' 
58     directory = '/etc/enigma2/' 
59     fileQuelle='epg.db'
60     fileZiel='epgSync.db'
61     
62     if path.isfile(directory_local+fileZiel):
63         try:
64             remove(directory_local+fileZiel)
65             myPrint ("file exist delete")
66         except OSError:
67             myPrint ("OSError remove file")
68         
69     remoteip = "%d.%d.%d.%d" % tuple(config.plugins.epgCopy.ip.value)
70     f = ftplib.FTP(remoteip) 
71     f.login(config.plugins.epgCopy.username.value, config.plugins.epgCopy.password.value)
72     f.cwd(directory)
73     #f.retrlines('LIST')
74     file = open(directory_local+fileZiel, 'wb')
75     f.retrbinary('RETR '+ fileQuelle, file.write)
76     file.close()
77     f.quit()
78     if path.isfile(directory+fileQuelle):
79         remove(directory+fileQuelle)
80     move(directory_local+fileZiel,directory+fileQuelle)
81
82 class copyEveryDay(Screen):
83     def __init__(self, session):
84         self.session = session
85         Screen.__init__(self, session)
86         self.timer = eTimer()
87         self.timer_conn = self.timer.timeout.connect(self.__doCopy)
88         self.configChange()
89     
90     def configChange(self, configElement = None):   
91         if self.timer.isActive(): # stop timer if running
92             self.timer.stop()
93         begin = int(mktime(
94             (now.tm_year, now.tm_mon, now.tm_mday,
95             config.plugins.epgCopy.copytime.value[0],
96             config.plugins.epgCopy.copytime.value[1],
97             now.tm_sec, now.tm_wday, now.tm_yday, now.tm_isdst)
98         ))
99         if begin < time():
100             begin += 86400
101         next = int(abs(time() - begin))
102         myPrint("[copyEveryDay] next reset: %s" % strftime("%c", localtime(time()+ next)))
103         self.timer.startLongTimer(next)
104         
105     def __doCopy(self):
106         if config.plugins.epgCopy.copytime.value:
107             myPrint("[__doCopy] do reset: %s" % strftime("%c", localtime(time())))
108             try:
109                 myFtp()
110             except:
111                 pass            
112         self.configChange()
113
114 class epgCopyScreen(Screen, ConfigListScreen):
115     if getDesktop(0).size().width() == 1280:
116         skin = """
117             <screen position="center,center" size="602,520" title="%s" >
118             <widget name="config" position="center,center" size="580,435" scrollbarMode="showOnDemand" enableWrapAround="1"/>
119             <ePixmap name="red" position="5,475" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
120             <widget name="key_red" position="5,475" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
121             <ePixmap name="green" position="150,475" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
122             <widget name="key_green" position="150,475" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
123             <ePixmap name="yellow" position="295,475" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
124             <widget name="key_yellow" position="295,475" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />    
125             </screen>"""% _("EPG Copy view 1280")
126     else:
127         skin = """
128             <screen position="center,center" size="585,410" title="%s" >
129             <widget name="config" position="center,center" size="580,335" scrollbarMode="showOnDemand" enableWrapAround="1"/>
130             <ePixmap name="red" position="5,365" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
131             <widget name="key_red" position="5,365" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
132             <ePixmap name="green" position="150,365" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
133             <widget name="key_green" position="150,365" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
134             <ePixmap name="yellow" position="295,365" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
135             <widget name="key_yellow" position="295,365" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />    
136             </screen>"""% _("EPG Copy view")
137             
138     def __init__(self, session):
139         Screen.__init__(self, session)
140         self["actions"] = ActionMap(["SetupActions", "ColorActions"],
141         {
142             "green": self.saveSettings,
143             "yellow": self.startManually,
144             "cancel": self.close,
145         }, -1)
146        
147         self["key_green"] = Button(_("save"))
148         self["key_yellow"] = Button(_("manually"))
149         self["key_red"] = Button(_("cancel"))
150         
151         ConfigListScreen.__init__(self, [
152             getConfigListEntry(_("EPG Copy - Source Network IP"), config.plugins.epgCopy.ip),
153             getConfigListEntry(_("EPG Copy - Username"), config.plugins.epgCopy.username),
154             getConfigListEntry(_("EPG Copy - Password"), config.plugins.epgCopy.password),
155             getConfigListEntry(_("EPG Copy - Time to Copy"), config.plugins.epgCopy.copytime),
156         ], session)
157       
158     def saveSettings(self):
159           config.plugins.epgCopy.save()
160           self.close()
161     
162     def startManually(self):
163         self.myStart()
164         
165     def myStart(self):
166         ftpOK = False
167         myEpg = None
168         myEpg = eEPGCache.getInstance()
169         try:
170             myFtp()
171             ftpOK = True
172         except:
173             ftpOK = False
174         
175         if ftpOK:
176             myEpg.load()
177             self.session.openWithCallback(self.epgLoadFinishedConfirm, MessageBox, _("successfully load EPG form the server"), MessageBox.TYPE_INFO, timeout=4)
178         else:
179             self.session.open(MessageBox,_("not successfully please check your config (FTPERROR)"), MessageBox.TYPE_INFO, timeout=10)
180             myPrint ("FTPERROR")
181         
182     def epgLoadFinishedConfirm(self, result):
183         self.close()
184         
185
186 def autoCpy(reason, **kwargs):
187     if kwargs.has_key("session"):
188         global Session
189         Session = kwargs["session"]
190         Session.open(copyEveryDay)
191
192 def main(session, **kwargs):
193     session.open(epgCopyScreen)
194
195 def Plugins(path,**kwargs):
196     global plugin_path
197     plugin_path = path
198     return  [
199              PluginDescriptor(name="epgCopy",description="copy epg.db", where = [ PluginDescriptor.WHERE_PLUGINMENU ], icon="epg.png", fnc = main),
200              PluginDescriptor(name="epgCopy", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main),
201              PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART,fnc = autoCpy)
202              ]
203