Using regexp on iTunes track name

Hi,

I wrote a script to work with iTunes.
It looks at thetvdb.com database to get the episode title and episode description based on the serie name / season number / episode number

the video files are usually name like this:

Serie.S01E01.ext or Serie - 1x01.ext or Serie.101.ext

I’d like to be able to get the show name, season number and episode number from the filename (which is the track name in iTunes) instead of having to enter it manually

I’m not sure of the regexp pattern and I don’t know how to used them

serie.1x09 ->> [\._ \-]([0-9]+)x([0-9]+)[^\\/]*

serie, s01e01, serie.s01.e01, seire.s01-e01- >> [\._ \-][Ss]([0-9]+)[\.\-]?[Ee]([0-9]+)[^\\/]*

serie.103 ->> [\._ \-]([0-9]+)([0-9][0-9])[\._ \-][^\\/]*

Is Regexp a good choice for what I have to do?

How to used them in my case?

Thanks

Model: iMac 24" alu 2.8Ghz
AppleScript: 2.0.1
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)

Hi,

what’s about a pure AppleScript solution


set theFiles to {"Serie.S02E06.ext", "Serie - 1x01.ext", "Serie.312.ext"}

set {epList, snList} to {{}, {}}
repeat with oneFile in theFiles
	set t to text 7 thru -5 of oneFile
	considering case
		if t contains "S" and t contains "E" then
			tell t to set {season, episode} to {text 2 thru 3, text 5 thru 6}
		else if t contains "x" then
			set t to text ((offset of " " in t) + 1) thru -1 of t
			set {TID, text item delimiters} to {text item delimiters, "x"}
			set {season, episode} to text items of t
			set text item delimiters to TID
		else
			if (count t) = 3 then
				tell t to set {season, episode} to {text 1, text -2 thru -1}
			else
				tell t to set {season, episode} to {text 1 thru 2, text -2 thru -1}
			end if
		end if
	end considering
	set {end of snList, end of epList} to {season as integer, episode as integer}
end repeat

The thing is I don’t know the position of the “S01E02” or “1x02” or “102” from the beginning or the end the string

I know they are in the name but not the exact position it can be:

Lost.S01E02.ext
or
How.i.met.your.mother.S01E02.title.of.this.episode.ext

Using TextCommands:

property kVideoNamePatterns : "
	(.+?)\\.S([0-9]+)E([0-9]+) # matches e.g. 'Serie.S01E01.ext'
	|
	(.+?)\\s*-\\s*([0-9]+)x([0-9]+) # matches e.g. 'Serie - 1x01.ext'
	|
	(.+?)\\.([0-9]+)([0-9]{2}) # matches e.g. 'Serie.101.ext'
	"

on splitVideoFileName(txt)
	tell application "TextCommands"
		set allMatches to search txt for kVideoNamePatterns with regex and comments allowed
	end tell
	if allMatches is {} then error "Unknown format."
	set theMatches to item 1 of allMatches
	repeat with i from 1 to 7 by 3
		set {showName, seasonNo, episodeNo} to items i thru (i + 2) of theMatches
		if showName is not "" then exit repeat
	end repeat
	return {showName, seasonNo as integer, episodeNo as integer}
end splitVideoFileName

-- TEST
set testData to {"Serie.S01E01.ext", "Serie - 1x01.ext", "Serie.101.ext"}
repeat with itemRef in testData
	set contents of itemRef to splitVideoFileName(itemRef)
end repeat
testData --> {{"Serie", 1, 1}, {"Serie", 1, 1}, {"Serie", 1, 1}}

If you need help getting to grips with regular expressions there are plently of online tutorials and various books available on the subject. e.g. I believe Jeffery Friedl’s “Mastering Regular Expressions” (3rd edition, O’Reilly) is very well regarded.

That’s exactly what I needed.

Thanks it’s working really great