Skip to content

Commit

Permalink
Unified enrichers.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixRilling committed Dec 26, 2023
1 parent e5a5675 commit e2c95c7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -46,20 +45,19 @@ class AppleMusicReleaseEnricher implements GenreEnricher {
}

@Override

public Set<String> fetchGenres(RelationWs2 relation) {
Optional<Document> document = scrapingService.load(relation.getTargetId());
if (document.isEmpty()) {
return Set.of();
}

// We can only process genres if they are in english.
if (!hasLocaleLanguage(document.get(), Locale.ENGLISH)) {
LOGGER.debug("Skipping '{}' because the locale is not supported.", relation.getTargetId());
return Set.of();
}

return genreMatcherService.match(extractTags(document.get()));
return scrapingService.load(relation.getTargetId())
.filter(document -> {
// We can only process genres if they are in english.
if (!hasLocaleLanguage(document, Locale.ENGLISH)) {
LOGGER.debug("Skipping '{}' because the locale is not supported.", relation.getTargetId());
return false;
}
return true;
})
.map(this::extractTags)
.map(genreMatcherService::match)
.orElse(Set.of());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

Expand All @@ -31,7 +30,7 @@ class BandcampReleaseEnricher implements GenreEnricher {
private static final Logger LOGGER = LoggerFactory.getLogger(BandcampReleaseEnricher.class);

private static final Pattern HOST_REGEX = Pattern.compile(".+\\.bandcamp\\.com");
private static final Evaluator TAG_QUERY = QueryParser.parse(".tralbum-tags > a");
private static final Evaluator TAG_QUERY = QueryParser.parse(".tralbum-tags > .tag");

private final GenreMatcherService genreMatcherService;
private final ScrapingService scrapingService;
Expand All @@ -52,7 +51,7 @@ public Set<String> fetchGenres(RelationWs2 relation) {


private Set<String> extractTags(Document document) {
return new HashSet<>(document.select(TAG_QUERY).eachText());
return Set.copyOf(document.select(TAG_QUERY).eachText());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public Set<String> fetchGenres(RelationWs2 relation) {
return Set.of();
}
return discogsQueryService.lookUpRelease(discogsId.get())
.map(release -> genreMatcherService.match(extractGenres(release)))
.map(this::extractGenres)
.map(genreMatcherService::match)
.orElse(Set.of());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.musicbrainz.model.RelationWs2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;
import se.michaelthelin.spotify.model_objects.specification.Album;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -35,10 +35,11 @@ class SpotifyReleaseEnricher implements GenreEnricher {
@Override

public Set<String> fetchGenres(RelationWs2 relation) {
return spotifyQueryService.lookUpRelease(findReleaseId(relation.getTargetId())).map(release -> {
Set<String> genres = new HashSet<>(Arrays.asList(release.getGenres()));
return genreMatcherService.match(genres);
}).orElse(Set.of());
return spotifyQueryService
.lookUpRelease(findReleaseId(relation.getTargetId()))
.map(this::extractGenres)
.map(genreMatcherService::match)
.orElse(Set.of());
}


Expand All @@ -49,6 +50,10 @@ private String findReleaseId(String relationUrl) {
return matcher.group("id");
}

private Set<String> extractGenres(Album album) {
return Set.copyOf(Arrays.asList(album.getGenres()));
}

@Override
public boolean isRelationSupported(RelationWs2 relation) {
if (!"http://musicbrainz.org/ns/rel-2.0#url".equals(relation.getTargetType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Set;

/**
* AllMusic does not seem to have an API, so scraping it is.
* AllMusic does not seem to have an API, so we scrape it.
*/
// https://musicbrainz.org/release-group/a63e5fa6-d6ad-47bd-986d-4a27b0c9de70
// https://www.allmusic.com/album/mw0003185404
Expand Down Expand Up @@ -57,7 +57,6 @@ public boolean isRelationSupported(RelationWs2 relation) {
}

@Override

public DataType getDataType() {
return DataType.RELEASE_GROUP;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public Set<String> fetchGenres(RelationWs2 relation) {
return Set.of();
}
return discogsQueryService.lookUpMaster(discogsId.get())
.map(release -> genreMatcherService.match(extractGenres(release)))
.map(this::extractGenres)
.map(genreMatcherService::match)
.orElse(Set.of());
}

Expand Down

0 comments on commit e2c95c7

Please sign in to comment.