Commits

Daniel Neuhäuser committed 7779972

iter_directories now only yield directories which are interesting for us

Comments (0)

Files changed (1)

 
 def iter_directories(directory):
     """
-    Yields the path for every directory in the given `directory` recursivly.
+    Yields the path and the music files for every directory which does not
+    already contain a cover.
     """
     for root, dirs, files in os.walk(directory):
         for dir in dirs:
-            yield os.path.join(root, dir)
+            dir_path = os.path.join(root, dir)
+            files = os.listdir(dir_path)
+            if any(f.startswith("cover") for f in files):
+                continue
+            music_files = [
+                f for f in files if any(imap(f.endswith, SUPPORTED_FORMATS))
+            ]
+            if not any(music_files):
+                continue
+            yield dir_path, [os.path.join(dir_path, f) for f in music_files]
 
 def make_request(method, **kwargs):
     """
         parser.error("pass one or more directory paths to the script")
         return False
     for path in args:
-        for directory in iter_directories(path):
-            files = os.listdir(directory)
-            if any(f.startswith("cover") for f in files):
-                continue
-            audio_files = [
-                f for f in files if any(
-                    imap(f.endswith, SUPPORTED_FORMATS)
-                )
-            ]
-            if not audio_files:
-                if options.debug:
-                    directory = directory.decode(FILESYSTEMENCODING)
-                    print u"skipped {0}: no audio file found.".format(
-                        directory
-                    ).encode(OUT_ENCODING)
-                continue
+        for directory, audio_files in iter_directories(path):
             for audio_file in audio_files:
-                audio_file = os.path.join(directory, audio_file)
                 m = get_metadata(audio_file)
                 try:
                     artist = m["artist"][0]