blob: e319b80b9f036791e53a63c141e70dbfedc67c4b (
plain)
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
|
module Sound.MusicDirTrans.File
( getMetadata
, trackFilePatterns
, mkArtistPath
)
where
import Control.Monad
import Control.Monad.IO.Class
import Data.List as L
import Data.Monoid
import Data.Ord
import Sound.HTagLib
import Sound.MusicDirTrans.Type
import System.Directory
import System.FilePath.Glob
import Data.Text as T
metadataGetter :: TagGetter Metadata
metadataGetter = Metadata <$> artistGetter <*> albumGetter <*> yearGetter
getMetadata :: MonadIO m => FilePath -> m Metadata
getMetadata path = getTags path metadataGetter
trackFileExt :: [String]
trackFileExt =
[ "*.flac"
, "*.wav"
, "*.mp3"
, "*.mp4"
, "*.asf"
, "*.aiff"
, "*.mpc"
, "*.spx"
, "*.tt"
, "*.wv"
]
trackFilePatterns :: [Pattern]
trackFilePatterns = L.map compile trackFileExt
mostCommon :: Ord c => (a -> c) -> [a] -> c
mostCommon e metadata =
L.head . maximumBy (comparing L.length) . L.group . sort $ L.map
e
metadata
mostCommonArtist :: [Metadata] -> T.Text
mostCommonArtist = mostCommon (unArtist . mArtist)
mostCommonAlbum :: [Metadata] -> T.Text
mostCommonAlbum = mostCommon (unAlbum . mAlbum)
mostCommonYear :: [Metadata] -> Maybe Int
mostCommonYear = mostCommon (fmap unYear . mYear)
mkArtistPath :: FilePath -> [Metadata] -> ArtistPath
mkArtistPath path metadata = ArtistPath path parentPath childPath
where
parentPath = unpack $ mostCommonArtist metadata
album = mostCommonAlbum metadata
year = mostCommonYear metadata
childPath = unpack $ mkNewPathName album year
mkNewPathName :: Text -> Maybe Int -> Text
mkNewPathName album year = append album year'
where
year' = case year of
Nothing -> pack ""
Just y -> pack $ " (" ++ show y ++ ")"
|