xspf2m3u

simple XSPF to M3U conversion
git clone https://git.ce9e.org/xspf2m3u.git

commit
f01110dd2ae907be23b4ed797378b225ecbe6d73
parent
0cf4cda7ba459ad5764fe4ba81aca96e758f1155
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-03-06 16:26
add --outdir

Diffstat

M xspf2m3u.py 27 +++++++++++++++++++++++++--

1 files changed, 25 insertions, 2 deletions


diff --git a/xspf2m3u.py b/xspf2m3u.py

@@ -57,7 +57,12 @@ def search_youtube(q):
   57    57 		_format = next(ydl_selector({'formats': info['formats']}))
   58    58 	except StopIteration:
   59    59 		return None
   60    -1 	return _format['url']
   -1    60 	filename = '{}-{}.{}'.format(
   -1    61 		info['title'].replace('/', '_'),
   -1    62 		info['id'],
   -1    63 		_format['ext'],
   -1    64 	)
   -1    65 	return _format['url'], filename
   61    66 
   62    67 
   63    68 def iter_tracks(src):
@@ -73,11 +78,20 @@ def iter_tracks(src):
   73    78 		yield track
   74    79 
   75    80 
   -1    81 def hard_link(location, outdir):
   -1    82 	os.makedirs(outdir, exist_ok=True)
   -1    83 	filename = os.path.basename(location)
   -1    84 	path = os.path.join(outdir, filename)
   -1    85 	os.link(location, path)
   -1    86 	return path
   -1    87 
   -1    88 
   76    89 def parse_args():
   77    90 	parser = argparse.ArgumentParser()
   78    91 	parser.add_argument('src')
   79    92 	parser.add_argument('folder')
   80    93 	parser.add_argument('-Y', '--youtube', action='store_true')
   -1    94 	parser.add_argument('-O', '--outdir')
   81    95 	return parser.parse_args()
   82    96 
   83    97 
@@ -85,6 +99,9 @@ def main():
   85    99 	args = parse_args()
   86   100 	files = list(iter_files(args.folder))
   87   101 
   -1   102 	if args.outdir:
   -1   103 		os.makedirs(args.outdir)
   -1   104 
   88   105 	for track in iter_tracks(args.src):
   89   106 		location = track['location']
   90   107 
@@ -92,9 +109,15 @@ def main():
   92   109 			context_key = ['creator', 'annotation']
   93   110 			context = [track[k] for k in context_key if track[k]]
   94   111 			location = find_by_title(track['title'], context, files)
   -1   112 			if location and args.outdir:
   -1   113 				location = hard_link(location, args.outdir)
   95   114 
   96   115 		if location is None and args.youtube and youtube_dl:
   97    -1 			location = search_youtube([q for q in track.values() if q])
   -1   116 			url, filename = search_youtube([q for q in track.values() if q])
   -1   117 			if args.outdir:
   -1   118 				location = os.path.join(args.outdir, filename)
   -1   119 			else:
   -1   120 				location = url
   98   121 
   99   122 		if location is None:
  100   123 			s = ' - '.join('{}: {}'.format(k, v) for k, v in track.items())