Page 1 of 1

[Synology NAS] Indexing files in the DLNA server

Posted: September 19th, 2020, 10:07 am
by LapinFou
[Edit 25th of September 2020 (version 1.2 is out):
  • Removed 7zip unpack option.
    This is now a built-in SABnzbd function


Hi folks,

Here is my Python3 script SabToSyno.py.
Some of those functions were already present in my retired [Synology NAS] Fixed non-ASCII chars and more...(2020/09/11) script.

With SABnzbd 3.x running with Python3 my script is not anymore useful. The default usage of the unicode UTF-8 in Python3 resolved the non-ASCII characters issues. However in this old script, some useful optional parameters to move downloaded file(s) and to add them in the Synology DLNA were present.

Thus, the goal of this post-processing script is to automatically add the downloaded multimedia file in the Synology DLNA server.
To do so, the script is executing the Synology command line /usr/syno/bin/synoindex.

To install the script, you must be connected to your Synology NAS through a Telnet/SSH session.
All the instructions below are for the SynoCommunity SABnzbd version:
  • You must open the SSH connection with your NAS using your "admin" login credentials.
  • You must switch in "root" mode by executing the command "sudo":

    Code: Select all

    admin@NAS:> sudo -i
  • Go in the SABnzbd scripts folder, then create/copy the script here (name can be SabToSyno.py).

    Code: Select all

    root@NAS:> cd /var/packages/sabnzbd/target/var/scripts/
  • Be sure that the file owner is correct and the script can be executed.

    Code: Select all

    root@NAS:>  ls -l
    total 372
    [...]
    -rwxr-xr-x 1 sc-sabnzbd sabnzbd  6520 Sep 19 16:30 SabToSyno.py
    [...]
    If not, then you can change the owner and permissions with those commands:

    Code: Select all

    root@NAS:> chown sc-sabnzbd:sabnzbd SabToSyno.py
    root@NAS:> chmod 755 SabToSyno.py
  • Optionally, setup the option to move the downloaded NZB to a dedicated destination.
The 2 optional parameters are (you must edit the script):

Code: Select all

MoveToThisFolder = ''
[...]
MoveMergeSubFolder = True
MoveToThisFolder This option is '' (disabled) by default.
If the option is filed-up with a path (e.g. MoveMergeSubFolder = '/volume1/MY_DOWLOAD/MY_VIDEO/'), the downloaded NZB will be moved to this destination folder. Then, after the move operation, the multimedia files will be added in the Syno DLNA from their new destination folder.
Important: the specified path is the same one as visible through a Telnet/SSH session. In others words, it is the true "Linux" path (case sensitive).
For example:

Code: Select all

MoveToThisFolder = '/volume1/video/Movies'
MoveMergeSubFolder This option is True by default.
If MoveMergeSubFolder = False, then it is equivalent to unix command:

Code: Select all

mv -rf srcFolder destFolder
In case of conflict between an already existing sub-folder in the destination folder: the destination sub-folder will be replaced with source sub-folder

If MoveMergeSubFolder = True, then it is equivalent to unix command:

Code: Select all

cp -rf srcFolder/* destFolder/
rm -rf srcFolder
In case of conflict between an already existing sub-folder in the destination folder: the destination sub-folder will be merged with source sub-folder (kind of incremental).

The script must be copied in the SABnzbd scripts folder as explained below.
The name of the script can be SabToSyno.py

Here is the code:

Code: Select all

#!/usr/local/python3
#
# Create by LapinFou.
# https://forums.sabnzbd.org/viewtopic.php?f=9&p=122338
#
#   date     | version |     comment
#---------------------------------------
# 2020-09-13 |   1.0   | Initial version
# 2020-09-19 |   1.1   | Code clean-up
# 2020-09-25 |   1.2   | Removed 7zip recursive unpack option
#

# get library modules
import sys
import os
import subprocess
import shutil

scriptVersionIs = 1.2

# If empty, then no move
# Format must be synology full path (case sensitive). For ex: /volume1/video/News
MoveToThisFolder = ''
# If MoveMergeSubFolder = False, then equivalent to unix command:
# mv -rf srcFolder destFolder
# In case of conflict between an already existing sub-folder in the destination folder:
#   the destination sub-folder will be replaced with source sub-folder
#
# If MoveMergeSubFolder = True, then equivalent to unix command:
# cp -rf srcFolder/* destFolder/
# rm -rf srcFolder
# In case of conflict between an already existing sub-folder in the destination folder:
#   the destination sub-folder will be merged with source sub-folder (kind of incremental)
MoveMergeSubFolder = True

########################
# ----- Functions ---- #
########################
# Get information from SABnzbd
try:
    (scriptname,directory,orgnzbname,jobname,reportnumber,category,group,postprocstatus,url) = sys.argv
except:
    print("No commandline parameters found")
    sys.exit(1)

# add folder in the Syno index database (DLNA server)
def addToSynoIndex(DirName):
    print("Adding folder in the DLNA server")
    synoindex_cmd = ['/usr/syno/bin/synoindex', '-A', DirName]
    try:
        p = subprocess.Popen(synoindex_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate()
        out = out.decode('ascii')
        err = err.decode('ascii')
        if (str(out) == ''):
            print("synoindex result: " + DirName + " successfully added to Synology database")
        else:
            print("synoindex result: " + str(out))
        if (str(err) != ''):
            print("synoindex failed: " + str(err))
    except OSError as e:
        print("Unable to run synoindex: "+str(e))
    return

########################
# --- Main Program --- #
########################
print("Running SabToSyno Python3 script (v%s)" %scriptVersionIs)
print("")

# Change current directory to SABnzbd "complete" directory
os.chdir(directory)

# display directory of the SABnzbd job
currentFolder = os.getcwd()
print("Current folder is " + currentFolder)

# Move current folder to an another destination if the option has been configured
if (MoveToThisFolder != ''):
    print("")
    print(100*'-')
    print("Moving folder:")
    print(currentFolder)
    print("to:")
    print(MoveToThisFolder)
    # Check if destination folder does exist and can be written
    # If destination doesn't exist, it will be created
    if (os.access(MoveToThisFolder, os.F_OK) == False):
        os.makedirs(MoveToThisFolder)
        os.chmod(MoveToThisFolder, 0o777)
    # Check write access
    if (os.access(MoveToThisFolder, os.W_OK) == False):
        print(100*'#')
        print("File(s)/Folder(s) can not be move in %s" %(MoveToThisFolder))
        print("Please, check Unix permissions")
        print(100*'#')
        sys.exit(1)

    # If MoveMergeSubFolder is True, then move all file(s)/folder(s) to destination
    # then remove source folder
    destFolder = os.path.join(MoveToThisFolder, os.path.split(currentFolder)[-1])
    if (MoveMergeSubFolder):
        print("    Info: Merge option is ON (incremental copy)")
        try:
            synoCopy_cmd = ['/bin/cp', '-Rpf', os.path.abspath(currentFolder), MoveToThisFolder]
            p = subprocess.Popen(synoCopy_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
            out = out.decode('ascii')
            err = err.decode('ascii')
            if (str(err) != ''):
                print("Copy failed: " + str(err))
                sys.exit(1)
        except OSError as e:
            print("Unable to run cp: " + str(e))
            sys.exit(1)
        os.chdir(MoveToThisFolder)
        shutil.rmtree(currentFolder)
    # If MoveMergeSubFolder is False, remove folder with same (if exists)
    # then move all file(s)/folder(s) to destination and remove source folder
    else:
        print("    Info: Merge option is OFF (existing data will be deleted and replaced)")
        # Remove if destination already exist
        if os.path.exists(destFolder):
            shutil.rmtree(destFolder)
        shutil.move(currentFolder, MoveToThisFolder)
else:
    # If move is not enabled, the current folder will be indexed
    destFolder = currentFolder

# Add multimedia files in the Syno DLNA
print("")
print(100*'-')
addToSynoIndex(destFolder)
print("")
print("Moving and indexing file(s) done!")

# Success code
sys.exit(0)
In the GUI, you must go here to create a category:
http://sab_url:8080/config/categories/
Image

When a NZB is downloaded, select the created category:
Image

Re: [Synology NAS] Indexing files in the DLNA server

Posted: September 20th, 2020, 8:37 am
by safihre
The unpacking of 7zip, is that really needed? Sabnzbd does this by default since recursive unpack is enabled by default.

Besides that, great work!

Re: [Synology NAS] Indexing files in the DLNA server

Posted: September 21st, 2020, 2:19 am
by LapinFou
Concerning the recursive unpacking, this was a request from someone long time ago. The downloaded NZB (after unpacking the RAR archives) was including many 7zip files. So, I added this option.
To be more clear: this option is to handle 7zip archives inside a NZB RAR archive. It was not supported by SABnzbd at this time.
If this option is now redundant with built-in SABnzbd option, I will be happy to remove it. :)

Thanks for your comment. I'm far to be a Python expert. I'm just playing a bit with it. ;)

Re: [Synology NAS] Indexing files in the DLNA server

Posted: September 25th, 2020, 3:24 am
by safihre
We do recursive unpack, up to 5 levels deep after the first unpack. So that can be removed :)

Re: [Synology NAS] Indexing files in the DLNA server

Posted: September 25th, 2020, 9:32 am
by LapinFou
Hi safihre,

I updated my script to version 1.2 (it is always very easy to remove some code :) ). I also added some explanations to how install this script.

If you think this script can be useful, you can add it in the SynoCommunity package. Maybe, you could also mark this topic as "Post-it / Pin-it" (I don't know the correct English word for that). Otherwise it will be soon lost to the bottom of the forum with all new messages. ;)

Best regards,
Seb

Re: [Synology NAS] Indexing files in the DLNA server

Posted: October 25th, 2020, 2:43 am
by safihre
The script will be in the next SynoCommunity package:
https://github.com/SynoCommunity/spksrc/pull/4229
Made a few tweaks for compatibility, adding the shebang line and reference to this thread.
I also updated your post to match.

Re: [Synology NAS] Indexing files in the DLNA server

Posted: October 25th, 2020, 8:13 am
by LapinFou
Great! ;D
Thanks for that.
I hope this will be useful for some users.

Maybe you could mark this topic as "Posted-it". Just to avoid this topic to be burried in the forum. ;)

Re: [Synology NAS] Indexing files in the DLNA server

Posted: October 25th, 2020, 4:01 pm
by safihre
Done!

Re: [Synology NAS] Indexing files in the DLNA server

Posted: October 25th, 2020, 4:34 pm
by LapinFou
Thanks!
:D