-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3_info_james.py
111 lines (81 loc) · 2.92 KB
/
mp3_info_james.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
#!/usr/bin/python
#This is my first python script to read the id3 tags from mp3 files and export them to a JSON file.
#Aubri Sandlin
#Feb. 02, 2014
#This import is to work with files and directories.
import sys
import os
import json
import eyeD3
import logging
from objects import MP3
from argparse import ArgumentParser
#import JSON
def getEyeD3Info(current_dir, songs, logging):
logging.debug("getEyeD3Info(" + current_dir + ", songs)")
dir_contents = os.listdir(current_dir)
to_return = {}
to_return["songs"] = []
to_return["folders"] = []
#
# Iterate over dir contents & generate 2 lists. music_list && dir_list
# Then iterate over each of these lists.
# For each music list, append to songs array.
# For each dir list, call getEyeD3Info & append to list. (recursion)
#
subfolder_list = []
subfile_list = []
for x in dir_contents:
x_full_path = current_dir + "/" + x
if os.path.isdir(x_full_path):
subfolder_list.append(x_full_path)
#print("------- " + x)
elif os.path.isfile(x_full_path) and not (x.startswith(".")) and eyeD3.isMp3File(x_full_path):
subfile_list.append(x)
# I know have a list of files and dirs.
#sys.exit(1)
# Now it"s time to do directories. This is where recursion will come in.
for d in subfolder_list:
folder = {
"folder_name": d,
"contents": getEyeD3Info(d, songs, logging)
}
to_return["folders"].append(folder)
# Create a collection of song dictionaries.
subfolder_song_list = []
for f in subfile_list:
my_mp3 = MP3.MP3(current_dir + "/" + f, logging)
try:
my_mp3.getTagInfo()
my_mp3.log_object_info()
song = {
"file_name": f,
"artist" : my_mp3.artist,
"album": my_mp3.album,
"title": my_mp3.title,
"genre": my_mp3.genre,
"track": my_mp3.track_num
}
subfolder_song_list.append(song)
except Exception, e:
logging.exception ("This is the mp3 causing the error: " + (current_dir + "/" + f))
logging.exception(e)
to_return["songs"] = subfolder_song_list
return to_return
##############################################
# END OF METHOD DEF. START EXECUTION
##############################################
logging.basicConfig(level=logging.DEBUG)
parser = ArgumentParser(description="""***Get all resources required.""")
parser.add_argument('--dir', help="The directory you want to scan", default="/Users/axsandlin/Music")
args = parser.parse_args()
if not os.path.isdir(args.dir):
print "Error: " + args.dir + " is not a directory"
sys.exit(1)
else:
songs = {}
songs["folder_name"] = args.dir
dir_eyed3_data = getEyeD3Info(args.dir, songs, logging)
f = open("./output.json", "w")
json.dump(dir_eyed3_data, f)
f.close