Example #1
0
def get_dir_info(dirname):
	files=sort.sorted_dir(dirname)
	trackinfo={}
	lastpuid=None
	lastfile=None
	albumfreq={}
	util.update_verbose_progress("Examining %s"%dirname)
	for i in files:
		util.update_verbose_progress(" %s" % (i))
		fname=os.path.join(dirname,i)
		trackinfo[fname]=musicfile.MusicFile(fname)
		# If this is a duplicate of the previous track, ignore it.
		# Dedupe by PUID
		if lastpuid is not None and trackinfo[fname].getPUID() == lastpuid:
			util.update_verbose_progress("WARNING: Duplicate track ignored",repr(trackinfo[fname].getFilename()),"and",repr(trackinfo[lastfile].getFilename()))
			del trackinfo[fname]
			continue
		lastpuid = trackinfo[fname].getFilename()
		lastfile = fname
		for mbtrack in trackinfo[fname].getTracks():
			for release in mbtrack.releases:
				albumfreq[release.id]=albumfreq.get(release.id,0)+1
	# Sort by the most likely album first.
	for fileid in trackinfo:
		trackinfo[fileid].getTracks().sort(
			lambda b,a:cmp_track(albumfreq,a,b))
	return trackinfo
Example #2
0
def get_dir_info(dirname):
    files = sort.sorted_dir(dirname)
    trackinfo = {}
    lastpuid = None
    lastfile = None
    albumfreq = {}
    util.update_verbose_progress("Examining %s" % dirname)
    for i in files:
        util.update_verbose_progress(" %s" % (i))
        fname = os.path.join(dirname, i)
        trackinfo[fname] = musicfile.MusicFile(fname)
        # If this is a duplicate of the previous track, ignore it.
        # Dedupe by PUID
        if lastpuid is not None and trackinfo[fname].getPUID() == lastpuid:
            util.update_verbose_progress(
                "WARNING: Duplicate track ignored",
                repr(trackinfo[fname].getFilename()), "and",
                repr(trackinfo[lastfile].getFilename()))
            del trackinfo[fname]
            continue
        lastpuid = trackinfo[fname].getFilename()
        lastfile = fname
        for mbtrack in trackinfo[fname].getTracks():
            for release in mbtrack.releases:
                albumfreq[release.id] = albumfreq.get(release.id, 0) + 1
    # Sort by the most likely album first.
    for fileid in trackinfo:
        trackinfo[fileid].getTracks().sort(
            lambda b, a: cmp_track(albumfreq, a, b))
    return trackinfo
	def process(self):
		print "dir",self.dirname
		self.files = sort.sorted_dir(self.dirname)

		i = 1
		for file in self.files:
			tags = tag.read_tags(os.path.join(self.dirname,file))
			self.fileTags.append(tags)
			trck = tags[tag.TRACK_NUMBER]
			if "/" in trck:
				trck = trck.split("/")[0]
			trck = int(trck)
			if trck != i:
				print "%s Missing an expected track. Expected %d but got %d" % (dir, i, trck)
				fp = open(os.path.expanduser("~/musicproblems"), "a")
				fp.write("%s Missing an expected track. Expected %d but got %d" % (dir, i, trck))
				fp.close()
				return
			releaseid = u"http://musicbrainz.org/release/"+tags[tag.ALBUM_ID]
			i+=1
	
		self.l = Logger(self.dirname, releaseid)

		self.release = lookups.get_release_by_releaseid(releaseid)
		if len(self.files) != len(self.release.tracks):
			self.l.write("Fewer files than the release says (release: %d files %d)" % (len(self.release.tracks), len(self.files)))
			self.l.close()
			return
		
		i=0
		for file in self.files:
			self.test(i, os.path.join(self.dirname, file))
			i+=1

		self.l.close()
Example #4
0
    def process(self):
        print "dir", self.dirname
        self.files = sort.sorted_dir(self.dirname)

        i = 1
        for file in self.files:
            tags = tag.read_tags(os.path.join(self.dirname, file))
            self.fileTags.append(tags)
            trck = tags[tag.TRACK_NUMBER]
            if "/" in trck:
                trck = trck.split("/")[0]
            trck = int(trck)
            if trck != i:
                print "%s Missing an expected track. Expected %d but got %d" % (
                    dir, i, trck)
                fp = open(os.path.expanduser("~/musicproblems"), "a")
                fp.write(
                    "%s Missing an expected track. Expected %d but got %d" %
                    (dir, i, trck))
                fp.close()
                return
            releaseid = u"http://musicbrainz.org/release/" + tags[tag.ALBUM_ID]
            i += 1

        self.l = Logger(self.dirname, releaseid)

        self.release = lookups.get_release_by_releaseid(releaseid)
        if len(self.files) != len(self.release.tracks):
            self.l.write(
                "Fewer files than the release says (release: %d files %d)" %
                (len(self.release.tracks), len(self.files)))
            self.l.close()
            return

        i = 0
        for file in self.files:
            self.test(i, os.path.join(self.dirname, file))
            i += 1

        self.l.close()
Example #5
0
def find_albumart(srcpath, disc, options):
    imageurl = lookups.get_album_art_url_for_asin(disc.asin)
    images = []
    if imageurl is not None:
        print "Downloading album art from %s" % imageurl
        if not options.noact:
            try:
                (fd, tmpfile) = tempfile.mkstemp(suffix=".jpg")
                os.close(fd)
                (f, h) = urllib.urlretrieve(imageurl, tmpfile)
                if h.getmaintype() != "image":
                    print "WARNING: image url returned unexpected mimetype: %s" % h.gettype(
                    )
                    os.unlink(tmpfile)
                else:
                    imagemime = h.gettype()
                    imagepath = tmpfile
                    try:
                        import Image
                        i = Image.open(imagepath)
                        # Sometimes amazon will give us a 1x1 gif as an image.  Ignore.
                        if i.size[0] != 1:
                            images.append(
                                (imagepath, imagemime, True, "amazon"))
                    except ImportError, e:
                        # If the image library isn't there, check the filesize of the 1x1
                        if os.path.getsize(imagepath) != 807:
                            images.append(
                                (imagepath, imagemime, True, "amazon"))
            except:
                print "WARNING: Failed to retrieve coverart (%s)" % imageurl
    if os.path.exists(os.path.join(srcpath, "folder.jpg")):
        print "Found existing image file"
        imagemime = "image/jpeg"
        imagepath = os.path.join(srcpath, "folder.jpg")
        images.append((imagepath, imagemime, False, "local file"))

    dir = sort.sorted_dir(srcpath)
    filetags = tag.read_tags(dir[0])
    if tag.IMAGE in filetags:
        # Yuck
        tagdata = filetags[tag.IMAGE]
        if not isinstance(tagdata, list):
            tagdata = [tagdata]
        for image in tagdata:
            print "Found image embedded in file"
            if image['mime'] == "image/jpeg":
                suffix = ".jpg"
            elif image['mime'] == "image/png":
                suffix = ".png"
            else:
                print "Embeded Image is of unknown Mime Type"
                continue
            if image['pictype'] != 3:
                print "Embeded Image not coverart"
                continue
            (fd, tmpfile) = tempfile.mkstemp(suffix)
            os.write(fd, image['imagedata'])
            os.close(fd)
            images.append((tmpfile, image['mime'], True, "embedded tag"))

    best = find_best_image(images)[0]
    if best[0] is not None:
        util.report("Best cover image: %s (from %s)" % (best[0], best[3]))
    return best
Example #6
0
def find_albumart(srcpath,disc,options):
	imageurl = lookups.get_album_art_url_for_asin(disc.asin)
	images = []
	if imageurl is not None:
                print "Downloading album art from %s" % imageurl
		if not options.noact:
                        try:
                                (fd,tmpfile) = tempfile.mkstemp(suffix = ".jpg")
                                os.close(fd)
                                (f,h) = urllib.urlretrieve(imageurl, tmpfile)
                                if h.getmaintype() != "image":
                                        print "WARNING: image url returned unexpected mimetype: %s" % h.gettype()
                                        os.unlink(tmpfile)
                                else:
                                        imagemime = h.gettype()
                                        imagepath = tmpfile
					try:
						import Image
						i = Image.open(imagepath)
						# Sometimes amazon will give us a 1x1 gif as an image.  Ignore.
						if i.size[0] != 1:
							images.append((imagepath, imagemime, True, "amazon"))
					except ImportError, e:
						# If the image library isn't there, check the filesize of the 1x1
						if os.path.getsize(imagepath) != 807:
							images.append((imagepath, imagemime, True, "amazon"))
			except:
                                print "WARNING: Failed to retrieve coverart (%s)" % imageurl
	if os.path.exists(os.path.join(srcpath, "folder.jpg")):
		print "Found existing image file"
                imagemime="image/jpeg"
                imagepath = os.path.join(srcpath, "folder.jpg")
		images.append((imagepath, imagemime, False, "local file"))
	
	dir = sort.sorted_dir(srcpath)
	filetags = tag.read_tags(dir[0])
	if tag.IMAGE in filetags:
		# Yuck
		tagdata = filetags[tag.IMAGE]
		if not isinstance(tagdata, list):
			tagdata = [tagdata]
		for image in tagdata:
			print "Found image embedded in file"
			if image['mime'] == "image/jpeg":
				suffix = ".jpg"
			elif image['mime'] == "image/png":
				suffix = ".png"
			else:
				print "Embeded Image is of unknown Mime Type"
				continue
			if image['pictype'] != 3:
				print "Embeded Image not coverart"
				continue
			(fd,tmpfile) = tempfile.mkstemp(suffix)
			os.write(fd, image['imagedata'])
			os.close(fd)
			images.append((tmpfile, image['mime'], True, "embedded tag"))

	best = find_best_image(images)[0]
	if best[0] is not None:
		util.report("Best cover image: %s (from %s)" % (best[0], best[3]))
	return best