File: shc.hs

package info (click to toggle)
shell-fm 0.7%2Bgit20100414-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 444 kB
  • ctags: 305
  • sloc: ansic: 4,422; makefile: 135; python: 80; haskell: 76; sh: 67; perl: 19
file content (110 lines) | stat: -rw-r--r-- 2,634 bytes parent folder | download | duplicates (2)
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

-- Shell.FM control tool.
-- Copyright (C) 2008-2009 by Jonas Kramer.
-- Published under the terms of the GNU General Public License (GPL).

import Network.Socket
import System.Environment
import Data.List
import System.IO
import System.IO.Error

main = do
	args <- getArgs
	case args of
		[ "play", station ] -> playStation station
		[ "stop"          ] -> stopStation
		[ "skip"          ] -> skipTrack
		[ "next"          ] -> skipTrack
		[ "love"          ] -> loveTrack
		[ "ban"           ] -> banTrack
		[ "quit"          ] -> quit
		[ "pause"         ] -> pause
		[ "tag-artist", _ ] -> tagArtist $ tail args
		[ "tag-album", _  ] -> tagAlbum $ tail args
		[ "tag-track", _  ] -> tagAlbum $ tail args
		[ "info", format  ] -> formatString format
		[ "info"          ] -> formatString "%a - %t"
		[ "artist-tags"   ] -> artistTags
		[ "album-tags"    ] -> albumTags
		[ "track-tags"    ] -> trackTags
		[]                  -> formatString "Now playing \"%t\" by %a."
		_                   -> putStrLn "Please read the shell-fm manual for valid commands."


unixConnect = do
	socketPath <- getEnv "SHELLFM_UNIX_PATH"
	unix <- socket AF_UNIX Stream 0
	connect unix (SockAddrUnix socketPath)
	return unix


playStation station = do
	sendCommand $ "play " ++ (prefixedStation station)


stopStation = do sendCommand "stop"
skipTrack   = do sendCommand "skip"
loveTrack   = do sendCommand "love"
banTrack    = do sendCommand "ban"
quit        = do sendCommand "quit"
pause       = do sendCommand "pause"

artistTags  = do sendCommandWithReply $ "artist-tags"
albumTags   = do sendCommandWithReply $ "album-tags"
trackTags   = do sendCommandWithReply $ "track-tags"

tagArtist tagList = do
	tagCommand tagList "artist"

tagAlbum tagList = do
	tagCommand tagList "album"

tagTrack tagList = do
	tagCommand tagList "track"

tagCommand tagList item = do
	sendCommand $ "tag-" ++ item ++ (joinTags tagList)


formatString format = do
	sendCommandWithReply $ "info " ++ format


joinTags xs = join "," xs


join _ []     = ""
join _ (x:[]) = x
join s (x:xs) = x ++ s ++ (join s xs)


sendCommand command = do
	unix <- unixConnect
	result <- send unix (command ++ "\n")
	return ()


sendCommandWithReply command = do
	unix <- unixConnect
	result <- send unix (command ++ "\n")
	safeReceive unix
	return ()


safeReceive unix = do
	result <- try action
	putStrLn $ either handleError replyString result
	where
		action = recvLen unix 1024


handleError error = "No reply or error occured."
replyString (result, length) = result


prefixedStation station =
	if "lastfm://" `isPrefixOf` station
		then station
		else "lastfm://" ++ station