sparky3387 Posted November 21, 2010 Report Share Posted November 21, 2010 This is a simple python script that I use to alert me on a Linux box when a download is completed.I used small parts of code from the uTorrent Python API and I copied the design on the notification from rss2email (rewrote it though), this needs the package simplejson to run (it can be installed via debian package manager).This script creates a file called cache.dat to track torrent progress, so make sure you put this file into /usr/share/uTorrent-notify or something, then add it to a cron job*/30 * * * * /usr/bin/python /usr/share/uTorrent-notify/uTNotify.pyuTNotify.py## Copyright (C) 2010 sparky3387## 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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.##import sys,os,smtplib,pickle,simplejson,urllib2,base64,re,cookielib,subprocessclass uTNotify(): def __init__( self ): self.host = "127.0.0.1" self.port = "8080" self.login = "admin" self.password = "" self.sender="email@server.com" self.recipient="email@server.com" #Send email on loading of torrent into utorrent self.sendonadd=False #Use sendmail to send the email self.sendmail = True #If not using sendmail change settings below self.smtpserver = "smtp.gmail.com:587" #If you use gmail change to true self.gmail=True self.gmailUser="me@gmail.com" self.gmailPassword="PASSWORD" #LINES BELOW HERE SHOULD BE LEFT ALONE self.cookiejar = cookielib.CookieJar() self.token = self.get_token(); if os.path.isfile(os.path.abspath(os.path.dirname(sys.argv[0]))+"/cache.dat"): pickledict = pickle.load(open(os.path.abspath(os.path.dirname(sys.argv[0]))+"/cache.dat")) else: pickledict = {} torrentdict = {} torrents = simplejson.loads(self.get_page("?list=1&token="+self.token)) if "Error" in torrents: print_err(0,self.torrents["Error"]) #print torrents for torrent in torrents["torrents"]: if torrent[4]==1000: if pickledict.has_key(torrent[0]): if pickledict[torrent[0]]!=1000: self.send_mail(torrent[2]) else: self.send_mail(torrent[2]) else: if (not pickledict.has_key(torrent[0])) & self.sendonadd: self.send_mail(torrent[2],1) torrentdict[(torrent[0])] = torrent[4] pickle.dump(torrentdict,open(os.path.abspath(os.path.dirname(sys.argv[0]))+"/cache.dat","w")) def get_token(self) : data = self.get_page('token.html') match = re.search("<div .*?id='token'.*?>(.+?)</div>",data) if match == None: print "Can\'t access security token" return match.group( 1 ) def get_page(self,page) : opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookiejar), urllib2.HTTPHandler()) self.conn = urllib2.Request("http://"+self.host+":"+self.port+"/gui/"+page) self.conn.add_header("Authorization", "Basic " + base64.b64encode(self.login+":"+self.password)) try: resp = opener.open(self.conn) data = resp.read() self.cookiejar.extract_cookies(resp,self.conn) except urllib2.HTTPError,e: self.print_err(e.code, "Unknown error") except urllib2.URLError,e: self.print_err(0, e.reason) return data.decode("utf8") def send_mail(self,file,add=0): if add: msg = "is" else: msg = "has just finished" body="From: uTorrent <"+self.sender+">\r\nTo: <"+self.recipient+">\r\nMIME-Version: 1.0\r\nContent-type: text/html\r\nSubject: uTorrent download alert\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<title>uTorrent Download Alert</title>\n<style type=\"text/css\">\n.header {\n background-color:#e0ecff;\n border:solid #c3d9ff 3px;\n width:100%;\n}\n.header a,.header a:visited {\n text-decoration: none;\n color:#00F;\n font: bold 15px Arial, Helvetica, sans-serif;\n}\n.content {\n width:100%;\n border-right-width: 3px;\n border-bottom-width: 3px;\n border-left-width: 3px;\n border-top-color: #c3d9ff;\n border-right-color: #c3d9ff;\n border-bottom-color: #c3d9ff;\n border-left-color: #c3d9ff;\n font:13px Arial, Helvetica, sans-serif;\n border-right-style: solid;\n border-bottom-style: solid;\n border-left-style: solid;\n padding: 5px 0px 5px 0px;\n}\n.content a {\n font-weight:bold;\n}\n.footer {\n width:100%;\n border-right-width: 3px;\n border-bottom-width: 3px;\n border-left-width: 3px;\n border-top-color: #c3d9ff;\n border-right-color: #c3d9ff;\n border-bottom-color: #c3d9ff;\n border-left-color: #c3d9ff;\n font:13px Arial, Helvetica, sans-serif;\n border-right-style: solid;\n border-bottom-style: solid;\n border-left-style: solid;\n}\n</style>\n</head>\n<body>\n<div class=\"header\">\n<a href=\"http://"+self.host+"/gui/\">uTorrent download alert</a>\n</div>\n<div class=\"content\">uTorrent "+msg+" downloading "+file+"</div>\n<div class=\"footer\" style=\"background-color:#c3d9ff;\">URL: <a href=\"http://"+self.host+"/gui//\">uTorrent Webinterface/</a></div>\n</body>\n</html>" if self.sendmail: p = subprocess.Popen(["/usr/sbin/sendmail", self.recipient], stdin=subprocess.PIPE, stdout=subprocess.PIPE) p.communicate(body) status = p.returncode assert status != None, "just a sanity check" if status != 0: print >>warn, "" print >>warn, ('Fatal error: sendmail exited with code %s' % status) sys.exit(1) else: smtpserver = smtplib.SMTP(self.smtpserver) if self.gmail: smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo() smtpserver.login(self.gmailUser, self.gmailPassword) smtpserver.sendmail(self.sender, self.recipient, body) smtpserver.close() def print_err(self,code,error) : if (code!=0): print "ERROR: uTNotify failed with error code "+str(code)+" and error message \""+error+"\"" else: print "ERROR: uTNotify failed with error message \""+str(error)+"\"" exit()ut = uTNotify()edit: many bugs and added feature to send email when a torrent is addedsecond edit: rewrote a small bit so you can either use gmail or sendmail Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.