-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui-torrent-fetcher.py
112 lines (104 loc) · 4.53 KB
/
gui-torrent-fetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python
import gi,json,os
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk,Pango
from urllib.request import urlopen
columns = ["Torrent Name", "Seeders", "Leechers", "Size","hash"]
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Python torrent fetcher")
grid = Gtk.Grid()
grid.set_row_spacing(5)
grid.set_column_spacing(5)
grid.set_margin_left(5)
grid.set_margin_top(5)
grid.set_margin_right(5)
grid.set_margin_bottom(5)
self.add(grid)
self.btnSearch = Gtk.Button(label="Search")
self.btnSearch.connect("clicked", self.on_btnSearch_clicked)
self.btnQuit = Gtk.Button(label="Quit")
self.btnQuit.connect("clicked", self.on_btnQuit_clicked)
self.btnDownload = Gtk.Button(label="Download")
self.btnDownload.connect("clicked", self.on_btnDownload_clicked)
self.txtSearch = Gtk.Entry()
self.txtSearch.set_placeholder_text("keyword(s)")
self.lblTotal = Gtk.Label("")
self.lsTorrent = Gtk.ListStore(str, str, str, str, str)
self.tvTorrent = Gtk.TreeView.new_with_model(model=self.lsTorrent)
self.scrList = Gtk.ScrolledWindow()
self.scrList.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.scrList.set_min_content_height(200)
self.scrList.set_min_content_width(800)
self.scrList.add(self.tvTorrent)
for i in range(len(columns)):
cell = Gtk.CellRendererText()
col = Gtk.TreeViewColumn(columns[i], cell, text=i)
col.set_resizable(True)
if i == 0:
col.set_fixed_width(500)
self.tvTorrent.append_column(col)
grid.add(self.txtSearch)
grid.attach(self.btnSearch,1,0,1,1)
grid.attach(self.btnQuit,1,3,1,1)
grid.attach(self.lblTotal,0,2,2,1)
grid.attach(self.scrList,0,1,2,1)
grid.attach(self.btnDownload,0,3,1,1)
def on_btnDownload_clicked(self, widget):
try:
tree_selection = self.tvTorrent.get_selection()
model, treeiter = tree_selection.get_selected()
thash = model[treeiter][4]
ttitle = model[treeiter][0]
trackers_url = "http://torrentproject.org/"+thash+"/trackers_json"
response = urlopen(trackers_url).read().decode("utf-8")
obj = json.loads(response)
torrent_magnet = "magnet:?xt=urn:btih:"+thash+"&dn="+ttitle
for i in obj:
torrent_magnet += "&tr="+i
print(torrent_magnet)
os.system("xdg-open \""+torrent_magnet+"\"")
except TypeError:
print("No selection")
def on_btnSearch_clicked(self, widget):
search_title = self.txtSearch.get_text()
if search_title != "":
search_title = search_title.replace(" ","+")
url = "http://torrentproject.org/?s="+search_title+"&out=json&num=150"
response = urlopen(url).read().decode("utf-8")
obj = json.loads(response)
total = int(obj.get("total_found"))
if total != 0:
self.lsTorrent.clear()
if total > 150:
search_max = 150
self.lblTotal.set_text(str(total)+" torrents found. Server limit is 150 torrents. The above list contains 150 results(most relevant).")
else:
search_max = total
self.lblTotal.set_text(str(total)+" torrents found")
dlist = []
for i in range(1, search_max+1):
title = str(obj.get(str(i), {}).get("title"))
seeders = str(obj.get(str(i), {}).get("seeds"))
leechers = str(obj.get(str(i), {}).get("leechs"))
size = hbytes(int(obj.get(str(i), {}).get("torrent_size")))
thash = str(obj.get(str(i), {}).get("torrent_hash"))
dlist.append([title,seeders,leechers,size,thash])
for i in range(len(dlist)):
self.lsTorrent.append(dlist[i])
else:
self.lblTotal.set_text("0 results")
else:
print("No keyword given")
def on_btnQuit_clicked(self, widget):
Gtk.main_quit()
def hbytes(num):
for x in ['bytes','KB','MB','GB']:
if num < 1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
return "%3.1f%s" % (num, 'TB')
win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()