-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_m3u.py
137 lines (105 loc) · 4.16 KB
/
generate_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
config = """
# the path to run the script against if no path was specified as an argument. leave blank to always require the commandline argument.
default_path:
# this script uses a keyword like "(Disc", (from something like "(Disc 1)" in the filename, used by NO-INTRO sets), to determine if a game is multi-disc.
disc_keyword: (Disc
# whether or not to skip generating m3u's for single-disc games. can be true or false
skip_singledisc: true
# the mode to operate in. choose from:
# samefolder: just create an m3u file for each multidisc game but leave the game files untouched
# gamefolders: create a folder per game and move the multidisc files into them, create the m3u's inside the folders. this is used by minUI
# subfolder: create one subfolder (such as .discs) and move all the game files into it, then create m3u's in the root folder. this is what muOS uses
mode: subfolder
# if you chose to use the subfolder mode, specify the name of that folder here
subfolder_name: .discs
"""
#############
# -----------
#############
import os
import sys
import configparser
# i don't like the [config] heading, so i'll just add it afterwards XD
config = f"[config]\n{config}"
confparse = configparser.ConfigParser()
confparse.read_string(config)
CONF = confparse['config']
# the usual input checking
if CONF['default_path'] and len(sys.argv) < 2:
target_path = CONF['default_path']
else:
if len(sys.argv) < 2:
print(f"usage: {sys.argv[0]} <target folder>")
sys.exit(0)
target_path = sys.argv[1]
if not os.path.isdir(target_path):
print("ERROR: invalid folder")
sys.exit(1)
if CONF['mode'] not in ('samefolder', 'gamefolders', 'subfolder'):
print("ERROR: incorrect mode set in config!")
sys.exit(1)
# get a list of all the files in the specified folder
os.chdir(target_path)
dirlist = os.listdir('.')
dirlist.sort() # make sure it's all in the right order
# create a mapping of all the files, laid out as a dictionary like this:
# {
# "Game Name (USA)": [
# "Game Name [USA] (Disc 1).chd"
# "Game Name [USA] (Disc 2).chd"
# "Game Name [USA] (Disc 3).chd"
# "Game Name [USA] (Disc 4).chd"
# ]
# }
mapping = {}
for filename in dirlist:
if os.path.isdir(filename): continue
filename_split = filename.rsplit('.', 1)
if len(filename_split) <= 1: continue
filename_noext, file_ext = filename_split
if file_ext.lower() == 'm3u':
continue
if file_ext.lower() in ('chd', 'bin', 'cue', 'iso'):
gamename_split = filename_noext.rsplit(CONF['disc_keyword'], 1)
if len(gamename_split) <= 1:
# skip roms that don't have multiple discs, if preferred
if CONF['skip_singledisc'] == 'true':
continue
pass
gamename = gamename_split[0].strip()
if gamename not in mapping.keys():
mapping[gamename] = []
mapping[gamename].append(filename)
# then use that mapping to generate the m3u's
for gamename, files in mapping.items():
m3u_path = gamename+'.m3u'
match CONF['mode']:
case "gamefolders" | "subfolder":
match CONF['mode']:
case "gamefolders":
target_folder = gamename
m3u_path = f"{target_folder}/{m3u_path}"
case "subfolder":
target_folder = CONF['subfolder_name']
if not os.path.isdir(target_folder):
os.mkdir(target_folder)
# actually move the files
for filename in files:
os.rename(filename, f"{target_folder}/{filename}")
files_m3u = []
for filename in files:
# skip the .bin files of a .bin .cue pair when writing the m3u's
if filename.rsplit('.')[1].lower() in ('bin'): continue
# modify the m3u so that it points to the subfolder
if CONF['mode'] == "subfolder":
filename = f"{target_folder}/{filename}"
files_m3u.append(filename)
with open(m3u_path, 'w') as fh:
fh.write("\n".join(files_m3u))
print(f"generated m3u for {gamename}")
if mapping:
print()
print("done!")
else:
print(f"no m3u's generated for folder {target_path}")