__author__ = 'Devataa'

from __future__ import print_function

#from send2trash import *

import shutil

import subprocess

import os

'''

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

5: Configure a category for "Music" in SabNZBD and post processing script to run, direct to this python script

6: Download an album, say a FLAC album 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

'''

class LosslesstoALAC:

def __init__ ( self ) :

#Dictionary 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 PATH HERE>" #This is the path where "Music" category files are saved from sabNZBD

self . xldpath = "/Applications/xld" #This is the path where the xld CLI .sh file is located to run XLD from command prompt

self . xldprofile = "<XLD SETTINGS PROFILE NAME>" #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" ] #This will scan the downlaod directory for all .flac files. You can append any other formats like .wav to this list

self . command = "sh /Applications/xld --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

#call function to gather all files in Audio Download directory and save parent directories in download path to later move to trash

#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

#self.Move2Trash()

self . DeleteFolder ( )

def GetContent ( self ) :

for root , dirs , files in os . walk ( self . path , topdown = False , onerror = None , followlinks = False ) :

#print(root, dirs, files)

for file in files:

# print(file)

for frmt in self . scanfrmt :

if file . endswith ( frmt ) :

file = os . path . join ( root , file ) . replace ( " " , " \ " ) #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 directory, remove the upper most directory

if remove_Dir != self . path :

self . Dir_Remove . append ( remove_Dir )

self . FLAC . append ( file )

#if the FLAC file is in the root directory, remove only the file

elif remove_Dir == self . path :

self . File_Remove . append ( file )

self . FLAC . append ( file )

#print(self.FLAC)

#print(self.Dir_Remove)

def ConvertAudio ( self ) :

for file in self . FLAC :

self . command = self . command + file

status = subprocess . call ( self . command , shell = True )

#if status == False:

#Insert logging capabilities here

#uncomment if you have the send2trash module installed and want to move your files to the trash ratehr than delte them

#def Move2Trash(self):

# for item in self.Dir_Remove:

# send2trash(item)

def DeleteFolder ( self ) :

for item in self . Dir_Remove :

print ( item )

shutil . rmtree ( item )

for item in self . File_Remove :

print ( item )

os . remove ( item )