-
Notifications
You must be signed in to change notification settings - Fork 3
/
combineplaylists.py
51 lines (36 loc) · 1.43 KB
/
combineplaylists.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
#!/usr/bin/python3
import argparse
import fnmatch
import os
def removeextinf(inputlist):
PATTERN = '#*'
matcheslist = fnmatch.filter(inputlist, PATTERN)
return [x for x in inputlist if x not in matcheslist]
def removeduplicates(mylist):
return list(set(mylist))
parser = argparse.ArgumentParser(description='Combine several playlists.')
parser.add_argument('-i', '--infile', required=True, action='append', type=str, help='Filename of playlist. Can be given multiple times.')
parser.add_argument('-o', '--outfile', required=True, type=str, help='Filename of output playlist')
parser.add_argument('-d', '--allowduplicates', action='store_true', help='Allow duplicate entries in output playlist.')
args = parser.parse_args()
print(args)
infilelist = args.infile
outfile = args.outfile
allowduplicates = args.allowduplicates
if os.path.exists(outfile):
print(f'Sorry, the output file {outfile} already exists. Stopping right there.')
exit(1)
# Create a list with all the lists combined
newplaylist = []
for infile in infilelist:
with open(infile, mode='r', encoding='utf-8-sig') as fp:
newplaylist.extend(removeextinf(fp.readlines()))
# Now remove duplicates
if not allowduplicates:
newplaylist = removeduplicates(newplaylist)
else:
pass
with open(outfile, mode="w", encoding='utf-8') as outfp:
outfp.write("#EXTM3U\n")
outfp.writelines(newplaylist)
print(f'Complete playlist written to {outfile}.')