New configs: Every weekday is now configureable (download: yes/no)
[enigma2-plugins.git] / rsdownloader / src / RS.py
1 ##\r
2 ## RS Downloader\r
3 ## by AliAbdul\r
4 ##\r
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
13 \r
14 ##############################################################################\r
15 \r
16 class RS:\r
17         def __init__(self):\r
18                 self.files = []\r
19                 self.downloaded_files = []\r
20                 self.failed_files = []\r
21                 self.downloading_file = ""\r
22                 \r
23                 self.downloading = False\r
24                 \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
31 \r
32         def addFile(self, file):\r
33                 writeLog("Adding %s to download-list..." % file)\r
34                 \r
35                 if self.files.__contains__(file):\r
36                         writeLog("File %s is already in the download-list!" % file)\r
37                 \r
38                 elif self.downloaded_files.__contains__(file):\r
39                         writeLog("File %s already downloaded!" % file)\r
40                 \r
41                 elif self.downloading_file == file:\r
42                         writeLog("Already downloading %s!" % file)\r
43                 \r
44                 else:\r
45                         self.files.append(file)\r
46                         writeLog("Added %s to the downloads." % file)\r
47 \r
48         def readLists(self):
49                 if config.plugins.RSDownloader.onoff.value == False:
50                         writeLog("Background downloader is turned off...")
51                         return
52                 \r
53                 writeLog("Reading all lists...")\r
54                 path = config.plugins.RSDownloader.lists_directory.value\r
55                 if not path.endswith("/"):\r
56                         path = path + "/"\r
57                 \r
58                 writeLog("Directory: " + path)\r
59                 \r
60                 try:\r
61                         file_list = listdir(path)\r
62                         writeLog("Count of lists: " + str(len(file_list)))\r
63                 except:\r
64                         file_list = []\r
65                         writeLog("Could not find any list!")\r
66                 \r
67                 for x in file_list:\r
68                         list = path + x\r
69                         try:\r
70                                 writeLog("Reading list %s..." % list)\r
71                                 \r
72                                 f = open(list, "r")\r
73                                 for l in f:\r
74                                         if l.startswith("http://"):\r
75                                                 self.addFile(l.replace("\n", "").replace("\r", ""))\r
76                                 f.close()\r
77                         except:\r
78                                 writeLog("Error while reading list %s!" % list)\r
79 \r
80         def startDownloading(self):\r
81                 self.reloadTimer.stop()\r
82                 \r
83                 mayDownload = self.mayDownload()\r
84                 if mayDownload:\r
85                         self.downloading = True\r
86                         writeLog("Starting downloads...")\r
87                         self.readLists()\r
88                         self.downloadTimer.start(1, 1)\r
89                         return True\r
90                 else:\r
91                         self.reloadTimer.start(5 * 60 * 1000, 1)\r
92                         return False\r
93 \r
94         def download(self):\r
95                 if len(self.files) > 0:\r
96                         writeLog("Getting next file...")\r
97                         self.downloading_file = self.files[0]\r
98                         del self.files[0]\r
99                         writeLog("Downloading file %s..." % self.downloading_file)\r
100                         \r
101                         username = config.plugins.RSDownloader.username.value\r
102                         password = config.plugins.RSDownloader.password.value
103                         freeRsDownload = False\r
104                         if username != "" and password != "":\r
105                                 downloading_file = self.downloading_file.replace("http://", "http://" + username + ":" + password + "@")\r
106                         else:\r
107                                 downloading_file = self.downloading_file
108                                 if downloading_file.startswith("http://rapidshare.com"):
109                                         freeRsDownload = True\r
110                         \r
111                         path = config.plugins.RSDownloader.downloads_directory.value\r
112                         if not path.endswith("/"):\r
113                                 path = path + "/"\r
114                         \r
115                         try:\r
116                                 list = downloading_file.split("/")\r
117                                 file = path + list[(len(list)-1)]\r
118                                 file = file.replace(".html", "")\r
119                         except:\r
120                                 file = downloading_file.replace(".html", "").replace("http://", "")\r
121                         writeLog("Local file: " + file)\r
122                         \r
123                         if fileExists(file):\r
124                                 writeLog("File %s already exists! Downloading next one...")\r
125                                 self.downloadTimer.start(1, 1)\r
126                         else:
127                                 if freeRsDownload:
128                                         if config.plugins.RSDownloader.reconnect_fritz.value == True:
129                                                 reconnect()
130                                                 sleep(3)
131                                         data = GET(downloading_file)
132                                         url = matchGet('<form[^>]+action="([^"]+)', data)
133                                         if not url:
134                                                 self.downloadError("Failed to get download page url")
135                                         else:
136                                                 data = POST(url, "dl.start=Free")
137                                                 seconds = matchGet('var c=([0-9]+)', data)
138                                                 if not seconds:
139                                                         self.downloadError("Failed to get download page url")
140                                                 else:
141                                                         writeLog("Free RS-download... must wait %s seconds!" % seconds)
142                                                         url = matchGet('"dlf" action="([^"]+)', data)
143                                                         if not url:
144                                                                 self.downloadError("Failed to get download page url")
145                                                         else:
146                                                                 self.freeDownloadUrl = url
147                                                                 self.freeDownloadFile = file
148                                                                 self.freeDownloadTimer.start((int(seconds) + 2) * 1000, 1)
149                                 else:\r
150                                         downloadPage(downloading_file, file).addCallback(self.downloadCallback).addErrback(self.downloadError)\r
151                 else:\r
152                         self.downloading_file = ""\r
153                         self.downloading = False\r
154                         writeLog("Empty list... everything done?")\r
155                         \r
156                         self.reloadTimer.start(5 * 60 * 1000, 1)
157
158         def freeDownload(self):
159                 downloadPage(self.freeDownloadUrl, self.freeDownloadFile).addCallback(self.downloadCallback).addErrback(self.downloadError)\r
160 \r
161         def downloadCallback(self, callback = None):\r
162                 writeLog("File %s downloaded." % self.downloading_file)\r
163                 \r
164                 self.cleanLists(self.downloading_file)\r
165                 \r
166                 self.downloaded_files.append(self.downloading_file)\r
167                 self.downloadTimer.start(1, 1)\r
168 \r
169         def downloadError(self, error = None):\r
170                 if error is not None:\r
171                         writeLog("Error while downloading: " + str(error))\r
172                 \r
173                 self.failed_files.append(self.downloading_file)\r
174                 self.downloadTimer.start(1, 1)\r
175 \r
176         def cleanLists(self, file):\r
177                 writeLog("Cleaning lists...")\r
178                 \r
179                 path = config.plugins.RSDownloader.lists_directory.value\r
180                 if not path.endswith("/"):\r
181                         path = path + "/"\r
182                 \r
183                 try:\r
184                         file_list = listdir(path)\r
185                 except:\r
186                         file_list = []\r
187                 \r
188                 for x in file_list:\r
189                         list = path + x\r
190                         try:\r
191                                 f = open(list, "r")\r
192                                 content = f.read()\r
193                                 f.close()\r
194                                 \r
195                                 if content.__contains__(file):\r
196                                         content = content.replace(file, "")\r
197                                         content = content.replace("\n\n", "\n").replace("\r\r", "\r")\r
198                                         \r
199                                         f = open(list, "w")\r
200                                         f.write(content)\r
201                                         f.close()\r
202                         except:\r
203                                 writeLog("Error while cleaning list %s!" % list)\r
204 \r
205         def mayDownload(self):
206                 if config.plugins.RSDownloader.onoff.value == False:
207                         writeLog("Background downloader is turned off...")
208                         return False
209                 
210                 if config.plugins.RSDownloader.ignore_time.value:
211                         return True
212                 \r
213                 start = config.plugins.RSDownloader.start_time.value\r
214                 end = config.plugins.RSDownloader.end_time.value\r
215                 t = localtime()\r
216                 \r
217                 #print "====>Start:", str(start)\r
218                 #print "====>End:", str(end)\r
219                 #print "====>Now:", str(t)\r
220                 
221                 weekday = t[6]
222                 if weekday == 0 and config.plugins.RSDownloader.download_monday.value == False:
223                         return False
224                 elif weekday == 1 and config.plugins.RSDownloader.download_tuesday.value == False:
225                         return False
226                 elif weekday == 2 and config.plugins.RSDownloader.download_wednesday.value == False:
227                         return False
228                 elif weekday == 3 and config.plugins.RSDownloader.download_thursday.value == False:
229                         return False
230                 elif weekday == 4 and config.plugins.RSDownloader.download_friday.value == False:
231                         return False
232                 elif weekday == 5 and config.plugins.RSDownloader.download_saturday.value == False:
233                         return False
234                 elif weekday == 6 and config.plugins.RSDownloader.download_sunday.value == False:
235                         return False
236                 \r
237                 hour_now = t[3]\r
238                 minute_now = t[4]\r
239                 hour_start = start[0]\r
240                 minute_start = start[1]\r
241                 hour_end = end[0]\r
242                 minute_end = end[1]\r
243                 \r
244                 if start == end: # Same start and end-time\r
245                         return True\r
246                 elif hour_end < hour_start: # Different days!!!\r
247                         if hour_now > hour_start or hour_now < hour_end:\r
248                                 return True\r
249                         elif hour_now == hour_start and minute_now > minute_start:\r
250                                 return True\r
251                         elif hour_now == hour_end and minute_now < minute_end:\r
252                                 return True\r
253                 elif hour_now > hour_start and hour_now < hour_end: # Same day...\r
254                         return True\r
255                 elif hour_now == hour_start and minute_now > minute_start: # Same day, same start-hour...\r
256                         return True\r
257                 elif hour_now == hour_end and minute_now < minute_end: # Same day, same end-hour...\r
258                         return True\r
259                 \r
260                 return False\r
261 \r
262 rapidshare = RS()\r