XBMC Library Updater

Come up with a useful post-processing script? Share it here!
Hitcher
Newbie
Newbie
Posts: 12
Joined: May 24th, 2010, 11:06 am

Re: XBMC Library Updater

Post by Hitcher »

Back again. I'm now running Frodo builds of XBMC and the script gives this error -

Code: Select all

wget: server returned error: HTTP/1.1 415 Unsupported Media Type
Any help appreciated.
drawde
Newbie
Newbie
Posts: 11
Joined: January 17th, 2012, 6:34 pm

Re: XBMC Library Updater

Post by drawde »

CapnBry wrote:You can do this without using python classes with just wget:

Code: Select all

wget -q -O/dev/null --header='Content-Type: application/json' --post-data='{"jsonrpc": "2.0", "method": "VideoLibrary.Scan"}' http://localhost:8000/jsonrpc
If you want to see the response, use -O- and add "id": "anything" to the end of the JSON POST data. Tested on Eden 11.0 Beta 1. Also note that my XBMC HTTP server is on port 8000, modify if necessary.
bumping an old thread here.. the above works great for me in frodo but i'm wondering how hard it would be to modify it to only update a newly downloaded item and not the entire library?
unRAID 4.7 Pro: ASUS M4A88T-M | AMD Sempron 145 Sargas @ 2.8GHz | 1GB DDR3 SDRAM 1066 (PC3 8500) RAM | Antec NEO ECO 620C 620W PSU | NETGEAR gigE
Ashex
Newbie
Newbie
Posts: 18
Joined: February 28th, 2009, 12:20 am

Re: XBMC Library Updater

Post by Ashex »

Occasionally the API will die but the listener is still active, the result is the script never exiting. I didn't notice it last week and found about 10 downloads in the post-process queue waiting for the script to exit.

I've added some stuff to my python script to handle this, I imagine there's a better way to do it but I haven't got a lot of time to sort it out.

Eventually I plan to directly update the video in XBMC, but this will require some additional work with sickbeard/couchpotato to get info and stuff.

Code: Select all

#!/usr/bin/python2.7
import jsonrpclib, thread, time

def xbmcscan():
        server = jsonrpclib.Server('http://xbmc:8080/jsonrpc')
        if server.VideoLibrary.Scan() == 'OK':
                print "Update command sent"
                thread.interrupt_main()
        else:
                print "Update command failed"

try:
        thread.start_new_thread(xbmcscan, ())
        time.sleep(3)
        thread.exit()
except KeyboardInterrupt:
        quit()
except SystemExit:
        print "Xbmc HTTP API hung up"
        quit()
ericab
Newbie
Newbie
Posts: 12
Joined: May 26th, 2009, 5:03 pm

Re: XBMC Library Updater

Post by ericab »

drawde wrote:
CapnBry wrote:You can do this without using python classes with just wget:

Code: Select all

wget -q -O/dev/null --header='Content-Type: application/json' --post-data='{"jsonrpc": "2.0", "method": "VideoLibrary.Scan"}' http://localhost:8000/jsonrpc
If you want to see the response, use -O- and add "id": "anything" to the end of the JSON POST data. Tested on Eden 11.0 Beta 1. Also note that my XBMC HTTP server is on port 8000, modify if necessary.
bumping an old thread here.. the above works great for me in frodo but i'm wondering how hard it would be to modify it to only update a newly downloaded item and not the entire library?

have you made and progress with this ?
for the life of me i cant figure out how to update just a single tv series, and not the whole library.
this is what i have sab run post proc; but it doesnt update new episode info.

Code: Select all

curl -s -H "Content-Type: application/json" -u xbmc:xbmc -X POST -d '{"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "params":{"directory":"nfs://192.168.2.104/export/_TV/*/"}, "id": "scan"}' http://192.168.2.33:8090/jsonrpc
Ashex
Newbie
Newbie
Posts: 18
Joined: February 28th, 2009, 12:20 am

Re: XBMC Library Updater

Post by Ashex »

Take a look at this function in the sickbeard xbmc notify script. The methods you need are described in there.
ericab
Newbie
Newbie
Posts: 12
Joined: May 26th, 2009, 5:03 pm

Re: XBMC Library Updater

Post by ericab »

Ashex wrote:Take a look at this function in the sickbeard xbmc notify script. The methods you need are described in there.

im really not sure what to do with it :'(
Ashex
Newbie
Newbie
Posts: 18
Joined: February 28th, 2009, 12:20 am

Re: XBMC Library Updater

Post by Ashex »

Updating a specific show isn't a simple one-liner.

You have to query XBMC for the shows file location using the Show name. You then take the file location and send an update request with that path.


Also, my python script doesn't work with Frodo as the content-type isn't set. Unfortunately you can't set it with jsonrpclib since it's hard coded in the library (a workaround is modifying the library, line 120).

Instead, Nemesis of the Maraschino project wrote a script that removes the jsonrpclib dependency.

Code: Select all

settings = {
    'hostname': '127.0.0.1',
    'port': '1234',
    'username': '',
    'password': ''
}



http_address = 'http://%s:%s/jsonrpc' % (settings['hostname'], settings['port'])
username = settings['username']
password = settings['password']

try:
    import json
except ImportError:
    import simplejson as json
import urllib2, base64

class XBMCJSON:

    def __init__(self, server):
        self.server = server
        self.version = '2.0'

    def __call__(self, **kwargs):
        method = '.'.join(map(str, self.n))
        self.n = []
        return XBMCJSON.__dict__['Request'](self, method, kwargs)

    def __getattr__(self,name):
        if not self.__dict__.has_key('n'):
            self.n=[]
        self.n.append(name)
        return self

    def Request(self, method, kwargs):
        data = [{}]
        data[0]['method'] = method
        data[0]['params'] = kwargs
        data[0]['jsonrpc'] = self.version
        data[0]['id'] = 1

        data = json.JSONEncoder().encode(data)
        content_length = len(data)

        content = {
            'Content-Type': 'application/json',
            'Content-Length': content_length,
        }
   
        request = urllib2.Request(self.server, data, content)
        base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
        request.add_header("Authorization", "Basic %s" % base64string)

        f = urllib2.urlopen(request)
        response = f.read()
        f.close()
        response = json.JSONDecoder().decode(response)

        try:
            return response[0]['result']
        except:
            return response[0]['error']


xbmc = XBMCJSON(http_address)
xbmc.VideoLibrary.Scan()
ericab
Newbie
Newbie
Posts: 12
Joined: May 26th, 2009, 5:03 pm

Re: XBMC Library Updater

Post by ericab »

my TV ep's are all on a remote server, and those videos are mounted via NFS.

"You have to query XBMC for the shows file location using the Show name. You then take the file location and send an update request with that path."


i got around this by doing this:

show=$(echo "$1" | sed 's/\/home\/eric\/Media\/_Downloads\/_TV\///g')

$1 is the full path of the final directory of the job; as seen here:
http://wiki.sabnzbd.org/user-scripts

which would give me something like for example: American Dad/s08

from there: i have/had in the next line:

curl -s -H "Content-Type: application/json" -u xbmc:xbmc -X POST -d '{"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "params":{"directory":"nfs://192.168.2.104/export/_TV/$shows"}, "id": "scan"}' http://192.168.2.33:8090/jsonrpc

notice the $shows in the directory parameter.

i quickly learned that the quotes, were screwing around with things, so i escaped them as such, and had it set to a variable, named "curl_payload", so:

curl_payload="\"Content-Type: application/json\" -u xbmc:xbmc -X POST -d '{\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.Scan\", \"params\":{\"directory\":\"nfs://192.168.2.104/export/_TV/$show/\"}, \"id\": \"scan\"}' http://192.168.2.33:8090/jsonrpc"",

so the final...command, sab was going to issue would look like this:

curl $curl_payload

which means this:

curl -s -H "Content-Type: application/json" -u xbmc:xbmc -X POST -d '{"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "params":{"directory":"nfs://192.168.2.104/export/_TV/American Dad/s08"}, "id": "scan"}' http://192.168.2.33:8090/jsonrpc

it looks good to me, but even though ive specified the full path of the show i want xbmc to populate with synopsis, ep name etc etc... without asking xbmc where it is, it does not work !
i think im very close to cracking this, but hopefully you can show me whats not right.

i believe it is one of 2 things:
A) curl does not like: curl $curl_payload or any other variable for that matter as its only argument, even though the value of the variable is correct

or

B) VideoLibrary.Scan is just plain wrong. which is hard to understand as this very command successfully populates movies info. just does not work for TV episodes.
i think im missing something very simple here. :/
Ashex
Newbie
Newbie
Posts: 18
Joined: February 28th, 2009, 12:20 am

Re: XBMC Library Updater

Post by Ashex »

Try changing the id.

This is what you have:

'{"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "params":{"directory":"nfs://192.168.2.104/export/_TV/American Dad/s08"}, "id": "scan"}'

This is what sickbeard uses (%s is a variable):
{"jsonrpc":"2.0","method":"VideoLibrary.Scan","params":{"directory":%s},"id":1}
ericab
Newbie
Newbie
Posts: 12
Joined: May 26th, 2009, 5:03 pm

Re: XBMC Library Updater

Post by ericab »

**edited
Last edited by ericab on February 22nd, 2013, 9:29 pm, edited 1 time in total.
ericab
Newbie
Newbie
Posts: 12
Joined: May 26th, 2009, 5:03 pm

Re: XBMC Library Updater

Post by ericab »

tl;dr,

ive found a solution.

the final command is:

Code: Select all

echo "curl -s -H \"Content-Type: application/json\" -u xbmc:xbmc -X POST -d '{\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.Scan\", \"params\":{\"directory\":\"nfs://192.168.2.104/export/_TV/show/\"}, \"id\": \"scan\"}' http://192.168.2.33:8090/jsonrpc" | sed 's_show_'"$show"'_g' | sh
[/size]

remember:
you will need to modify the IP, the "directory" parameter, and the XBMC username/pass in the above for this to work for your environment.

------------------------------------


so i found the problem ultimatley was not with the new API code, but using a variable within the header, as it was not processed.
also using 'curl $variable', even if the variable contained the proper arguments didnt work.
so the trick was to make curl's url dynamic, depending on the path to the show, but also, not contain any variables, since it would be passed along to xbmc in its raw form., so how to do this ?

here ive defined the shows relative path, by taking what sab tells me is the full job path.
show=$(echo "$1" | sed 's/\/home\/eric\/Media\/_Downloads\/_TV\///g')

if the shows full path is for example:

/home/eric/Media/_Downloads/_TV/American Dad/s05
we will end up with:
show == American Dad/s05

no this is the tricky part, as i originally was taking the $show variable, and adding it like so:

curl -s -H "Content-Type: application/json" -u xbmc:xbmc -X POST -d '{"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "params":{"directory":"nfs://192.168.2.104/export/_TV/$show"}, "id": "scan"}' http://192.168.2.33:8090/jsonrpc

but as ive stated above, $shows will not be expanded; its passed along to xbmc as is, and of course xbmc has no clue what "$show" is, so in the xbmc log we have something like this:

Code: Select all

18:17:49 T:2889874240  DEBUG: JSONRPC: Incoming request: {"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "params":{"directory":"nfs://192.168.2.104/export/_TV/$show/"}, "id": "scan"}
18:17:49 T:2889874240   DEBUG: JSONRPC: Calling videolibrary.scan
18:17:49 T:2603612992  NOTICE: Thread CVideoInfoScanner start, auto delete: false
18:17:49 T:2603612992  NOTICE: VideoInfoScanner: Starting scan ..
18:17:49 T:2603612992   DEBUG: CAnnouncementManager - Announcement: OnScanStarted from xbmc
18:17:49 T:2603612992   DEBUG: GOT ANNOUNCEMENT, type: 16, from xbmc, message OnScanStarted
18:17:49 T:2603612992   DEBUG: SECTION:LoadDLL(libnfs.so.1)
18:17:49 T:2603612992   DEBUG: Loading: libnfs.so.1
18:17:49 T:2603612992   DEBUG: NFS: Context for 192.168.2.104/export/_TV not open - get a new context.
18:17:49 T:2603612992   DEBUG: NFS: Connected to server 192.168.2.104 and export /export/_TV
18:17:49 T:2603612992   DEBUG: NFS: chunks: r/w 1048576/32768
18:17:49 T:2603612992 WARNING: Process directory 'nfs://192.168.2.104/export/_TV/$show/' does not exist - skipping scan.
so how can we do this ?
were going to echo the proper json api call, and in place of "$show", were going to have "show".
it looks like this:

Code: Select all

echo "curl -s -H \"Content-Type: application/json\" -u xbmc:xbmc -X POST -d '{\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.Scan\", \"params\":{\"directory\":\"nfs://192.168.2.104/export/_TV/show/\"}, \"id\": \"scan\"}' http://192.168.2.33:8090/jsonrpc"
but why are we echoing it ?
were going to pipe it to sed, so we can replace "show" with the contents of "$show", and we do it like so:
(note the double quoting; this is important because we want the contents of $show, not $show itself. ---> single quote + double quote

Code: Select all

sed 's_show_'"$show"'_g'
now we have a properly formatted api call to update your library, with the exact episode that was just downloaded. and it looks like this:

Code: Select all

curl -s -H "Content-Type: application/json" -u xbmc:xbmc -X POST -d '{"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "params":{"directory":"nfs://192.168.2.104/export/_TV/American Dad/s05"}, "id": "scan"}' http://192.168.2.33:8090/jsonrpc
now we will pipe it to 'sh'.

the final command is:

Code: Select all

echo "curl -s -H \"Content-Type: application/json\" -u xbmc:xbmc -X POST -d '{\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.Scan\", \"params\":{\"directory\":\"nfs://192.168.2.104/export/_TV/show/\"}, \"id\": \"scan\"}' http://192.168.2.33:8090/jsonrpc" | sed 's_show_'"$show"'_g' | sh
Ashex
Newbie
Newbie
Posts: 18
Joined: February 28th, 2009, 12:20 am

Re: XBMC Library Updater

Post by Ashex »

Oh shell scripting, how I don't miss you :P

Thanks for posting the resolution to the problem, you've also given me an idea for updating shows via my python script. I've got everything mounted via NFS on the htpc, the paths are identical to that on the file server so i should be able to copy what you're doing. I'll post back with an updated python script when I've finished it.
ericab
Newbie
Newbie
Posts: 12
Joined: May 26th, 2009, 5:03 pm

Re: XBMC Library Updater

Post by ericab »

Ashex,
hows your script coming ?
Ashex
Newbie
Newbie
Posts: 18
Joined: February 28th, 2009, 12:20 am

Re: XBMC Library Updater

Post by Ashex »

Unfortunately I haven't been able to spend any time on the script. I upgraded my htpc to XBMCbuntu Frodo and I've been having stability issues (randomly locks up) so testing is kinda impossible at the moment :/

I'm going to reinstall this weekend and set XBMC up manually, after that I'll be able to work on the script.
scotia
Newbie
Newbie
Posts: 2
Joined: April 10th, 2013, 7:36 am

Re: XBMC Library Updater

Post by scotia »

Hi all,

I've got a Perl script which I use with xbmc to automate updates, cleans, sleep, artwork downloading, etc.

If you're interested let me know and I'll tidy it up and post it here.

Scott
Post Reply