-
- #!/usr/local/bin/python
-
- from __future__ import print_function
- try:
- from send2trash import *
- except:
- pass
-
- import shutil
- import subprocess
- import os
- import shlex
-
- _author_ = "Devataa"
- '''
- READ ME:
-
- 1: Install latest version of XLD for Mac from http://tmkk.undo.jp/xld/index_e.html
-
- 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
-
- 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
-
- 4: Save profile (top menu)
-
- 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
-
- 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!
-
- '''
-
- class LosslesstoALAC:
-
- def __init__(self):
-
- #List of all FLAC files to be converted where the key is the original folder directory and values are the full file directories
- self.FLAC = []
-
- #list of Directories to Delete
- self.Dir_Remove = []
- self.File_Remove = []
-
- #Root path for scanning and xld shell script and xld profile name
- self.path = "[INSERT MUSIC DOWNLOADS DIRECTORY HERE]" #This is the path where "Music" category files are saved from sabNZBD
-
- if self.path[-1] == "/":
- self.path = self.path[:-1]
-
- 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
- self.xldprofile = "[INSERT XLD PROFILE]" #This is the saved profile setting to which all files will be transcoded to
-
- #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
- self.num_dir = len(self.path.split("/"))
-
- #Audio formats to scan for in the download (path) directory
- 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
- 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
-
-
- #1: Get file names and directories
- self.GetContent()
- #2 Convert the files using XLD CLI and export them to the "automatically add to iTunes Folder"
- self.ConvertAudio()
- #3 Remove the original files to Trash (using send2trash module) or directly from HDD
- #insert switch statement if send2trash module is found
- #self.Move2Trash()
- self.DeleteFolder()
-
-
- def GetContent(self):
- for root, dirs, files in os.walk(self.path, topdown=False, onerror=None, followlinks=False):
- #print("this is root, dir, files: ", root, dirs, files)
- for file in files:
- #print(file)
- for frmt in self.scanfrmt:
- if file.endswith(frmt):
- #print("This file ends in: ", frmt)
- 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?!?!
- pathsplit = file.split("/")
- remove_Dir = "/".join(pathsplit[:self.num_dir+1]) + "/"
-
- #If the FLAC file is in a subfolder in the root Music directory, remove the upper most directory with all its contents
- if remove_Dir != self.path:
- # print(remove_Dir)
- # print(file)
-
- if not self.Dir_Remove or self.Dir_Remove[-1] != remove_Dir:
- self.Dir_Remove.append(remove_Dir)
-
- self.FLAC.append(file)
-
- #if the FLAC file is in the root Music directory, remove only the file
- elif remove_Dir == self.path:
-
-
- if not self.File_Remove or self.File_Remove[-1] != remove_Dir:
- self.File_Remove.append(file)
-
- self.FLAC.append(file)
-
- def ConvertAudio(self):
- print("Converting Files:")
- for file in self.FLAC:
- print(file)
- command = self.command + file.replace(' ', '\ ')
- status = subprocess.call(shlex.split(command))
- #print("This is the status: ", status)
- #if status == False:
- #Insert logging capabilities here
-
- def Move2Trash(self):
- for item in self.Dir_Remove:
- send2trash(item)
-
- def DeleteFolder(self):
- print("Removing Directory/ Files: ")
- for item in self.Dir_Remove:
- print(item)
- shutil.rmtree(item)
- for item in self.File_Remove:
- print(item)
- os.remove(item)
-
- LosslesstoALAC()
-