Skip to content

Commit

Permalink
Remplacement complet de Discogs par MusicBrainz #4
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-warnan committed Sep 20, 2017
1 parent 7f4a4d3 commit d4e64c3
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 88 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ java -jar missing-albums.jar [options]

- `-g`, `--genre` — Search missing albums for all artists of the specified genre.
Example: `--genre Metal`

- `-o`, `--only-albums` — If this flag is set, the program will only return releases that are albums.
This is very convenient to limit the number of results.
But it relies on HTML parsing and not on the discogs webservice because the API does not allow to search for album only.
For this reason, this function might be broken at some point.
Example: `--only-albums`

- `-f`, `--output-format` — Format of the output. There are three formats:
- csv: comma separated values,
Expand Down
62 changes: 0 additions & 62 deletions src/main/java/fr/plaisance/arn/bean/TestWs.java

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/java/fr/plaisance/arn/main/Params.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ public static Params getInstance(){
"Example: --skip-siblings")
public Boolean skipSiblings = false;

@Parameter(names = {"-o", "--only-albums"}, description =
"If this flag is set, the program will only return releases that are albums. " +
"This is very convenient to limit the number of results. " +
"But it rely on HTML parsing and not on the discogs webservice because the API does not allow to search for album only. " +
"For this reason, this function might be broken at some point." +
"Example: --only-albums")
public Boolean onlyAlbums = false;

@Parameter(names = {"-y", "--year"}, description =
"If set, missing albums will not be displayed if they were released before this year. " +
"If not, only missing albums that have been released after the latest one in the library will be displayed. " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import fr.plaisance.arn.model.Artist;

public interface DiscogsService {
public interface ArtistFinder {

Artist find(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import fr.plaisance.arn.model.Album;
import fr.plaisance.arn.model.Artist;
import fr.plaisance.arn.model.Model;
import fr.plaisance.arn.service.DiscogsService;
import fr.plaisance.arn.service.ArtistFinder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
Expand All @@ -24,8 +23,8 @@
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DiscogsServiceSimple implements DiscogsService {
// @Service
public class DiscogsFinder implements ArtistFinder {

@Autowired
private Properties properties;
Expand Down Expand Up @@ -68,9 +67,6 @@ private DiscogsQuery query(String artistName) {
}

private DiscogsReleases releases(DiscogsArtist artist){
if(Params.getInstance().onlyAlbums){
return this.onlyAlbums(artist);
}
return this.allReleases(artist);
}

Expand Down
88 changes: 88 additions & 0 deletions src/main/java/fr/plaisance/arn/service/impl/MusicBrainzFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fr.plaisance.arn.service.impl;

import fr.plaisance.arn.bean.MusicBrainzAlbum;
import fr.plaisance.arn.bean.MusicBrainzReleases;
import fr.plaisance.arn.main.Params;
import fr.plaisance.arn.model.Album;
import fr.plaisance.arn.model.Artist;
import fr.plaisance.arn.model.Model;
import fr.plaisance.arn.service.ArtistFinder;
import org.apache.commons.collections.CollectionUtils;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MusicBrainzFinder implements ArtistFinder {

private static final String HOST = "https://musicbrainz.org";
private static final String QUERY = "artist:\"%s\" AND status:official AND primarytype:album NOT secondarytype:[* TO *]";

@Autowired
private Properties properties;

private static Client client;

@PostConstruct
private void postConstruct() {
ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
config.property(ClientProperties.PROXY_URI, properties.getProperty("proxy"));
client = ClientBuilder.newClient(config);
}

@Override
public Artist find(String name) {
Params.logger.debug(String.format("Searching missing albums for artist '%s'", name));
Artist artist = Model.newArtist(name);
List<UUID> releases = this.releases(name);
if (CollectionUtils.isNotEmpty(releases)) {
Params.logger.trace(String.format("Artist '%s' found in database", name));
Set<Album> albums = releases
.stream()
.map(this::album)
.map(a -> Model.newAlbum(a.getTitle(), String.valueOf(a.getYear())))
.collect(Collectors.toSet());
artist.setAlbums(new TreeSet<>(albums));
}
else {
Params.logger.trace(String.format("Artist '%s' not found!", name));
}
return artist;
}

private List<UUID> releases(String artistName) {
return client.target(HOST)
.path("ws/2")
.path("release-group")
.queryParam("limit", 100)
.queryParam("query", String.format(QUERY, artistName))
.request(MediaType.APPLICATION_XML)
.get(MusicBrainzReleases.class)
.getList()
.getReleases()
.stream()
.filter(release -> release.getScore() > 90)
.map(release -> release.getId())
.collect(Collectors.toList());
}

private MusicBrainzAlbum.MusicBrainzReleaseGroup album(UUID release) {
return client.target(HOST)
.path("ws/2")
.path("release-group")
.path(release.toString())
.request(MediaType.APPLICATION_XML)
.get(MusicBrainzAlbum.class)
.getAlbum();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
import fr.plaisance.arn.model.Artist;
import fr.plaisance.arn.model.Library;
import fr.plaisance.arn.model.Model;
import fr.plaisance.arn.service.DiscogsService;
import fr.plaisance.arn.service.ArtistFinder;
import fr.plaisance.arn.service.RemoteLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DiscogsLibraryService implements RemoteLibraryService {
public class SimpleLibraryService implements RemoteLibraryService {

@Autowired
private DiscogsService discogsService;
private ArtistFinder artistFinder;

@Override
public Library library(Library localLibrary) {
Params.logger.trace("Building remote library from local library");
Library remoteLibrary = Model.newLibrary();
for (Artist artist : localLibrary.getArtists()) {
Artist remoteArtist = discogsService.find(artist.getName());
Artist remoteArtist = artistFinder.find(artist.getName());
Model.addArtistToLibrary(remoteLibrary, remoteArtist);
}
return remoteLibrary;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<property name="locations">
<list>
<value>classpath*:discogs.properties</value>
<value>classpath*:proxy.properties</value>
</list>
</property>
</bean>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/proxy.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
proxy=http://proxy-rie.http.insee.fr:8080

0 comments on commit d4e64c3

Please sign in to comment.