5 from enigma import eTimer
6 from FritzReconnect import reconnect
\r
7 from os import listdir
\r
8 from RSConfig import config
\r
9 from RSDownloader import downloadPage, GET, POST, matchGet
\r
10 from RSLog import writeLog
\r
11 from time import localtime, sleep
\r
12 from Tools.Directories import fileExists
\r
14 ##############################################################################
\r
19 self.downloaded_files = []
\r
20 self.failed_files = []
\r
21 self.downloading_file = ""
\r
23 self.downloading = False
\r
25 self.downloadTimer = eTimer()
\r
26 self.downloadTimer.timeout.get().append(self.download)
27 self.freeDownloadTimer = eTimer()
\r
28 self.freeDownloadTimer.timeout.get().append(self.freeDownload)
\r
29 self.reloadTimer = eTimer()
\r
30 self.reloadTimer.timeout.get().append(self.startDownloading)
\r
32 def addFile(self, file):
\r
33 writeLog("Adding %s to download-list..." % file)
\r
35 if self.files.__contains__(file):
\r
36 writeLog("File %s is already in the download-list!" % file)
\r
38 elif self.downloaded_files.__contains__(file):
\r
39 writeLog("File %s already downloaded!" % file)
\r
41 elif self.downloading_file == file:
\r
42 writeLog("Already downloading %s!" % file)
\r
45 self.files.append(file)
\r
46 writeLog("Added %s to the downloads." % file)
\r
48 def readLists(self):
\r
49 writeLog("Reading all lists...")
\r
50 path = config.plugins.RSDownloader.lists_directory.value
\r
51 if not path.endswith("/"):
\r
54 writeLog("Directory: " + path)
\r
57 file_list = listdir(path)
\r
58 writeLog("Count of lists: " + str(len(file_list)))
\r
61 writeLog("Could not find any list!")
\r
66 writeLog("Reading list %s..." % list)
\r
70 if l.startswith("http://"):
\r
71 self.addFile(l.replace("\n", "").replace("\r", ""))
\r
74 writeLog("Error while reading list %s!" % list)
\r
76 def startDownloading(self):
\r
77 self.reloadTimer.stop()
\r
79 mayDownload = self.mayDownload()
\r
81 self.downloading = True
\r
82 writeLog("Starting downloads...")
\r
84 self.downloadTimer.start(1, 1)
\r
87 self.reloadTimer.start(5 * 60 * 1000, 1)
\r
91 if len(self.files) > 0:
\r
92 writeLog("Getting next file...")
\r
93 self.downloading_file = self.files[0]
\r
95 writeLog("Downloading file %s..." % self.downloading_file)
\r
97 username = config.plugins.RSDownloader.username.value
\r
98 password = config.plugins.RSDownloader.password.value
99 freeRsDownload = False
\r
100 if username != "" and password != "":
\r
101 downloading_file = self.downloading_file.replace("http://", "http://" + username + ":" + password + "@")
\r
103 downloading_file = self.downloading_file
104 if downloading_file.startswith("http://rapidshare.com"):
105 freeRsDownload = True
\r
107 path = config.plugins.RSDownloader.downloads_directory.value
\r
108 if not path.endswith("/"):
\r
112 list = downloading_file.split("/")
\r
113 file = path + list[(len(list)-2)] + "_" + list[(len(list)-1)]
\r
114 file = file.replace(".html", "")
\r
116 file = downloading_file.replace(".html", "").replace("http://", "")
\r
117 writeLog("Local file: " + file)
\r
119 if fileExists(file):
\r
120 writeLog("File %s already exists! Downloading next one...")
\r
121 self.downloadTimer.start(1, 1)
\r
124 if config.plugins.RSDownloader.reconnect_fritz.value == True:
127 data = GET(downloading_file)
128 url = matchGet('<form[^>]+action="([^"]+)', data)
130 self.downloadError("Failed to get download page url")
132 data = POST(url, "dl.start=Free")
133 seconds = matchGet('var c=([0-9]+)', data)
135 self.downloadError("Failed to get download page url")
137 writeLog("Free RS-download... must wait %s seconds!" % seconds)
138 url = matchGet('"dlf" action="([^"]+)', data)
140 self.downloadError("Failed to get download page url")
142 self.freeDownloadUrl = url
143 self.freeDownloadFile = file
144 self.freeDownloadTimer.start((int(seconds) + 2) * 1000, 1)
146 downloadPage(downloading_file, file).addCallback(self.downloadCallback).addErrback(self.downloadError)
\r
148 self.downloading_file = ""
\r
149 self.downloading = False
\r
150 writeLog("Empty list... everything done?")
\r
152 self.reloadTimer.start(5 * 60 * 1000, 1)
154 def freeDownload(self):
155 downloadPage(self.freeDownloadUrl, self.freeDownloadFile).addCallback(self.downloadCallback).addErrback(self.downloadError)
\r
157 def downloadCallback(self, callback = None):
\r
158 writeLog("File %s downloaded." % self.downloading_file)
\r
160 self.cleanLists(self.downloading_file)
\r
162 self.downloaded_files.append(self.downloading_file)
\r
163 self.downloadTimer.start(1, 1)
\r
165 def downloadError(self, error = None):
\r
166 if error is not None:
\r
167 writeLog("Error while downloading: " + str(error))
\r
169 self.failed_files.append(self.downloading_file)
\r
170 self.downloadTimer.start(1, 1)
\r
172 def cleanLists(self, file):
\r
173 writeLog("Cleaning lists...")
\r
175 path = config.plugins.RSDownloader.lists_directory.value
\r
176 if not path.endswith("/"):
\r
180 file_list = listdir(path)
\r
184 for x in file_list:
\r
187 f = open(list, "r")
\r
191 if content.__contains__(file):
\r
192 content = content.replace(file, "")
\r
193 content = content.replace("\n\n", "\n").replace("\r\r", "\r")
\r
195 f = open(list, "w")
\r
199 writeLog("Error while cleaning list %s!" % list)
\r
201 def mayDownload(self):
\r
202 start = config.plugins.RSDownloader.start_time.value
\r
203 end = config.plugins.RSDownloader.end_time.value
\r
206 #print "====>Start:", str(start)
\r
207 #print "====>End:", str(end)
\r
208 #print "====>Now:", str(t)
\r
212 hour_start = start[0]
\r
213 minute_start = start[1]
\r
215 minute_end = end[1]
\r
217 if start == end: # Same start and end-time
\r
219 elif hour_end < hour_start: # Different days!!!
\r
220 if hour_now > hour_start or hour_now < hour_end:
\r
222 elif hour_now == hour_start and minute_now > minute_start:
\r
224 elif hour_now == hour_end and minute_now < minute_end:
\r
226 elif hour_now > hour_start and hour_now < hour_end: # Same day...
\r
228 elif hour_now == hour_start and minute_now > minute_start: # Same day, same start-hour...
\r
230 elif hour_now == hour_end and minute_now < minute_end: # Same day, same end-hour...
\r