Miran's TheTVDB Renamer

This rename script tries to get the Episode Title of TV-Shows from theTVDB.com and adds it to the File-Name.

Warnings:

This script is very basic, therefore there is no tolerance regarding the directory structure and file naming. And at the moment there is no error handling at all. If there is a problem the script will just fail.

Feel free to use, optimize and enhance the script. Please post your changes in this thread of the DOpus Resource Centre forum.

Notes:

If you are not using English series and episode titles, edit this line in the script:

TVDB_language = "en" ' "en" for English, "de" for Deutsch, etc.

The script assumes the series title is the second-last folder in the path. e.g. season 2 of The Simpsons would be files stored under D:\TV\The Simpsons\S02. If you do not use season folders and keep the whole series flat, switch which of these two lines are commented out:

	regExp.Pattern = ".*\\(.*)\\.*" ' < This uses the SECOND-LAST folder as the series title.
'	regExp.Pattern = ".*\\([^\\]+)" ' < This uses the LAST folder as the series title.

Many Thanks go to the thetvdb.com community for this great service.

Downloads:

V0.5 fixed by @eyebex:

Original v0.5 without fix (does not work with current TVDB API):

Script Code:

If you just want to use the script, see the Downloads section above. The script code is reproduced here to help people browsing the forum for scripting techniques.

Option Explicit

' ************************** Script Information **************************
' Mirans TheTVDB Renamer v0.5 (modified by eyebex)
' (tested with Directory Opus 10.0.1.0)
' by /^\iran 2010-12-30
' ************************************************************************
'
' ***************************** Installation *****************************
' Download the .orp file. Open the Directory Opus rename dialog and set
' it to advanced mode. Select import from the file menu and open the .orp
' file. You can then add it to the presets with the add button.
' ************************************************************************

' **************************** Instructions ******************************
' This script tries to get the episode title of TV shows from TheTVDB.com
' and adds it to the file name.
' The script expects a directory structure like this:
' <series title>\<season>\<filename with season and episode>
' e.g. "How I met your mother\S01\S01E01.avi"
'
' This script is very basic, therefore there is no tolerance regarding
' the directory structure and file naming. And at the moment there is no
' error handling at all. If there is a problem the script will just fail.
'
' Feel free to use, optimize and enhance the script. Please post your
' changes in the Directory Opus Resource Centre forum thread where you
' got this script from.
'
' Many thanks go to the TheTVDB.com community for this great service.
' ************************************************************************


' ************************************************************************
' *                               Options                                *
' ************************************************************************

Dim TVDB_APIKey, TVDB_Url, TVDB_language

TVDB_Url = "http://www.thetvdb.com/"
TVDB_language = "en" ' "en" for English, "de" for Deutsch, etc.
TVDB_APIKey = "..." ' See download for the key, or generate your own.

' ************************************************************************
' *                               System                                 *
' ************************************************************************

Const LOG_DEBUG = 0
Const LOG_INFO	= 1
Const LOG_WARN	= 2
Const LOG_ERR	= 3
Const LOG_ALERT	= 4
Dim LogLevel
LogLevel = LOG_INFO

' ************************************************************************
' *                             Functions                                *
' ************************************************************************

Function Rename_GetNewName ( strFileName, strFilePath, fIsFolder, strOldName, ByRef strNewName )
	Dim regExp
	Dim SeriesTitle, SeasonNum, EpisodeNum, strippedSeriesTitle, strippedSeasonNum, strippedEpisodeNum, FileExtension, EpisodeTitle

	Set regExp = New RegExp
	regExp.IgnoreCase = True
	regExp.Global = False
	regExp.Pattern = ".*S([0-9][0-9])E([0-9][0-9])(.*)\.(.*)"

	SeasonNum = regExp.Replace(strFileName, "$1")
	EpisodeNum = regExp.Replace(strFileName, "$2")
	FileExtension = regExp.Replace(strFileName, "$4")

	regExp.Pattern = ".*\\(.*)\\.*" ' < This uses the SECOND-LAST folder as the series title.
'	regExp.Pattern = ".*\\([^\\]+)" ' < This uses the LAST folder as the series title.
	SeriesTitle = regExp.Replace(strFilePath, "$1")
		
	regExp.Pattern = "(Dr. )(.*)"
	strippedSeriesTitle = regExp.Replace(SeriesTitle, "$2")

	regExp.Pattern = "(0)([0-9]*)"
	strippedSeasonNum = regExp.Replace(SeasonNum, "$2")

	regExp.Pattern = "(0)([0-9]*)"
	strippedEpisodeNum = regExp.Replace(EpisodeNum, "$2")

	Logger LOG_DEBUG, "Series title: " & SeriesTitle & " (" & strippedSeriesTitle & ")"
	Logger LOG_DEBUG, "Season number: " & strippedSeasonNum
	Logger LOG_DEBUG, "Episode number: " & strippedEpisodeNum
	Logger LOG_DEBUG, "File extension: " & FileExtension

	EpisodeTitle = getEpisodeTitleFromTVDB ( strippedSeriesTitle, strippedSeasonNum, strippedEpisodeNum)
	strNewName = ReplaceIllegalCharacters(SeriesTitle & " - S" & SeasonNum & "E" & EpisodeNum & " - " & EpisodeTitle & "." & FileExtension)
End Function

Function getEpisodeTitleFromTVDB ( seriesTitle, SeasonNum, EpisodeNum )
	Dim xmlDoc, url, xmlNodeList, xmlNode, xmlElement, TVDB_SeriesId, TVDB_MirrorUrl, TVDB_EpisodeTitle

	Set xmlDoc = CreateObject("Msxml2.DOMDocument")
	xmlDoc.async = False

	url = TVDB_Url & "/api/" & TVDB_APIKey & "/mirrors.xml"
	xmlDoc.load(url)
	Set xmlNodeList = xmlDoc.getElementsByTagName("mirrorpath")
	xmlElement = xmlNodeList.item(0).Text
	TVDB_MirrorUrl = xmlElement
	Logger LOG_DEBUG, "Using mirror: " & TVDB_MirrorUrl
	
	url = TVDB_MirrorUrl & "/api/GetSeries.php?seriesname=" & seriesTitle
	xmlDoc.load(url)
	Set xmlNodeList = xmlDoc.getElementsByTagName("seriesid")
	TVDB_SeriesId = xmlNodeList.item(0).Text
	Logger LOG_DEBUG, "Series ID: " & TVDB_SeriesId

	url = TVDB_MirrorUrl & "/api/" & TVDB_APIKey & "/series/" & TVDB_SeriesId & "/all/" & TVDB_Language & ".xml"
	xmlDoc.load(url)
	Set xmlNodeList = xmlDoc.getElementsByTagName("Episode")
	For Each xmlNode In xmlNodeList
		If (xmlNode.getElementsByTagName("SeasonNumber").item(0).Text=SeasonNum) And (xmlNode.getElementsByTagName("EpisodeNumber").item(0).Text=EpisodeNum) Then
			TVDB_EpisodeTitle = xmlNode.getElementsByTagName("EpisodeName").item(0).Text
			Exit For
		End If
	Next
	Logger LOG_DEBUG, "Episode title: " & TVDB_EpisodeTitle

	getEpisodeTitleFromTVDB = TVDB_EpisodeTitle
End Function

Function ReplaceIllegalCharacters ( strToClean )
	Dim objRegExp, outputStr
	Set objRegExp = New Regexp

	objRegExp.IgnoreCase = True
	objRegExp.Global = True
	objRegExp.Pattern = "[(?*""\\<>#~%{}+_@:\/;]+"
	outputStr = objRegExp.Replace(strtoclean, "_")

	objRegExp.Pattern = "\-+"
	outputStr = objRegExp.Replace(outputStr, "-")

	ReplaceIllegalCharacters = outputStr
End Function

Function Logger( Level, strMessage )
	If Level>=LogLevel Then
		Dopus.OutputString strMessage
	End If
End Function

'Test without DOpus
'Dim NewName
'Rename_GetNewName "S02E03.avi", "D:\The Simpsons\S02" , false, "S02E03.avi", newName
'MsgBox NewName

See Also:

Thanks much.

I've been evaluating WE replacements and naturally landed here. I am most impressed with the plugin support (and active community) that DOPUS has.
I took some episodes of a TV show I had and ... Wow, just wow!

So I have to thank you for starting me down this trail...

Thanks a lot for the script, Miran. I've modified it as the TVDB API has changed and it seems there's no single URL for an episode anymore. Also, I've slightly customized it so it can properly rename German Dr. House episodes in particular.

[Customized version has been merged into the first post.]