forked from adrienverge/rhythmbox_playlist_to_m3u
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rhythmbox_playlist_to_m3u.py
executable file
·103 lines (84 loc) · 2.86 KB
/
rhythmbox_playlist_to_m3u.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2013 Adrien Vergé
"""This script converts playlists stored by Rhythmbox to M3U files.
It reads information from ~/.local/share/rhythmbox/playlists.xml and converts
every 'static' playlist into a M3U file. If the new playlists are the same as
existing M3U files, they don't replace them.
This program has been written in September 2013 and works well with
Rhythmbox 2.99.1.
"""
__author__ = "Adrien Vergé"
__copyright__ = "Copyright 2013, Adrien Vergé"
__license__ = "GPL"
__version__ = "1.0"
import argparse
import filecmp
import os
import shutil
import sys
import re
import urllib.parse
import xml.etree.ElementTree
if sys.version_info < (3, 0):
raise Exception('This script is made for Python 3.0 or higher.')
def save_playlist(playlist, filename, outdir=None):
f = open(filename, 'w')
f.write('#EXTM3U\n')
for song in playlist:
if song.tag == 'location':
if song.text[0:8] == 'file:///':
songfile = song.text[7:]
songfile = urllib.parse.unquote(songfile)
# if needed calculate relative path
if not outdir == None:
songfile = os.path.relpath(songfile,outdir)
f.write(songfile+'\n')
else:
print('WARNING: unexpected path')
else:
print('WARNING: unexpected tag')
f.close()
def main():
"""This is the entry point of the script."""
def_pl = '.local/share/rhythmbox/playlists.xml'
if 'HOME' in os.environ:
def_pl = os.environ['HOME']+'/'+def_pl
# Parse arguments
parser = argparse.ArgumentParser(
description='Converts playlists stored by Rhythmbox to M3U files.')
parser.add_argument('-o', metavar='/path/to/output/dir',
help='output directory to save M3U playlists')
parser.add_argument('-i', metavar='/path/to/playlist.xml',
default=def_pl,
help='Rhythmbox playlist file, in XML format')
parser.add_argument('-r', '--relative', action='store_true',
help='Calculate relative path to songs insid m3u files')
args = parser.parse_args()
infile = args.i
outdir = args.o # '/docs/music/__playlists'
is_relative = args.relative
tmpdir = '/tmp'
if not outdir:
print('Error you have to specify an output dir')
sys.exit()
tree = xml.etree.ElementTree.parse(infile)
root = tree.getroot()
donesomething = False
for child in root:
if child.attrib['type'] == 'static':
filename = re.sub('[^\w\-_\. ]', '_', child.attrib['name'])+'.m3u'
save_playlist(child, tmpdir+'/'+filename, outdir if is_relative else None)
if os.path.exists(outdir+'/'+filename) and \
filecmp.cmp(tmpdir+'/'+filename, outdir+'/'+filename):
# The playlist has not changed
os.remove(tmpdir+'/'+filename)
else:
# Move to overwrite previous playlist
shutil.move(tmpdir+'/'+filename, outdir+'/'+filename)
print('Saving new "%s"' % filename)
donesomething = True
if not donesomething:
print('No modification made')
if __name__ == "__main__":
main()