Better track year handling
So back in a previous version (forget the number but it was back in May of 2010), a lot of us were seeing stack traces for invalid years in our logs. That was because subsonic couldnt handle dates in the id3 tag, even though most taggers now seem to use full dates rather than just a 4 character year. Jump ahead to the current version and the same kind of issue is present, though it doesn't dump stack traces into the logs. What I see is on the sorted albums some have years next to them and others dont even for Albums I know have files that contain tags with the date in them. So looking through the code it appears that this is down to essentially the same issue as before in that the parser can only handle 4 digit years in the tag.
I would like to request that more robust tag handling be added to give the ability for it to properly identify the year off of files which have a date stamp in the tag rather than just the year. As a quick fix for simple plug into the existing JaudioTaggerParser class I added a couple private methods, as seen below, and then modified the call for setting the year from:
metaData.setYear(parseInteger(getTagField(tag, FieldKey.YEAR)));
To:
metaData.setYear(parseInteger(dateToYear(getTagField(tag, FieldKey.YEAR))));
If this were to be put in place I would recommend that on the version released that a util be run on initial install to rerun tags and update the year field for existing entries (I haven't coded that up yet as I just decided to clear out and recreate that part of the database).
I would like to request that more robust tag handling be added to give the ability for it to properly identify the year off of files which have a date stamp in the tag rather than just the year. As a quick fix for simple plug into the existing JaudioTaggerParser class I added a couple private methods, as seen below, and then modified the call for setting the year from:
metaData.setYear(parseInteger(getTagField(tag, FieldKey.YEAR)));
To:
metaData.setYear(parseInteger(dateToYear(getTagField(tag, FieldKey.YEAR))));
If this were to be put in place I would recommend that on the version released that a util be run on initial install to rerun tags and update the year field for existing entries (I haven't coded that up yet as I just decided to clear out and recreate that part of the database).
- Code: Select all
/**
* Try and retrieve a valid year from a date tag, since most taggers put full release date
* Will only try to grab a different date if the length of the string is > 4
*/
private String dateToYear(String date) {
String yr = date;
if (date.length() > 4) {
try {
if (date.matches("^\\d{4}.*")) {
int i = Integer.parseInt(date.substring(0,4));
if (isValidYear(i)) {
yr = date.substring(0,4);
}
}
if (date.matches(".*\\d{4}$") && yr.equals(date)){
int i = Integer.parseInt(date.substring(date.length()-4));
if (isValidYear(i)) {
yr = date.substring(date.length()-4);
}
}
}
catch (NumberFormatException nfe) {
// Ignore NFEs
// nfe.printStackTrace();
}
}
return yr;
}
/**
* Return true if integer appears to be a valid year
*/
private boolean isValidYear(Integer i) {
boolean val = false;
if (i>1900 && i<2100) {
val = true;
}
return val;
}