Commits

Daniel Neuhäuser committed 7ffde4c

Created a process_directory function which basically does what main() did before just for one directory

  • Participants
  • Parent commits 3291fbe

Comments (0)

Files changed (1)

 import sys
 from urllib import urlencode
 from urllib2 import Request, urlopen
-from contextlib import closing
+from contextlib import closing, nested
 from optparse import OptionParser
 from itertools import imap
 
             if image.text:
                 return image.text
 
+def process_directory(directory, music_files):
+    for music_file in music_files:
+        m = get_metadata(music_file)
+        try:
+            artist = m["artist"][0]
+            album = m["album"][0]
+        except KeyError:
+            continue
+        cover_url = get_cover_url(artist, album)
+        if cover_url is None:
+            continue
+        ext = os.path.splitext(cover_url)[1]
+        coverpath = os.path.join(directory, "cover" + ext)
+        try:
+            with nested(open(coverpath, "wb"), urlopen(cover_url)) as (cover_file, cover):
+                coverfile.write(cover.read())
+        except EnvironmentError as exc:
+            os.remove(covername)
+        break
+
 def main(args=sys.argv[1:]):
     """
     :param args:
         return False
     for path in args:
         for directory, audio_files in iter_directories(path):
-            for audio_file in audio_files:
-                m = get_metadata(audio_file)
-                try:
-                    artist = m["artist"][0]
-                    album = m["album"][0]
-                except KeyError:
-                    audio_file.decode(FILESYSTEMENCODING)
-                    if options.debug:
-                        print u"couldn't process: {0}".format(
-                            audio_file
-                        ).encode(OUT_ENCODING)
-                    continue
-                image_url = get_cover_url(artist, album)
-                if image_url is None:
-                    if options.debug:
-                        print u"Couldn't find cover for {0} - {1}".format(
-                            artist,
-                            album
-                        ).encode(OUT_ENCODING)
-                    continue
-                ext = os.path.splitext(image_url)[1]
-                covername = os.path.join(
-                    directory,
-                    "cover" + os.path.splitext(image_url)[1]
-                )
-                try:
-                    with open(covername, "wb") as f:
-                        f.write(urlopen(image_url).read())
-                except Exception as exc:
-                    print "{0}: {1}".format(
-                        exc.__class__.__name__,
-                        exc.args[0]
-                    )
-                    os.remove(covername)
-                break
-            else:
-                print u"Couldn't find cover for directory: {0}".format(
-                    directory.decode(sys.getfilesystemencoding())
-                ).encode(OUT_ENCODING)
+            process_directory(directory, audio_files)
     return True
 
 if __name__ == "__main__":