-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
156 lines (131 loc) · 4.76 KB
/
scanner.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""This program is made possible by the awesome python knowledge of my darling Ivana <3 for ever"""
import os, fnmatch, json, sys, codecs, time, argparse, logging
from mutagen.flac import FLAC
from mutagen.mp4 import MP4
from mutagen.mp3 import MP3
from mutagen.oggvorbis import OggVorbis
from mutagen._util import MutagenError
from scanner._utils import Time
import scanner.FlacTrack as FlacTrack
import scanner.Mp3Track as Mp3Track
import scanner.Mp4Track as Mp4Track
import scanner.OggTrack as OggTrack
parser = argparse.ArgumentParser(
description=
'Scans a given directory for music files and places the output file in an optional directory'
)
parser.add_argument('scanpath', metavar='scanpath', help='directory to scan')
parser.add_argument('--destpath',
metavar='destination path',
help='directory to place the output json in')
parser.add_argument('--stdout',
metavar='stdout',
help='print status to stdout or not')
args = parser.parse_args()
rootpath = args.scanpath
destpath = args.destpath or args.scanpath
showInfo = args.stdout == 'True'
""" logging """
logging.basicConfig(filename='scanner.log', level=logging.DEBUG)
p = codecs.open(destpath + '/progress.txt', 'w', "utf-8")
jsonFile = list()
nrScanned = 0
start = time.time()
def find_files(directory, pattern):
for root, folder, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
yield os.path.join(root, basename)
def updateInfo():
global nrScanned
global showInfo
nrScanned = nrScanned + 1
perc = int((float(float(nrScanned) / float(countfiles))) * 100)
p.seek(0)
p.write(str(perc))
p.truncate()
if (countfiles > 100 and nrScanned % int(countfiles / 100) == 0
and showInfo):
inc = time.time()
diff = inc - start
if (perc > 0):
tot = (diff / perc) * 100
eta = tot - diff
sys.stdout.write("" + str(perc) + "% done, ETA: " +
Time.ums(eta, False) + "\r")
sys.stdout.flush()
def parseMP3(filename):
global jsonFile
try:
song = MP3(filename)
if song is not None:
track = Mp3Track.Track(song, filename, rootpath)
updateInfo()
jsonFile.append(
json.dumps(track.__dict__, sort_keys=True, indent=2))
except MutagenError:
print("Error occured")
def parseFlac(filename):
global jsonFile
try:
song = FLAC(filename)
if song is not None:
track = FlacTrack.Track(song, filename, rootpath)
updateInfo()
jsonFile.append(
json.dumps(track.__dict__, sort_keys=True, indent=2))
except MutagenError:
print("Error occured")
def parseM4A(filename):
global jsonFile
try:
song = MP4(filename)
if song is not None:
track = Mp4Track.Track(song, filename, rootpath)
updateInfo()
jsonFile.append(
json.dumps(track.__dict__, sort_keys=True, indent=2))
except MutagenError:
print("Error occured")
def parseOgg(filename):
global jsonFile
try:
song = OggVorbis(filename)
if song is not None:
track = OggTrack.Track(song, filename, rootpath)
updateInfo()
jsonFile.append(
json.dumps(track.__dict__, sort_keys=True, indent=2))
except MutagenError:
print("Error occured")
allfiles = find_files(rootpath, '*.mp3')
allfilesflac = find_files(rootpath, '*.flac')
allfilesm4a = find_files(rootpath, '*.m4a')
allfilesogg = find_files(rootpath, '*.ogg')
countfiles = sum(1 for e in allfiles)
countfiles += sum(1 for e in allfilesflac)
countfiles += sum(1 for e in allfilesm4a)
countfiles += sum(1 for e in allfilesogg)
inc = time.time()
diff = inc - start
print("Found {0} media files in {1}".format(countfiles, Time.ums(diff, False)))
print("Starting scan for {0} media files in '{1}'".format(countfiles, rootpath))
for filename in find_files(rootpath, '*.mp3'):
parseMP3(filename)
for filename in find_files(rootpath, '*.flac'):
parseFlac(filename)
for filename in find_files(rootpath, '*.m4a'):
parseM4A(filename)
for filename in find_files(rootpath, '*.ogg'):
parseOgg(filename)
f = codecs.open(destpath + '/node-music.json', 'w', "utf-8")
f.write("[" + ",\n".join(jsonFile) + "]")
f.close()
p.seek(0)
p.write("100")
p.truncate()
p.close()
end = time.time()
print("Done scanning, time taken: {0}".format(Time.ums(end - start, False)))