Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

Commit

Permalink
Correctly parse the year from a date string.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raptor399 committed Jul 22, 2013
1 parent 8593bfd commit a442757
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/main/java/net/pms/dlna/LibMediaInfoParser.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package net.pms.dlna;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.lowerCase;
import static org.apache.commons.lang3.StringUtils.substringAfterLast;

import java.io.File;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.pms.configuration.FormatConfiguration;
import net.pms.formats.v2.SubtitleType;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.StringTokenizer;

import static org.apache.commons.lang3.StringUtils.*;

public class LibMediaInfoParser {
private static final Logger logger = LoggerFactory.getLogger(LibMediaInfoParser.class);

/** Regular expression to parse a 4 digit year number from a string */
private static final String YEAR_REGEX = ".*([\\d]{4}).*";

/** Pattern to parse the year from a string */
private static final Pattern yearPattern = Pattern.compile(YEAR_REGEX);

private static MediaInfo MI;
private static Base64 base64;

Expand Down Expand Up @@ -178,12 +191,15 @@ public synchronized static void parse(DLNAMediaInfo media, InputFile inputFile,
} else if (key.equals("Genre") && streamType == MediaInfo.StreamType.General) {
currentAudioTrack.setGenre(ovalue);
} else if (key.equals("Recorded_Date") && streamType == MediaInfo.StreamType.General) {
try {
// Try to parse incorrectly stored date
String recordedDate = value.replaceAll("[^\\d]{4}", "");
currentAudioTrack.setYear(Integer.parseInt(recordedDate));
} catch (NumberFormatException nfe) {
logger.debug("Could not parse year \"" + value + "\"");
// Try to parse the year from the stored date
Matcher matcher = yearPattern.matcher(value);

if (matcher.matches()) {
try {
currentAudioTrack.setYear(Integer.parseInt(matcher.group(1)));
} catch (NumberFormatException nfe) {
logger.debug("Could not parse year from recorded date \"" + value + "\"");
}
}
} else if (key.equals("Track/Position") && streamType == MediaInfo.StreamType.General) {
try {
Expand Down

0 comments on commit a442757

Please sign in to comment.