mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-22 06:13:19 -07:00
Added linktastic support for linking
This commit is contained in:
parent
4adec9e8de
commit
606aa2d9c9
2 changed files with 88 additions and 23 deletions
|
@ -3,7 +3,7 @@
|
||||||
import autoProcessMovie
|
import autoProcessMovie
|
||||||
import autoProcessTV
|
import autoProcessTV
|
||||||
import sys, os, ConfigParser, shutil
|
import sys, os, ConfigParser, shutil
|
||||||
from subprocess import call
|
import linktastic
|
||||||
|
|
||||||
from nzbToMediaEnv import *
|
from nzbToMediaEnv import *
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ if len(sys.argv) == 4:
|
||||||
##Finished - 11
|
##Finished - 11
|
||||||
##Queued - 12
|
##Queued - 12
|
||||||
##Stopped - 13
|
##Stopped - 13
|
||||||
|
|
||||||
## We will pass in %D, %N, %L from uTorrent
|
## We will pass in %D, %N, %L from uTorrent
|
||||||
print "INFO: Script called from utorrent"
|
print "INFO: Script called from utorrent"
|
||||||
Directory = sys.argv[1] ## %D -- Example output: F:\path\to\dir\My.Series.S01E01.720p.HDTV.x264-2HD
|
Directory = sys.argv[1] ## %D -- Example output: F:\path\to\dir\My.Series.S01E01.720p.HDTV.x264-2HD
|
||||||
|
@ -85,7 +85,7 @@ else:
|
||||||
print "Error: There was a problem loading variables from Transmission", "Exiting"
|
print "Error: There was a problem loading variables from Transmission", "Exiting"
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
Category = '' #We dont have a category, so assume the last directory is the category for now.
|
Category = '' #We dont have a category, so assume the last directory is the category for now.
|
||||||
|
|
||||||
print "DEBUG: Received Directory: %s" % (Directory)
|
print "DEBUG: Received Directory: %s" % (Directory)
|
||||||
print "DEBUG: Received Torrent Name: %s" % (Name)
|
print "DEBUG: Received Torrent Name: %s" % (Name)
|
||||||
print "DEBUG: Received Category: %s" % (Category)
|
print "DEBUG: Received Category: %s" % (Category)
|
||||||
|
@ -121,7 +121,7 @@ if DirBase[1] == Name:
|
||||||
if not Category:
|
if not Category:
|
||||||
print "INFO: Determined Category to be: %s" % (DirBase2[1])
|
print "INFO: Determined Category to be: %s" % (DirBase2[1])
|
||||||
Category = DirBase2[1]
|
Category = DirBase2[1]
|
||||||
|
|
||||||
elif DirBase[1] == Movie_Cat or DirBase == TV_Cat:
|
elif DirBase[1] == Movie_Cat or DirBase == TV_Cat:
|
||||||
if os.path.isdir(os.path.join(Directory, Name)):
|
if os.path.isdir(os.path.join(Directory, Name)):
|
||||||
print "INFO: Found torrent directory %s in category directory %s" % (os.path.join(Directory, Name), Directory)
|
print "INFO: Found torrent directory %s in category directory %s" % (os.path.join(Directory, Name), Directory)
|
||||||
|
@ -134,13 +134,13 @@ elif DirBase[1] == Movie_Cat or DirBase == TV_Cat:
|
||||||
if not Category:
|
if not Category:
|
||||||
print "INFO: Determined Category to be: %s" % (DirBase2[1])
|
print "INFO: Determined Category to be: %s" % (DirBase2[1])
|
||||||
Category = DirBase[1]
|
Category = DirBase[1]
|
||||||
|
|
||||||
else: # no category found in directory. For Utorrent we can do a recursive scan.
|
else: # no category found in directory. For Utorrent we can do a recursive scan.
|
||||||
print "INFO: The directory passed does not appear to include a category or the torrent name"
|
print "INFO: The directory passed does not appear to include a category or the torrent name"
|
||||||
print "WARNING: You should change settings to download torrents to their own directory"
|
print "WARNING: You should change settings to download torrents to their own directory"
|
||||||
print "INFO: We will try and determine which files to process, individually"
|
print "INFO: We will try and determine which files to process, individually"
|
||||||
root = 1
|
root = 1
|
||||||
|
|
||||||
if Category == Movie_Cat:
|
if Category == Movie_Cat:
|
||||||
destination = os.path.join(Movie_dest, Name)
|
destination = os.path.join(Movie_dest, Name)
|
||||||
elif Category == TV_Cat:
|
elif Category == TV_Cat:
|
||||||
|
@ -167,7 +167,7 @@ if root == 1:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
ext = [os.path.splitext(file)[1] for file in f[1]]
|
ext = [os.path.splitext(file)[1] for file in f[1]]
|
||||||
if set(ext).intersection(set(test)):
|
if set(ext).intersection(set(test)):
|
||||||
print "INFO: Found compressed archives, extracting"
|
print "INFO: Found compressed archives, extracting"
|
||||||
|
@ -183,7 +183,7 @@ else:
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
if useLink == 0 and packed == 0 and video == 1: ## copy
|
if useLink == 0 and packed == 0 and video == 1: ## copy
|
||||||
if root == 0: #move all files in tier own directory
|
if root == 0: #move all files in tier own directory
|
||||||
print "INFO: Copying all files from %s to %s." % (Directory, destination)
|
print "INFO: Copying all files from %s to %s." % (Directory, destination)
|
||||||
shutil.copytree(Directory, destination)
|
shutil.copytree(Directory, destination)
|
||||||
else: #we only want to move files matching the torrent name when root directory is used.
|
else: #we only want to move files matching the torrent name when root directory is used.
|
||||||
|
@ -210,14 +210,9 @@ elif useLink == 1 and packed == 0 and video == 1: ## hardlink
|
||||||
continue #ignore the other files
|
continue #ignore the other files
|
||||||
source = os.path.join(dirpath, file)
|
source = os.path.join(dirpath, file)
|
||||||
target = os.path.join(destination, file)
|
target = os.path.join(destination, file)
|
||||||
|
|
||||||
if os.name == 'nt'
|
linktastic.link(source, target)
|
||||||
subprocess.call(['cmd', '/C', 'mklink', '/H', source, target], stdout=subprocess.PIPE)
|
|
||||||
elif os.name == 'posix':
|
|
||||||
os.link(source, target)
|
|
||||||
else:
|
|
||||||
print "ERROR: Hardlink failed, cannot determine OS."
|
|
||||||
|
|
||||||
elif packed == 1: ## unpack
|
elif packed == 1: ## unpack
|
||||||
## Using Windows?
|
## Using Windows?
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
|
@ -225,7 +220,7 @@ elif packed == 1: ## unpack
|
||||||
ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"]
|
ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"]
|
||||||
EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip)
|
EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip)
|
||||||
print "INFO: We are using Windows"
|
print "INFO: We are using Windows"
|
||||||
|
|
||||||
## Using linux?
|
## Using linux?
|
||||||
elif os.name == 'posix':
|
elif os.name == 'posix':
|
||||||
required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"]
|
required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"]
|
||||||
|
@ -242,7 +237,7 @@ elif packed == 1: ## unpack
|
||||||
".7z": ["7zr", "x"],
|
".7z": ["7zr", "x"],
|
||||||
}
|
}
|
||||||
print "INFO: We are using *nix"
|
print "INFO: We are using *nix"
|
||||||
|
|
||||||
## Need to add a check for which commands that can be utilized in *nix systems..
|
## Need to add a check for which commands that can be utilized in *nix systems..
|
||||||
else:
|
else:
|
||||||
print "ERROR: Unknown OS, exiting"
|
print "ERROR: Unknown OS, exiting"
|
||||||
|
@ -253,7 +248,7 @@ elif packed == 1: ## unpack
|
||||||
if (Name in file) or (file in Name):
|
if (Name in file) or (file in Name):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
continue #ignore the other files
|
continue #ignore the other files
|
||||||
ext = os.path.splitext(f)
|
ext = os.path.splitext(f)
|
||||||
fp = os.path.join(Directory, os.path.normpath(f))
|
fp = os.path.join(Directory, os.path.normpath(f))
|
||||||
if ext[1] in (".gz", ".bz2", ".lzma"):
|
if ext[1] in (".gz", ".bz2", ".lzma"):
|
||||||
|
@ -276,8 +271,8 @@ elif packed == 1: ## unpack
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print"INFO: Extracting to %s" % (destination)
|
print"INFO: Extracting to %s" % (destination)
|
||||||
|
|
||||||
## Running..
|
## Running..
|
||||||
print "INFO: Extracting %s %s %s %s" % (cmd[0], cmd[1], fp, destination)
|
print "INFO: Extracting %s %s %s %s" % (cmd[0], cmd[1], fp, destination)
|
||||||
pwd = os.getcwd() # Get our Present Working Directory
|
pwd = os.getcwd() # Get our Present Working Directory
|
||||||
os.chdir(destination) #not all unpack commands accept full paths, so just extract into this directory.
|
os.chdir(destination) #not all unpack commands accept full paths, so just extract into this directory.
|
||||||
|
@ -304,7 +299,7 @@ elif packed == 1: ## unpack
|
||||||
else:
|
else:
|
||||||
print "ERROR: Extraction failed for %s. 7zip result was %s" % (fp, res)
|
print "ERROR: Extraction failed for %s. 7zip result was %s" % (fp, res)
|
||||||
except:
|
except:
|
||||||
print "ERROR: Extraction failed for %s. Could not call command %s %s %s %s" % (fp, cmd[0], cmd[1], fp)
|
print "ERROR: Extraction failed for %s. Could not call command %s %s %s %s" % (fp, cmd[0], cmd[1], fp)
|
||||||
os.chdir(pwd) # Go back to our Original Working Directory
|
os.chdir(pwd) # Go back to our Original Working Directory
|
||||||
|
|
||||||
for dirpath, dirnames, filenames in os.walk(destination): #flatten out the directory to make postprocessing easier.
|
for dirpath, dirnames, filenames in os.walk(destination): #flatten out the directory to make postprocessing easier.
|
||||||
|
@ -319,7 +314,7 @@ removeEmptyFolders(destination) #cleanup empty directories.
|
||||||
|
|
||||||
status = int(status)
|
status = int(status)
|
||||||
## Now we pass off to CouchPotato or SickBeard.
|
## Now we pass off to CouchPotato or SickBeard.
|
||||||
if Category == Movie_Cat:
|
if Category == Movie_Cat:
|
||||||
autoProcessMovie.process(destination, Name, status)
|
autoProcessMovie.process(destination, Name, status)
|
||||||
elif Category == TV_Cat:
|
elif Category == TV_Cat:
|
||||||
autoProcessTV.processEpisode(destination, Name, status)
|
autoProcessTV.processEpisode(destination, Name, status)
|
||||||
|
|
70
linktastic.py
Normal file
70
linktastic.py
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
# Linktastic Module
|
||||||
|
# - A python2/3 compatible module that can create hardlinks/symlinks on windows-based systems
|
||||||
|
#
|
||||||
|
# Linktastic is distributed under the MIT License. The follow are the terms and conditions of using Linktastic.
|
||||||
|
#
|
||||||
|
# The MIT License (MIT)
|
||||||
|
# Copyright (c) 2012 Solipsis Development
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||||
|
# associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||||
|
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
# subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||||
|
# portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||||
|
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
from subprocess import CalledProcessError
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
# Private function to create link on nt-based systems
|
||||||
|
def _link_windows(src, dest):
|
||||||
|
try:
|
||||||
|
subprocess.check_output(
|
||||||
|
['cmd', '/C', 'mklink', '/H', dest, src],
|
||||||
|
stderr=subprocess.STDOUT)
|
||||||
|
except CalledProcessError as err:
|
||||||
|
raise IOError(err.output.decode('utf-8'))
|
||||||
|
|
||||||
|
# TODO, find out what kind of messages Windows sends us from mklink
|
||||||
|
# print(stdout)
|
||||||
|
# assume if they ret-coded 0 we're good
|
||||||
|
|
||||||
|
|
||||||
|
def _symlink_windows(src, dest):
|
||||||
|
try:
|
||||||
|
subprocess.check_output(
|
||||||
|
['cmd', '/C', 'mklink', dest, src],
|
||||||
|
stderr=subprocess.STDOUT)
|
||||||
|
except CalledProcessError as err:
|
||||||
|
raise IOError(err.output.decode('utf-8'))
|
||||||
|
|
||||||
|
# TODO, find out what kind of messages Windows sends us from mklink
|
||||||
|
# print(stdout)
|
||||||
|
# assume if they ret-coded 0 we're good
|
||||||
|
|
||||||
|
|
||||||
|
# Create a hard link to src named as dest
|
||||||
|
# This version of link, unlike os.link, supports nt systems as well
|
||||||
|
def link(src, dest):
|
||||||
|
if os.name == 'nt':
|
||||||
|
_link_windows(src, dest)
|
||||||
|
else:
|
||||||
|
os.link(src, dest)
|
||||||
|
|
||||||
|
|
||||||
|
# Create a symlink to src named as dest, but don't fail if you're on nt
|
||||||
|
def symlink(src, dest):
|
||||||
|
if os.name == 'nt':
|
||||||
|
_symlink_windows(src, dest)
|
||||||
|
else:
|
||||||
|
os.symlink(src, dest)
|
Loading…
Add table
Add a link
Reference in a new issue