# -*- coding: utf-8 -*- """ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . @author: zoidberg """ from urlparse import urlparse import re from time import time from module.plugins.Hoster import Hoster from module.utils import html_unescape, fixup, parseFileSize from module.network.RequestFactory import getURL from module.network.CookieJar import CookieJar def replace_patterns(string, ruleslist): for r in ruleslist: rf, rt = r string = re.sub(rf, rt, string) #self.logDebug(rf, rt, string) return string def set_cookies(cj, cookies): for cookie in cookies: if isinstance(cookie, tuple) and len(cookie) == 3: domain, name, value = cookie cj.setCookie(domain, name, value) def parseHtmlTagAttrValue(attr_name, tag): m = re.search(r"%s\s*=\s*([\"']?)((?<=\")[^\"]+|(?<=')[^']+|[^>\s\"'][^>\s]*)\1" % attr_name, tag, re.I) return m.group(2) if m else None def parseHtmlForm(attr_str, html, input_names=None): for form in re.finditer(r"(?P]*%s[^>]*>)(?P.*?)]*>" % attr_str, html, re.S | re.I): inputs = {} action = parseHtmlTagAttrValue("action", form.group('tag')) for inputtag in re.finditer(r'(<(input|textarea)[^>]*>)([^<]*(?=file_name) (?Pfile_size) (?Punits)' or FILE_NAME_PATTERN = r'(?Pfile_name)' and FILE_SIZE_PATTERN = r'(?Pfile_size) (?Punits)' FILE_OFFLINE_PATTERN = r'File (deleted|not found)' TEMP_OFFLINE_PATTERN = r'Server maintainance' You can also define a PREMIUM_ONLY_PATTERN to detect links that can be downloaded only with a premium account. """ FILE_SIZE_REPLACEMENTS = [] FILE_NAME_REPLACEMENTS = [("&#?\w+;", fixup)] FILE_URL_REPLACEMENTS = [] SH_BROKEN_ENCODING = False # Set to True or encoding name if encoding in http header is not correct SH_COOKIES = True # or False or list of tuples [(domain, name, value)] SH_CHECK_TRAFFIC = False # True = force check traffic left for a premium account def init(self): self.file_info = {} def setup(self): self.resumeDownload = self.multiDL = self.premium if isinstance(self.SH_COOKIES, list): set_cookies(self.req.cj, self.SH_COOKIES) def process(self, pyfile): pyfile.url = replace_patterns(pyfile.url, self.FILE_URL_REPLACEMENTS) self.req.setOption("timeout", 120) # Due to a 0.4.9 core bug self.load would keep previous cookies even if overridden by cookies parameter. # Workaround using getURL. Can be reverted in 0.5 as the cookies bug has been fixed. self.html = getURL(pyfile.url, decode=not self.SH_BROKEN_ENCODING, cookies=self.SH_COOKIES) self.getFileInfo() if self.premium and (not self.SH_CHECK_TRAFFIC or self.checkTrafficLeft()): self.handlePremium() else: # This line is required due to the getURL workaround. Can be removed in 0.5 self.html = self.load(pyfile.url, decode=not self.SH_BROKEN_ENCODING, cookies=self.SH_COOKIES) if hasattr(self, 'PREMIUM_ONLY_PATTERN') and re.search(self.PREMIUM_ONLY_PATTERN, self.html): self.fail("This link require a premium account") self.handleFree() def load(self, url, get={}, post={}, ref=True, cookies=True, just_header=False, decode=False): if type(url) == unicode: url = url.encode('utf8') return Hoster.load(self, url=url, get=get, post=post, ref=ref, cookies=cookies, just_header=just_header, decode=decode) def getFileInfo(self): self.logDebug("URL: %s" % self.pyfile.url) if hasattr(self, "TEMP_OFFLINE_PATTERN") and re.search(self.TEMP_OFFLINE_PATTERN, self.html): self.tempOffline() name, size, status = parseFileInfo(self)[:3] if status == 1: self.offline() elif status != 2: self.logDebug(self.file_info) self.parseError('File info') if name: self.pyfile.name = name else: self.pyfile.name = html_unescape(urlparse(self.pyfile.url).path.split("/")[-1]) if size: self.pyfile.size = size else: self.logError("File size not parsed") self.logDebug("FILE NAME: %s FILE SIZE: %s" % (self.pyfile.name, self.pyfile.size)) return self.file_info def handleFree(self): self.fail("Free download not implemented") def handlePremium(self): self.fail("Premium download not implemented") def parseError(self, msg): raise PluginParseError(msg) def longWait(self, wait_time=None, max_tries=3): if wait_time and isinstance(wait_time, (int, long, float)): time_str = "%dh %dm" % divmod(wait_time / 60, 60) else: wait_time = 900 time_str = "(unknown time)" max_tries = 100 self.logInfo("Download limit reached, reconnect or wait %s" % time_str) self.setWait(wait_time, True) self.wait() self.retry(max_tries=max_tries, reason="Download limit reached") def parseHtmlForm(self, attr_str='', input_names=None): return parseHtmlForm(attr_str, self.html, input_names) def checkTrafficLeft(self): traffic = self.account.getAccountInfo(self.user, True)["trafficleft"] if traffic == -1: return True size = self.pyfile.size / 1024 self.logInfo("Filesize: %i KiB, Traffic left for user %s: %i KiB" % (size, self.user, traffic)) return size <= traffic # TODO: Remove in 0.5 def wait(self, seconds=False, reconnect=False): if seconds: self.setWait(seconds, reconnect) super(SimpleHoster, self).wait()