Board index Scripting and Automation [CODE] Examples and Snippets

[CODE] Examples and Snippets

Running FileBot from the console, Groovy / FileBot scripting, shell scripts, etc

Post 30 May 2012, 05:17
rednoah User avatar
The Source

Posts: 2078
Location: 北京

Reading torrent files might be useful for some people. Since FileBot already includes the code for that I'll quickly paste a Groovy snippet here on how to do it.

args.getFiles{ it.extension == 'torrent' }.each{
   def torrent = new net.sourceforge.filebot.torrent.Torrent(it)
   
   println "Torrent: $torrent.name"
   torrent.files.eachWithIndex{ entry, index ->
      println "$index: $entry.path [$entry.length]"
   }
}
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 2078
Location: 北京

Here's example code for mapping files to torrents:
def torrents = args.getFiles{ it.extension == 'torrent' }.findResults{ new net.sourceforge.filebot.torrent.Torrent(it) }
def videos = args.getFiles{ it.isVideo() }

videos.each{ f ->
   def m = torrents.find{ t ->
      t.files.find{ e ->
         e.name == f.name && e.length == f.length()
      }
   }
   if (m) {
      println "file [$f.name] belongs to torrent [$m.name]"
   }
}
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 2078
Location: 北京

Sometimes it might be useful to override series auto-detection by forcing a series title for a specific set of files. This is how you do it. Only process a certain set of files via file parameter and force the series title via the query parameter.
rename(file: args.getFiles{ it =~ /(?i:colbert.report)/}, query: "The Colbert Report", db: "TheTVDB")
rename(file: args.getFiles{ it =~ /(?i:daily.show)/}, query: "The Daily Show with Jon Stewart", db: "TheTVDB")
...
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 2078
Location: 北京

Print rename log of this session:
getRenameLog().each{ from, to -> println "$from => $to"}
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

Post 27 Aug 2012, 11:04
rednoah User avatar
The Source

Posts: 2078
Location: 北京

In scripting you can just pass in a closure to replace the standard action with your own arbitrary logic. Anything is possible.

e.g.
rename(
   file: args.getFiles(),
   action: { from, to ->
      execute('echo', 'FROM', from, 'TO', to)
   }
)
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 2078
Location: 北京

Sometimes you might wanna grab data from websites, aka page scraping. Groovy with JSoup will greatly simplify any kind of scraping you wanna do. Here's an example of how to write a script that'll extract all links from a given page.

@Grab('org.jsoup:jsoup')

def page = org.jsoup.Jsoup
   .connect('http://filebot.sf.net')
   .userAgent('Mozilla/5.0')
   .get()

page.select("A").each { println it }
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 2078
Location: 北京

Here's a an example of how to use Groovy XML-RPC to hook into rtorrent and retrieve the list of completed downloads. If you got rutorrent running fine you can just use it's rpc proxy. Otherwise you might wanna look into the RTorrentXMLRPCGuide.

@Grab('org.codehaus.groovy:groovy-xmlrpc')
import groovy.net.xmlrpc.*

def rpc = new XMLRPCServerProxy("http://localhost/rutorrent/plugins/rpc/rpc.php", true)
rpc.setBasicAuth("username", "password")

rpc.invokeMethod("d.multicall", ['complete', 'd.get_directory_base=', 'd.get_name=']).each{
    println it
}
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

Post 19 Nov 2012, 18:49
rednoah User avatar
The Source

Posts: 2078
Location: 北京

With the format engine and mediainfo bindings at your command it's pretty straight-forward to index your music collection.

e.g. print artist, album and title for each audio file:
args.getFiles{ it.isAudio() }.each{
   println getMediaInfo(file:it, format:'''{media.Performer} / {media.Album} / {media.TrackPosition.pad(2)} - {media.Title}''')
}
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image


Return to Scripting and Automation