summaryrefslogtreecommitdiffstats
path: root/pyload/plugins/addon/MergeFiles.py
diff options
context:
space:
mode:
authorGravatar Walter Purcaro <vuolter@gmail.com> 2014-10-03 19:58:50 +0200
committerGravatar Walter Purcaro <vuolter@gmail.com> 2014-10-03 19:58:50 +0200
commit56f75b634356602c5f7a93f65a1260fe2c0d66d8 (patch)
tree0a4cea353074894d031286d09ccde72c2bc53f25 /pyload/plugins/addon/MergeFiles.py
parentRename accounts directory to account (diff)
downloadpyload-56f75b634356602c5f7a93f65a1260fe2c0d66d8.tar.xz
Rename hooks directory to addon
Diffstat (limited to 'pyload/plugins/addon/MergeFiles.py')
-rw-r--r--pyload/plugins/addon/MergeFiles.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/pyload/plugins/addon/MergeFiles.py b/pyload/plugins/addon/MergeFiles.py
new file mode 100644
index 000000000..5a39cef65
--- /dev/null
+++ b/pyload/plugins/addon/MergeFiles.py
@@ -0,0 +1,76 @@
+# -*- coding: utf-8 -*-
+
+import os
+import re
+import traceback
+
+from pyload.plugins.base.Hook import Hook, threaded
+from pyload.utils import safe_join, fs_encode
+
+
+class MergeFiles(Hook):
+ __name__ = "MergeFiles"
+ __type__ = "hook"
+ __version__ = "0.12"
+
+ __config__ = [("activated", "bool", "Activated", False)]
+
+ __description__ = """Merges parts splitted with hjsplit"""
+ __author_name__ = "and9000"
+ __author_mail__ = "me@has-no-mail.com"
+
+ BUFFER_SIZE = 4096
+
+
+ def setup(self):
+ # nothing to do
+ pass
+
+ @threaded
+ def packageFinished(self, pack):
+ files = {}
+ fid_dict = {}
+ for fid, data in pack.getChildren().iteritems():
+ if re.search("\.[0-9]{3}$", data['name']):
+ if data['name'][:-4] not in files:
+ files[data['name'][:-4]] = []
+ files[data['name'][:-4]].append(data['name'])
+ files[data['name'][:-4]].sort()
+ fid_dict[data['name']] = fid
+
+ download_folder = self.config['general']['download_folder']
+
+ if self.config['general']['folder_per_package']:
+ download_folder = safe_join(download_folder, pack.folder)
+
+ for name, file_list in files.iteritems():
+ self.logInfo(_("Starting merging of"), name)
+ final_file = open(safe_join(download_folder, name), "wb")
+
+ for splitted_file in file_list:
+ self.logDebug("Merging part", splitted_file)
+ pyfile = self.core.files.getFile(fid_dict[splitted_file])
+ pyfile.setStatus("processing")
+ try:
+ s_file = open(os.path.join(download_folder, splitted_file), "rb")
+ size_written = 0
+ s_file_size = int(os.path.getsize(os.path.join(download_folder, splitted_file)))
+ while True:
+ f_buffer = s_file.read(self.BUFFER_SIZE)
+ if f_buffer:
+ final_file.write(f_buffer)
+ size_written += self.BUFFER_SIZE
+ pyfile.setProgress((size_written * 100) / s_file_size)
+ else:
+ break
+ s_file.close()
+ self.logDebug("Finished merging part", splitted_file)
+ except Exception, e:
+ print traceback.print_exc()
+ finally:
+ pyfile.setProgress(100)
+ pyfile.setStatus("finished")
+ pyfile.release()
+
+ final_file.close()
+ self.logInfo(_("Finished merging of"), name)