[POC] Custom Format Server

Running FileBot from the console, Groovy / FileBot scripting, shell scripts, etc
Post Reply
User avatar
rednoah
The Source
Posts: 20846
Joined: 16 Nov 2011, 08:59

[POC] Custom Format Server

Post by rednoah »

Proof of Concept:

Externalize format logic to a remote custom service.



Client Format:

Code: Select all

{ json('http://localhost:8080/'.toURL(f:f)) }

:!: json() will cache the response. Add System.currentTimeMillis() to the URL as cache buster during development:

Code: Select all

{ json('http://localhost:8080/'.toURL(f:f, time:System.currentTimeMillis())) }


Client Command:

Code: Select all

$ filebot -rename *.mp4 --db file --format "{ json('http://localhost:8080/'.toURL(f:f)) }" --action TEST --log INFO
[TEST] from [The.Man.from.Earth.2007.mp4] to [THE_MAN_FROM_EARTH_2007.mp4]


Server Log:

Code: Select all

$ ./server-node.js
{ src: 'The.Man.from.Earth.2007', dst: 'THE_MAN_FROM_EARTH_2007' }


Server Code:

Code: Select all

#!/usr/bin/env node

const http = require('http')
const url = require('url')
const path = require('path')


const handler = (request, response) => {
	const query = url.parse(request.url, true).query
	const f = query['f']

	const src = path.parse(f).name
	const dst = src.toUpperCase().replace(/[^A-Z0-9]/g, '_')

	console.log({src: src, dst: dst})

	response.writeHead(200)
	response.end(JSON.stringify(dst))
}


const server = http.createServer(handler)
server.listen(8080)
:idea: Please read the FAQ and How to Request Help.
devster
Posts: 400
Joined: 06 Jun 2017, 22:56

Re: [POC] Custom Format Server

Post by devster »

Could this be reworked so that FileBot receives a file name plus information about the type of file and it returns a filename and location?
E.g.

Code: Select all

curl "somethingsomething/post" --data-urlencode "file=${file}" --data-urlencode dir="$(basename ${file})" --data-urlencode output=/Media --data-urlencode label=tv_shows --data @<(mediainfo --full --output=xml "${file}")
and it'd return file destination?
I only work in black and sometimes very, very dark grey. (Batman)
User avatar
rednoah
The Source
Posts: 20846
Joined: 16 Nov 2011, 08:59

Re: [POC] Custom Format Server

Post by rednoah »

devster wrote: 25 Jan 2022, 00:11 Could this be reworked so that FileBot receives a file name plus information about the type of file and it returns a filename and location?
That is what the code above does:

Code: Select all

{ json('http://localhost:8080/'.toURL(f:f, time:System.currentTimeMillis())) }
* pass arbitrary GET parameters (e.g. file path and current time in this specific example)
* pass back the String value returned by the HTTP request to be used as destination path (server response is JSON in this specific example; but you can modify the code to assume a plain text response)
:idea: Please read the FAQ and How to Request Help.
devster
Posts: 400
Joined: 06 Jun 2017, 22:56

Re: [POC] Custom Format Server

Post by devster »

Oh, didn't realize.
But it doesn't seem like FileBot is involved in the remote processing.
This line: const dst = src.toUpperCase().replace(/[^A-Z0-9]/g, '_') is the only piece of code involved in the renaming, right?
I was wondering if it could be made so that FileBot processes the information on the server end (like using an uploaded mediainfo file instead of analyzing the file on disk directly).
I only work in black and sometimes very, very dark grey. (Batman)
User avatar
rednoah
The Source
Posts: 20846
Joined: 16 Nov 2011, 08:59

Re: [POC] Custom Format Server

Post by rednoah »

The server-side code above is just a simple example. This particular example upper-cases one of the GET parameters it was sent as input and sends back the result as output. Your server-side code could do anything though, and your client-side can send arbitrary information, file path, media info, current time, etc.
:idea: Please read the FAQ and How to Request Help.
devster
Posts: 400
Joined: 06 Jun 2017, 22:56

Re: [POC] Custom Format Server

Post by devster »

To expand a bit on this idea, is there a function or an option to use as source file, instead of a media file present on disk, a MediaInfo xml report?

The idea would be to host a FileBot service on a remote server, upload the MediaInfo report file named as the source file and have FileBot return the correct name.
This would not use the move/copy/keeplink functions of FileBot, the xattr bits (although they could be potentially returned in the response or processed from the source), and it would probably require a separate version of amc.groovy which doesn't check folders or file existence.
I only work in black and sometimes very, very dark grey. (Batman)
Post Reply