forked from jarainf/nyupdate
/
Torrentclient.py
157 lines (138 loc) · 4.84 KB
/
Torrentclient.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python3
import libtorrent as lt
import urllib.request
import threading
import time
import re
import json
import os
import traceback
import smtplib
LOG = 1
EMAIL_CONFIG = "email.conf.json"
REFRESH_INTERVAL = 10
def green(msg):
return "\033[0;32m" + str(msg) + "\033[0;m"
def blue(msg):
return "\033[0;34m" + str(msg) + "\033[0;m"
def yellow(msg):
return "\033[0;33m" + str(msg) + "\033[0;m"
def red(msg):
return "\033[0;31m" + str(msg) + "\033[0;m"
def log(msg):
if LOG > 0:
print(msg)
class Torrentclient(threading.Thread):
def __init__(self, upload_limit=-1, download_limit=-1, status_mail=False):
threading.Thread.__init__(self)
if status_mail:
self.status_mail = True
try:
email_config_file = open(EMAIL_CONFIG, "r")
self.config = json.load(email_config_file)
email_config_file.close()
# check for necessary fields
self.config["username"]
self.config["password"]
self.config["recipient"]
log(green("Status mails successfully enabled"))
except:
log(yellow("Error while pasing email config, disabling status_mail"))
self.status_mail = False
self.files = dict()
self.session = lt.session()
self.session.listen_on(6881, 6891)
self.torrent_dir = ".torrents"
self.download_dir = "Downloads"
self.do_things = True
settings = self.session.settings()
if upload_limit != -1:
log(blue("Upload limit set to non-default value: " + str(upload_limit)))
settings.upload_rate_limit = upload_limit
if download_limit != -1:
log(blue("Download limit set to non-default value: " + str(download_limit)))
settings.download_rate_limit = download_limit
self.session.set_settings(settings)
def get_filename(self, urllib_response):
filename = ""
try:
filename = re.sub(".*filename=\"", "", re.sub("\"$", "", dict(urllib_response.info())["Content-Disposition"]))
return filename
except BaseException as e:
log(yellow(traceback.format_exc()))
return None
def set_upload_limit(self, limit):
settings = self.session.settings()
settings.upload_rate_limit = limit
self.session.set_settings(settings)
log(yellow("Set upload limit to " + str(limit)))
def set_download_limit(self, limit):
settings = self.session.settings()
settings.download_rate_limit = limit
self.session.set_settings(settings)
log(yellow("Set download limit to " + str(limit)))
def add_torrent_by_file(self, filename):
torrent_info = lt.torrent_info(filename)
torrent_handle = self.session.add_torrent({"ti": torrent_info, "save_path": self.download_dir})
torrent_handle.set_upload_limit(0)
self.files[torrent_handle.name()] = filename
def add_torrent(self, url):
response = urllib.request.urlopen(url)
torrent_filename = self.get_filename(response)
if torrent_filename == None:
log(red("Failed to add torrent: " + url))
return
filename = self.torrent_dir + "/" + self.get_filename(response)
torrent = open(filename, "wb")
torrent.write(response.read())
torrent.close()
self.add_torrent_by_file(filename)
def send_mail(self, subject, text):
gmail_user = self.config["username"]
gmail_pwd = self.config["password"]
FROM = self.config["username"]
TO = [self.config["recipient"]] #must be a list
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), subject, text)
try:
#server = smtplib.SMTP(SERVER)
server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
log(green("Successfully sent status mail!"))
except BaseException as e:
log(red(traceback.format_exc()))
def deep_copy(self, elements):
new_elements = []
for element in elements:
new_elements.append(element)
return new_elements
def kill(self):
self.do_things = False
def run(self):
for torrent in os.listdir(self.torrent_dir):
self.add_torrent_by_file(self.torrent_dir + "/" + torrent)
while self.do_things:
log(blue("Checking on torrents now..."))
torrents = self.deep_copy(self.session.get_torrents())
for torrent in torrents:
log(green("Torrent: " + torrent.name() + " %.2f%% (%.1f kiB/s)" % (torrent.status().progress * 100, torrent.status().download_rate / 1024)))
if torrent.is_paused():
log(yellow("Torrent: " + torrent.name() + " was paused. Resumed."))
torrent.resume()
if torrent.is_seed():
log(green("Torrent: " + torrent.name() + " is done. Removed."))
if self.status_mail:
threading.Thread(target=self.send_mail, args=("Torrent Complete!", "Torrent %s was completed!" % (torrent.name()))).start()
os.remove(self.files[torrent.name()])
self.session.remove_torrent(torrent)
time.sleep(REFRESH_INTERVAL)
self.session.pause()
log(green("Pausing session..."))
while not self.session.is_paused():
time.sleep(1)