diff --git a/source/net/filebot/web/ID3Lookup.java b/source/net/filebot/web/ID3Lookup.java index 384ad53c..1187de10 100644 --- a/source/net/filebot/web/ID3Lookup.java +++ b/source/net/filebot/web/ID3Lookup.java @@ -6,9 +6,11 @@ import static net.filebot.util.FileUtilities.*; import static net.filebot.util.StringUtilities.*; import java.io.File; +import java.io.FileFilter; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; +import java.util.function.Function; import javax.swing.Icon; @@ -34,46 +36,25 @@ public class ID3Lookup implements MusicIdentificationService { } @Override - public Map lookup(Collection files) throws Exception { - Map info = new LinkedHashMap(); + public Map lookup(Collection files) { + return read(files, this::getAudioTrack, AUDIO_FILES, VIDEO_FILES); + } - try (MediaInfo mediaInfo = new MediaInfo()) { - for (File f : filter(files, AUDIO_FILES, VIDEO_FILES)) { + private Map read(Collection files, Function parse, FileFilter... filters) { + Map info = new LinkedHashMap(files.size()); + + try (MediaInfo m = new MediaInfo()) { + for (File f : filter(files, filters)) { try { // open or throw exception - mediaInfo.open(f); + m.open(f); - // artist and song title information is required - String artist = getString(mediaInfo, "Performer", "Composer"); - String title = getString(mediaInfo, "Title", "Track"); - - if (artist != null && title != null) { - // all other properties are optional - String album = getString(mediaInfo, "Album"); - String albumArtist = getString(mediaInfo, "Album/Performer"); - String trackTitle = getString(mediaInfo, "Track"); - Integer mediumIndex = null; - Integer mediumCount = null; - Integer trackIndex = getInteger(mediaInfo, "Track/Position"); - Integer trackCount = getInteger(mediaInfo, "Track/Position_Total"); - String mbid = getString(mediaInfo, "Acoustid Id"); - - // try to parse 2016-03-10 - String dateString = getString(mediaInfo, "Recorded_Date"); - SimpleDate albumReleaseDate = SimpleDate.parse(dateString); - - // try to parse 2016 - if (albumReleaseDate == null) { - Integer year = matchInteger(dateString); - if (year != null) { - albumReleaseDate = new SimpleDate(year, 1, 1); - } - } - - info.put(f, new AudioTrack(artist, title, album, albumArtist, trackTitle, albumReleaseDate, mediumIndex, mediumCount, trackIndex, trackCount, mbid, getIdentifier())); + T object = parse.apply(m); + if (object != null) { + info.put(f, object); } } catch (Throwable e) { - debug.warning(e.getMessage()); + debug.warning(e::getMessage); } } } @@ -81,6 +62,73 @@ public class ID3Lookup implements MusicIdentificationService { return info; } + public AudioTrack getAudioTrack(File file) { + return read(file, this::getAudioTrack); + } + + public Episode Episode(File file) { + return read(file, this::getEpisode); + } + + private T read(File file, Function parse) { + try (MediaInfo m = new MediaInfo()) { + try { + return parse.apply(m.open(file)); + } catch (Throwable e) { + debug.warning(e::getMessage); + } + } + + return null; + } + + public AudioTrack getAudioTrack(MediaInfo m) { + // artist and song title information is required + String artist = getString(m, "Performer", "Composer"); + String title = getString(m, "Title", "Track"); + + if (artist == null || title == null) { + return null; + } + + // all other properties are optional + String album = getString(m, "Album"); + String albumArtist = getString(m, "Album/Performer"); + String trackTitle = getString(m, "Track"); + Integer mediumIndex = null; + Integer mediumCount = null; + Integer trackIndex = getInteger(m, "Track/Position"); + Integer trackCount = getInteger(m, "Track/Position_Total"); + String mbid = getString(m, "Acoustid Id"); + + // try to parse 2016-03-10 + String dateString = getString(m, "Recorded_Date"); + SimpleDate albumReleaseDate = SimpleDate.parse(dateString); + + // try to parse 2016 + if (albumReleaseDate == null) { + Integer year = matchInteger(dateString); + if (year != null) { + albumReleaseDate = new SimpleDate(year, 1, 1); + } + } + + return new AudioTrack(artist, title, album, albumArtist, trackTitle, albumReleaseDate, mediumIndex, mediumCount, trackIndex, trackCount, mbid, getIdentifier()); + } + + public Episode getEpisode(MediaInfo m) { + String series = getString(m, "Album"); + Integer season = getInteger(m, "Season"); + Integer episode = getInteger(m, "Part", "Track/Position"); + String title = getString(m, "Title", "Track"); + + if (series == null || episode == null) { + return null; + } + + return new Episode(series, season, episode, title); + } + private String getString(MediaInfo mediaInfo, String... keys) { for (String key : keys) { String value = mediaInfo.get(StreamKind.General, 0, key); @@ -91,8 +139,8 @@ public class ID3Lookup implements MusicIdentificationService { return null; } - private Integer getInteger(MediaInfo mediaInfo, String field) { - return matchInteger(getString(mediaInfo, field)); + private Integer getInteger(MediaInfo mediaInfo, String... keys) { + return matchInteger(getString(mediaInfo, keys)); } }