Skip to content

Commit

Permalink
Added scene exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
berkona committed Mar 1, 2013
1 parent 54d5358 commit 140926e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 80 deletions.
64 changes: 22 additions & 42 deletions autoProcessMovie.py
Expand Up @@ -4,10 +4,11 @@
import shutil
import ConfigParser
import time
import json
import json
import logging

from nzbToMediaEnv import *
from nzbToMediaSceneExceptions import process_all_exceptions

Logger = logging.getLogger()

Expand All @@ -17,7 +18,7 @@ def __init__(self, user, pw):
self.password = pw
self.numTries = 0
urllib.FancyURLopener.__init__(self)

def prompt_user_passwd(self, host, realm):
if self.numTries == 0:
self.numTries = 1
Expand All @@ -29,36 +30,20 @@ def openit(self, url):
self.numTries = 0
return urllib.FancyURLopener.open(self, url)

def custom_groups(group, dirName):
mediaContainer = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.iso']
if group == "[=-< Q o Q >-=]": # for my NL friends :) we want to reverse the file names for the video files.
for dirpath, dirnames, filenames in os.walk(dirName):
for file in filenames:
filePath = os.path.join(dirpath, file)
fileExtention = os.path.splitext(file)[1]
if fileExtention in mediaContainer: # If the file is a video file
Logger.debug("Reversing the file name for a QoQ release %s", file)
newname = os.path.splitext(file)[0][::-1]
newfile = newname + fileExtention
newfilePath = os.path.join(dirpath, newfile)
os.rename(filePath, newfilePath)
Logger.debug("New file name is %s", newfile)
else: # we can add more customizations here.
pass

def process(dirName, nzbName=None, status=0):

status = int(status)
config = ConfigParser.ConfigParser()
configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg")
Logger.info("Loading config from %s", configFilename)

if not os.path.isfile(configFilename):
Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?")
sys.exit(-1)

config.read(configFilename)

host = config.get("CouchPotato", "host")
port = config.get("CouchPotato", "port")
username = config.get("CouchPotato", "username")
Expand All @@ -72,7 +57,7 @@ def process(dirName, nzbName=None, status=0):
ssl = int(config.get("CouchPotato", "ssl"))
except (ConfigParser.NoOptionError, ValueError):
ssl = 0

try:
web_root = config.get("CouchPotato", "web_root")
except ConfigParser.NoOptionError:
Expand All @@ -86,22 +71,17 @@ def process(dirName, nzbName=None, status=0):
protocol = "https://"
else:
protocol = "http://"
# don't delay when we are calling this script manually.
if nzbName == "Manual Run":
# don't delay when we are calling this script manually.
if nzbName == "Manual Run":
delay = 0

# check for custom groups
customgroups = ['[=-< Q o Q >-=]'] # we can add more to this list
for index in range(len(customgroups)):
if customgroups[index].lower() in nzbName.lower(): # match the group in the nzbname
custom_groups(customgroups[index], dirName) # files have been renamned
break

process_all_exceptions(nzbName.lower(), dirname)

if status == 0:
if method == "manage":
command = "manage.update"
command = "manage.update"
else:
command = "renamer.scan"
command = "renamer.scan"

url = protocol + host + ":" + port + web_root + "/api/" + apikey + "/" + command

Expand All @@ -110,13 +90,13 @@ def process(dirName, nzbName=None, status=0):
time.sleep(delay)

Logger.debug("Opening URL: %s", url)

try:
urlObj = myOpener.openit(url)
except IOError, e:
Logger.error("Unable to open URL: %s", str(e))
sys.exit(1)

result = json.load(urlObj)
Logger.info("CouchPotatoServer returned %s", result)
if result['success']:
Expand All @@ -132,9 +112,9 @@ def process(dirName, nzbName=None, status=0):
imdbid=nzbName1[a:b]

url = protocol + host + ":" + port + web_root + "/api/" + apikey + "/movie.list"

Logger.debug("Opening URL: %s", url)

try:
urlObj = myOpener.openit(url)
except IOError, e:
Expand All @@ -158,21 +138,21 @@ def process(dirName, nzbName=None, status=0):
Logger.warning("please manually ignore this release and refresh the wanted movie")
Logger.error("exiting postprocessing script")
sys.exit(1)

url = protocol + host + ":" + port + web_root + "/api/" + apikey + "/searcher.try_next/?id=" + movid

Logger.debug("Opening URL: %s", url)

try:
urlObj = myOpener.openit(url)
except IOError, e:
Logger.error("Unable to open URL: %s", str(e))
sys.exit(1)

result = urlObj.readlines()
for line in result:
Logger.info("%s", line)

Logger.info("movie %s set to try the next best release on CouchPotatoServer", movid)
if delete_failed:
Logger.info("Deleting failed files and folder %s", dirName)
Expand Down
56 changes: 18 additions & 38 deletions autoProcessTV.py
Expand Up @@ -12,7 +12,7 @@
# 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 Sick Beard. If not, see <http://www.gnu.org/licenses/>.

Expand All @@ -27,13 +27,14 @@

Logger = logging.getLogger()


class AuthURLOpener(urllib.FancyURLopener):
def __init__(self, user, pw):
self.username = user
self.password = pw
self.numTries = 0
urllib.FancyURLopener.__init__(self)

def prompt_user_passwd(self, host, realm):
if self.numTries == 0:
self.numTries = 1
Expand All @@ -45,42 +46,26 @@ def openit(self, url):
self.numTries = 0
return urllib.FancyURLopener.open(self, url)

def custom_groups(group, dirName):
mediaContainer = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.iso']
if group == "[=-< Q o Q >-=]": # for my NL friends :) we want to reverse the file names for the video files.
for dirpath, dirnames, filenames in os.walk(dirName):
for file in filenames:
filePath = os.path.join(dirpath, file)
fileExtention = os.path.splitext(file)[1]
if fileExtention in mediaContainer: # If the file is a video file
Logger.debug("Reversing the file name for a QoQ release %s", file)
newname = os.path.splitext(file)[0][::-1]
newfile = newname + fileExtention
newfilePath = os.path.join(dirpath, newfile)
os.rename(filePath, newfilePath)
Logger.debug("New file name is %s", newfile)
else: # we can add more customizations here.
pass

def processEpisode(dirName, nzbName=None, failed=False):

status = int(failed)
config = ConfigParser.ConfigParser()
configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg")
Logger.info("Loading config from %s", configFilename)

if not os.path.isfile(configFilename):
Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?")
sys.exit(-1)

try:
fp = open(configFilename, "r")
config.readfp(fp)
fp.close()
except IOError, e:
Logger.error("Could not read configuration file: %s", str(e))
sys.exit(1)

watch_dir = ""
host = config.get("SickBeard", "host")
port = config.get("SickBeard", "port")
Expand All @@ -90,40 +75,35 @@ def processEpisode(dirName, nzbName=None, failed=False):
ssl = int(config.get("SickBeard", "ssl"))
except (ConfigParser.NoOptionError, ValueError):
ssl = 0

try:
web_root = config.get("SickBeard", "web_root")
except ConfigParser.NoOptionError:
web_root = ""

try:
watch_dir = config.get("SickBeard", "watch_dir")
except ConfigParser.NoOptionError:
watch_dir = ""

try:
failed_fork = int(config.get("SickBeard", "failed_fork"))
except (ConfigParser.NoOptionError, ValueError):
failed_fork = 0

# check for custom groups
customgroups = ['[=-< Q o Q >-=]'] # we can add more to this list
for index in range(len(customgroups)):
if customgroups[index].lower() in nzbName.lower(): # match the group in the nzbname
custom_groups(customgroups[index], dirName) # files have been renamned
break
process_all_exceptions(nzbName.lower(), dirname)



#allows manual call of postprocess script if we have specified a watch_dir. Check that here.
if nzbName == "Manual Run" and watch_dir == "":
Logger.error("In order to run this script manually you must specify a watch_dir in autoProcessTV.cfg")
sys.exit(-1)
#allows us to specify the default watch directory and call the postproecssing on another PC with different directory structure.
if watch_dir != "":
dirName = watch_dir

params = {}

params['quiet'] = 1

# if you have specified you are using development branch from fork https://github.com/Tolstyak/Sick-Beard.git
Expand All @@ -136,7 +116,7 @@ def processEpisode(dirName, nzbName=None, failed=False):
Logger.info("The download failed. Sending 'failed' process request to SickBeard's failed branch")
else:
Logger.info("The download succeeded. Sending process request to SickBeard's failed branch")

# this is our default behaviour to work with the standard Master branch of SickBeard
else:
params['dir'] = dirName
Expand All @@ -150,22 +130,22 @@ def processEpisode(dirName, nzbName=None, failed=False):
Logger.info("The download succeeded. Sending process request to SickBeard")

myOpener = AuthURLOpener(username, password)

if ssl:
protocol = "https://"
else:
protocol = "http://"

url = protocol + host + ":" + port + web_root + "/home/postprocess/processEpisode?" + urllib.urlencode(params)

Logger.debug("Opening URL: %s", url)

try:
urlObj = myOpener.openit(url)
except IOError, e:
Logger.error("Unable to open URL: %s", str(e))
sys.exit(1)

result = urlObj.readlines()
for line in result:
Logger.info("%s", line)
35 changes: 35 additions & 0 deletions nzbToMediaSceneExceptions.py
@@ -0,0 +1,35 @@
# System imports
import os
import logging

# Custom imports
from nzbToMediaUtil import iterate_media_files


Logger = logging.getLogger(__name__)


def process_all_exceptions(name, dirname):
for group, exception in __customgroups__.items():
if not group in name:
continue
process_exception(exception, name, dirname)


def process_exception(exception, name, dirname):
for parentDir, filename in iterate_media_files(dirname):
exception(filename, parentDir)


def process_qoq(filename, dirname):
Logger.debug("Reversing the file name for a QoQ release %s", filename)
head, fileExtention = os.path.splitext(filename)
newname = head[::-1]
newfile = newname + fileExtention
newfilePath = os.path.join(dirname, newfile)
os.rename(filename, newfilePath)
Logger.debug("New file name is %s", newfile)

# dict for custom groups
# we can add more to this list
__customgroups__ = {'[=-< Q o Q >-=]': process_qoq}
12 changes: 12 additions & 0 deletions nzbToMediaUtil.py
Expand Up @@ -27,6 +27,18 @@ def create_destination(outputDestination):
sys.exit(-1)


def iterate_media_files(dirname):
mediaContainer = [ '.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv',
'.mp4', '.mpg', '.mpeg', '.iso' ]

for dirpath, dirnames, filesnames in os.walk(dirname):
for filename in filesnames:
fileExtention = os.path.splitext(filename)[0]
if not (fileExtention in mediaContainer):
continue
yield dirpath, os.path.join(dirpath, filename)


def parse_other(args):
return os.path.normpath(sys.argv[1]), '', '', ''

Expand Down

0 comments on commit 140926e

Please sign in to comment.