From 5433d9ad208be3ef7f136a001465478f2a51bdae Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Mon, 15 Apr 2013 16:05:14 +0930 Subject: [PATCH] added Wake (WOL) option --- TorrentToMedia.py | 3 ++ autoProcess/migratecfg.py | 10 ++++++ autoProcess/nzbToMediaUtil.py | 68 +++++++++++++++++++++++++++++++++++ autoProcessMedia.cfg.sample | 8 +++++ nzbToCouchPotato.py | 2 ++ nzbToGamez.py | 2 ++ nzbToHeadPhones.py | 2 ++ nzbToMedia.py | 5 ++- nzbToMylar.py | 2 ++ nzbToSickBeard.py | 2 ++ 10 files changed, 103 insertions(+), 1 deletion(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index eb074f94..940af1cb 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -210,6 +210,9 @@ if __name__ == "__main__": Logger.info("====================") # Seperate old from new log Logger.info("TorrentToMedia %s", VERSION) + + WakeUp() + config = ConfigParser.ConfigParser() configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg") diff --git a/autoProcess/migratecfg.py b/autoProcess/migratecfg.py index 36259703..640dad75 100644 --- a/autoProcess/migratecfg.py +++ b/autoProcess/migratecfg.py @@ -114,6 +114,16 @@ def migrate(): option, value = item confignew.set(section, option, value) + section = "WakeOnLan" + original = [] + try: + original = configold.items(section) + except: + pass + for item in original: + option, value = item + confignew.set(section, option, value) + section = "loggers" original = [] try: diff --git a/autoProcess/nzbToMediaUtil.py b/autoProcess/nzbToMediaUtil.py index e06b7bca..92072cc3 100644 --- a/autoProcess/nzbToMediaUtil.py +++ b/autoProcess/nzbToMediaUtil.py @@ -4,6 +4,9 @@ import os import re import sys import shutil +import struct +import socket +import time import linktastic.linktastic as linktastic @@ -209,6 +212,71 @@ def iterate_media_files(dirname): yield dirpath, os.path.join(dirpath, filename) +#Wake function +def WakeOnLan(ethernet_address): + + addr_byte = ethernet_address.split(':') + hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16), + int(addr_byte[1], 16), + int(addr_byte[2], 16), + int(addr_byte[3], 16), + int(addr_byte[4], 16), + int(addr_byte[5], 16)) + + # Build the Wake-On-LAN "Magic Packet"... + + msg = '\xff' * 6 + hw_addr * 16 + + # ...and send it to the broadcast address using UDP + + ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + ss.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) + ss.sendto(msg, ('', 9)) + ss.close() + + +#Test Connection function +def TestCon(host,port): + (family, socktype, proto, garbage, address) = socket.getaddrinfo(host, port)[0] + s = socket.socket(family, socktype, proto) + try: + s.connect(address) + return "Up" + except: + return "Down" + + +def WakeUp(): + config = ConfigParser.ConfigParser() + configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg") + + if not os.path.isfile(configFilename): + Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?") + return 1 # failure + + config.read(configFilename) + wake = config.get("WakeOnLan", "wake") + if wake == 0: # just return if we don't need to wake anything. + return + Logger.info("Loading WakeOnLan config from %s", configFilename) + config.get("WakeOnLan", "host") + host = config.get("WakeOnLan", "host") + port = config.get("WakeOnLan", "port") + mac = config.get("WakeOnLan", "mac") + + i=1 + while TestCon(host,port) == "Down" and i < 4: + Logger.info("Sending WakeOnLan Magic Packet for mac: %s", mac) + WakeOnLan(mac) + time.sleep(20) + i=i+1 + + if TestCon(host,port) == "Down": # final check. + Logger.warning("System with mac: %s has not woken after 3 attempts. Continuing with the rest of the script.", mac) + else: + Logger.info("System with mac: %s has been woken. Continuing with the rest of the script.", mac) + + def parse_other(args): return os.path.normpath(sys.argv[1]), '', '', '', '' diff --git a/autoProcessMedia.cfg.sample b/autoProcessMedia.cfg.sample index 99746e93..de6b1d5c 100644 --- a/autoProcessMedia.cfg.sample +++ b/autoProcessMedia.cfg.sample @@ -113,6 +113,14 @@ outputAudioCodec = libmp3lame outputAudioBitrate = 128k outputSubtitleCodec = +[WakeOnLan] +###### set wake = 1 to send WOL broadcast to the mac and test the server (e.g. xbmc) the host and port specified. +wake = 0 +host = 192.168.1.37 +port = 80 +mac = 00:01:2e:2D:64:e1 + + # Logging configuration [loggers] keys = root diff --git a/nzbToCouchPotato.py b/nzbToCouchPotato.py index 72cbdb5f..e1211a6b 100755 --- a/nzbToCouchPotato.py +++ b/nzbToCouchPotato.py @@ -19,6 +19,8 @@ Logger = logging.getLogger(__name__) Logger.info("====================") # Seperate old from new log Logger.info("nzbToCouchPotato %s", VERSION) +WakeUp() + # SABnzbd if len(sys.argv) == SABNZB_NO_OF_ARGUMENTS: # SABnzbd argv: diff --git a/nzbToGamez.py b/nzbToGamez.py index eb47c8b5..2e554bb2 100755 --- a/nzbToGamez.py +++ b/nzbToGamez.py @@ -19,6 +19,8 @@ Logger = logging.getLogger(__name__) Logger.info("====================") # Seperate old from new log Logger.info("nzbToGamez %s", VERSION) +WakeUp() + # SABnzbd if len(sys.argv) == SABNZB_NO_OF_ARGUMENTS: # SABnzbd argv: diff --git a/nzbToHeadPhones.py b/nzbToHeadPhones.py index 494b68ab..04f10bfd 100755 --- a/nzbToHeadPhones.py +++ b/nzbToHeadPhones.py @@ -19,6 +19,8 @@ Logger = logging.getLogger(__name__) Logger.info("====================") # Seperate old from new log Logger.info("nzbToHeadPhones %s", VERSION) +WakeUp() + # SABnzbd if len(sys.argv) == SABNZB_NO_OF_ARGUMENTS: # SABnzbd argv: diff --git a/nzbToMedia.py b/nzbToMedia.py index 98ba8d4e..4a3a9728 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -23,6 +23,9 @@ Logger = logging.getLogger(__name__) Logger.info("====================") # Seperate old from new log Logger.info("nzbToMedia %s", VERSION) + +WakeUp() + config = ConfigParser.ConfigParser() configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg") if not os.path.isfile(configFilename): @@ -36,7 +39,7 @@ cpsCategory = config.get("CouchPotato", "cpsCategory") sbCategory = config.get("SickBeard", "sbCategory") # tv hpCategory = config.get("HeadPhones", "hpCategory") # music mlCategory = config.get("Mylar", "mlCategory") # comics -gzCategory = config.get("Gamez", "gzCategory") +gzCategory = config.get("Gamez", "gzCategory") # games # SABnzbd if len(sys.argv) == SABNZB_NO_OF_ARGUMENTS: diff --git a/nzbToMylar.py b/nzbToMylar.py index 5109fd88..d2ace54a 100755 --- a/nzbToMylar.py +++ b/nzbToMylar.py @@ -19,6 +19,8 @@ Logger = logging.getLogger(__name__) Logger.info("====================") # Seperate old from new log Logger.info("nzbToMylar %s", VERSION) +WakeUp() + # SABnzbd if len(sys.argv) == SABNZB_NO_OF_ARGUMENTS: # SABnzbd argv: diff --git a/nzbToSickBeard.py b/nzbToSickBeard.py index 8d05ff2c..4139b254 100755 --- a/nzbToSickBeard.py +++ b/nzbToSickBeard.py @@ -41,6 +41,8 @@ Logger = logging.getLogger(__name__) Logger.info("====================") # Seperate old from new log Logger.info("nzbToSickBeard %s", VERSION) +WakeUp + # SABnzbd if len(sys.argv) == SABNZB_NO_OF_ARGUMENTS: # SABnzbd argv: