📜 ⬆️ ⬇️

Console management of the DLNA server Mediatomb

For a long time, I have been using Mediatomb as a home DLNA server. This is a very reliable and easy server that allows you to access your video-audio-photo archive from any device that supports DLNA.
Mediatomb has an interactive WEB interface through which you can add directories with media data under the control of a specified server. However, I needed the possibility of not interactive, but console management of this server, in particular, it became necessary to add and delete folders with media information. Unfortunately, the system does not provide any standard tools for these operations, so a small script was created based on CURL, which, of course, simulates the user's work with the browser and actually allowed me to achieve the desired result.

Perhaps the work done may need someone else, so I decided to play it on Habré.

Actually, the initial conditions, namely:


I will not describe here, but I’ll go straight to the procedure for creating a script in bash, which will manage media directories controlled by Mediatomb.
')

Creating an additional MYSQL user to access the Mediatomb database


In fact, there is no special need to add a new user to MYSQL, you can use the user credentials specified in the /etc/mediatomb/config.xml configuration file:

<mysql enabled="yes"> <host>localhost</host> <username>mediatomb</username> <database>mediatomb</database> </mysql> 

However, the existing user mediatomb does not have a password and its access is restricted only from localhost , so for a script that will work on a host even different from where Mediatomb itself is located, create a user mt with the password mt in the MySQL database mediatomb:

 reate user 'mt'@'%' identified by 'mt'; grant select on mediatomb.* to 'mt'@'%'; 

Adding a catalog to the Mediatomb library


When adding a directory with recursive traversal enabled, the browser will send the following request to the Mediatomb media library:

 http://<mediatomb_ip>:50500/content/interface?req_type=autoscan&return_type=xml&sid=33a2c429c3c4c82e03baca9564f05908&action=as_edit_save&object_id=2f6d6e742f566964656f&from_fs=1&scan_mode=inotify&scan_level=full&recursive=true&hidden=true&cancel=Cancel 

As you can see, the necessary attributes of the request, which we somehow need to calculate to add our catalog are as follows:


The session ID turned out to be easy. When you first enter the Mediatomb home page, the browser sends the following request:

 http://<mediatomb_ip>:50500/content/interface?req_type=auth&return_type=xml&sid=null&action=get_sid 

and gets the answer:

 <?xml version="1.0" encoding="UTF-8"?> <root sid_was_valid="0" sid="a8ffd95c341aa410a44afaeaf354e105" logged_in="1" success="1"/> 

Thus, we get a string with sid, simultaneously freeing it from quotes as follows:

 sid=`curl -s "http://<mediatomb_ip>:50500/content/interface?req_type=auth&return_type=xml&sid=null&action=get_sid" | grep sid | awk '{print $3}' | sed -e 's/\"//g'` 

With the object identifier a little more complicated, it turned out that the string 2f6d6e742f566964656f is a simple byte sequence of the string with the path to the object being added (file and directory), converted to hexadecimal. So our hex string is just the / mnt / Video string (the directory I just added to Mediatomb).

Those. to get object_Id, for example, from the second parameter of the bash script, you can use the following algorithm:

 catalog=$2 oid="" for ((i=0;$i<${#catalog};i=$(($i+1)))) do sym=`printf '%0.2x' "'${catalog:$i:1}"` oid=$oid$sym done 

Well, collecting all of the above in a CURL query, we get a command that adds the / mnt / Video directory to the MediaTomb library:

 curl -s -o /dev/null "http://<mediatomb_ip>:50500/content/interface?req_type=autoscan&return_type=xml&$sid&action=as_edit_save&object_id=$oid&from_fs=1&scan_mode=inotify&scan_level=full&recursive=true&hidden=true&cancel=Cancel" 

Removing a directory from the Mediatomb library


Deleting a directory from the Mediatomb library is similar to adding:

 http://<mediatomb_ip>:50500/content/interface?req_type=remove&return_type=xml&sid=ea2d65d2dbc72d96ed1ed37dc1d2bbf6&object_id=686994&all=0&updates=check 

However, object_id here is already the identifier of our resource that we want to delete in the MYSQL database. To get object_id , for example, for the / mnt / Video directory, from the database, you can use the following SQL query:

 select id from mt_cds_object where location="D/mnt/Video" 

Thus, getting the catalog identifier from the 2nd argument of the bash script will look something like this:

 catalog=$2 oid=`echo "select id from mt_cds_object where location=\"D$catalog\"" | mysql mediatomb | grep -v id` 

And again a CURL request, already to delete the catalog from the media library:

 curl -s -o /dev/null "http://<meditomb_ip>:50500/content/interface?req_type=remove&return_type=xml&$sid&object_id=$oid&all=0&updates=check" 

Well, perhaps that's all. Finally I will give the full text of the mtomb script, which takes two parameters

 mtomb <add | del> <  > 

CODE BASH:
 #!/bin/bash cmd=$1 catalog=$2 mediatomb_ip=192.168.7.10 mtombdb="mysql -h $mediatomb_ip mediatomb -umt -pmt" #            if [ "${catalog:(-1):1}" = "/" ]; then catalog=${catalog:0:(-1)} fi if [ "$cmd" = "add" ]; then echo "Try add $catalog in mediatomb" #  object_id (   16- ) oid="" for ((i=0;$i<${#catalog};i=$(($i+1)))) do sym=`printf '%0.2x' "'${catalog:$i:1}"` oid=$oid$sym done #  SID (Session ID) sid=`curl -s "http://$mediatomb_ip:50500/content/interface?req_type=auth&return_type=xml&sid=null&action=get_sid" | grep sid | awk '{print $3}' | sed -e 's/\"//g'` #   if [ -a "$catalog" ]; then curl -s -o /dev/null "http://$mediatomb_ip:50500/content/interface?req_type=autoscan&return_type=xml&$sid&action=as_edit_save&object_id=$oid&from_fs=1&scan_mode=inotify&scan_level=full&recursive=true&hidden=true&cancel=Cancel" else echo "Nothing to add..." fi fi if [ "$cmd" = "del" ]; then echo "Try delete $catalog from mediatomb" #  SID (Session ID) sid=`curl -s "http://$mediatomb_ip:50500/content/interface?req_type=auth&return_type=xml&sid=null&action=get_sid" | grep sid | awk '{print $3}' | sed -e 's/\"//g'` #       (object_id) oid=`echo "select id from mt_cds_object where location=\"D$catalog\"" | $mtombdb | grep -v id` #   if [ -n "$oid" ]; then curl -s -o /dev/null "http://$mediatomb_ip:50500/content/interface?req_type=remove&return_type=xml&$sid&object_id=$oid&all=0&updates=check" else echo "Nothing to delete..." fi fi 


Link to GIST: https://gist.github.com/mitshel/6b140467182c3377c000

Source: https://habr.com/ru/post/277585/


All Articles