spacepaste

  1.  
  2. #!/usr/local/bin/python
  3. from __future__ import print_function
  4. try:
  5. from send2trash import *
  6. except:
  7. pass
  8. import shutil
  9. import subprocess
  10. import os
  11. import shlex
  12. _author_ = "Devataa"
  13. '''
  14. READ ME:
  15. 1: Install latest version of XLD for Mac from http://tmkk.undo.jp/xld/index_e.html
  16. 2: Open the DMG, drag and drop the XLD.app into your /Applications directory, drag and drop the XLD.sh file within the CLI directory into your /Application directory too
  17. 3: Run XLD.app and configure to your preference as to what format and settings to output the file to. Make sure your output directory is set to "/Users/<your_username>/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ iTunes" for automatic iTunes import
  18. 4: Save profile (top menu)
  19. 5: Configure a category for "Music" in SabNZBD and under "Folders" tab on the left, assign a post processing folder holding this .py script to run
  20. 6: Download an album, say a FLAC album and categorize it as Music and see if it works! The album can be snatched by Headphones too and pushed to SABnzbd and then have this script post process the file. Remeber to disable the Post processing in Headphones though!
  21. '''
  22. class LosslesstoALAC:
  23. def __init__(self):
  24. #List of all FLAC files to be converted where the key is the original folder directory and values are the full file directories
  25. self.FLAC = []
  26. #list of Directories to Delete
  27. self.Dir_Remove = []
  28. self.File_Remove = []
  29. #Root path for scanning and xld shell script and xld profile name
  30. self.path = "[INSERT MUSIC DOWNLOADS DIRECTORY HERE]" #This is the path where "Music" category files are saved from sabNZBD
  31. if self.path[-1] == "/":
  32. self.path = self.path[:-1]
  33. self.xldpath = "[INSERT WHERE XLD.SH DIRECTORY]" #This is the path where the xld CLI .sh file is located to run XLD from command prompt
  34. self.xldprofile = "[INSERT XLD PROFILE]" #This is the saved profile setting to which all files will be transcoded to
  35. #Note it is assumed that the output directory in the XLD profile being used is set to "/Users/<your_username>/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ iTunes" for automatic iTunes import
  36. self.num_dir = len(self.path.split("/"))
  37. #Audio formats to scan for in the download (path) directory
  38. self.scanfrmt = [".flac", ".wav"] #This will scan the downlaod directory for all .flac files. You can append any other formats like .wav to this list
  39. self.command = "sh " + self.xldpath + " --profile " + self.xldprofile + " " #The first part is the location of your xld.sh file found in the CLI folder from the XLD.dmg install image
  40. #1: Get file names and directories
  41. self.GetContent()
  42. #2 Convert the files using XLD CLI and export them to the "automatically add to iTunes Folder"
  43. self.ConvertAudio()
  44. #3 Remove the original files to Trash (using send2trash module) or directly from HDD
  45. #insert switch statement if send2trash module is found
  46. #self.Move2Trash()
  47. self.DeleteFolder()
  48. def GetContent(self):
  49. for root, dirs, files in os.walk(self.path, topdown=False, onerror=None, followlinks=False):
  50. #print("this is root, dir, files: ", root, dirs, files)
  51. for file in files:
  52. #print(file)
  53. for frmt in self.scanfrmt:
  54. if file.endswith(frmt):
  55. #print("This file ends in: ", frmt)
  56. file = os.path.join(root,file)#.replace(" ", r'\ ') #How do I replace " " with "\ " in a string incase there is a folder with a space in its name?!?!
  57. pathsplit = file.split("/")
  58. remove_Dir = "/".join(pathsplit[:self.num_dir+1]) + "/"
  59. #If the FLAC file is in a subfolder in the root Music directory, remove the upper most directory with all its contents
  60. if remove_Dir != self.path:
  61. # print(remove_Dir)
  62. # print(file)
  63. if not self.Dir_Remove or self.Dir_Remove[-1] != remove_Dir:
  64. self.Dir_Remove.append(remove_Dir)
  65. self.FLAC.append(file)
  66. #if the FLAC file is in the root Music directory, remove only the file
  67. elif remove_Dir == self.path:
  68. if not self.File_Remove or self.File_Remove[-1] != remove_Dir:
  69. self.File_Remove.append(file)
  70. self.FLAC.append(file)
  71. def ConvertAudio(self):
  72. print("Converting Files:")
  73. for file in self.FLAC:
  74. print(file)
  75. command = self.command + file.replace(' ', '\ ')
  76. status = subprocess.call(shlex.split(command))
  77. #print("This is the status: ", status)
  78. #if status == False:
  79. #Insert logging capabilities here
  80. def Move2Trash(self):
  81. for item in self.Dir_Remove:
  82. send2trash(item)
  83. def DeleteFolder(self):
  84. print("Removing Directory/ Files: ")
  85. for item in self.Dir_Remove:
  86. print(item)
  87. shutil.rmtree(item)
  88. for item in self.File_Remove:
  89. print(item)
  90. os.remove(item)
  91. LosslesstoALAC()
  92.