/spk/sabnzbd/src/SabToSyno.py

https://github.com/SynoCommunity/spksrc · Python · 138 lines · 80 code · 12 blank · 46 comment · 16 complexity · e3cd88cf81e2fd6b1fae52b4f2dce9ed MD5 · raw file

  1. #!/usr/local/python3
  2. #
  3. # Created by LapinFou.
  4. # https://forums.sabnzbd.org/viewtopic.php?f=9&p=122338
  5. #
  6. # date | version | comment
  7. #---------------------------------------
  8. # 2020-09-13 | 1.0 | Initial version
  9. # 2020-09-19 | 1.1 | Code clean-up
  10. # 2020-09-25 | 1.2 | Removed 7zip recursive unpack option
  11. #
  12. # get library modules
  13. import sys
  14. import os
  15. import subprocess
  16. import shutil
  17. scriptVersionIs = 1.2
  18. # If empty, then no move
  19. # Format must be synology full path (case sensitive). For ex: /volume1/video/News
  20. MoveToThisFolder = ''
  21. # If MoveMergeSubFolder = False, then equivalent to unix command:
  22. # mv -rf srcFolder destFolder
  23. # In case of conflict between an already existing sub-folder in the destination folder:
  24. # the destination sub-folder will be replaced with source sub-folder
  25. #
  26. # If MoveMergeSubFolder = True, then equivalent to unix command:
  27. # cp -rf srcFolder/* destFolder/
  28. # rm -rf srcFolder
  29. # In case of conflict between an already existing sub-folder in the destination folder:
  30. # the destination sub-folder will be merged with source sub-folder (kind of incremental)
  31. MoveMergeSubFolder = True
  32. ########################
  33. # ----- Functions ---- #
  34. ########################
  35. # Get information from SABnzbd
  36. try:
  37. (scriptname,directory,orgnzbname,jobname,reportnumber,category,group,postprocstatus,url) = sys.argv
  38. except:
  39. print("No commandline parameters found")
  40. sys.exit(1)
  41. # add folder in the Syno index database (DLNA server)
  42. def addToSynoIndex(DirName):
  43. print("Adding folder in the DLNA server")
  44. synoindex_cmd = ['/usr/syno/bin/synoindex', '-A', DirName]
  45. try:
  46. p = subprocess.Popen(synoindex_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  47. out, err = p.communicate()
  48. out = out.decode('ascii')
  49. err = err.decode('ascii')
  50. if (str(out) == ''):
  51. print("synoindex result: " + DirName + " successfully added to Synology database")
  52. else:
  53. print("synoindex result: " + str(out))
  54. if (str(err) != ''):
  55. print("synoindex failed: " + str(err))
  56. except OSError as e:
  57. print("Unable to run synoindex: "+str(e))
  58. return
  59. ########################
  60. # --- Main Program --- #
  61. ########################
  62. print("Running SabToSyno Python3 script (v%s)" %scriptVersionIs)
  63. print("")
  64. # Change current directory to SABnzbd "complete" directory
  65. os.chdir(directory)
  66. # display directory of the SABnzbd job
  67. currentFolder = os.getcwd()
  68. print("Current folder is " + currentFolder)
  69. # Move current folder to an another destination if the option has been configured
  70. if (MoveToThisFolder != ''):
  71. print("")
  72. print(100*'-')
  73. print("Moving folder:")
  74. print(currentFolder)
  75. print("to:")
  76. print(MoveToThisFolder)
  77. # Check if destination folder does exist and can be written
  78. # If destination doesn't exist, it will be created
  79. if (os.access(MoveToThisFolder, os.F_OK) == False):
  80. os.makedirs(MoveToThisFolder)
  81. os.chmod(MoveToThisFolder, 0o777)
  82. # Check write access
  83. if (os.access(MoveToThisFolder, os.W_OK) == False):
  84. print(100*'#')
  85. print("File(s)/Folder(s) can not be move in %s" %(MoveToThisFolder))
  86. print("Please, check Unix permissions")
  87. print(100*'#')
  88. sys.exit(1)
  89. # If MoveMergeSubFolder is True, then move all file(s)/folder(s) to destination
  90. # then remove source folder
  91. destFolder = os.path.join(MoveToThisFolder, os.path.split(currentFolder)[-1])
  92. if (MoveMergeSubFolder):
  93. print(" Info: Merge option is ON (incremental copy)")
  94. try:
  95. synoCopy_cmd = ['/bin/cp', '-Rpf', os.path.abspath(currentFolder), MoveToThisFolder]
  96. p = subprocess.Popen(synoCopy_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  97. out, err = p.communicate()
  98. out = out.decode('ascii')
  99. err = err.decode('ascii')
  100. if (str(err) != ''):
  101. print("Copy failed: " + str(err))
  102. sys.exit(1)
  103. except OSError as e:
  104. print("Unable to run cp: " + str(e))
  105. sys.exit(1)
  106. os.chdir(MoveToThisFolder)
  107. shutil.rmtree(currentFolder)
  108. # If MoveMergeSubFolder is False, remove folder with same (if exists)
  109. # then move all file(s)/folder(s) to destination and remove source folder
  110. else:
  111. print(" Info: Merge option is OFF (existing data will be deleted and replaced)")
  112. # Remove if destination already exist
  113. if os.path.exists(destFolder):
  114. shutil.rmtree(destFolder)
  115. shutil.move(currentFolder, MoveToThisFolder)
  116. else:
  117. # If move is not enabled, the current folder will be indexed
  118. destFolder = currentFolder
  119. # Add multimedia files in the Syno DLNA
  120. print("")
  121. print(100*'-')
  122. addToSynoIndex(destFolder)
  123. print("")
  124. print("Moving and indexing file(s) done!")
  125. # Success code
  126. sys.exit(0)