def parse_releases(self, releases): for node in releases: release = Metadata() release_to_metadata(node, release) release['score'] = node['score'] rg_node = node['release-group'] release_group_to_metadata(rg_node, release) if "media" in node: media = node['media'] release["format"] = media_formats_from_node(media) release["tracks"] = node['track-count'] countries = countries_from_node(node) if countries: release["country"] = ", ".join(countries) self.search_results.append(release)
def parse_tracks(self, tracks): for node in tracks: if "releases" in node: for rel_node in node['releases']: track = Metadata() recording_to_metadata(node, track) track['score'] = node['score'] release_to_metadata(rel_node, track) rg_node = rel_node['release-group'] release_group_to_metadata(rg_node, track) countries = countries_from_node(rel_node) if countries: track["country"] = ", ".join(countries) self.search_results.append((track, node)) else: # This handles the case when no release is associated with a track # i.e. the track is an NAT track = Metadata() recording_to_metadata(node, track) track['score'] = node['score'] track["album"] = _("Standalone Recording") self.search_results.append((track, node))
def test_countries_from_node(self): countries = countries_from_node(self.json_doc) self.assertEqual(countries, [])
def test_countries_from_node_no_event(self): del self.json_doc["release-events"] countries = countries_from_node(self.json_doc) self.assertEqual([], countries)
def test_countries_from_node(self): countries = countries_from_node(self.json_doc) self.assertEqual(['GB'], countries)
def _parse_versions(self, document): """Parse document and return a list of releases""" del self.versions[:] data = [] namekeys = ("tracks", "year", "country", "format", "label", "catnum") headings = { "tracks": N_('Tracks'), "year": N_('Year'), "country": N_('Country'), "format": N_('Format'), "label": N_('Label'), "catnum": N_('Cat No'), } extrakeys = ("packaging", "barcode", "disambiguation") try: releases = document['releases'] except (TypeError, KeyError): releases = [] max_tracks = 10 for node in releases: labels, catnums = label_info_from_node(node['label-info']) countries = countries_from_node(node) if len(node['media']) > max_tracks: tracks = "+".join([ str(m['track-count']) for m in node['media'][:max_tracks] ]) + '+…' else: tracks = "+".join( [str(m['track-count']) for m in node['media']]) formats = [] for medium in node['media']: if "format" in medium: formats.append(medium['format']) release = { "id": node['id'], "year": node['date'][:4] if "date" in node else "????", "country": limited_join(countries, 10, '+', '…') if countries else node.get('country', '') or "??", "format": media_formats_from_node(node['media']), "label": ", ".join([' '.join(x.split(' ')[:2]) for x in set(labels)]), "catnum": ", ".join(set(catnums)), "tracks": tracks, "barcode": node.get('barcode', '') or _('[no barcode]'), "packaging": node.get('packaging', '') or '??', "disambiguation": node.get('disambiguation', ''), "_disambiguate_name": list(), "totaltracks": sum([m['track-count'] for m in node['media']]), "countries": countries, "formats": formats, } data.append(release) versions = defaultdict(list) for release in data: name = " / ".join([release[k] for k in namekeys]).replace("&", "&&") if name == release["tracks"]: name = "%s / %s" % (_('[no release info]'), name) versions[name].append(release) # de-duplicate names if possible for name, releases in versions.items(): for a, b in combinations(releases, 2): for key in extrakeys: (value1, value2) = (a[key], b[key]) if value1 != value2: a['_disambiguate_name'].append(value1) b['_disambiguate_name'].append(value2) for name, releases in versions.items(): for release in releases: dis = " / ".join( filter(None, uniqify(release['_disambiguate_name']))).replace( "&", "&&") disname = name if not dis else name + ' / ' + dis version = { 'id': release['id'], 'name': disname, 'totaltracks': release['totaltracks'], 'countries': release['countries'], 'formats': release['formats'], } self.versions.append(version) self.version_headings = " / ".join(_(headings[k]) for k in namekeys)
def _parse_versions(self, document): """Parse document and return a list of releases""" del self.versions[:] data = [] namekeys = ("tracks", "year", "country", "format", "label", "catnum") headings = { "tracks": N_('Tracks'), "year": N_('Year'), "country": N_('Country'), "format": N_('Format'), "label": N_('Label'), "catnum": N_('Cat No'), } extrakeys = ("packaging", "barcode", "disambiguation") try: releases = document['releases'] except (TypeError, KeyError): releases = [] for node in releases: labels, catnums = label_info_from_node(node['label-info']) countries = countries_from_node(node) formats = [] for medium in node['media']: if "format" in medium: formats.append(medium['format']) release = { "id": node['id'], "year": node['date'][:4] if "date" in node else "????", "country": "+".join(countries) if countries else node.get('country', '') or "??", "format": media_formats_from_node(node['media']), "label": ", ".join([' '.join(x.split(' ')[:2]) for x in set(labels)]), "catnum": ", ".join(set(catnums)), "tracks": "+".join([str(m['track-count']) for m in node['media']]), "barcode": node.get('barcode', '') or _('[no barcode]'), "packaging": node.get('packaging', '') or '??', "disambiguation": node.get('disambiguation', ''), "_disambiguate_name": list(), "totaltracks": sum([m['track-count'] for m in node['media']]), "countries": countries, "formats": formats, } data.append(release) versions = defaultdict(list) for release in data: name = " / ".join([release[k] for k in namekeys]).replace("&", "&&") if name == release["tracks"]: name = "%s / %s" % (_('[no release info]'), name) versions[name].append(release) # de-duplicate names if possible for name, releases in versions.items(): for a, b in combinations(releases, 2): for key in extrakeys: (value1, value2) = (a[key], b[key]) if value1 != value2: a['_disambiguate_name'].append(value1) b['_disambiguate_name'].append(value2) for name, releases in versions.items(): for release in releases: dis = " / ".join(filter(None, uniqify(release['_disambiguate_name']))).replace("&", "&&") disname = name if not dis else name + ' / ' + dis version = { 'id': release['id'], 'name': disname, 'totaltracks': release['totaltracks'], 'countries': release['countries'], 'formats': release['formats'], } self.versions.append(version) self.version_headings = " / ".join(_(headings[k]) for k in namekeys)