Commits

Daniel Neuhäuser committed d15c6e9

Extracted get_cover_url from the main function

  • Participants
  • Parent commits 579c686

Comments (0)

Files changed (1)

File coverfetch.py

     with closing(urlopen(request)) as response:
         return parse(response)
 
+def get_cover_url(artist, album):
+    """
+    Returns the url to the cover of the given `album` from the given `artist`.
+    If no cover can be found ``None`` is returned.
+    """
+    album_info = make_request("album.getInfo", artist=artist, album=album)
+    for size in ["large", "medium", "small"]:
+        images = album_info.xpath('album//image[@size="{0}"]'.format(size))
+        for image in images:
+            if image.text:
+                return image.text
+
 def main(args=sys.argv[1:]):
     """
     :param args:
                             audio_file
                         ).encode(OUT_ENCODING)
                     continue
-                album_info = make_request(
-                    "album.getInfo",
-                    artist=artist,
-                    album=album
-                )
-                image_url = None
-                for size in ["large", "medium", "small"]:
-                    images = album_info.xpath(
-                        'album//image[@size="{0}"]'.format(size)
-                    )
-                    if images[0].text:
-                        image_url = images[0].text
-                        break
+                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(