diff options
author | Walter Purcaro <vuolter@gmail.com> | 2014-11-09 03:08:19 +0100 |
---|---|---|
committer | Walter Purcaro <vuolter@gmail.com> | 2014-11-09 03:08:19 +0100 |
commit | bd8259220ab4d56ab419b7b32045b08cc9b0a7c8 (patch) | |
tree | 92f1c69d4280f8f57021083dbf878e62033eb502 /module | |
parent | Cookie support for getURL method (diff) | |
download | pyload-bd8259220ab4d56ab419b7b32045b08cc9b0a7c8.tar.xz |
Use with statement instead open method when accessing fod + handle i/o error
Diffstat (limited to 'module')
-rw-r--r-- | module/plugins/Container.py | 8 | ||||
-rw-r--r-- | module/plugins/captcha/captcha.py | 17 | ||||
-rw-r--r-- | module/plugins/container/RSDF.py | 12 | ||||
-rw-r--r-- | module/plugins/crypter/RelinkUs.py | 7 | ||||
-rw-r--r-- | module/plugins/hooks/ExtractArchive.py | 33 | ||||
-rw-r--r-- | module/plugins/hooks/HotFolder.py | 67 | ||||
-rw-r--r-- | module/plugins/hoster/MegaCoNz.py | 14 | ||||
-rw-r--r-- | module/plugins/hoster/PremiumTo.py | 5 |
8 files changed, 89 insertions, 74 deletions
diff --git a/module/plugins/Container.py b/module/plugins/Container.py index 16d0045a6..913c80481 100644 --- a/module/plugins/Container.py +++ b/module/plugins/Container.py @@ -44,9 +44,11 @@ class Container(Crypter): self.pyfile.name = re.findall("([^\/=]+)", self.pyfile.url)[-1] content = self.load(self.pyfile.url) self.pyfile.url = save_join(self.config['general']['download_folder'], self.pyfile.name) - f = open(self.pyfile.url, "wb" ) - f.write(content) - f.close() + try: + with open(self.pyfile.url, "wb") as f: + f.write(content) + except IOError, e: + self.fail(str(e)) else: self.pyfile.name = basename(self.pyfile.url) diff --git a/module/plugins/captcha/captcha.py b/module/plugins/captcha/captcha.py index 93c8164c6..e0cd7d31c 100644 --- a/module/plugins/captcha/captcha.py +++ b/module/plugins/captcha/captcha.py @@ -57,12 +57,17 @@ class OCR(object): def run_tesser(self, subset=False, digits=True, lowercase=True, uppercase=True): #tmpTif = tempfile.NamedTemporaryFile(suffix=".tif") - tmpTif = open(join("tmp", "tmpTif_%s.tif" % self.__name__), "wb") - tmpTif.close() - - #tmpTxt = tempfile.NamedTemporaryFile(suffix=".txt") - tmpTxt = open(join("tmp", "tmpTxt_%s.txt" % self.__name__), "wb") - tmpTxt.close() + try: + tmpTif = open(join("tmp", "tmpTif_%s.tif" % self.__name__), "wb") + tmpTif.close() + + #tmpTxt = tempfile.NamedTemporaryFile(suffix=".txt") + tmpTxt = open(join("tmp", "tmpTxt_%s.txt" % self.__name__), "wb") + tmpTxt.close() + + except IOError, e: + self.logError(str(e)) + return self.logger.debug("save tiff") self.image.save(tmpTif.name, 'TIFF') diff --git a/module/plugins/container/RSDF.py b/module/plugins/container/RSDF.py index c3846b9e4..9c57c8ddb 100644 --- a/module/plugins/container/RSDF.py +++ b/module/plugins/container/RSDF.py @@ -5,6 +5,7 @@ import binascii import re from module.plugins.Container import Container +from module.utils import fs_encode class RSDF(Container): @@ -23,7 +24,7 @@ class RSDF(Container): from Crypto.Cipher import AES - infile = pyfile.url.replace("\n", "") + infile = fs_encode(pyfile.url.replace("\n", "")) Key = binascii.unhexlify('8C35192D964DC3182C6F84F3252239EB4A320D2500000000') IV = binascii.unhexlify('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF') @@ -32,10 +33,11 @@ class RSDF(Container): obj = AES.new(Key, AES.MODE_CFB, IV) - rsdf = open(infile, 'r') - - data = rsdf.read() - rsdf.close() + try: + with open(infile, 'r') as rsdf: + data = rsdf.read() + except IOError, e: + self.fail(str(e)) if re.search(r"<title>404 - Not Found</title>", data) is None: data = binascii.unhexlify(''.join(data.split())) diff --git a/module/plugins/crypter/RelinkUs.py b/module/plugins/crypter/RelinkUs.py index cdb699ae0..4c84b62f7 100644 --- a/module/plugins/crypter/RelinkUs.py +++ b/module/plugins/crypter/RelinkUs.py @@ -216,12 +216,11 @@ class RelinkUs(Crypter): dlc = self.load(container_url) dlc_filename = self.fileid + ".dlc" dlc_filepath = os.path.join(self.config['general']['download_folder'], dlc_filename) - f = open(dlc_filepath, "wb") - f.write(dlc) - f.close() + with open(dlc_filepath, "wb") as f: + f.write(dlc) package_links.append(dlc_filepath) except: - self.logDebug("Unable to download DLC container") + self.fail("Unable to download DLC container") return package_links diff --git a/module/plugins/hooks/ExtractArchive.py b/module/plugins/hooks/ExtractArchive.py index 3d6df5b02..39da1ee7b 100644 --- a/module/plugins/hooks/ExtractArchive.py +++ b/module/plugins/hooks/ExtractArchive.py @@ -309,16 +309,18 @@ class ExtractArchive(Hook): def reloadPasswords(self): passwordfile = self.getConfig("passwordfile") - if not exists(passwordfile): - open(passwordfile, "wb").close() - - passwords = [] - f = open(passwordfile, "rb") - for pw in f.read().splitlines(): - passwords.append(pw) - f.close() - - self.passwords = passwords + + try: + passwords = [] + with open(passwordfile, "a+") as f: + for pw in f.read().splitlines(): + passwords.append(pw) + + except IOError, e: + self.logError(str(e)) + + else: + self.passwords = passwords @Expose @@ -328,12 +330,15 @@ class ExtractArchive(Hook): if pw in self.passwords: self.passwords.remove(pw) + self.passwords.insert(0, pw) - f = open(passwordfile, "wb") - for pw in self.passwords: - f.write(pw + "\n") - f.close() + try: + with open(passwordfile, "wb") as f: + for pw in self.passwords: + f.write(pw + "\n") + except IOError, e: + self.logError(str(e)) def setPermissions(self, files): diff --git a/module/plugins/hooks/HotFolder.py b/module/plugins/hooks/HotFolder.py index d7e8093b3..5e9dd9547 100644 --- a/module/plugins/hooks/HotFolder.py +++ b/module/plugins/hooks/HotFolder.py @@ -7,6 +7,7 @@ from os.path import exists, isfile, join from shutil import move from module.plugins.Hook import Hook +from module.utils import fs_encode, save_join class HotFolder(Hook): @@ -29,37 +30,35 @@ class HotFolder(Hook): def periodical(self): - if not exists(join(self.getConfig("folder"), "finished")): - makedirs(join(self.getConfig("folder"), "finished")) - - if self.getConfig("watch_file"): - - if not exists(self.getConfig("file")): - f = open(self.getConfig("file"), "wb") - f.close() - - f = open(self.getConfig("file"), "rb") - content = f.read().strip() - f.close() - f = open(self.getConfig("file"), "wb") - f.close() - if content: - name = "%s_%s.txt" % (self.getConfig("file"), time.strftime("%H-%M-%S_%d%b%Y")) - - f = open(join(self.getConfig("folder"), "finished", name), "wb") - f.write(content) - f.close() - - self.core.api.addPackage(f.name, [f.name], 1) - - for f in listdir(self.getConfig("folder")): - path = join(self.getConfig("folder"), f) - - if not isfile(path) or f.endswith("~") or f.startswith("#") or f.startswith("."): - continue - - newpath = join(self.getConfig("folder"), "finished", f if self.getConfig("keep") else "tmp_" + f) - move(path, newpath) - - self.logInfo(_("Added %s from HotFolder") % f) - self.core.api.addPackage(f, [newpath], 1) + folder = fs_encode(self.getConfig("folder")) + + try: + if not exists(join(folder, "finished")): + makedirs(join(folder, "finished")) + + if self.getConfig("watch_file"): + with open(fs_encode(self.getConfig("file")), "a+") as f: + content = f.read().strip() + + if content: + name = "%s_%s.txt" % (self.getConfig("file"), time.strftime("%H-%M-%S_%d%b%Y")) + + with open(save_join(folder, "finished", name), "wb") as f: + f.write(content) + + self.core.api.addPackage(f.name, [f.name], 1) + + for f in listdir(folder): + path = join(folder, f) + + if not isfile(path) or f.endswith("~") or f.startswith("#") or f.startswith("."): + continue + + newpath = join(folder, "finished", f if self.getConfig("keep") else "tmp_" + f) + move(path, newpath) + + self.logInfo(_("Added %s from HotFolder") % f) + self.core.api.addPackage(f, [newpath], 1) + + except IOError, e: + self.logError(str(e)) diff --git a/module/plugins/hoster/MegaCoNz.py b/module/plugins/hoster/MegaCoNz.py index 70688c26a..9b120827c 100644 --- a/module/plugins/hoster/MegaCoNz.py +++ b/module/plugins/hoster/MegaCoNz.py @@ -48,9 +48,9 @@ class MegaCoNz(Hoster): # generate a session id, no idea where to obtain elsewhere uid = random.randint(10 << 9, 10 ** 10) - resp = self.load(self.API_URL % uid, post=json_dumps([kwargs])) - self.logDebug("Api Response: " + resp) - return json_loads(resp) + res = self.load(self.API_URL % uid, post=json_dumps([kwargs])) + self.logDebug("Api Response: " + res) + return json_loads(res) def decryptAttr(self, data, key): @@ -78,8 +78,12 @@ class MegaCoNz(Hoster): file_crypted = self.lastDownload file_decrypted = file_crypted.rsplit(self.FILE_SUFFIX)[0] - f = open(file_crypted, "rb") - df = open(file_decrypted, "wb") + + try: + f = open(file_crypted, "rb") + df = open(file_decrypted, "wb") + except IOError, e: + self.fail(str(e)) # TODO: calculate CBC-MAC for checksum diff --git a/module/plugins/hoster/PremiumTo.py b/module/plugins/hoster/PremiumTo.py index 183182689..beab4391e 100644 --- a/module/plugins/hoster/PremiumTo.py +++ b/module/plugins/hoster/PremiumTo.py @@ -54,9 +54,8 @@ class PremiumTo(Hoster): lastDownload = fs_encode(self.lastDownload) if exists(lastDownload): - f = open(lastDownload, "rb") - err = f.read(256).strip() - f.close() + with open(lastDownload, "rb") as f: + err = f.read(256).strip() remove(lastDownload) else: err = _('File does not exist') |