-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_tracks.py
132 lines (113 loc) · 4.13 KB
/
import_tracks.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
"""Handle importation of files."""
import glob
import argparse
from metadata import MusicFile
import shutil
import os
import re
import filecmp
from unidecode import unidecode
conf = {}
args = {}
def sanitize(name):
"""Rename something to something more sensible."""
name = unidecode(name) #Unicode filenames are such a bad idea
if name[0] == ".":
name = name[1:]
#Strip nasty chars
name = re.sub("[/]", "", name)
if conf["lowercase"]:
name = name.lower();
if conf["nostrange"]:
name = name.replace(" ", "_")
return name
#TODO: make it fix non-ascii characters
def get_files():
files = {}
root = args["dir"]
files["tracks"] = glob.glob(root + u"/*.flac") #All compatible audio files
files["tracks"].extend(glob.glob(root + u"/*.mp3"))
files["tracks"].extend(glob.glob(root + u"/*.ogg"))
files["tracks"].extend(glob.glob(root + u"/*.wav"))
files["tracks"].extend(glob.glob(root + u"/*.m4a"))
if args["cue"] == True:
files["cue"] = glob.glob(root + u"/*.cue")
if args["log"] == True:
files["log"] = glob.glob(root + u"/*.log")
return files
def get_track_filename(track):
info = MusicFile(track)
#TODO: use the rename mask
return
pass
def get_path(track):
"""Get the artist/album format path for this particular track"""
info = MusicFile(track)
art = sanitize(info.get_artist())
alb = sanitize(info.get_album())
return art + "/" + alb
def get_albums(tracks):
"""Associate each album with a path leading to the directory with the
tracks in the album."""
albums = {}
for track in tracks:
print track
info = MusicFile(track)
if info.get_album() not in albums:
albums[info.get_album()] = []
albums[info.get_album()] = get_path(track)
return albums
def move_track(track):
info = MusicFile(track)
path = get_path(track)
name = sanitize(info.get_title()) + info.suffix
#if not os.path.exists(root + path):
# os.makedirs(root + path)
print "Rename: " + track + " to " + name
#os.rename(track, name)
try:
print "Moving " + name + " into " + path
#shutil.move(name, path)
except shutil.Error:
print "Looks like that file already exists. Comparing..."
d = filecmp.cmp(name, path)
if d:
print "Files are identical. Deleting this copy..."
os.remove(name)
else:
print "Files are different. Leaving it alone."
def main(argv, config):
parser = argparse.ArgumentParser(
description = "Import files in the current directory.")
parser.add_argument("-r", "--rename", action="store_true",
help="Rename the files.")
parser.add_argument("-m", "--move", action="store_true",
help="Move the files to the music directory.")
parser.add_argument("-a", "--albumart", action="store_true",
help="Set album art as metadata.")
parser.add_argument("-n", "--normalize", action="store_true",
help="Normalize tracks with ReplayGain.")
parser.add_argument("-d", "--descriptionfile", action="store",
default="info.txt", help="Which file to use for the DESCRIPTION tag.")
parser.add_argument("-c", "--cue", action="store_true",
help="Process cue files (move them, rename tracks if necessary).")
parser.add_argument("-l", "--log", action="store_true",
help="Move log files along with tracks (only applies if -m is set).")
parser.add_argument('dir', action="store", default="./",
help="Which directory to process (default %(default)s)", nargs="?")
argv = vars(parser.parse_args(argv))
global conf, args
conf = config
args = argv
files = get_files()
albums = get_albums(files["tracks"])
for track in files["tracks"]:
#We temporarily organize tracks into a hierarchy at the present
#directory so that we can have proper album replay data.
move_track(track)
get_track_filename(track)
print albums
"""print files
for track in files["tracks"]:
move_track(track, config["dir"])
#albs = get_albums(files["tracks"])"""