Dacht dat ik het hier al eens geplaatst heb, maar hier is het.
Er staat redelijk veel uitleg in, maar als je vragen hebt hoor ik het wel.
Ik heb het poortnummer van SickRage en de APIkey er even uitgehaald, die moet je van je eigen SickRage installatie overnemen.
#
# This script is ment to be called from AutoSub as a postprocess when a sub is downloaded.
# The script is tested on a windows enviroment and on a synology enviroment
# It will expect the following arguments:
# argv[1]: Path + filename of the subtitle file
# argv[2]: Path + filename of the video file
# argv[3]: Language of the subtile (not used)
# argv[4]: Name of the Serie (not used)
# argv[5]: Season number of the serie
# argv[6]: Episode number of the serie
#
# The videofile and the subfile will be moved from where sickbeard put them
# to a new location accessble from my mediaplayer and will get a more userfriendly name like below
# /volume1/video/Alleen Series/Arrow/Seizoen 01/01. Pilot.mkv
# /01. Pilot.srt
# /02. Honor Thy Father.mkv
# /02. Honor Thy Father.srt
# /03. Lone Gunmen.mkv
# /03. Lone Gunmen.srt
# / etc .....
# /volume1/video/Alleen Series/Arrow/Seizoen 02/01. City of Heroes.mkv
# /01. City of Heroes.srt
# /02. Identity.mkv
# /02. Identity.srt
# / etc .....
# This script can easly be adapted to also move things like a folder thumbnail, episode thumbnail etc.
# After this rename-ing and moving the status in sickbeard will be updated to "Archived"(e.g. out of control of sickbeard)
# Information about the sickbeadr api can be found here: http://sickbeard.com/api/
# Sickbeard has a build in api-builder which can be invoked via the browser http://localhost:8083/api/builder
# substitute 'localhost' and 'port' with your the 'ipadress' and 'port' of your sickbeard installation and fill in the generated api-key of sickbeard
import os, urllib, json, sys, shutil, datetime, codecs
#-----------------------------------------------------------------------
# Some static information for easy changing
# The "src_loc" part of the files will be changed into "dest_loc" for the move operation
#-----------------------------------------------------------------------
src_loc = '/volume1/sync/TvSeries'
dst_loc = '/volume1/sync/Alleen Series'
HistoryLoc = '/volume1/sync/AutoSub/AutoSubPostProcessHistory.csv'
# List of characters not allowed in a filename
not_allowed_table = dict.fromkeys(map(ord, '/\:*?"<>|'), None)
# Sickbeard Commands and information
# If sickbeard is located on the another machine replace "localhost" with the IPnumber of that machine
# The portnumber is the port sickbeard is using
# The apikey must be generated by your own sickbeard installation
IpAdress = "localhost"
port = ""
ApiKey = ""
# Copy the arguments from AutoSub to variables for easy changing
sub = sys.argv[1]
video = sys.argv[2]
Season = sys.argv[5]
Episode = sys.argv[6]
#sys.stdout.write("Postprocess routine started")
#sys.stdout.flush()
# Opening for Append of the history file so we will know want the original filename was in case we have to search for a better subfile
if not os.path.isfile(HistoryLoc):
HistoryFile = codecs.open(HistoryLoc,encoding='cp1252',mode='a')
HistoryFile.write('"Datum/Tijd","Location","Title","Original Filename"\r\n')
else:
HistoryFile = codecs.open(HistoryLoc,encoding='cp1252',mode='a')
# Here we create the new location for the sub file
# First we find the Serie name.
# I decided to use the directoryname because that is created by sickbeard and is exactly equal to the seriename as used by sickbeard.
# So we get the name from the last directory of the full path
# In case of the serie "Marvel Agents of S.H.I.E.L.D." we have to add an extra dot to the name because a directory name cannot end with a dot and is removed when creating the direcory by sickbeard
# but sickbeard uses the name including the last dot.
# We also change the filenames into easy names (like : %E. EpisodeTitle.mkv and %E. EpisodeTitle.srt)
Serie = video.split("/")[-2]
if 'Marvel' in Serie:
Serie = Serie + '.'
# Here we use the urllib to open a stream from sickbeard with the "Shows" command
# We get a structure with info about all shows in sickbeard
# Fist we open the stream with the correct command (see sickbeard API documentation)
# Then we read the number of bytes we have to read and convert the resulting json structure in a python structure.
GetShowList = "http://" + IpAdress + ":" + port + "/api/" + ApiKey + "/?cmd=shows&sort=name"
fp = urllib.urlopen(GetShowList)
headers = fp.info()
bytecount = int(headers['content-length'])
SickbeardShows = json.loads(fp.read(bytecount))
fp.close
if (SickbeardShows['result'] != "success") :
sys.stdout.write("Connection with Sickbeard failed.")
sys.stdout.flush()
sys.exit(1)
else:
Tvdbid = str(SickbeardShows['data'][Serie]['indexerid'])
fp= urllib.urlopen("http://localhost:" + port + "/api/" + ApiKey + "/?cmd=episode&indexerid=" + Tvdbid + "&season=" + Season + "&episode=" + Episode)
headers = fp.info()
bytecount = int(headers['content-length'])
EpisodeInfo = json.loads(fp.read(bytecount))
fp.close
if (EpisodeInfo['result'] != "success") :
sys.stdout.write(" - No information on this episode available from sickbeard. ")
sys.stdout.flush()
sys.exit(1)
else :
EpisodeName = EpisodeInfo['data']['name'].translate(not_allowed_table)
# All info is gathered and we build the destination path for the file moving operation
# - Split the source in a path and a filename
# - Retrieve the file extension of the video file
# - Construct the new locations
# - Create a "Seizoen" subdirectory if not yet present
src_path, video_file = os.path.split(video)
temp, video_ext = os.path.splitext(video_file)
SeasonPath = src_path.replace(src_loc, dst_loc,1) + "/" + "Seizoen " + Season
Episode_Title = unicode(Episode + ". " + EpisodeName + video_ext)
video_dst = unicode(SeasonPath + "/" + Episode_Title)
sub_dst = video_dst.replace(video_ext, ".srt")
# Now we check wether the "Seizoen" directory already exists
# if not we create it.
if not os.path.isdir(SeasonPath):
os.mkdir(SeasonPath)
# Here we move the files the new location.
# We use shutil.move instead of os.rename so the destination can also be another disk or network location.
if not os.path.isfile(sub_dst):
# move the subfile
shutil.move(sub,sub_dst)
if not os.path.isfile(video_dst):
shutil.move(video,video_dst)
LogTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
HistoryFile.write('"' + LogTime + '"' + ',"' + SeasonPath + "/" + '"' + ',"' + Episode_Title + '"'+ ',"' + video_file + '"\r\n')
SetStatus = "http://" + IpAdress + ":" + port + "/api/" + ApiKey + "/?cmd=episode.setstatus&indexerid=" + Tvdbid + "&season=" + Season + "&episode="+ Episode + "&status=archived&force=1"
fp= urllib.urlopen(SetStatus)
headers = fp.info()
bytecount = int(headers['content-length'])
StatusResult = json.loads(fp.read(bytecount))
fp.close
if StatusResult['result'] != "success" :
sys.stdout.write("Status change in Sickbeard Failed!")
sys.stdout.flush()
sys.exit(1)
else:
sys.stdout.write( " " + video_dst + "Already exists!")
sys.stdout.flush()
sys.exit(1)
else:
sys.stdout.write(" " + sub_dst + " Already exists!")
sys.stdout.flush()
sys.exit(1)
HistoryFile.close()
sys.stdout.write("Postprocess routine finished")
sys.stdout.flush()
sys.exit(0)