-
Notifications
You must be signed in to change notification settings - Fork 0
/
maketitles.py
99 lines (87 loc) · 2.48 KB
/
maketitles.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
import os
import sys
from optparse import OptionParser
class TitleMaker():
def __init__(self, delim, tpos, apos):
self.delim = delim
self.tpos = tpos
self.apos = apos
def titleize(self, instring):
outstring = ''
vals = instring.split()
for val in vals:
outstring = "%s %s" % (outstring, val.capitalize())
return outstring.lstrip()
def maketitle(self, filename):
file, ext = os.path.splitext(filename)
chunks = file.split(self.delim)
if len(chunks) >= 2:
outstring = "%s|%s|%s" % (
filename.strip(),
self.titleize(chunks[self.tpos].strip()),
self.titleize(chunks[self.apos].strip())
)
return outstring
#if len(chunks) >= 2:
# print "%s|%s|%s" % (
# filename.strip(),
# titleize(chunks[-2].split('.')[-1].strip()),
# titleize(chunks[-1].split('.')[0].strip()),
# )
#else:
# print "%s|%s|" % (
# filename.strip(),
# titleize(chunks[-1].split('.')[0].strip()),
# )
def maketitles(self, files):
titles = []
for file in files:
if file.endswith('.cdg'):
titles.append(self.maketitle(file))
if file.endswith('.zip'):
titles.append(
self.maketitle(os.path.join(
file,
file.replace('.zip', '.cdg')
))
)
return titles
def main():
parser = OptionParser()
parser.add_option(
"-d",
"--delimiter",
action = "store",
help = "Character(s) seperating each item",
default = "-"
)
parser.add_option(
"-t",
"--title_pos",
action = "store",
help = "Position of song title in file path",
default = "1"
)
parser.add_option(
"-a",
"--artist_pos",
action = "store",
help = "Position of artist name in file path",
default = "0"
)
opts, args = parser.parse_args()
if args > 1:
path = "."
else:
path = args[0]
files = os.listdir(path)
tm = TitleMaker(
opts.delimiter,
int(opts.title_pos),
int(opts.artist_pos)
)
titles = tm.maketitles(files)
for title in titles:
print title
if __name__ == "__main__":
main()