[PP-Script] CharTranslator and DLNA Indexing for Synology

Share your scripts or request scripts with specific features.
Forum rules
Please keep the forum clean - one topic per script. Questions not related to a specific script should be posted in Support forum.
Post Reply
mannibis
Posts: 60
Joined: 29 Jul 2014, 15:10

[PP-Script] CharTranslator and DLNA Indexing for Synology

Post by mannibis » 12 Sep 2014, 21:00

I ported CharTranslator.py over from the Sabnzbd+ version to NZBGet. It has been tested by a friend of mine who owns a Synology and he confirmed that his movie files are added to the SynoIndex. This was the only thing keeping him from using NZBGet on his Synology and I figured I would share it for everyone. Feel free to test and report back with any errors or issues.

The script will fix the encoding issues after unpack and add the media files to the Syno DLNA Index

Attachment is below.
Credit to LapinFou for writing the original CharTranslator.py script for Sabnzbd+

Installation Instructions
* Download CharTranslatorGet.py into your PP-Scripts directory
* Fix permissions/owner for file on Synology
* Configure options in NZBGet Web UI.

Code: Select all

#!/usr/local/python/bin/python -OO
#-*- coding: iso-8859-15 -*-
#
##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###

# CharTranslator for NZBGet.
#
# If a file has been archieved under an ISO-8859 environment and unarchived
# under an UTF8 environment, then you will get an encoding format problem.
# The file will not be readable through SAMBA.
#
# Renaming script for SABnzbd runnning under Synology NAS.
# By default the NZB software is running under UTF-8 encoding format
# in order to correctly handle the french accents (éèàç...) a SABnzbd
# post-script must be run in order to convert the file encoding.
#
# To fix this problem, you must convert the encoding format
# to the UTF8 (default Synology encoding)
# The script is trying to detect if the original file/directory are coming
# from a RAR archive. In this case the unrar command on your Syno system will
# unpack in CP850 format (DOS).
# NB: in all cases, files will be readable through samba, even if the detection
# failed. But converted characters will not be good, ex: Î? instead é
#
# Ported to NZBGet from LapinFu's CharTranslator.py v1.9.
#
# NOTE: This script requires Python to be installed on your system

##############################################################################
### OPTIONS                                                                ###

## General

# Index in Synology DLNA (yes, no).
#
#IndexInSynoDLNA=yes

# Move and Merge Subfolder (yes,no).
#
#MoveMergeSubFolder=yes

# Specify Folder to Move To.
#
#MoveToThisFolder=

### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################


import sys
import os
import subprocess
import shutil

scriptVersionIs = 1.9

POSTPROCESS_SUCCESS=93
POSTPROCESS_ERROR=94
POSTPROCESS_NONE=95

required_options = ('NZBPO_INDEXINSYNODLNA', 'NZBPO_MOVEMERGESUBFOLDER', 'NZBPO_MOVETOTHISFOLDER')
for optname in required_options:
	if (not optname in os.environ):
		print('[ERROR] Option %s is missing in configuration file. Please check script settings' % optname[6:])
		sys.exit(POSTPROCESS_ERROR)


IndexInSynoDLNA = os.environ['NZBPO_INDEXINSYNODLNA']
MoveMergeSubFolder = os.environ['NZBPO_MOVEMERGESUBFOLDER']
MoveToThisFolder = os.environ['NZBPO_MOVETOTHISFOLDER']
nzbDir = os.environ['NZBPP_DIRECTORY']


def renameFunc(fullPath, fileDirName):
    encodingDetected = False
    # parsing all files/directories in odrer to detect if CP850 is used
    for Idx in range(len(fileDirName)):
        # /!\ detection is done 2char by 2char for UTF-8 special character
        if (len(fileDirName) != 1) & (Idx < (len(fileDirName) - 1)):
            # Detect UTF-8
            if ((fileDirName[Idx] == '\xC2') | (fileDirName[Idx] == '\xC3')) & ((fileDirName[Idx+1] >= '\xA0') & (fileDirName[Idx+1] <= '\xFF')):
                print "[INFO] " + os.path.join(fullPath, fileDirName) + " -> UTF-8 detected: Nothing to be done"
                encodingDetected = True
                break;
            # Detect CP850
            elif ((fileDirName[Idx] >= '\x80') & (fileDirName[Idx] <= '\xA5')):
                utf8Name = fileDirName.decode('cp850')
                utf8Name = utf8Name.encode('utf-8')
                os.rename(os.path.join(fullPath, fileDirName), os.path.join(fullPath, utf8Name))
                print "[INFO] " + os.path.join(fullPath, utf8Name) + " -> CP850 detected: Renamed"
                encodingDetected = True
                break;
            # Detect ISO-8859-15
            elif (fileDirName[Idx] >= '\xA6') & (fileDirName[Idx] <= '\xFF'):
                utf8Name = fileDirName.decode('iso-8859-15')
                utf8Name = utf8Name.encode('utf-8')
                os.rename(os.path.join(fullPath, fileDirName), os.path.join(fullPath, utf8Name))
                print "[INFO] " + os.path.join(fullPath, utf8Name) + " -> ISO-8859-15 detected: Renamed"
                encodingDetected = True
                break;
        else:
            # Detect CP850
            if ((fileDirName[Idx] >= '\x80') & (fileDirName[Idx] <= '\xA5')):
                utf8Name = fileDirName.decode('cp850')
                utf8Name = utf8Name.encode('utf-8')
                os.rename(os.path.join(fullPath, fileDirName), os.path.join(fullPath, utf8Name))
                print "[INFO] " + os.path.join(fullPath, utf8Name) + " -> CP850 detected: Renamed"
                encodingDetected = True
                break;
            # Detect ISO-8859-15
            elif (fileDirName[Idx] >= '\xA6') & (fileDirName[Idx] <= '\xFF'):
                utf8Name = fileDirName.decode('iso-8859-15')
                utf8Name = utf8Name.encode('utf-8')
                os.rename(os.path.join(fullPath, fileDirName), os.path.join(fullPath, utf8Name))
                print "[INFO] " + os.path.join(fullPath, utf8Name) + " -> ISO-8859-15 detected: Renamed"
                encodingDetected = True
                break;
    if (encodingDetected == False):
        print "[INFO] " + os.path.join(fullPath, fileDirName) + " -> No special characters detected: Nothing to be done"
    return

# add folder in the Syno index database (DLNA server)
def addToSynoIndex(DirName):
    print "[INFO] 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()
        if (str(out) == ''):
            print("[INFO] Synoindex result: " + DirName + " successfully added to Synology database")
        else:
            print("[INFO] Synoindex result: " + str(out))
        if (str(err) != ''):
            print("[ERROR] Synoindex failed: " + str(err))
            sys.exit(POSTPROCESS_ERROR)
    except OSError, e:
        print("[ERROR] Unable to run synoindex: "+str(e))
        sys.exit(POSTPROCESS_NONE)
    return

print "[INFO] Launching CharTranslator Python script (v%s) ..." % scriptVersionIs



# process each sub-folders starting from the deepest level
print "[INFO] Renaming folders to UTF-8 format..."
for dirname, dirnames, filenames in os.walk(nzbDir, topdown=False):
    for subdirname in dirnames:
        renameFunc(dirname, subdirname)
print "[INFO] Folder renaming Done !"

# process each file recursively
print "[INFO] Renaming files to UTF-8 format..."
for dirname, dirnames, filenames in os.walk(nzbDir):
    for filename in filenames:
        renameFunc(dirname, filename)
print "[INFO] Files renaming Done !"

# Move current folder to an another destination if the option has been configured
if (MoveToThisFolder != ''):
    print "[INFO] Moving folder: " + nzbDir + "to: " + 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, 0777)
    # Check write access
    if (os.access(MoveToThisFolder, os.W_OK) == False):
        print "[ERROR] File(s)/Folder(s) cannot be moved to %s" % (MoveToThisFolder)
        print "[ERROR] Please, check Unix permissions"
        sys.exit(POSTPROCESS_ERROR)

    # 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(nzbDir)[-1])
    if (MoveMergeSubFolder=='yes'):
        print "[INFO] Merge option is ON (incremental copy)"
        try:
            synoCopy_cmd = ['/bin/cp', '-Rpf', os.path.abspath(nzbDir), MoveToThisFolder]
            p = subprocess.Popen(synoCopy_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
            if (str(err) != ''):
                print("[ERROR] Copy failed: " + str(err))
                sys.exit(POSTPROCESS_ERROR)
        except OSError, e:
            print("[ERROR] Unable to run cp: " + str(e))
            sys.exit(POSTPROCESS_ERROR)
        os.chdir(MoveToThisFolder)
        shutil.rmtree(nzbDir)
    # 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(nzbDir, MoveToThisFolder)
        os.chdir(MoveToThisFolder)
    # Update currentFolder variable
    os.chdir(destFolder)
    nzbDir = os.getcwd()

# Add multimedia files in the Syno DLNA if the option has been enabled
if (IndexInSynoDLNA == 'yes'):
    addToSynoIndex(nzbDir)

print "[INFO] Character encoding translation done!"
sys.exit(POSTPROCESS_SUCCESS)
Attachments
CharTranslatorGet.py
(8.99 KiB) Downloaded 850 times

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests