Need help getting {fn} to work with sendemail.exe

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
FatBasta
Donor
Posts: 11
Joined: 31 Oct 2013, 19:07

Need help getting {fn} to work with sendemail.exe

Post by FatBasta »

Hello to everyone first! This is my first post. I have been dabbling with FileBot for a couple of days now and I'm trying to make a custom script for myself.

My scripts operation:
1. Watches my temp folder.
2. Runs as hidden.
3. Detects if it's a movie, tv show or anime,
4. Downloads subtitles.
5. Moves to correct media folders.
6. Sends email to inform me of new media.

The script is mostly copy/paste of other peoples fine work. I have some rudimentary knowledge of coding but am a novice in this fine art.

The problem I am having is with the email part of my script, I can get it to send me an email, but I just cannot figure out how to get it to include the filename in the message body.

Email part of my script:

Code: Select all

// send email
def msg = "{fn}" //<-- Where I want {fn} filename or equivalent.
execute("sendemail.exe", "-f", "[email protected]", "-t", "[email protected]", "-s", "smtp.server.com:25", "-u", "New episode to watch", "-m", msg)
This part runs after rename() and it will send me an email but with only {fn} as the message. If I take the quotation marks out it will send me some gibberish like "Script3$_run_closure1_closure9_closure16@e527bd7" (gibberish to me at least)
The original coder had used "def msg = "${it.name} has been added to the library"", but if I use ${it.name} my script doesn't send me a email at all.

Any help or suggestions would be welcomed.
User avatar
rednoah
The Source
Posts: 23938
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need help getting {fn} to work with sendemail.exe

Post by rednoah »

{fn} is format template for the rename call, not script code. {fn} and the likes is only internally available within the rename call.

What you have is the filenames before, and the filenames after. Add some random println and see what the variables are. Wherever you're calling rename() you gotta have a folder/filelist. That's where you know the original file paths.
:idea: Please read the FAQ and How to Request Help.
FatBasta
Donor
Posts: 11
Joined: 31 Oct 2013, 19:07

Re: Need help getting {fn} to work with sendemail.exe

Post by FatBasta »

After fiddling around for some time, I have it kinda working
Instead of getting just the filename after the rename process I got it to send me the filename and full path to the original file. This is good enough for me at this point as I still get notified of what has been added.
And there's one MAJOR problem with the script, you get an email for every .rar packet.... so not that great.

My full script for those who are interested:

Code: Select all

//filebot -script fatty.groovy -extract -rename -get-subtitles d:/temp/ --lang en -non-strict

    // Input Folders
def episodeDir    = '''D:/temp/TV Shows'''
def movieDir      = '''D:/temp/Movies'''
def animeDir      = '''D:/temp/Anime'''

    // Output Folders and format 
def episodeFormat = '''D:/TV Shows/{n}/Season {s}/{fn}'''
def movieFormat   = '''D:/Movies/{n}/{fn}'''
def animeFormat   = '''D:/Anime/{primaryTitle}/{fn}'''


//START ALL THE AUTOMATIONS!!!

// watch folders and print files that were added/modified
def watchman = args.watch { changes ->
    // extract all
    if (_args.extract)
        changes += extract(file:changes.findAll{ it.isArchive() }, output:'.')
    
    // subtitles for all
    if (_args.getSubtitles)
        changes += getMissingSubtitles(file:changes.findAll{ it.isVideo() }, output:'srt')
    
    // rename all
    if (_args.rename)
episodeDir.getFolders{ it.hasFile{ it.isVideo() } }.each{ dir ->
    println "Processing $dir"
    def files = dir.listFiles{ it.isVideo() }
    
    // sort episodes
    rename(file:changes, db:'TheTVDB', format:episodeFormat)
        execute("sendemail.exe", "-f", "[email protected]", "-t", "[email protected]", "-s", "smtp.server.com:port", "-u", "Email title for TV Shows", "-m", files)
    }

movieDir.getFolders{it.hasFile{ it.isVideo() } }.each{ dir ->
    println "Processing $dir"
    def files = dir.listFiles{ it.isVideo() }
   
    // sort movies
    rename(file:changes, db:'TheMovieDB', format:movieFormat)
        execute("sendemail.exe", "-f", "[email protected]", "-t", "[email protected]", "-s", "smtp.server.com:port", "-u", "Email title for movies", "-m", files)
    
}

animeDir.getFolders{it.hasFile{ it.isVideo() } }.each{ dir ->
    println "Processing $dir"
    def files = dir.listFiles{ it.isVideo() }
        
    // sort anime 
    rename(file:changes, db:'AniDB', format:animeFormat)
        execute("sendemail.exe", "-f", "[email protected]", "-t", "[email protected]", "-s", "smtp.server.com:port", "-u", "Email title for anime", "-m", files)
}    
}

watchman.commitDelay = 5 * 1000            // default = 5s
watchman.commitPerFolder = true            // default = true

println "Waiting for events"
if (console) { console.readLine() } else { sleep(Long.MAX_VALUE) } // keep running and watch for changes
I use this script with a seedbox with automated rss downloads, automated ftp download to get files from the seedbox and plex media server.

edit: I forgot to write about that MAJOR problem... my bad...

Most of the code is "stolen" from rednoahs watcher.groovy script and Evos email.groovy. So thank you guys!
Last edited by FatBasta on 01 Nov 2013, 19:03, edited 1 time in total.
User avatar
rednoah
The Source
Posts: 23938
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need help getting {fn} to work with sendemail.exe

Post by rednoah »

This won't work.

You start by watching args, and the { ... } block will be called for each change set, which could be any new file in your /temp, but then you force renaming the change set via TVDB, multiple times actually, on the same files (which won't exist after the first run). When trying understand any code, first make sure where the variables come from.


This is what you do:
1. Modify the watcher.groovy script and add your email line. changes is the variables with files, so the message you wanna send will be changes.join('\n') => i.e. you only add 1 line
2. Then call your watcher.groovy script 3 times with different cmdline options for Movie/TV/Anime types

EDIT: It'll look like this => http://pastebin.com/5LpbSpf6
:idea: Please read the FAQ and How to Request Help.
FatBasta
Donor
Posts: 11
Joined: 31 Oct 2013, 19:07

Re: Need help getting {fn} to work with sendemail.exe

Post by FatBasta »

Hmm...

It does sort my stuff like I want it to.
I did have watcher.groovy script called in three different calls but I didn't like that it ran java three times. All java.exe processes were hogging over 100mb of memory a piece. That is why I wanted one script to watch three directories at the same time.
FatBasta
Donor
Posts: 11
Joined: 31 Oct 2013, 19:07

Re: Need help getting {fn} to work with sendemail.exe

Post by FatBasta »

So, I updated my code with changes.join('\n')

so now it's:

Code: Select all

execute("sendemail.exe", "-f", "[email protected]", "-t", "[email protected]", "-s", "smtp.server.com:port", "-u", "Email title for anime", "-m", changes.join('\n'))
This fixed the multiple emails when adding .rar files. So thank you!!

For me this script works, one script, one java.exe, three folders.
User avatar
rednoah
The Source
Posts: 23938
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need help getting {fn} to work with sendemail.exe

Post by rednoah »

Do it like this:
http://pastebin.com/5LpbSpf6

If you wanna watch 3 folders you'd be doing 3 times <folder>.watch { do something }


PS: your script will force all files to be episodes, did you ever test with a movie?
:idea: Please read the FAQ and How to Request Help.
FatBasta
Donor
Posts: 11
Joined: 31 Oct 2013, 19:07

Re: Need help getting {fn} to work with sendemail.exe

Post by FatBasta »

I have tested it with movies, it does sort them correctly to D:/Movies/

Forgive me for I am not seeing the problem anymore. My "coding skills" would not be called skills by a coder.
I don't understand what you mean by
You start by watching args, and the { ... } block will be called for each change set, which could be any new file in your /temp, but then you force renaming the change set via TVDB, multiple times actually,
How I "understood" the script is that it contacts TheTVDB only when it's recognized a tv episode.

for example this is what i get when i add a movie to the d:/temp/movies/ directory:

Code: Select all

Processing d:\temp\Movies
Rename movies using [TheMovieDB]
Auto-detect movie from context: [D:\temp\Movies\Riddick.2013.WEBRip.720p.XviD.MP3.mkv]
[MOVE] Rename [D:\temp\Movies\Riddick.2013.WEBRip.720p.XviD.MP3.mkv] to [D:\Movies\Riddick\Riddick.2013.WEBRip.720p.XviD.MP3.
mkv]
Processed 1 files
Nov 01 20:42:47 fatmedia sendemail.exe[17276]: Email was sent successfully!
User avatar
rednoah
The Source
Posts: 23938
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need help getting {fn} to work with sendemail.exe

Post by rednoah »

Well, according to your code, if you don't set -rename in the cmdline, it'll just treat everything as movie.
:idea: Please read the FAQ and How to Request Help.
FatBasta
Donor
Posts: 11
Joined: 31 Oct 2013, 19:07

Re: Need help getting {fn} to work with sendemail.exe

Post by FatBasta »

That really isn't a problem for me, I'll just keep that -rename in my batch file.
Now if the code was causing unnecessary connections to the db sites, that would be a problem. It's not is it?
User avatar
rednoah
The Source
Posts: 23938
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need help getting {fn} to work with sendemail.exe

Post by rednoah »

The more you test your code the more you'd find weird strange and redundant behavior.


But since you donated to the project and even tried to do it yourself, I made this script for you:
http://pastebin.com/1nBifwGw


This should work exactly like you want and not run any unnecessary logic. Have fun. ;)
:idea: Please read the FAQ and How to Request Help.
FatBasta
Donor
Posts: 11
Joined: 31 Oct 2013, 19:07

Re: Need help getting {fn} to work with sendemail.exe

Post by FatBasta »

Oh no you shouldn't have! But thank you very much!!! I'll install it tomorrow, I have to go get drunk right now.

I'll come back tomorrow and give you my comments!

ps. I only donated because of two reasons, #1 I like the software, #2 I'm getting technical support at 21:52 on a Friday, that is a first! So yeah, primarily #2, if only I had more money...

Good night!
User avatar
rednoah
The Source
Posts: 23938
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need help getting {fn} to work with sendemail.exe

Post by rednoah »

Actually it's 4am here in Taiwan. Already back from drinking. :P


Have fun drinking! Happy Halloween!
:idea: Please read the FAQ and How to Request Help.
User avatar
rednoah
The Source
Posts: 23938
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need help getting {fn} to work with sendemail.exe

Post by rednoah »

Here's the final solution:

Code: Select all

def root = 'D:/temp'

def configs = [
	[inputDir: "$root/TV Shows", format: "D:/TV Shows/{n}/Season {s}/{fn}", db:'TheTVDB'],
	[inputDir: "$root/Movies", format: "D:/Movies/{n}/{fn}", db:'TheMovieDB'],
	[inputDir: "$root/Anime", format: "D:/Anime/{primaryTitle}/{fn}", db:'AniDB']
]

configs.each{ config ->
	def watchman = new File(config.inputDir).watch { changes ->
		// extract all
		changes += extract(file:changes.findAll{ it.isArchive() }, output:'.' )

		// delete archives and leftover "garbage" after extract
		def paths = config.inputDir

		config.inputDir.getFolders{ it.hasFile{ it =~ /[.]rar$/ } }.each{ dir ->
			if (paths != null && !paths.isEmpty()) {
				dir.listFiles{ it =~ /[.]rar$|[.]r[\d]+$|[.]sfv$|[.]nfo$/ }*.delete()
			}
		}

		// get subtitles, rename all, send email
		getSubtitles(file:changes)

		renamedFiles = rename(file:changes, db:config.db, format:config.format)

		def videos = changes.findAll{ it.isVideo() }
		if (videos.size() > 0) {
			def msg = videos.name.join('\n')
			execute("sendemail.exe", "-f", "[email protected]", "-t", "[email protected]", "-s", "smtp.server.com:25", "-u", "Added media", "-m", msg)

		}
	}

	watchman.commitDelay = 5 * 60 * 1000            // default = 5min
	watchman.commitPerFolder = true                 // default = true
}

println "Waiting for events"
if (console) { console.readLine() } else { sleep(Long.MAX_VALUE) } // keep running and watch for changes
Also put it on my pastebin => http://pastebin.com/1nBifwGw
:idea: Please read the FAQ and How to Request Help.
Post Reply