How To: Send emails when FileBot renames or moves files.

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
Evo
Posts: 16
Joined: 30 Jan 2012, 19:54

How To: Send emails when FileBot renames or moves files.

Post by Evo »

Well, for this one we can thanks rednoah for fixing it up.
I'm using my Gmail account, however you can probably use SMTP server you want.

1) Download filebot 2.6 from here: http://filebot.sourceforge.net/#download
2) Install it or upgrade the older version
3) Download a program called SendEmail from: here or from DropBox
4) Extract the SendEmail.exe into C:\Windows\System32
5) Create a file called email.groovy and put it in the directory of Filebot
6) Open email.grooxy and copy the following code into it:

Code: Select all

def updateFreq = 5 * 60 * 1000

Thread.startDaemon 
{
	while (sleep(updateFreq) || true) {
	def groups = args.getFiles().groupBy{
	def tvs = detectSeriesName(it)
	def mov = detectMovie(it, false)
	println "$it.name [series: $tvs, movie: $mov]"
	
	// DECIDE EPISODE VS MOVIE (IF NOT CLEAR)
	if (tvs && mov) {
		if (it.name =~ "(?i:$tvs - .+)" || parseEpisodeNumber(it.name) || parseDate(it.name)) {
			println "Exclude Movie: $mov"
			mov = null
		} else if (detectMovie(it, true)) {
			println "Exclude Series: $tvs"
			tvs = null
		}
	}
	return [tvs:tvs, mov:mov]
}

groups.each{ group, files ->
	// EPISODE MODE
	if (group.tvs && !group.mov) {
args.eachMediaFolder() {
//CHANGE THE FORMAT AS YOU WISH
   def paths = rename(file:files, format:'H:/TV/{n}/Season {s}/{n} Season {s} Episode {e} {t}', db:'thetvdb', strict:false)
   paths.each {
      def msg = "${it.name} has been added to the library"
//EDIT YOUR DETAILS
      execute("sendemail.exe", "-f", "[b]FROM EMAIL ADDRESS[/b]", "-t", "[b]TO EMAIL ADDRESS[/b]", "-s", "[b]SMTP SERVER:PORT[/b]", "-xu", "[b]SMTP USERNAME[/b]", "-xp", "[b]SMTP PASSWORD[/b]", "-u", msg, "-m", msg)
   }
   
}	}
	
	// MOVIE MODE
	if (group.mov && !group.tvs) {
args.eachMediaFolder() {
//CHANGE THE FORMAT AS YOU WISH
   def paths = rename(file:files, format:'G:/Videos/Films/{n} ({y})', db:'TheMovieDB', strict:false)
      paths.each {
      def msg = "${it.name} has been added to the library"
//EDIT YOUR DETAILS
      execute("sendemail.exe", "-f", "[b]FROM EMAIL ADDRESS[/b]", "-t", "[b]TO EMAIL ADDRESS[/b]", "-s", "[b]SMTP SERVER:PORT[/b]", "-xu", "[b]SMTP USERNAME[/b]", "-xp", "[b]SMTP PASSWORD[/b]", "-u", msg, "-m", msg)
   }

}	}
}

}
}
println "Press ENTER to abort"
console.readLine()
For example:
From Email Address = [email protected]
To Email Address = [email protected]
SMTP Server & Port = smtp.gmail.com:587
SMTP Username = [email protected]
SMTP Password = Password123

Now, when you call Filebot you need to call this script with it. If it renames the media then it will send you an email saying:
<Media Name> has been added to the library
and it will store them in:
G:/TV/<Show Name>/<Season Number>/<Show> <Season Number> <Episode Number> - <Episode Name>
For example. I call it using

Code: Select all

filebot -script "C:/Program Files/Filebot/email.groovy" -trust-script
Last edited by Evo on 04 May 2012, 18:20, edited 4 times in total.
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How To: Send emails when FileBot renames or moves files

Post by rednoah »

Nope, all done in the script right there! :D

Code: Select all

def paths = rename(folder:it)
Now that I see you use -rename as well as -script. If -script is specified it only runs the script, setting -rename doesn't do anything. All the other parameters like --format serve as default values and are not overriden by the rename() call.
:idea: Please read the FAQ and How to Request Help.
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How To: Send emails when FileBot renames or moves files

Post by rednoah »

Yep that'll work fine. Btw do u really wanna send a mail for each file? Wouldn't it be better to just send one per rename batch?
:idea: Please read the FAQ and How to Request Help.
Evo
Posts: 16
Joined: 30 Jan 2012, 19:54

Re: How To: Send emails when FileBot renames or moves files

Post by Evo »

rednoah wrote:Yep that'll work fine. Btw do u really wanna send a mail for each file? Wouldn't it be better to just send one per rename batch?
I think for some people, having one mail per rename batch would be a better solution. eg.

Subject: New files have been added to the media library
Text: The following files have been added to the media library:
Lost Season 1 Episode 1 - Pilot.avi
Homeland Season 1 Episode 1.avi

however what I would like to do is to execute Filebot once a download has completed. Not sure how I would go about either of these tbh. I assume the groovy script has a "per batch" option?
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How To: Send emails when FileBot renames or moves files

Post by rednoah »

rename(...) is called for eachMediaFolder, so you already have the batch right there. Just dont run sendmail for each output path but do something like this.

def paths = rename(...)
def msg = paths.name.join('\n')
sendmail msg...

Get it? Even simpler than per-path.
:idea: Please read the FAQ and How to Request Help.
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How To: Send emails when FileBot renames or moves files

Post by rednoah »

As for post-download automation you'll need to check your torrent app or whatever. They can all run some command when download finishes. Or you can specify a folder for completed downloads that you can have FileBot watch via scripting.
:idea: Please read the FAQ and How to Request Help.
Evo
Posts: 16
Joined: 30 Jan 2012, 19:54

Re: How To: Send emails when FileBot renames or moves files

Post by Evo »

How can I modify this to detect films, rename them and move them to a different folder to the TV Shows?

Also, im not sure it differentiates properly.
Ie. Sherlock Holmes A Game of Shadows gets detected as a TV show and put into the TV Shows folder with a random episode name
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How To: Send emails when FileBot renames or moves files

Post by rednoah »

Look at the script, it doesn't differentiate, always works in tvdb mode. Check out the sortivo script for that.
:idea: Please read the FAQ and How to Request Help.
Evo
Posts: 16
Joined: 30 Jan 2012, 19:54

Re: How To: Send emails when FileBot renames or moves files

Post by Evo »

Hmmm, seem unable to get this to work. Any ideas?

Code: Select all

def updateFreq = 5 * 60 * 1000

Thread.startDaemon 
{
	while (sleep(updateFreq) || true) {
	
def groups = args.getFiles().groupBy{
	def tvs = detectSeriesName(it)
	def mov = detectMovie(it, false)
	println "$it.name [series: $tvs, movie: $mov]"
	
	// DECIDE EPISODE VS MOVIE (IF NOT CLEAR)
	if (tvs && mov) {
		if (it.name =~ "(?i:$tvs - .+)" || parseEpisodeNumber(it.name) || parseDate(it.name)) {
			println "Exclude Movie: $mov"
			mov = null
		} else if (detectMovie(it, true)) {
			println "Exclude Series: $tvs"
			tvs = null
		}
	}
	return [tvs:tvs, mov:mov]
}

groups.each{ group, files ->
	// EPISODE MODE
	if (group.tvs && !group.mov) {
args.eachMediaFolder() {
   def paths = rename(folder:it, format:'H:/TV/{n}/Season {s}/{n} Season {s} Episode {e} {t}', db:'thetvdb', strict:false)
   
   // log rename history
   paths.each {
      def msg = "${it.name} has been added to the library"
      execute("sendemail.exe", "-f", "[email protected]", "-t", "[email protected]", "-s", "smtp.gmail.com:587", "-xu", "[email protected]", "-xp", "x", "-u", msg, "-m", msg)
   }
}	}
	
	// MOVIE MODE
	if (group.mov && !group.tvs) {
args.eachMediaFolder() {
   def paths = rename(folder:it, format:'G:/Videos/Films/{n} ({y})', db:'TheMovieDB')
   
   // log rename history
   paths.each {
      def msg = "${it.name} has been added to the library"
      execute("sendemail.exe", "-f", "[email protected]", "-t", "[email protected]", "-s", "smtp.gmail.com:587", "-xu", "[email protected]", "-xp", "x", "-u", msg, "-m", msg)
   }
}	}
}


}
}
println "Press ENTER to abort"
console.readLine()
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How To: Send emails when FileBot renames or moves files

Post by rednoah »

You just copy & pasted things together without understanding how sortivo works. You only need to add the email code after the rename calls. Dont do all at once if you don't know what you're doing. Add things slowly step by step.
:idea: Please read the FAQ and How to Request Help.
Evo
Posts: 16
Joined: 30 Jan 2012, 19:54

Re: How To: Send emails when FileBot renames or moves files

Post by Evo »

Edit: Silly me. Ok will try getting it working with a daemon now
Evo
Posts: 16
Joined: 30 Jan 2012, 19:54

Re: How To: Send emails when FileBot renames or moves files.

Post by Evo »

Updated the script to differentiate between movies and tv shows.
Maxxodd
Posts: 23
Joined: 27 May 2012, 21:01

Re: How To: Send emails when FileBot renames or moves files.

Post by Maxxodd »

Anyone know if I can just append this to the end of the rednoah's "utorrent-postprocess.groovy" script?
def msg = "${it.name} has been added to the library"
//EDIT YOUR DETAILS
execute("sendemail.exe", "-f", "FROM EMAIL ADDRESS", "-t", "TO EMAIL ADDRESS", "-s", "SMTP SERVER:PORT", "-xu", "SMTP USERNAME", "-xp", "SMTP PASSWORD", "-u", msg, "-m", msg)
I have tried as is (substituting my gmail smtp details) and it doesn't work for me.
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How To: Send emails when FileBot renames or moves files.

Post by rednoah »

You can't "just append" it. Each rename() call will return the output fileset and then you can just run sendmail.exe with those filenames. It's quite easy. Just put it after the rename() call.
:idea: Please read the FAQ and How to Request Help.
Maxxodd
Posts: 23
Joined: 27 May 2012, 21:01

Re: How To: Send emails when FileBot renames or moves files.

Post by Maxxodd »

rednoah wrote:You can't "just append" it. Each rename() call will return the output fileset and then you can just run sendmail.exe with those filenames. It's quite easy. Just put it after the rename() call.
groups.each{ group, files ->
// fetch subtitles
def subs = getMissingSubtitles(file:files, output:"srt", encoding:"utf-8")
if (subs) files += subs

// EPISODE MODE
if (group.tvs && !group.mov) {
def dest = rename(file:files, format:'TV Shows/{n}/{episode.special ? "Special" : "Season "+s}/{n} - {episode.special ? "S00E"+special.pad(2) : s00e00} - {t}', db:'TheTVDB')
if (dest) {
dest.mapByFolder().keySet().each{ dir ->
println "Fetching artwork for $dir from TheTVDB"
def query = group.tvs
def sxe = dest.findResult{ parseEpisodeNumber(it) }
def options = TheTVDB.search(query)
if (options.isEmpty()) {
println "TV Series not found: $query"
return
}
options = options.sortBySimilarity(query, { it.name })
fetchSeriesArtworkAndNfo(dir.dir, dir, options[0], sxe && sxe.season > 0 ? sxe.season : 1)
:arrow: Here?
}
}
}
Are you saying that placing it here (where the yellow/black arrow is above) would be the appropriate place for TV shows? P.S. I know I probably sound like an idiot. I can usually fumble through things, but this Java/groovy is really difficult for me to get even the faintest understanding of.
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How To: Send emails when FileBot renames or moves files.

Post by rednoah »

No. That's the loop that fetches artwork. I recommend doing a basic groovy tutorial.

Consider this:
def paths = rename(...)
paths.each{ f ->
println f.name
}
Once you understand this the rest will be easy. Do some small experiments before you start modifying to big script.
:idea: Please read the FAQ and How to Request Help.
Maxxodd
Posts: 23
Joined: 27 May 2012, 21:01

Re: How To: Send emails when FileBot renames or moves files.

Post by Maxxodd »

rednoah wrote:No. That's the loop that fetches artwork. I recommend doing a basic groovy tutorial.

Consider this:
def paths = rename(...)
paths.each{ f ->
println f.name
}
Once you understand this the rest will be easy. Do some small experiments before you start modifying to big script.
Thank you. I'll work on it. Are there any books that you would recommend that are introductory in nature? I'd like to learn more, but don't have time to take a class. I can usually read a few pages here and there though and get somewhere over time.
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How To: Send emails when FileBot renames or moves files.

Post by rednoah »

You'll be fine with doing a beginners tutorial online for 2-3h. ;)
:idea: Please read the FAQ and How to Request Help.
Post Reply