Exemplo n.º 1
0
    def artwork_download_img_to_file(self,
                                     progress_callback,
                                     artist,
                                     album,
                                     dest_filename,
                                     all_images=False):
        # Returns False if no images found
        if not artist and not album:
            return False

        # Amazon currently doesn't support utf8 and suggests latin1 encoding instead:
        artist = urllib.quote(artist.encode('latin1', 'replace'))
        album = urllib.quote(album.encode('latin1', 'replace'))

        # Try searching urls from most specific (artist, title) to least specific (artist only)
        urls = [(AMAZON_URI + "&Title=%s") % (AMAZON_KEY, artist, album),
                (AMAZON_URI + "&Keywords=%s") % (AMAZON_KEY, artist, album),
                AMAZON_URI % (AMAZON_KEY, artist)]

        for url in urls:
            request = urllib2.Request(url)
            opener = urllib2.build_opener()
            try:
                body = opener.open(request).read()
                xml = ElementTree.fromstring(body)
                largeimgs = xml.getiterator(AMAZON_NS + "LargeImage")
            except:
                largeimgs = None

            if largeimgs:
                break
            elif url == urls[-1]:
                return False

        imgs = misc.iunique(url.text for img in largeimgs
                            for url in img.getiterator(AMAZON_NS + "URL"))
        # Prevent duplicate images in remote art window:
        # FIXME the line above should already accomplish this
        # FIXME this loses the order of the results
        imglist = list(set(list(imgs)))

        if not all_images:
            urllib.urlretrieve(imglist[0], dest_filename)
            return True
        else:
            try:
                imgfound = False
                for i, image in enumerate(imglist):
                    dest_filename_curr = dest_filename.replace(
                        "<imagenum>", str(i + 1))
                    urllib.urlretrieve(image, dest_filename_curr)
                    if not progress_callback(dest_filename_curr, i):
                        return imgfound  # cancelled
                    if os.path.exists(dest_filename_curr):
                        imgfound = True

            except:
                pass
            return imgfound
Exemplo n.º 2
0
    def artwork_download_img_to_file(self, progress_callback, artist, album, dest_filename, all_images=False):
        # Returns False if no images found
        if not artist and not album:
            return False

            # Amazon currently doesn't support utf8 and suggests latin1 encoding instead:
        artist = urllib.quote(artist.encode("latin1", "replace"))
        album = urllib.quote(album.encode("latin1", "replace"))

        # Try searching urls from most specific (artist, title) to least specific (artist only)
        urls = [
            (AMAZON_URI + "&Title=%s") % (AMAZON_KEY, artist, album),
            (AMAZON_URI + "&Keywords=%s") % (AMAZON_KEY, artist, album),
            AMAZON_URI % (AMAZON_KEY, artist),
        ]

        for url in urls:
            request = urllib2.Request(url)
            opener = urllib2.build_opener()
            try:
                body = opener.open(request).read()
                xml = ElementTree.fromstring(body)
                largeimgs = xml.getiterator(AMAZON_NS + "LargeImage")
            except:
                largeimgs = None

            if largeimgs:
                break
            elif url == urls[-1]:
                return False

        imgs = misc.iunique(url.text for img in largeimgs for url in img.getiterator(AMAZON_NS + "URL"))
        # Prevent duplicate images in remote art window:
        # FIXME the line above should already accomplish this
        # FIXME this loses the order of the results
        imglist = list(set(list(imgs)))

        if not all_images:
            urllib.urlretrieve(imglist[0], dest_filename)
            return True
        else:
            try:
                imgfound = False
                for i, image in enumerate(imglist):
                    dest_filename_curr = dest_filename.replace("<imagenum>", str(i + 1))
                    urllib.urlretrieve(image, dest_filename_curr)
                    if not progress_callback(dest_filename_curr, i):
                        return imgfound  # cancelled
                    if os.path.exists(dest_filename_curr):
                        imgfound = True

            except:
                pass
            return imgfound
Exemplo n.º 3
0
	def artwork_download_img_to_file(self, artist, album, dest_filename, all_images=False):
		# Returns False if no images found
		if not artist and not album:
			self.downloading_image = False
			return False
		self.downloading_image = True
		# Amazon currently doesn't support utf8 and suggests latin1 encoding instead:
		artist = urllib.quote(artist.encode('latin1', 'replace'))
		album = urllib.quote(album.encode('latin1', 'replace'))

		# Try searching urls from most specific (artist, title) to least specific (artist only)
		urls = [AMAZON_URI % (AMAZON_KEY, artist) + "&Title=" + album,
			AMAZON_URI % (AMAZON_KEY, artist) + "&Keywords=" + album,
			AMAZON_URI % (AMAZON_KEY, artist)]

		for url in urls:
			request = urllib2.Request(url)
			opener = urllib2.build_opener()
			try:
				body = opener.open(request).read()
				xml = ElementTree.fromstring(body)
				largeimgs = xml.getiterator(AMAZON_NS + "LargeImage")
			except:
				largeimgs = None

			if largeimgs:
				break
			elif url == urls[-1]:
				self.downloading_image = False
				return False

		imgs = misc.iunique(url.text for img in largeimgs for url in img.getiterator(AMAZON_NS + "URL"))
		imglist = list(imgs)

		if not all_images:
			urllib.urlretrieve(imglist[0], dest_filename)
			self.downloading_image = False
			return True
		else:
			try:
				imgfound = False
				for i in range(len(imglist)):
					dest_filename_curr = dest_filename.replace("<imagenum>", str(i+1))
					urllib.urlretrieve(imglist[i], dest_filename_curr)
					# This populates Main.imagelist for the remote image window
					if os.path.exists(dest_filename_curr):
						pix = gtk.gdk.pixbuf_new_from_file(dest_filename_curr)
						pix = pix.scale_simple(148, 148, gtk.gdk.INTERP_HYPER)
						pix = self.artwork_apply_composite_case(pix, 148, 148)
						pix = img.pixbuf_add_border(pix)
						if self.stop_art_update:
							del pix
							self.downloading_image = False
							return imgfound
						self.imagelist_append([i+1, pix])
						del pix
						imgfound = True
						self.remotefilelist_append(dest_filename_curr)
						if i == 0:
							self.allow_art_search()
					ui.change_cursor(None)
			except:
				pass
			self.downloading_image = False
			return imgfound
Exemplo n.º 4
0
    def artwork_download_img_to_file(self,
                                     artist,
                                     album,
                                     dest_filename,
                                     all_images=False):
        # Returns False if no images found
        if not artist and not album:
            self.downloading_image = False
            return False
        self.downloading_image = True
        # Amazon currently doesn't support utf8 and suggests latin1 encoding instead:
        artist = urllib.quote(artist.encode('latin1', 'replace'))
        album = urllib.quote(album.encode('latin1', 'replace'))

        # Try searching urls from most specific (artist, title) to least specific (artist only)
        urls = [
            AMAZON_URI % (AMAZON_KEY, artist) + "&Title=" + album,
            AMAZON_URI % (AMAZON_KEY, artist) + "&Keywords=" + album,
            AMAZON_URI % (AMAZON_KEY, artist)
        ]

        for url in urls:
            request = urllib2.Request(url)
            opener = urllib2.build_opener()
            try:
                body = opener.open(request).read()
                xml = ElementTree.fromstring(body)
                largeimgs = xml.getiterator(AMAZON_NS + "LargeImage")
            except:
                largeimgs = None

            if largeimgs:
                break
            elif url == urls[-1]:
                self.downloading_image = False
                return False

        imgs = misc.iunique(url.text for img in largeimgs
                            for url in img.getiterator(AMAZON_NS + "URL"))
        # Prevent duplicate images in remote art window:
        imglist = list(set(list(imgs)))

        if not all_images:
            urllib.urlretrieve(imglist[0], dest_filename)
            self.downloading_image = False
            return True
        else:
            try:
                imgfound = False
                for i, image in enumerate(imglist):
                    dest_filename_curr = dest_filename.replace(
                        "<imagenum>", str(i + 1))
                    urllib.urlretrieve(image, dest_filename_curr)
                    # This populates Main.imagelist for the remote image window
                    if os.path.exists(dest_filename_curr):
                        pix = gtk.gdk.pixbuf_new_from_file(dest_filename_curr)
                        pix = pix.scale_simple(148, 148, gtk.gdk.INTERP_HYPER)
                        pix = self.artwork_apply_composite_case(pix, 148, 148)
                        pix = img.pixbuf_add_border(pix)
                        if self.stop_art_update:
                            del pix
                            self.downloading_image = False
                            return imgfound
                        self.imagelist_append([i + 1, pix])
                        del pix
                        imgfound = True
                        self.remotefilelist_append(dest_filename_curr)
                        if i == 0:
                            self.allow_art_search()
                    ui.change_cursor(None)
            except:
                pass
            self.downloading_image = False
            return imgfound