-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_playing_file
executable file
·55 lines (42 loc) · 1.07 KB
/
copy_playing_file
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
#!/usr/bin/env julia
#= 2018-02-05 First script in Julia! =#
using PyCall
@pyimport mpd
function get_client(addr, port, password=nothing)
client = mpd.MPDClient(use_unicode=true)
client[:connect](addr, port)
if password != nothing
client[:password](password)
end
return client
end
function playing_file_rel(client)
rel_path = client[:currentsong]()["file"]
return rel_path
end
function music_root(conf="/.config/mpd/mpd.conf")
fn = homedir() * conf
for ln in eachline(fn)
if ismatch(r"music_directory", ln)
return expanduser(split(ln,'"')[2])
end
end
throw(KeyError())
end
function main(tgt = "music/from_mpd")
mobile_dir = ENV["MOBILE_DIR"]
port = ENV["MPD_PORT"]
host = split(ENV["MPD_HOST"],'@')
password = length(host) == 2 ? host[1] : nothing
addr = host[end]
abs_path_tgt = joinpath(mobile_dir, tgt)
mkpath(abs_path_tgt)
root = music_root()
client = get_client(addr, port, password)
from = joinpath(root, playing_file_rel(client))
tgt = joinpath(abs_path_tgt, basename(from))
try
cp(from, tgt)
end
end
main()