-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.hs
174 lines (156 loc) · 6.66 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{-
TODO
* might be good to copy files using system calls rather than by creating a shell script. (Wouldn't have to use wildcards for Unicode characters)
* flag to delete files
* filter out some of the rsync output
-}
{-
Copyright (c) Timothy Chevalier, 2011
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-}
{-# OPTIONS -Wall #-}
module Main(main) where
import Prelude hiding (catch)
import Control.Exception
import Control.Monad
import Data.Char
import System.Cmd
import System.Console.GetOpt
import System.Directory
import System.Environment
import System.Exit
import System.FilePath
import System.IO
import Applescript
import Config
import Debug
import Utils
import Flags
playlistsToFilenames :: [Flag] -> [String] -> IO [(FilePath,[FilePath])]
playlistsToFilenames flags fs = do
tmpD <- tempDir flags
mapM (go tmpD) fs
where go tmpD playListName = do
outFile <- uniqify $ tmpD </> playListName <.> "m3u"
let script = playlistScript playListName outFile
debug script
(appleScriptName,hdl_as) <- mkSysTempFileName "applescript"
hPutStrLn hdl_as script
hClose hdl_as
makeExecutable appleScriptName
exitCode <- system appleScriptName
removeAFile flags appleScriptName
case exitCode of
ExitSuccess -> do
songFiles <- (liftM lines) $ readFile outFile
rewriteM4a outFile
return (outFile,
map m4aHack songFiles)
_ -> error ("Failed to extract playlist "
++ playListName ++ ": " ++ show exitCode)
rewriteM4a = mapFile m4aHack
m4aHack fn | takeExtension fn == ".m4a" =
case m4aParentDir of
Just d ->
replaceExtension
(d </> (joinPath (drop 6 (splitPath fn))))
"mp3"
Nothing -> fn
m4aHack fn = fn
makeExecutable :: FilePath -> IO ()
makeExecutable fn = do
perms <- getPermissions fn
setPermissions fn (perms{executable=True})
musicPlayerPlaylistRoot :: FilePath
musicPlayerPlaylistRoot = musicPlayerRoot </> "Playlists"
musicSubdir :: FilePath
musicSubdir = "Music"
mkTempFileName :: [Flag] -> String -> IO (String,Handle)
mkTempFileName flags s = do
parent <- tempDir flags
parentExists <- doesDirectoryExist parent
putStrLn ("mkTempFileName: " ++ show (parent, parentExists))
unless parentExists
(throwIO (userError ("directory doesn't seem to exist: " ++ parent)))
outF <- uniqify $ parent </> ("hplaylist_" ++ s)
h <- openFile outF WriteMode
return (outF, h)
albumArtistTrack :: FilePath -> (FilePath,FilePath,FilePath)
albumArtistTrack fn = (case reverse parts of
(track:album:artist:_) -> (artist,album,track)
_ -> error ("error parsing filename " ++ show fn))
where parts = splitPath fn
generateCopyScript :: [Flag] -> [String] -> IO ()
generateCopyScript flags playlistNames = do
allMusicFiles <- playlistsToFilenames flags playlistNames
let playlistFileNames = fst (unzip allMusicFiles)
(do debug (show allMusicFiles)
mapM_ fixPaths (fst (unzip allMusicFiles))
(copyScriptName,hdl) <- mkTempFileName flags "copyit"
let copyScript = "#!/bin/bash\n" ++ concatMap (\ (playlistFn,songs) ->
(rsyncCommand (isQuiet flags) playlistFn musicPlayerPlaylistRoot ++
concatMap (\ s -> let (artist,album,_) = albumArtistTrack s
parent = musicPlayerRoot </> musicSubdir </> artist </> album in
"mkdir -p " ++ (escape parent) ++ "\n"
++ rsyncCommand (isQuiet flags) s parent) songs))
allMusicFiles
hPutStrLn hdl copyScript
hClose hdl
(do makeExecutable copyScriptName
if (isDryRun flags)
then putStrLn ("To copy the files, execute the script " ++ copyScriptName)
else do
res <- system copyScriptName
removeFilesOnExit flags (copyScriptName:playlistFileNames)
case res of
ExitFailure _ -> putStrLn ("An error occurred: " ++ show res) >>
exitWith res
_ -> putStrLn ("Processed " ++ show (sum (map (length . snd)
allMusicFiles)) ++ " files"))
`catch` (myHandler flags copyScriptName))
`onException` (deleteFiles flags playlistFileNames)
myHandler :: [Flag] -> FilePath -> IOException -> IO a
myHandler flags copyScriptName ex = do
removeAFile flags copyScriptName
throw ex
-- :-(
windowsPath :: FilePath -> FilePath
windowsPath = map (\ c -> if c == '/' then '\\' else c)
fixPaths :: FilePath -> IO ()
fixPaths fn = do
debug ("fixPaths: " ++ fn)
hdl <- openFile fn ReadMode
tracks <- (liftM lines) (hGetContents hdl)
let new = map (\ fp -> let (artist,album,track) = albumArtistTrack fp in
-- Sansa Clip requires a filepath relative to the root, like:
-- Music\Billy Bragg\Don't Try This At Home\Sexuality
-- You can't leave off the Music\ or have a \ at the beginning.
-- You may need to adjust the format for other players.
windowsPath $ joinPath [musicSubdir,artist,album,track]) tracks
-- The seq is important, else we'll delete the file before we read it
length new `seq` hClose hdl
removeFile fn
debug ("contents = " ++ show tracks)
debug ("result = " ++ unlines new)
hdlOut <- openFile fn WriteMode
hPutStrLn hdlOut (unlines new)
hClose hdlOut
main :: IO ()
main = do
args <- getArgs
case getOpt Permute options args of
(opts, playlistNames, _) ->
case playlistNames of
[] -> putStrLn (usageInfo "hplaylist" options) >>
exitWith (ExitFailure 1)
_ -> debug ("playlists = " ++ show playlistNames) >>
generateCopyScript opts playlistNames