Skip to content

Instantly share code, notes, and snippets.

@dhead666
Last active December 21, 2015 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhead666/6384442 to your computer and use it in GitHub Desktop.
Save dhead666/6384442 to your computer and use it in GitHub Desktop.
Remove downloaded torrents that have been seeded (and stopped by SB, CP or Transmission) with a workaround for SB.
#!/usr/bin/env python
"""Growl 0.6 Network Protocol Client for Python"""
__version__ = "0.6.3"
__author__ = "Rui Carmo (http://the.taoofmac.com)"
__copyright__ = "(C) 2004 Rui Carmo. Code under BSD License."
__contributors__ = "Ingmar J Stein (Growl Team), John Morrissey (hashlib patch)"
try:
import hashlib
md5_constructor = hashlib.md5
except ImportError:
import md5
md5_constructor = md5.new
import struct
from socket import AF_INET, SOCK_DGRAM, socket
GROWL_UDP_PORT=9887
GROWL_PROTOCOL_VERSION=1
GROWL_TYPE_REGISTRATION=0
GROWL_TYPE_NOTIFICATION=1
class GrowlRegistrationPacket:
"""Builds a Growl Network Registration packet.
Defaults to emulating the command-line growlnotify utility."""
def __init__(self, application="growlnotify", password = None ):
self.notifications = []
self.defaults = [] # array of indexes into notifications
self.application = application.encode("utf-8")
self.password = password
# end def
def addNotification(self, notification="Command-Line Growl Notification", enabled=True):
"""Adds a notification type and sets whether it is enabled on the GUI"""
self.notifications.append(notification)
if enabled:
self.defaults.append(len(self.notifications)-1)
# end def
def payload(self):
"""Returns the packet payload."""
self.data = struct.pack( "!BBH",
GROWL_PROTOCOL_VERSION,
GROWL_TYPE_REGISTRATION,
len(self.application) )
self.data += struct.pack( "BB",
len(self.notifications),
len(self.defaults) )
self.data += self.application
for notification in self.notifications:
encoded = notification.encode("utf-8")
self.data += struct.pack("!H", len(encoded))
self.data += encoded
for default in self.defaults:
self.data += struct.pack("B", default)
self.checksum = md5_constructor()
self.checksum.update(self.data)
if self.password:
self.checksum.update(self.password)
self.data += self.checksum.digest()
return self.data
# end def
# end class
class GrowlNotificationPacket:
"""Builds a Growl Network Notification packet.
Defaults to emulating the command-line growlnotify utility."""
def __init__(self, application="growlnotify",
notification="Command-Line Growl Notification", title="Title",
description="Description", priority = 0, sticky = False, password = None ):
self.application = application.encode("utf-8")
self.notification = notification.encode("utf-8")
self.title = title.encode("utf-8")
self.description = description.encode("utf-8")
flags = (priority & 0x07) * 2
if priority < 0:
flags |= 0x08
if sticky:
flags = flags | 0x0100
self.data = struct.pack( "!BBHHHHH",
GROWL_PROTOCOL_VERSION,
GROWL_TYPE_NOTIFICATION,
flags,
len(self.notification),
len(self.title),
len(self.description),
len(self.application) )
self.data += self.notification
self.data += self.title
self.data += self.description
self.data += self.application
self.checksum = md5_constructor()
self.checksum.update(self.data)
if password:
self.checksum.update(password)
self.data += self.checksum.digest()
# end def
def payload(self):
"""Returns the packet payload."""
return self.data
# end def
# end class
if __name__ == '__main__':
print "Starting Unit Test"
print " - please make sure Growl is listening for network notifications"
addr = ("localhost", GROWL_UDP_PORT)
s = socket(AF_INET,SOCK_DGRAM)
print "Assembling registration packet like growlnotify's (no password)"
p = GrowlRegistrationPacket()
p.addNotification()
print "Sending registration packet"
s.sendto(p.payload(), addr)
print "Assembling standard notification packet"
p = GrowlNotificationPacket()
print "Sending standard notification packet"
s.sendto(p.payload(), addr)
print "Assembling priority -2 (Very Low) notification packet"
p = GrowlNotificationPacket(priority=-2)
print "Sending priority -2 notification packet"
s.sendto(p.payload(), addr)
print "Assembling priority 2 (Very High) sticky notification packet"
p = GrowlNotificationPacket(priority=2,sticky=True)
print "Sending priority 2 (Very High) sticky notification packet"
s.sendto(p.payload(), addr)
s.close()
print "Done."
#!/usr/bin/env python
# This script will send Growl messages when a torrent removed with the help of netgrowl.py
# Michael Kuhn is the original creator of this script and it was published at http://blog.mckuhn.de/2007/10/sending-growl-notifications-from-python.html
# Rui Carmo is the creator of netgrowl.py and it was published at https://the.taoofmac.com/space/projects/netgrowl
#
# Instructions:
# * Set the ip address and the password (if one exist) of the Growl target.
# * At first run you'll need to registrate the script on the Growl target, just remove the comment.
# * After the initial registration you can comment the lines back.
#
# edited by dhead666
from py_netgrowl import *
import sys
growl_message = sys.argv[1]
def growlNotify(title = "Torrent Removed :", message = growl_message):
addr = ("192.168.1.100", GROWL_UDP_PORT)
s = socket(AF_INET,SOCK_DGRAM)
#
# p = GrowlRegistrationPacket(application="Torrent Cleaning", password="")
# p.addNotification("Script Finished", enabled=True)
#
# s.sendto(p.payload(), addr)
if not message:
message = sys.argv[0]
p = GrowlNotificationPacket(application="Torrent Cleaning",
notification="Torrent Removed", title=title,
description=message, priority=1,
sticky=True, password="")
s.sendto(p.payload(),addr)
s.close()
if __name__ == '__main__':
growlNotify()
#!/bin/sh
# This script will remove torrents that have been downloaded and seeded by Transmission.
# The idea is to set a seeding limit in CP, SB & Transmission that will stop torrent that have been downloaded and seeded.
# The best apporach when setting download folder is to have separate folders for SB, CP and other Transmission downloads.
# Add appropriate line to your crontab like "0 * * * * /usr/bin/transmission_clean.sh 1> /dev/null", this will initiate the script once an hour without logging.
# I'm not the original author of the script so license is
# Originally posted on http://1000umbrellas.com/2010/10/05/updated-how-to-automatically-move-and-remove-transmission-daemon-downloads (available on Internet Archive)
# and also at http://www.goedonthouden.com/transmission-daemon-auto-remove-torrents/
TORRENTLIST=`transmission-remote --auth=user:password --list | sed -e '1d;$d;s/^ *//' | cut --only-delimited --delimiter=' ' --fields=1`
for TORRENTID in $TORRENTLIST
do
echo "* * * * * Operations on torrent ID $TORRENTID starting. * * * * *"
DL_COMPLETED=`transmission-remote --auth=user:password --torrent $TORRENTID --info | grep "Percent Done: 100%"`
DL_STOPPED=`transmission-remote --auth=user:password --torrent $TORRENTID --info | grep "State: Stopped"`
if [ "$DL_COMPLETED" != "" ] && [ "$DL_STOPPED" != "" ]; then
echo "Torrent #$TORRENTID is completed."
echo "Removing torrent from list."
transmission-remote --auth=user:password --torrent $TORRENTID --remove
else
echo "Torrent #$TORRENTID is not completed. Ignoring."
fi
echo "* * * * * Operations on torrent ID $TORRENTID completed. * * * * *"
done
#!/bin/sh
# This script will remove torrents that have been downloaded and seeded by Transmission.
# The idea is to set a seeding limit in CP, SB & Transmission that will stop torrent that have been downloaded and seeded.
# The best apporach when setting download folder is to have separate folders for SB, CP and other Transmission downloads.
# Add appropriate line to your crontab like "0 * * * * /usr/bin/transmission_clean.sh 1> /dev/null", this will initiate the script once an hour without logging.
# I'm not the original author of the script so license is
# Originally posted on http://1000umbrellas.com/2010/10/05/updated-how-to-automatically-move-and-remove-transmission-daemon-downloads (available on Internet Archive)
# and also at http://www.goedonthouden.com/transmission-daemon-auto-remove-torrents/
# adapted by dhead666
TORRENTLIST=`transmission-remote --auth=user:password --list | sed -e '1d;$d;s/^ *//' | cut --only-delimited --delimiter=' ' --fields=1`
for TORRENTID in $TORRENTLIST
do
echo "* * * * * Operations on torrent ID $TORRENTID starting. * * * * *"
DL_COMPLETED=`transmission-remote --auth=user:password --torrent $TORRENTID --info | grep "Percent Done: 100%"`
DL_STOPPED=`transmission-remote --auth=user:password --torrent $TORRENTID --info | grep "State: Stopped"`
DL_NAME=`transmission-remote --auth=user:password --torrent $TORRENTID --info | grep "Name:" | cut -c9-`
if [ "$DL_COMPLETED" != "" ] && [ "$DL_STOPPED" != "" ]; then
echo "Torrent #$TORRENTID is completed."
echo "Removing torrent from list."
transmission-remote --auth=user:password --torrent $TORRENTID --remove
echo "Sending Growl message."
/boot/scripts/py_torrentgrowl.py $DL_NAME
else
echo "Torrent #$TORRENTID is not completed. Ignoring."
fi
echo "* * * * * Operations on torrent ID $TORRENTID completed. * * * * *"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment