This repository has been archived by the owner on Oct 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist_to_mpd.py
119 lines (95 loc) · 3.4 KB
/
playlist_to_mpd.py
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
#!/usr/bin/env python2
import os
import codecs
import chardet
import logging
import mpd
import sys
"""
Usage: ./playlist_to_mpd.py <identifier>
Clears MPD's playlist and adds everything from playlist.m3u
"""
playlist_path = "/mnt/mckillican/automation/"
logging.basicConfig(level=logging.DEBUG)
# Read playlist file
identifier = sys.argv[1]
#with open(playlist_file) as f:
playlist_file = None
for prefix in ["Cycle_", "CYCLE_"]:
for extension in [".m3u", ".M3U"]:
guessfile = prefix + identifier + extension
guesspath = os.path.join(playlist_path, guessfile)
if os.path.isfile(guesspath):
playlist_file = guesspath
break
if playlist_file is not None:
# Escape the guessing game
break
if playlist_file == None:
logging.Error("Unable to find playlist specified by " + identifier)
exit()
with open(playlist_file) as f:
encoding = chardet.detect(f.read())
logging.debug("Detected character set as " + encoding["encoding"] + " with " + str(encoding["confidence"]) + " certainty")
if encoding["confidence"] > 0.9:
encoding = encoding["encoding"]
else:
logging.debug("Not certain enough, falling back on iso-8859-1")
encoding = "iso-8859-1"
with open(playlist_file) as f:
playlist_items = f.readlines()
logging.debug("Reencoding playlist")
for i in xrange(len(playlist_items)):
utfitem = playlist_items[i].decode(encoding).encode("utf8")
if playlist_file[0:4] != 'unix':
logging.debug("Non unix m3u, making unix file path")
playlist_items[i] = utfitem.replace('\\','/').replace('Z:/','/mnt/mckillican/automation/')
else:
playlist_items[i] = utfitem
# Connection
logging.debug("Connecting to MPD")
client = mpd.MPDClient()
client.timeout = 10
client.idletimeout = None
client.connect("localhost", 6600)
logging.debug("MPD Client Ver: " + client.mpd_version)
# Update client in case anything has changed in automation recently
logging.debug("Updating MPD")
try:
client.update()
except:
pass
# clear stored playlist of the same name if it exists
playlist_name = os.path.basename(playlist_file)
logging.debug("New playlist: " + playlist_name)
(root, ext) = os.path.splitext(playlist_name)
playlist_name = root
if playlist_name in [playlist["playlist"] for playlist in client.listplaylists()]:
logging.debug("Clearing old version of the playlist")
client.playlistclear(playlist_name)
else:
logging.debug("No previous playlist found")
logging.info("Updating playlist: " + playlist_name)
# add files from the given playlist into an MPD-stored playlist.
logging.debug("Adding {0} tracks".format(len(playlist_items)))
for track in playlist_items:
if track[:7] == '#EXTINF' or track[:7] == '#EXTM3U':
continue
reltrack = os.path.relpath(track, '/mnt/mckillican/automation/').rstrip()
logging.debug("adding: " + reltrack)
try:
client.playlistadd(playlist_name, reltrack)
except:
logging.info("Could not add: " + reltrack)
# clear playlist
logging.info("Switching playlist: " + playlist_name)
logging.debug("Clearing current playlist except currently playing song")
#client.clear()
status = client.status()
client.delete(0, int(status["song"]))
status = client.status()
client.load(playlist_name)
logging.debug("Loading new playlist: " + playlist_name)
client.delete((1, status["playlistlength"]))
logging.debug("Closing client")
client.close()