def _pseudo_release_downloaded(album, metadata, original_id, document, http, error):
	global tracks
	tracks = {}
	tracks['has_transliteration'] = True

	try:
		if error:
			album.log.error("%r", unicode(http.errorString()))
		else:
			try:
				release_node = document.metadata[0].release[0]
				if release_node.text_representation[0].script[0].text != script:
					return

				album_latin = release_node.title[0].text
				metadata["album"] = album_latin
				tracks['album'] = album_latin

				artistcredit, tmp = artist_credit_from_node(release_node.artist_credit[0], config)
				metadata["albumartist"] = artistcredit
				tracks["artist"] = artistcredit

				mediumpos = 1
				tracks["mediums"] = {}
				for i, node in enumerate(release_node.medium_list[0].medium):
					try:
						tracks["mediums"][ mediumpos ] = release_node.medium_list[0].medium[i].title[0].text
					except: pass

					tracks[mediumpos] = {}
					trackpos = 1
					for j, node in enumerate(release_node.medium_list[0].medium[i].track_list[0].track):
						title = ""
						try:
							title = node.title[0].text
						except:
							title = node.recording[0].title[0].text

						tartist = ""
						try:
							tartist, tmp = artist_credit_from_node(node.artist_credit[0], config)
						except:
							tartist, tmp = artist_credit_from_node(node.recording[0].artist_credit[0], config)

						tracks[ mediumpos ][ trackpos ] = {};
						tracks[ mediumpos ][ trackpos ]["artist"] = tartist

						tracks[ mediumpos ][ trackpos ]["title"] = title;
						tracks[ mediumpos ][ trackpos ]["mbid"] = node.recording[0].id;

						trackpos = trackpos + 1
					mediumpos = mediumpos + 1
			except:
				error = True
				album.log.error("some error occurred :(")
	finally:
		album._requests -= 1
		album._finalize_loading(None)
Esempio n. 2
0
 def __init__(self, releases, disc, parent=None):
     QtGui.QDialog.__init__(self, parent)
     self.releases = releases
     self.disc = disc
     self.ui = Ui_Dialog()
     self.ui.setupUi(self)
     self.ui.release_list.setSortingEnabled(True)
     self.ui.release_list.setHeaderLabels([_(u"Album"), _(u"Artist"), _(u"Date"), _(u"Country"),
         _(u"Labels"), _(u"Catalog #s"), _(u"Barcode")])
     if self.releases:
         for release in self.releases:
             labels, catalog_numbers = label_info_from_node(release.label_info_list[0])
             date = release.date[0].text if "date" in release.children else ""
             country = release.country[0].text if "country" in release.children else ""
             barcode = release.barcode[0].text if "barcode" in release.children else ""
             item = QtGui.QTreeWidgetItem(self.ui.release_list)
             item.setText(0, release.title[0].text)
             item.setText(1, artist_credit_from_node(release.artist_credit[0])[0])
             item.setText(2, date)
             item.setText(3, country)
             item.setText(4, ", ".join(labels))
             item.setText(5, ", ".join(catalog_numbers))
             item.setText(6, barcode)
             item.setData(0, QtCore.Qt.UserRole, QtCore.QVariant(release.id))
         self.ui.release_list.setCurrentItem(self.ui.release_list.topLevelItem(0))
         self.ui.ok_button.setEnabled(True)
     for i in range(self.ui.release_list.columnCount() - 1):
         self.ui.release_list.resizeColumnToContents(i)
     # Sort by descending date, then ascending country
     self.ui.release_list.sortByColumn(3, QtCore.Qt.AscendingOrder)
     self.ui.release_list.sortByColumn(2, QtCore.Qt.DescendingOrder)
     self.ui.lookup_button.clicked.connect(self.lookup)
Esempio n. 3
0
    def _compare_to_release(self, release):
        """
        Compare cluster metadata to a MusicBrainz release. Produces a
        probability as a linear combination of weights that the
        cluster is a certain album.

        Weights:
          * title                = 17
          * artist name          = 6
          * number of tracks     = 5
          * release country      = 2
          * format               = 2

        """
        total = 0.0
        parts = []
        w = Cluster.comparison_weights

        a = self.metadata['albumartist']
        b = artist_credit_from_node(release.artist_credit[0], self.config)[0]
        parts.append((similarity2(a, b), w["artist"]))
        total += w["artist"]

        t, p = self.metadata.compare_to_release(release, w, self.config)
        total += t
        parts.extend(p)

        return reduce(lambda x, y: x + y[0] * y[1] / total, parts, 0.0)
Esempio n. 4
0
 def __init__(self, releases, disc, parent=None):
     QtGui.QDialog.__init__(self, parent)
     self.releases = releases
     self.disc = disc
     self.ui = Ui_Dialog()
     self.ui.setupUi(self)
     self.ui.release_list.setSortingEnabled(True)
     self.ui.release_list.setHeaderLabels([_(u"Album"), _(u"Artist"), _(u"Date"), _(u"Country"),
         _(u"Labels"), _(u"Catalog #s"), _(u"Barcode")])
     if self.releases:
         for release in self.releases:
             labels, catalog_numbers = label_info_from_node(release.label_info_list[0])
             date = release.date[0].text if "date" in release.children else ""
             country = release.country[0].text if "country" in release.children else ""
             barcode = release.barcode[0].text if "barcode" in release.children else ""
             item = QtGui.QTreeWidgetItem(self.ui.release_list)
             item.setText(0, release.title[0].text)
             item.setText(1, artist_credit_from_node(release.artist_credit[0])[0])
             item.setText(2, date)
             item.setText(3, country)
             item.setText(4, ", ".join(labels))
             item.setText(5, ", ".join(catalog_numbers))
             item.setText(6, barcode)
             item.setData(0, QtCore.Qt.UserRole, QtCore.QVariant(release.id))
         self.ui.release_list.setCurrentItem(self.ui.release_list.topLevelItem(0))
         self.ui.ok_button.setEnabled(True)
     [self.ui.release_list.resizeColumnToContents(i) for i in range(self.ui.release_list.columnCount() - 1)]
     # Sort by descending date, then ascending country
     self.ui.release_list.sortByColumn(3, QtCore.Qt.AscendingOrder)
     self.ui.release_list.sortByColumn(2, QtCore.Qt.DescendingOrder)
     self.ui.lookup_button.clicked.connect(self.lookup)
Esempio n. 5
0
    def _compare_to_release(self, release):
        """
        Compare cluster metadata to a MusicBrainz release. Produces a
        probability as a linear combination of weights that the
        cluster is a certain album.

        Weights:
          * title                = 17
          * artist name          = 6
          * number of tracks     = 5
          * release country      = 2
          * format               = 2

        """
        total = 0.0
        parts = []
        w = Cluster.comparison_weights

        a = self.metadata['albumartist']
        b = artist_credit_from_node(release.artist_credit[0], self.config)[0]
        parts.append((similarity2(a, b), w["artist"]))
        total += w["artist"]

        t, p = self.metadata.compare_to_release(release, w, self.config)
        total += t
        parts.extend(p)

        return reduce(lambda x, y: x + y[0] * y[1] / total, parts, 0.0)
Esempio n. 6
0
 def __init__(self, releases, disc, parent=None):
     QtGui.QDialog.__init__(self, parent)
     self.releases = releases
     self.disc = disc
     self.ui = Ui_Dialog()
     self.ui.setupUi(self)
     self.ui.release_list.setHeaderLabels([_(u"Album"), _(u"Artist"),
         _(u"Labels"), _(u"Catalog #s"), _(u"Barcode")])
     if self.releases:
         for release in self.releases:
             labels, catalog_numbers = label_info_from_node(release.label_info_list[0])
             barcode = release.barcode[0].text if "barcode" in release.children else ""
             item = QtGui.QTreeWidgetItem(self.ui.release_list)
             item.setText(0, release.title[0].text)
             item.setText(1, artist_credit_from_node(release.artist_credit[0], self.config)[0])
             item.setText(2, ", ".join(labels))
             item.setText(3, ", ".join(catalog_numbers))
             item.setText(4, barcode)
             item.setData(0, QtCore.Qt.UserRole, QtCore.QVariant(release.id))
         self.ui.release_list.setCurrentItem(self.ui.release_list.topLevelItem(0))
         self.ui.ok_button.setEnabled(True)
     self.ui.release_list.resizeColumnToContents(0)
     self.ui.release_list.resizeColumnToContents(1)
     self.ui.release_list.resizeColumnToContents(4)
     self.connect(self.ui.lookup_button, QtCore.SIGNAL("clicked()"), self.lookup)
Esempio n. 7
0
    def _compare_to_track(self, track):
        """
        Compare file metadata to a MusicBrainz track.

        Weigths:
          * title                = 13
          * artist name          = 4
          * release name         = 5
          * length               = 10
          * number of tracks     = 4
          * album type           = 20
          * release country      = 2
          * format               = 2

        """
        total = 0.0
        parts = []
        w = self.comparison_weights

        if 'title' in self.metadata:
            a = self.metadata['title']
            b = track.title[0].text
            parts.append((similarity2(a, b), w["title"]))
            total += w["title"]

        if 'artist' in self.metadata:
            a = self.metadata['artist']
            b = artist_credit_from_node(track.artist_credit[0], self.config)[0]
            parts.append((similarity2(a, b), w["artist"]))
            total += w["artist"]

        a = self.metadata.length
        if a > 0 and 'length' in track.children:
            b = int(track.length[0].text)
            score = 1.0 - min(abs(a - b), 30000) / 30000.0
            parts.append((score, w["length"]))
            total += w["length"]

        releases = []
        if "release_list" in track.children and "release" in track.release_list[
                0].children:
            releases = track.release_list[0].release

        if not releases:
            return (total, None)

        scores = []
        for release in releases:
            t, p = self.metadata.compare_to_release(release, w, self.config)
            total_ = total + t
            parts_ = list(parts) + p
            scores.append((reduce(lambda x, y: x + y[0] * y[1] / total_,
                                  parts_, 0.0), release.id))

        return max(scores, key=lambda x: x[0])
Esempio n. 8
0
    def _compare_to_track(self, track):
        """
        Compare file metadata to a MusicBrainz track.

        Weigths:
          * title                = 13
          * artist name          = 4
          * release name         = 5
          * length               = 10
          * number of tracks     = 4
          * album type           = 20
          * release country      = 2
          * format               = 2

        """
        total = 0.0
        parts = []
        w = self.comparison_weights

        if 'title' in self.metadata:
            a = self.metadata['title']
            b = track.title[0].text
            parts.append((similarity2(a, b), w["title"]))
            total += w["title"]

        if 'artist' in self.metadata:
            a = self.metadata['artist']
            b = artist_credit_from_node(track.artist_credit[0], self.config)[0]
            parts.append((similarity2(a, b), w["artist"]))
            total += w["artist"]

        a = self.metadata.length
        if a > 0 and 'length' in track.children:
            b = int(track.length[0].text)
            score = 1.0 - min(abs(a - b), 30000) / 30000.0
            parts.append((score, w["length"]))
            total += w["length"]

        releases = []
        if "release_list" in track.children and "release" in track.release_list[0].children:
            releases = track.release_list[0].release

        if not releases:
            return (total, None)

        scores = []
        for release in releases:
            t, p = self.metadata.compare_to_release(release, w, self.config)
            total_ = total + t
            parts_ = list(parts) + p
            scores.append((reduce(lambda x, y: x + y[0] * y[1] / total_, parts_, 0.0), release.id))

        return max(scores, key=lambda x: x[0])
Esempio n. 9
0
    def compare_to_track(self, track, weights):
        total = 0.0
        parts = []

        if 'title' in self:
            a = self['title']
            b = track.title[0].text
            parts.append((similarity2(a, b), weights["title"]))
            total += weights["title"]

        if 'artist' in self:
            a = self['artist']
            b = artist_credit_from_node(track.artist_credit[0])[0]
            parts.append((similarity2(a, b), weights["artist"]))
            total += weights["artist"]

        a = self.length
        if a > 0 and 'length' in track.children:
            b = int(track.length[0].text)
            score = 1.0 - min(abs(a - b), 30000) / 30000.0
            parts.append((score, weights["length"]))
            total += weights["length"]

        releases = []
        if "release_list" in track.children and "release" in track.release_list[
                0].children:
            releases = track.release_list[0].release

        if not releases:
            sim = reduce(lambda x, y: x + y[0] * y[1] / total, parts, 0.0)
            return (sim, None, None, track)

        result = (-1, )
        for release in releases:
            t, p = self.compare_to_release(release, weights, return_parts=True)
            sim = reduce(lambda x, y: x + y[0] * y[1] / (total + t), parts + p,
                         0.0)
            if sim > result[0]:
                rg = release.release_group[
                    0] if "release_group" in release.children else None
                result = (sim, rg, release, track)

        return result
Esempio n. 10
0
 def test_1(self):
     config.setting = settings
     node = XmlNode(children={
         'name_credit': [XmlNode(attribs={'joinphrase': ' & '}, children={
             'artist': [XmlNode(attribs={'id': '456'}, children={
                 'name': [XmlNode(text='Foo Bar')],
                 'sort_name': [XmlNode(text='Bar, Foo')]
             })]
         }), XmlNode(children={
             'artist': [XmlNode(attribs={'id': '789'}, children={
                 'name': [XmlNode(text='Baz')],
                 'sort_name': [XmlNode(text='Baz')]
             })]
         })]
     })
     artist, artist_sort, artists = artist_credit_from_node(node)
     self.assertEqual(['Foo Bar', 'Baz'], artists)
     self.assertEqual('Foo Bar & Baz', artist)
     self.assertEqual('Bar, Foo & Baz', artist_sort)
Esempio n. 11
0
 def test_1(self):
     config.setting = settings
     node = XmlNode(children={
         'name_credit': [XmlNode(attribs={'joinphrase': ' & '}, children={
             'artist': [XmlNode(attribs={'id': '456'}, children={
                 'name': [XmlNode(text='Foo Bar')],
                 'sort_name': [XmlNode(text='Bar, Foo')]
             })]
         }), XmlNode(children={
             'artist': [XmlNode(attribs={'id': '789'}, children={
                 'name': [XmlNode(text='Baz')],
                 'sort_name': [XmlNode(text='Baz')]
             })]
         })]
     })
     artist, artist_sort, artists, artists_sort = artist_credit_from_node(node)
     self.assertEqual('Foo Bar & Baz', artist)
     self.assertEqual(['Foo Bar', 'Baz'], artists)
     self.assertEqual('Bar, Foo & Baz', artist_sort)
     self.assertEqual(['Bar, Foo', 'Baz'], artists_sort)
Esempio n. 12
0
    def compare_to_track(self, track, weights):
        config = QObject.config
        total = 0.0
        parts = []

        if 'title' in self:
            a = self['title']
            b = track.title[0].text
            parts.append((similarity2(a, b), weights["title"]))
            total += weights["title"]

        if 'artist' in self:
            a = self['artist']
            b = artist_credit_from_node(track.artist_credit[0], config)[0]
            parts.append((similarity2(a, b), weights["artist"]))
            total += weights["artist"]

        a = self.length
        if a > 0 and 'length' in track.children:
            b = int(track.length[0].text)
            score = 1.0 - min(abs(a - b), 30000) / 30000.0
            parts.append((score, weights["length"]))
            total += weights["length"]

        releases = []
        if "release_list" in track.children and "release" in track.release_list[0].children:
            releases = track.release_list[0].release

        if not releases:
            sim = reduce(lambda x, y: x + y[0] * y[1] / total, parts, 0.0)
            return (sim, None, None, track)

        result = (-1,)
        for release in releases:
            t, p = self.compare_to_release(release, weights, return_parts=True)
            sim = reduce(lambda x, y: x + y[0] * y[1] / (total + t), parts + p, 0.0)
            if sim > result[0]:
                rg = release.release_group[0] if "release_group" in release.children else None
                result = (sim, rg, release, track)

        return result
Esempio n. 13
0
    def compare_to_track(self, track, weights):
        parts = []

        if 'title' in self:
            a = self['title']
            b = track.title[0].text
            parts.append((similarity2(a, b), weights["title"]))

        if 'artist' in self:
            a = self['artist']
            b = artist_credit_from_node(track.artist_credit[0])[0]
            parts.append((similarity2(a, b), weights["artist"]))

        a = self.length
        if a > 0 and 'length' in track.children:
            b = int(track.length[0].text)
            score = 1.0 - min(abs(a - b), 30000) / 30000.0
            parts.append((score, weights["length"]))

        releases = []
        if "release_list" in track.children and "release" in track.release_list[
                0].children:
            releases = track.release_list[0].release

        if not releases:
            sim = linear_combination_of_weights(parts)
            return (sim, None, None, track)

        result = (-1, )
        for release in releases:
            release_parts = self.compare_to_release_parts(release, weights)
            sim = linear_combination_of_weights(parts + release_parts)
            if sim > result[0]:
                rg = release.release_group[
                    0] if "release_group" in release.children else None
                result = (sim, rg, release, track)

        return result
Esempio n. 14
0
 def __init__(self, releases, disc, parent=None):
     QtGui.QDialog.__init__(self, parent)
     self.releases = releases
     self.disc = disc
     self.ui = Ui_Dialog()
     self.ui.setupUi(self)
     self.ui.release_list.setHeaderLabels([
         _(u"Album"),
         _(u"Artist"),
         _(u"Labels"),
         _(u"Catalog #s"),
         _(u"Barcode")
     ])
     if self.releases:
         for release in self.releases:
             labels, catalog_numbers = label_info_from_node(
                 release.label_info_list[0])
             barcode = release.barcode[
                 0].text if "barcode" in release.children else ""
             item = QtGui.QTreeWidgetItem(self.ui.release_list)
             item.setText(0, release.title[0].text)
             item.setText(
                 1,
                 artist_credit_from_node(release.artist_credit[0],
                                         self.config)[0])
             item.setText(2, ", ".join(labels))
             item.setText(3, ", ".join(catalog_numbers))
             item.setText(4, barcode)
             item.setData(0, QtCore.Qt.UserRole,
                          QtCore.QVariant(release.id))
         self.ui.release_list.setCurrentItem(
             self.ui.release_list.topLevelItem(0))
         self.ui.ok_button.setEnabled(True)
     self.ui.release_list.resizeColumnToContents(0)
     self.ui.release_list.resizeColumnToContents(1)
     self.ui.release_list.resizeColumnToContents(4)
     self.connect(self.ui.lookup_button, QtCore.SIGNAL("clicked()"),
                  self.lookup)
Esempio n. 15
0
 def test_1(self):
     config.setting = settings
     node = XmlNode(
         children={
             "name_credit": [
                 XmlNode(
                     attribs={"joinphrase": u" & "},
                     children={
                         "artist": [
                             XmlNode(
                                 attribs={"id": u"456"},
                                 children={
                                     "name": [XmlNode(text=u"Foo Bar")],
                                     "sort_name": [XmlNode(text=u"Bar, Foo")],
                                 },
                             )
                         ]
                     },
                 ),
                 XmlNode(
                     children={
                         "artist": [
                             XmlNode(
                                 attribs={"id": u"789"},
                                 children={"name": [XmlNode(text=u"Baz")], "sort_name": [XmlNode(text=u"Baz")]},
                             )
                         ]
                     }
                 ),
             ]
         }
     )
     artist, artist_sort, artists, artists_sort = artist_credit_from_node(node)
     self.assertEqual(u"Foo Bar & Baz", artist)
     self.assertEqual([u"Foo Bar", u"Baz"], artists)
     self.assertEqual(u"Bar, Foo & Baz", artist_sort)
     self.assertEqual([u"Bar, Foo", u"Baz"], artists_sort)
Esempio n. 16
0
    def compare_to_track(self, track, weights):
        parts = []

        if 'title' in self:
            a = self['title']
            b = track.title[0].text
            parts.append((similarity2(a, b), weights["title"]))

        if 'artist' in self:
            a = self['artist']
            b = artist_credit_from_node(track.artist_credit[0])[0]
            parts.append((similarity2(a, b), weights["artist"]))

        a = self.length
        if a > 0 and 'length' in track.children:
            b = int(track.length[0].text)
            score = 1.0 - min(abs(a - b), 30000) / 30000.0
            parts.append((score, weights["length"]))

        releases = []
        if "release_list" in track.children and "release" in track.release_list[0].children:
            releases = track.release_list[0].release

        if not releases:
            sim = linear_combination_of_weights(parts)
            return (sim, None, None, track)

        result = (-1,)
        for release in releases:
            release_parts = self.compare_to_release_parts(release, weights)
            sim = linear_combination_of_weights(parts + release_parts)
            if sim > result[0]:
                rg = release.release_group[0] if "release_group" in release.children else None
                result = (sim, rg, release, track)

        return result
Esempio n. 17
0
    def compare_to_release(self, release, weights, return_parts=False):
        """
        Compare metadata to a MusicBrainz release. Produces a probability as a
        linear combination of weights that the metadata matches a certain album.
        """
        total = 0.0
        parts = []

        if "album" in self:
            b = release.title[0].text
            parts.append((similarity2(self["album"], b), weights["album"]))
            total += weights["album"]

        if "albumartist" in self and "albumartist" in weights:
            a = self["albumartist"]
            b = artist_credit_from_node(release.artist_credit[0])[0]
            parts.append((similarity2(a, b), weights["albumartist"]))
            total += weights["albumartist"]

        if "totaltracks" in self:
            a = int(self["totaltracks"])
            if "title" in weights:
                b = int(release.medium_list[0].medium[0].track_list[0].count)
            else:
                b = int(release.medium_list[0].track_count[0].text)
            score = 0.0 if a > b else 0.3 if a < b else 1.0
            parts.append((score, weights["totaltracks"]))
            total += weights["totaltracks"]

        preferred_countries = config.setting["preferred_release_countries"]
        preferred_formats = config.setting["preferred_release_formats"]

        total_countries = len(preferred_countries)
        if total_countries:
            score = 0.0
            if "country" in release.children:
                try:
                    i = preferred_countries.index(release.country[0].text)
                    score = float(total_countries - i) / float(total_countries)
                except ValueError:
                    pass
            parts.append((score, weights["releasecountry"]))

        total_formats = len(preferred_formats)
        if total_formats:
            score = 0.0
            subtotal = 0
            for medium in release.medium_list[0].medium:
                if "format" in medium.children:
                    try:
                        i = preferred_formats.index(medium.format[0].text)
                        score += float(total_formats - i) / float(total_formats)
                    except ValueError:
                        pass
                    subtotal += 1
            if subtotal > 0:
                score /= subtotal
            parts.append((score, weights["format"]))

        if "releasetype" in weights:
            type_scores = load_release_type_scores(config.setting["release_type_scores"])
            if 'release_group' in release.children and 'type' in release.release_group[0].attribs:
                release_type = release.release_group[0].type
                score = type_scores.get(release_type, type_scores.get('Other', 0.5))
            else:
                score = 0.0
            parts.append((score, weights["releasetype"]))
            total += weights["releasetype"]

        rg = QObject.tagger.get_release_group_by_id(release.release_group[0].id)
        if release.id in rg.loaded_albums:
            parts.append((1.0, 6))

        return (total, parts) if return_parts else \
               (reduce(lambda x, y: x + y[0] * y[1] / total, parts, 0.0), release)
Esempio n. 18
0
    def compare_to_release(self, release, weights, return_parts=False):
        """
        Compare metadata to a MusicBrainz release. Produces a probability as a
        linear combination of weights that the metadata matches a certain album.
        """
        total = 0.0
        parts = []

        if "album" in self:
            b = release.title[0].text
            parts.append((similarity2(self["album"], b), weights["album"]))
            total += weights["album"]

        if "albumartist" in self and "albumartist" in weights:
            a = self["albumartist"]
            b = artist_credit_from_node(release.artist_credit[0])[0]
            parts.append((similarity2(a, b), weights["albumartist"]))
            total += weights["albumartist"]

        if "totaltracks" in self:
            a = int(self["totaltracks"])
            if "title" in weights:
                b = int(release.medium_list[0].medium[0].track_list[0].count)
            else:
                b = int(release.medium_list[0].track_count[0].text)
            score = 0.0 if a > b else 0.3 if a < b else 1.0
            parts.append((score, weights["totaltracks"]))
            total += weights["totaltracks"]

        preferred_countries = config.setting[
            "preferred_release_countries"].split("  ")
        preferred_formats = config.setting["preferred_release_formats"].split(
            "  ")

        total_countries = len(preferred_countries)
        if total_countries:
            score = 0.0
            if "country" in release.children:
                try:
                    i = preferred_countries.index(release.country[0].text)
                    score = float(total_countries - i) / float(total_countries)
                except ValueError:
                    pass
            parts.append((score, weights["releasecountry"]))

        total_formats = len(preferred_formats)
        if total_formats:
            score = 0.0
            subtotal = 0
            for medium in release.medium_list[0].medium:
                if "format" in medium.children:
                    try:
                        i = preferred_formats.index(medium.format[0].text)
                        score += float(total_formats -
                                       i) / float(total_formats)
                    except ValueError:
                        pass
                    subtotal += 1
            if subtotal > 0:
                score /= subtotal
            parts.append((score, weights["format"]))

        if "releasetype" in weights:
            type_scores = load_release_type_scores(
                config.setting["release_type_scores"])
            if 'release_group' in release.children and 'type' in release.release_group[
                    0].attribs:
                release_type = release.release_group[0].type
                score = type_scores.get(release_type,
                                        type_scores.get('Other', 0.5))
            else:
                score = 0.0
            parts.append((score, weights["releasetype"]))
            total += weights["releasetype"]

        rg = QObject.tagger.get_release_group_by_id(
            release.release_group[0].id)
        if release.id in rg.loaded_albums:
            parts.append((1.0, 6))

        return (total, parts) if return_parts else \
               (reduce(lambda x, y: x + y[0] * y[1] / total, parts, 0.0), release)
Esempio n. 19
0
    def compare_to_release_parts(self, release, weights):
        parts = []

        if "album" in self:
            b = release.title[0].text
            parts.append((similarity2(self["album"], b), weights["album"]))

        if "albumartist" in self and "albumartist" in weights:
            a = self["albumartist"]
            b = artist_credit_from_node(release.artist_credit[0])[0]
            parts.append((similarity2(a, b), weights["albumartist"]))

        if "totaltracks" in self:
            try:
                a = int(self["totaltracks"])
            except ValueError:
                pass
            else:
                if "title" in weights:
                    b = int(release.medium_list[0].medium[0].track_list[0].count)
                else:
                    b = int(release.medium_list[0].track_count[0].text)
                score = 0.0 if a > b else 0.3 if a < b else 1.0
                parts.append((score, weights["totaltracks"]))

        preferred_countries = config.setting["preferred_release_countries"]
        preferred_formats = config.setting["preferred_release_formats"]

        total_countries = len(preferred_countries)
        if total_countries:
            score = 0.0
            if "country" in release.children:
                try:
                    i = preferred_countries.index(release.country[0].text)
                    score = float(total_countries - i) / float(total_countries)
                except ValueError:
                    pass
            parts.append((score, weights["releasecountry"]))

        total_formats = len(preferred_formats)
        if total_formats:
            score = 0.0
            subtotal = 0
            for medium in release.medium_list[0].medium:
                if "format" in medium.children:
                    try:
                        i = preferred_formats.index(medium.format[0].text)
                        score += float(total_formats - i) / float(total_formats)
                    except ValueError:
                        pass
                    subtotal += 1
            if subtotal > 0:
                score /= subtotal
            parts.append((score, weights["format"]))

        if "releasetype" in weights:
            type_scores = dict(config.setting["release_type_scores"])
            if 'release_group' in release.children and 'type' in release.release_group[0].attribs:
                release_type = release.release_group[0].type
                score = type_scores.get(release_type, type_scores.get('Other', 0.5))
            else:
                score = 0.0
            parts.append((score, weights["releasetype"]))

        rg = QObject.tagger.get_release_group_by_id(release.release_group[0].id)
        if release.id in rg.loaded_albums:
            parts.append((1.0, 6))

        return parts