New config (Ignore download times: 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):\r
49                 writeLog("Reading all lists...")\r
50                 path = config.plugins.RSDownloader.lists_directory.value\r
51                 if not path.endswith("/"):\r
52                         path = path + "/"\r
53                 \r
54                 writeLog("Directory: " + path)\r
55                 \r
56                 try:\r
57                         file_list = listdir(path)\r
58                         writeLog("Count of lists: " + str(len(file_list)))\r
59                 except:\r
60                         file_list = []\r
61                         writeLog("Could not find any list!")\r
62                 \r
63                 for x in file_list:\r
64                         list = path + x\r
65                         try:\r
66                                 writeLog("Reading list %s..." % list)\r
67                                 \r
68                                 f = open(list, "r")\r
69                                 for l in f:\r
70                                         if l.startswith("http://"):\r
71                                                 self.addFile(l.replace("\n", "").replace("\r", ""))\r
72                                 f.close()\r
73                         except:\r
74                                 writeLog("Error while reading list %s!" % list)\r
75 \r
76         def startDownloading(self):\r
77                 self.reloadTimer.stop()\r
78                 \r
79                 mayDownload = self.mayDownload()\r
80                 if mayDownload:\r
81                         self.downloading = True\r
82                         writeLog("Starting downloads...")\r
83                         self.readLists()\r
84                         self.downloadTimer.start(1, 1)\r
85                         return True\r
86                 else:\r
87                         self.reloadTimer.start(5 * 60 * 1000, 1)\r
88                         return False\r
89 \r
90         def download(self):\r
91                 if len(self.files) > 0:\r
92                         writeLog("Getting next file...")\r
93                         self.downloading_file = self.files[0]\r
94                         del self.files[0]\r
95                         writeLog("Downloading file %s..." % self.downloading_file)\r
96                         \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
102                         else:\r
103                                 downloading_file = self.downloading_file
104                                 if downloading_file.startswith("http://rapidshare.com"):
105                                         freeRsDownload = True\r
106                         \r
107                         path = config.plugins.RSDownloader.downloads_directory.value\r
108                         if not path.endswith("/"):\r
109                                 path = path + "/"\r
110                         \r
111                         try:\r
112                                 list = downloading_file.split("/")\r
113                                 file = path + list[(len(list)-1)]\r
114                                 file = file.replace(".html", "")\r
115                         except:\r
116                                 file = downloading_file.replace(".html", "").replace("http://", "")\r
117                         writeLog("Local file: " + file)\r
118                         \r
119                         if fileExists(file):\r
120                                 writeLog("File %s already exists! Downloading next one...")\r
121                                 self.downloadTimer.start(1, 1)\r
122                         else:
123                                 if freeRsDownload:
124                                         if config.plugins.RSDownloader.reconnect_fritz.value == True:
125                                                 reconnect()
126                                                 sleep(3)
127                                         data = GET(downloading_file)
128                                         url = matchGet('<form[^>]+action="([^"]+)', data)
129                                         if not url:
130                                                 self.downloadError("Failed to get download page url")
131                                         else:
132                                                 data = POST(url, "dl.start=Free")
133                                                 seconds = matchGet('var c=([0-9]+)', data)
134                                                 if not seconds:
135                                                         self.downloadError("Failed to get download page url")
136                                                 else:
137                                                         writeLog("Free RS-download... must wait %s seconds!" % seconds)
138                                                         url = matchGet('"dlf" action="([^"]+)', data)
139                                                         if not url:
140                                                                 self.downloadError("Failed to get download page url")
141                                                         else:
142                                                                 self.freeDownloadUrl = url
143                                                                 self.freeDownloadFile = file
144                                                                 self.freeDownloadTimer.start((int(seconds) + 2) * 1000, 1)
145                                 else:\r
146                                         downloadPage(downloading_file, file).addCallback(self.downloadCallback).addErrback(self.downloadError)\r
147                 else:\r
148                         self.downloading_file = ""\r
149                         self.downloading = False\r
150                         writeLog("Empty list... everything done?")\r
151                         \r
152                         self.reloadTimer.start(5 * 60 * 1000, 1)
153
154         def freeDownload(self):
155                 downloadPage(self.freeDownloadUrl, self.freeDownloadFile).addCallback(self.downloadCallback).addErrback(self.downloadError)\r
156 \r
157         def downloadCallback(self, callback = None):\r
158                 writeLog("File %s downloaded." % self.downloading_file)\r
159                 \r
160                 self.cleanLists(self.downloading_file)\r
161                 \r
162                 self.downloaded_files.append(self.downloading_file)\r
163                 self.downloadTimer.start(1, 1)\r
164 \r
165         def downloadError(self, error = None):\r
166                 if error is not None:\r
167                         writeLog("Error while downloading: " + str(error))\r
168                 \r
169                 self.failed_files.append(self.downloading_file)\r
170                 self.downloadTimer.start(1, 1)\r
171 \r
172         def cleanLists(self, file):\r
173                 writeLog("Cleaning lists...")\r
174                 \r
175                 path = config.plugins.RSDownloader.lists_directory.value\r
176                 if not path.endswith("/"):\r
177                         path = path + "/"\r
178                 \r
179                 try:\r
180                         file_list = listdir(path)\r
181                 except:\r
182                         file_list = []\r
183                 \r
184                 for x in file_list:\r
185                         list = path + x\r
186                         try:\r
187                                 f = open(list, "r")\r
188                                 content = f.read()\r
189                                 f.close()\r
190                                 \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
194                                         \r
195                                         f = open(list, "w")\r
196                                         f.write(content)\r
197                                         f.close()\r
198                         except:\r
199                                 writeLog("Error while cleaning list %s!" % list)\r
200 \r
201         def mayDownload(self):
202                 if config.plugins.RSDownloader.ignore_time.value:
203                         return True
204                 \r
205                 start = config.plugins.RSDownloader.start_time.value\r
206                 end = config.plugins.RSDownloader.end_time.value\r
207                 t = localtime()\r
208                 \r
209                 #print "====>Start:", str(start)\r
210                 #print "====>End:", str(end)\r
211                 #print "====>Now:", str(t)\r
212                 \r
213                 hour_now = t[3]\r
214                 minute_now = t[4]\r
215                 hour_start = start[0]\r
216                 minute_start = start[1]\r
217                 hour_end = end[0]\r
218                 minute_end = end[1]\r
219                 \r
220                 if start == end: # Same start and end-time\r
221                         return True\r
222                 elif hour_end < hour_start: # Different days!!!\r
223                         if hour_now > hour_start or hour_now < hour_end:\r
224                                 return True\r
225                         elif hour_now == hour_start and minute_now > minute_start:\r
226                                 return True\r
227                         elif hour_now == hour_end and minute_now < minute_end:\r
228                                 return True\r
229                 elif hour_now > hour_start and hour_now < hour_end: # Same day...\r
230                         return True\r
231                 elif hour_now == hour_start and minute_now > minute_start: # Same day, same start-hour...\r
232                         return True\r
233                 elif hour_now == hour_end and minute_now < minute_end: # Same day, same end-hour...\r
234                         return True\r
235                 \r
236                 return False\r
237 \r
238 rapidshare = RS()\r