Added support for itunes:duration tag in feeds

Ported from AntennaPodSP
This commit is contained in:
daniel oeh 2014-10-19 19:07:26 +02:00
parent 3a1ced0301
commit eff021c149
4 changed files with 54 additions and 4 deletions

View File

@ -1,12 +1,16 @@
package de.danoeh.antennapod.core.syndication.handler; package de.danoeh.antennapod.core.syndication.handler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Stack;
import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.syndication.namespace.Namespace; import de.danoeh.antennapod.core.syndication.namespace.Namespace;
import de.danoeh.antennapod.core.syndication.namespace.SyndElement; import de.danoeh.antennapod.core.syndication.namespace.SyndElement;
import java.util.*;
/** /**
* Contains all relevant information to describe the current state of a * Contains all relevant information to describe the current state of a
* SyndHandler. * SyndHandler.
@ -35,6 +39,11 @@ public class HandlerState {
*/ */
protected StringBuffer contentBuf; protected StringBuffer contentBuf;
/**
* Temporarily saved objects.
*/
protected HashMap<String, Object> tempObjects;
public HandlerState(Feed feed) { public HandlerState(Feed feed) {
this.feed = feed; this.feed = feed;
alternateUrls = new LinkedHashMap<String, String>(); alternateUrls = new LinkedHashMap<String, String>();
@ -42,6 +51,7 @@ public class HandlerState {
tagstack = new Stack<SyndElement>(); tagstack = new Stack<SyndElement>();
namespaces = new HashMap<String, Namespace>(); namespaces = new HashMap<String, Namespace>();
defaultNamespaces = new Stack<Namespace>(); defaultNamespaces = new Stack<Namespace>();
tempObjects = new HashMap<String, Object>();
} }
public Feed getFeed() { public Feed getFeed() {
@ -95,4 +105,7 @@ public class HandlerState {
alternateUrls.put(url, title); alternateUrls.put(url, title);
} }
public HashMap<String, Object> getTempObjects() {
return tempObjects;
}
} }

View File

@ -4,6 +4,8 @@ import de.danoeh.antennapod.core.feed.FeedImage;
import de.danoeh.antennapod.core.syndication.handler.HandlerState; import de.danoeh.antennapod.core.syndication.handler.HandlerState;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import java.util.concurrent.TimeUnit;
public class NSITunes extends Namespace { public class NSITunes extends Namespace {
public static final String NSTAG = "itunes"; public static final String NSTAG = "itunes";
public static final String NSURI = "http://www.itunes.com/dtds/podcast-1.0.dtd"; public static final String NSURI = "http://www.itunes.com/dtds/podcast-1.0.dtd";
@ -13,6 +15,7 @@ public class NSITunes extends Namespace {
private static final String IMAGE_HREF = "href"; private static final String IMAGE_HREF = "href";
private static final String AUTHOR = "author"; private static final String AUTHOR = "author";
public static final String DURATION = "duration";
@Override @Override
@ -25,10 +28,10 @@ public class NSITunes extends Namespace {
if (state.getCurrentItem() != null) { if (state.getCurrentItem() != null) {
// this is an items image // this is an items image
image.setTitle(state.getCurrentItem().getTitle() + IMAGE_TITLE); image.setTitle(state.getCurrentItem().getTitle()+IMAGE_TITLE);
state.getCurrentItem().setImage(image); state.getCurrentItem().setImage(image);
} else { } else {
// this is the feed image // this is the feed image
if (state.getFeed().getImage() == null) { if (state.getFeed().getImage() == null) {
state.getFeed().setImage(image); state.getFeed().setImage(image);
@ -44,6 +47,25 @@ public class NSITunes extends Namespace {
public void handleElementEnd(String localName, HandlerState state) { public void handleElementEnd(String localName, HandlerState state) {
if (localName.equals(AUTHOR)) { if (localName.equals(AUTHOR)) {
state.getFeed().setAuthor(state.getContentBuf().toString()); state.getFeed().setAuthor(state.getContentBuf().toString());
} else if (localName.equals(DURATION)) {
String[] parts = state.getContentBuf().toString().split(":");
try {
int duration = 0;
if (parts.length == 2) {
duration += TimeUnit.MINUTES.toMillis(Long.valueOf(parts[0])) +
TimeUnit.SECONDS.toMillis(Long.valueOf(parts[1]));
} else if (parts.length >= 3) {
duration += TimeUnit.HOURS.toMillis(Long.valueOf(parts[0])) +
TimeUnit.MINUTES.toMillis(Long.valueOf(parts[1])) +
TimeUnit.SECONDS.toMillis(Long.valueOf(parts[2]));
} else {
return;
}
state.getTempObjects().put(DURATION, duration);
} catch (NumberFormatException e) {
e.printStackTrace();
}
} }
} }

View File

@ -85,6 +85,13 @@ public class NSRSS20 extends Namespace {
state.getCurrentItem().setTitle( state.getCurrentItem().setTitle(
state.getCurrentItem().getDescription()); state.getCurrentItem().getDescription());
} }
if (state.getTempObjects().containsKey(NSITunes.DURATION)) {
if (state.getCurrentItem().hasMedia()) {
state.getCurrentItem().getMedia().setDuration((Integer) state.getTempObjects().get(NSITunes.DURATION));
}
state.getTempObjects().remove(NSITunes.DURATION);
}
} }
state.setCurrentItem(null); state.setCurrentItem(null);
} else if (state.getTagstack().size() >= 2 } else if (state.getTagstack().size() >= 2

View File

@ -6,6 +6,7 @@ import de.danoeh.antennapod.core.feed.FeedImage;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.syndication.handler.HandlerState; import de.danoeh.antennapod.core.syndication.handler.HandlerState;
import de.danoeh.antennapod.core.syndication.namespace.NSITunes;
import de.danoeh.antennapod.core.syndication.namespace.NSRSS20; import de.danoeh.antennapod.core.syndication.namespace.NSRSS20;
import de.danoeh.antennapod.core.syndication.namespace.Namespace; import de.danoeh.antennapod.core.syndication.namespace.Namespace;
import de.danoeh.antennapod.core.syndication.namespace.SyndElement; import de.danoeh.antennapod.core.syndication.namespace.SyndElement;
@ -128,6 +129,13 @@ public class NSAtom extends Namespace {
@Override @Override
public void handleElementEnd(String localName, HandlerState state) { public void handleElementEnd(String localName, HandlerState state) {
if (localName.equals(ENTRY)) { if (localName.equals(ENTRY)) {
if (state.getCurrentItem() != null &&
state.getTempObjects().containsKey(NSITunes.DURATION)) {
if (state.getCurrentItem().hasMedia()) {
state.getCurrentItem().getMedia().setDuration((Integer) state.getTempObjects().get(NSITunes.DURATION));
}
state.getTempObjects().remove(NSITunes.DURATION);
}
state.setCurrentItem(null); state.setCurrentItem(null);
} }