summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Huuhko <kai.huuhko@gmail.com>2020-05-23 16:42:02 +0300
committerKai Huuhko <kai.huuhko@gmail.com>2020-05-23 16:42:02 +0300
commit5eb19682ff67eaf85bf7fd5c0a455c9d531a76f4 (patch)
tree09e0dfb3df5967677f175e690fcc632b713800af
parent6b605cea95f361ffee69a3fbe0325c237c285b31 (diff)
Add bin/epour-create-torrentfeature/create-torrent
-rwxr-xr-xbin/epour-create-torrent249
1 files changed, 249 insertions, 0 deletions
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 @@
1#!/usr/bin/env python3
2#
3# Epour - A bittorrent client using EFL and libtorrent
4#
5# Copyright 2012-2017 Kai Huuhko <kai.huuhko@gmail.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20# MA 02110-1301, USA.
21#
22
23import os
24
25import libtorrent as lt
26
27from efl import elementary as elm
28from efl.evas import EXPAND_BOTH, FILL_BOTH, EXPAND_HORIZ, FILL_HORIZ
29
30
31data = {"trackers": list()}
32
33class CreateTorrentGui(elm.StandardWindow):
34 def __init__(self):
35 super().__init__("testi", "testi", autodel=True)
36
37 data["storage"] = lt.file_storage()
38
39 nf = elm.Naviframe(self, size_hint_weight=EXPAND_BOTH)
40 self.resize_object_add(nf)
41 nf.show()
42
43 first_next = elm.Button(self, text="Files/Directories")
44 second_prev = elm.Button(self, text="Base Directory")
45 second_next = elm.Button(self, text="Tracker URLs")
46 third_prev = elm.Button(self, text="Files/Directories")
47 third_next = elm.Button(self, text="Generate")
48 fourth_prev = elm.Button(self, text="Tracker URLs")
49
50 first_content = BaseDirectorySelection(self)
51 second_content = FileSelection(self)
52 third_content = TrackerAddition(self)
53 fourth_content = TorrentGenerate(self)
54
55 first = elm.NaviframeItem("Select Base Directory", next_btn=first_next, content=first_content)
56 second = elm.NaviframeItem("Select Files/Directories", prev_btn=second_prev, next_btn=second_next, content=second_content)
57 third = elm.NaviframeItem("Add Tracker URLs", prev_btn=third_prev, next_btn=third_next, content=third_content)
58 fourth = elm.NaviframeItem("Generate Torrent", prev_btn=fourth_prev, content=fourth_content)
59
60 def first_next_cb(obj):
61 second.promote()
62
63 def second_prev_cb(obj):
64 first.promote()
65
66 def second_next_cb(obj):
67 third.promote()
68
69 def third_prev_cb(obj):
70 second.promote()
71
72 def third_next_cb(obj):
73 fourth.promote()
74
75 def fourth_prev_cb(obj):
76 third.promote()
77
78 first_content.selector.callback_directory_open_add(second_content.basedir_changed_cb)
79
80
81 first_next.callback_clicked_add(first_next_cb)
82 second_prev.callback_clicked_add(second_prev_cb)
83 second_next.callback_clicked_add(second_next_cb)
84 third_prev.callback_clicked_add(third_prev_cb)
85 third_next.callback_clicked_add(third_next_cb)
86 fourth_prev.callback_clicked_add(fourth_prev_cb)
87
88 for page in first, second, third, fourth:
89 page.push_to(nf)
90
91 first.promote()
92
93 self.size = 400, 400
94 self.show()
95
96
97class BaseDirectorySelection(elm.Box):
98 def __init__(self, parent, *args, **kwargs):
99 super().__init__(parent, *args, **kwargs)
100
101 def bdir_cb(obj, path):
102 if not path:
103 return
104 data["base_dir"] = path
105
106 selector = self.selector = elm.Fileselector(
107 self,
108 size_hint_weight=EXPAND_BOTH,
109 size_hint_align=FILL_BOTH,
110 buttons_ok_cancel=False,
111 folder_only=True,
112 expandable=False
113 )
114 selector.callback_directory_open_add(bdir_cb)
115 selector.show()
116 self.pack_end(selector)
117
118class FileSelection(elm.Box):
119 def __init__(self, parent, *args, **kwargs):
120 super().__init__(parent, *args, **kwargs)
121
122 self.size_hint_weight=EXPAND_BOTH
123 self.size_hint_align=FILL_BOTH
124
125 def add_cb(obj, path):
126 if not path:
127 return
128
129 if not path.startswith(data["base_dir"]):
130 print("Error! Target must be inside base directory!")
131 return
132 size = os.stat(path).st_size
133 lt.add_files(data["storage"], path)
134 flist.item_append(path)
135 flist.go()
136
137 def del_db(obj):
138 if not flist.selected_item:
139 return
140 lt.del_files(data["storage"], flist.selected_item.text)
141
142 flist = elm.List(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH)
143 flist.show()
144 flist.go()
145 self.pack_end(flist)
146
147 add_btn = self.add_btn = elm.FileselectorButton(
148 self,
149 size_hint_weight=EXPAND_BOTH,
150 size_hint_align=FILL_BOTH,
151 text="Add file"
152 )
153 add_btn.callback_file_chosen_add(add_cb)
154 add_btn.show()
155 self.pack_end(add_btn)
156
157 # del_btn = self.del_btn = elm.Button(
158 # self,
159 # text="Delete file"
160 # )
161 # del_btn.callback_clicked_add(del_db)
162 # del_btn.show()
163 # self.pack_end(del_btn)
164
165 def basedir_changed_cb(self, obj, path):
166 print("basedir")
167 self.add_btn.path = path
168
169
170class TrackerAddition(elm.Box):
171 def __init__(self, parent, *args, **kwargs):
172 super().__init__(parent, *args, **kwargs)
173
174 tlist = elm.List(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH)
175 tlist.show()
176 tlist.go()
177 self.pack_end(tlist)
178
179 add_box = elm.Box(
180 self,
181 size_hint_weight=EXPAND_BOTH,
182 size_hint_align=FILL_BOTH
183 )
184
185 def add_cb(obj):
186 url = add_entry.entry
187 if not url:
188 return
189 data["trackers"].append(url)
190 tlist.item_append(url)
191 tlist.go()
192
193 add_entry = elm.Entry(
194 self,
195 size_hint_weight=EXPAND_HORIZ,
196 size_hint_align=FILL_HORIZ
197 )
198 add_entry.show()
199 add_box.pack_end(add_entry)
200
201 add_btn = elm.Button(self, text="Add")
202 add_btn.callback_clicked_add(add_cb)
203 add_btn.show()
204 add_box.pack_end(add_btn)
205
206 add_box.show()
207 self.pack_end(add_box)
208
209 self.show()
210
211
212class TorrentGenerate(elm.Box):
213 def __init__(self, parent, *args, **kwargs):
214 super().__init__(parent, *args, **kwargs)
215
216 def gen_cb(obj, path):
217 if not path:
218 return
219 t = lt.create_torrent(data["storage"])
220 lt.set_piece_hashes(t, data["base_dir"])
221 for tracker in data["trackers"]:
222 t.add_tracker(tracker)
223 entry = t.generate()
224 print(entry)
225 bentry = lt.bencode(entry)
226 with open(path, "wb") as fp:
227 fp.write(bentry)
228
229 print("Done!")
230
231 gen_btn = FSButton(self, text="Generate Torrent", is_save=True, window_title="Name and path for new torrent")
232 gen_btn.callback_file_chosen_add(gen_cb)
233 gen_btn.show()
234 self.pack_end(gen_btn)
235
236
237class FSButton(elm.Fileselector, elm.FileselectorButton):
238 def __init__(self, parent, *args, **kwargs):
239 elm.FileselectorButton.__init__(self, parent, *args, **kwargs)
240
241
242class FSEntry(elm.Fileselector, elm.FileselectorEntry):
243 def __init__(self, parent, *args, **kwargs):
244 elm.FileselectorEntry.__init__(self, parent, *args, **kwargs)
245
246
247elm.policy_set(elm.ELM_POLICY_QUIT, elm.ELM_POLICY_QUIT_LAST_WINDOW_CLOSED)
248CreateTorrentGui()
249elm.run()