kuuko pushed a commit to branch feature/create-torrent.

http://git.enlightenment.org/apps/epour.git/commit/?id=5eb19682ff67eaf85bf7fd5c0a455c9d531a76f4

commit 5eb19682ff67eaf85bf7fd5c0a455c9d531a76f4
Author: Kai Huuhko <kai.huu...@gmail.com>
Date:   Sat May 23 16:42:02 2020 +0300

    Add bin/epour-create-torrent
---
 bin/epour-create-torrent | 249 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 249 insertions(+)

diff --git a/bin/epour-create-torrent b/bin/epour-create-torrent
new file mode 100755
index 0000000..6f095b8
--- /dev/null
+++ b/bin/epour-create-torrent
@@ -0,0 +1,249 @@
+#!/usr/bin/env python3
+#
+#  Epour - A bittorrent client using EFL and libtorrent
+#
+#  Copyright 2012-2017 Kai Huuhko <kai.huu...@gmail.com>
+#
+#  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, write to the Free Software
+#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+#  MA 02110-1301, USA.
+#
+
+import os
+
+import libtorrent as lt
+
+from efl import elementary as elm
+from efl.evas import EXPAND_BOTH, FILL_BOTH, EXPAND_HORIZ, FILL_HORIZ
+
+
+data = {"trackers": list()}
+
+class CreateTorrentGui(elm.StandardWindow):
+    def __init__(self):
+        super().__init__("testi", "testi", autodel=True)
+
+        data["storage"] = lt.file_storage()
+
+        nf = elm.Naviframe(self, size_hint_weight=EXPAND_BOTH)
+        self.resize_object_add(nf)
+        nf.show()
+
+        first_next = elm.Button(self, text="Files/Directories")
+        second_prev = elm.Button(self, text="Base Directory")
+        second_next = elm.Button(self, text="Tracker URLs")
+        third_prev = elm.Button(self, text="Files/Directories")
+        third_next = elm.Button(self, text="Generate")
+        fourth_prev = elm.Button(self, text="Tracker URLs")
+
+        first_content = BaseDirectorySelection(self)
+        second_content = FileSelection(self)
+        third_content = TrackerAddition(self)
+        fourth_content = TorrentGenerate(self)
+
+        first = elm.NaviframeItem("Select Base Directory", 
next_btn=first_next, content=first_content)
+        second = elm.NaviframeItem("Select Files/Directories", 
prev_btn=second_prev, next_btn=second_next, content=second_content)
+        third = elm.NaviframeItem("Add Tracker URLs", prev_btn=third_prev, 
next_btn=third_next, content=third_content)
+        fourth = elm.NaviframeItem("Generate Torrent", prev_btn=fourth_prev, 
content=fourth_content)
+
+        def first_next_cb(obj):
+            second.promote()
+
+        def second_prev_cb(obj):
+            first.promote()
+
+        def second_next_cb(obj):
+            third.promote()
+
+        def third_prev_cb(obj):
+            second.promote()
+
+        def third_next_cb(obj):
+            fourth.promote()
+
+        def fourth_prev_cb(obj):
+            third.promote()
+
+        
first_content.selector.callback_directory_open_add(second_content.basedir_changed_cb)
+
+
+        first_next.callback_clicked_add(first_next_cb)
+        second_prev.callback_clicked_add(second_prev_cb)
+        second_next.callback_clicked_add(second_next_cb)
+        third_prev.callback_clicked_add(third_prev_cb)
+        third_next.callback_clicked_add(third_next_cb)
+        fourth_prev.callback_clicked_add(fourth_prev_cb)
+
+        for page in first, second, third, fourth:
+            page.push_to(nf)
+
+        first.promote()
+
+        self.size = 400, 400
+        self.show()
+
+
+class BaseDirectorySelection(elm.Box):
+    def __init__(self, parent, *args, **kwargs):
+        super().__init__(parent, *args, **kwargs)
+
+        def bdir_cb(obj, path):
+            if not path:
+                return
+            data["base_dir"] = path
+
+        selector = self.selector = elm.Fileselector(
+            self,
+            size_hint_weight=EXPAND_BOTH,
+            size_hint_align=FILL_BOTH,
+            buttons_ok_cancel=False,
+            folder_only=True,
+            expandable=False
+        )
+        selector.callback_directory_open_add(bdir_cb)
+        selector.show()
+        self.pack_end(selector)
+
+class FileSelection(elm.Box):
+    def __init__(self, parent, *args, **kwargs):
+        super().__init__(parent, *args, **kwargs)
+
+        self.size_hint_weight=EXPAND_BOTH
+        self.size_hint_align=FILL_BOTH
+
+        def add_cb(obj, path):
+            if not path:
+                return
+            
+            if not path.startswith(data["base_dir"]):
+                print("Error! Target must be inside base directory!")
+                return
+            size = os.stat(path).st_size
+            lt.add_files(data["storage"], path)
+            flist.item_append(path)
+            flist.go()
+
+        def del_db(obj):
+            if not flist.selected_item:
+                return
+            lt.del_files(data["storage"], flist.selected_item.text)
+
+        flist = elm.List(self, size_hint_weight=EXPAND_BOTH, 
size_hint_align=FILL_BOTH)
+        flist.show()
+        flist.go()
+        self.pack_end(flist)
+
+        add_btn = self.add_btn = elm.FileselectorButton(
+            self,
+            size_hint_weight=EXPAND_BOTH,
+            size_hint_align=FILL_BOTH,
+            text="Add file"
+        )
+        add_btn.callback_file_chosen_add(add_cb)
+        add_btn.show()
+        self.pack_end(add_btn)
+
+        # del_btn = self.del_btn = elm.Button(
+        #     self,
+        #     text="Delete file"
+        # )
+        # del_btn.callback_clicked_add(del_db)
+        # del_btn.show()
+        # self.pack_end(del_btn)
+
+    def basedir_changed_cb(self, obj, path):
+        print("basedir")
+        self.add_btn.path = path
+
+
+class TrackerAddition(elm.Box):
+    def __init__(self, parent, *args, **kwargs):
+        super().__init__(parent, *args, **kwargs)
+
+        tlist = elm.List(self, size_hint_weight=EXPAND_BOTH, 
size_hint_align=FILL_BOTH)
+        tlist.show()
+        tlist.go()
+        self.pack_end(tlist)
+
+        add_box = elm.Box(
+            self,
+            size_hint_weight=EXPAND_BOTH,
+            size_hint_align=FILL_BOTH
+        )
+
+        def add_cb(obj):
+            url = add_entry.entry
+            if not url:
+                return
+            data["trackers"].append(url)
+            tlist.item_append(url)
+            tlist.go()
+
+        add_entry = elm.Entry(
+            self,
+            size_hint_weight=EXPAND_HORIZ,
+            size_hint_align=FILL_HORIZ
+        )
+        add_entry.show()
+        add_box.pack_end(add_entry)
+
+        add_btn = elm.Button(self, text="Add")
+        add_btn.callback_clicked_add(add_cb)
+        add_btn.show()
+        add_box.pack_end(add_btn)
+
+        add_box.show()
+        self.pack_end(add_box)
+
+        self.show()
+
+
+class TorrentGenerate(elm.Box):
+    def __init__(self, parent, *args, **kwargs):
+        super().__init__(parent, *args, **kwargs)
+
+        def gen_cb(obj, path):
+            if not path:
+                return
+            t = lt.create_torrent(data["storage"])
+            lt.set_piece_hashes(t, data["base_dir"])
+            for tracker in data["trackers"]:
+                t.add_tracker(tracker)
+            entry = t.generate()
+            print(entry)
+            bentry = lt.bencode(entry)
+            with open(path, "wb") as fp:
+                fp.write(bentry)
+
+            print("Done!")
+
+        gen_btn = FSButton(self, text="Generate Torrent", is_save=True, 
window_title="Name and path for new torrent")
+        gen_btn.callback_file_chosen_add(gen_cb)
+        gen_btn.show()
+        self.pack_end(gen_btn)
+
+
+class FSButton(elm.Fileselector, elm.FileselectorButton):
+    def __init__(self, parent, *args, **kwargs):
+        elm.FileselectorButton.__init__(self, parent, *args, **kwargs)
+
+
+class FSEntry(elm.Fileselector, elm.FileselectorEntry):
+    def __init__(self, parent, *args, **kwargs):
+        elm.FileselectorEntry.__init__(self, parent, *args, **kwargs)
+
+
+elm.policy_set(elm.ELM_POLICY_QUIT, elm.ELM_POLICY_QUIT_LAST_WINDOW_CLOSED)
+CreateTorrentGui()
+elm.run()

-- 


Reply via email to