Extract methods in NSITunes

References: #3024
This commit is contained in:
Anderson Mesquita 2019-05-02 22:07:20 -04:00
parent 0107cf22cc
commit a4c852dac0
1 changed files with 70 additions and 50 deletions

View File

@ -7,6 +7,7 @@ import org.xml.sax.Attributes;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.syndication.handler.HandlerState;
public class NSITunes extends Namespace {
@ -22,7 +23,6 @@ public class NSITunes extends Namespace {
private static final String SUBTITLE = "subtitle";
private static final String SUMMARY = "summary";
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
@ -44,65 +44,85 @@ public class NSITunes extends Namespace {
@Override
public void handleElementEnd(String localName, HandlerState state) {
if(state.getContentBuf() == null) {
if (state.getContentBuf() == null) {
return;
}
SyndElement secondElement = state.getSecondTag();
String second = secondElement.getName();
if (AUTHOR.equals(localName)) {
if (state.getFeed() != null) {
String author = state.getContentBuf().toString();
state.getFeed().setAuthor(author);
}
parseAuthor(state);
} else if (DURATION.equals(localName)) {
String durationStr = state.getContentBuf().toString();
if(TextUtils.isEmpty(durationStr)) {
return;
}
String[] parts = durationStr.trim().split(":");
try {
int durationMs = 0;
if (parts.length == 2) {
durationMs += TimeUnit.MINUTES.toMillis(Long.parseLong(parts[0])) +
TimeUnit.SECONDS.toMillis((long)Float.parseFloat(parts[1]));
} else if (parts.length >= 3) {
durationMs += TimeUnit.HOURS.toMillis(Long.parseLong(parts[0])) +
TimeUnit.MINUTES.toMillis(Long.parseLong(parts[1])) +
TimeUnit.SECONDS.toMillis((long)Float.parseFloat(parts[2]));
} else {
return;
}
state.getTempObjects().put(DURATION, durationMs);
} catch (NumberFormatException e) {
Log.e(NSTAG, "Duration \"" + durationStr + "\" could not be parsed");
}
parseDuration(state);
} else if (SUBTITLE.equals(localName)) {
String subtitle = state.getContentBuf().toString();
if (TextUtils.isEmpty(subtitle)) {
return;
}
if (state.getCurrentItem() != null) {
if (TextUtils.isEmpty(state.getCurrentItem().getDescription())) {
state.getCurrentItem().setDescription(subtitle);
}
} else {
if (state.getFeed() != null && TextUtils.isEmpty(state.getFeed().getDescription())) {
state.getFeed().setDescription(subtitle);
}
}
parseSubtitle(state);
} else if (SUMMARY.equals(localName)) {
String summary = state.getContentBuf().toString();
if (TextUtils.isEmpty(summary)) {
SyndElement secondElement = state.getSecondTag();
parseSummary(state, secondElement.getName());
}
}
private void parseAuthor(HandlerState state) {
if (state.getFeed() != null) {
String author = state.getContentBuf().toString();
state.getFeed().setAuthor(author);
}
}
private void parseDuration(HandlerState state) {
String durationStr = state.getContentBuf().toString();
if (TextUtils.isEmpty(durationStr)) {
return;
}
String[] parts = durationStr.trim().split(":");
try {
int durationMs = 0;
if (parts.length == 2) {
durationMs += TimeUnit.MINUTES.toMillis(Long.parseLong(parts[0])) +
TimeUnit.SECONDS.toMillis((long) Float.parseFloat(parts[1]));
} else if (parts.length >= 3) {
durationMs += TimeUnit.HOURS.toMillis(Long.parseLong(parts[0])) +
TimeUnit.MINUTES.toMillis(Long.parseLong(parts[1])) +
TimeUnit.SECONDS.toMillis((long) Float.parseFloat(parts[2]));
} else {
return;
}
if (state.getCurrentItem() != null &&
(TextUtils.isEmpty(state.getCurrentItem().getDescription()) ||
state.getCurrentItem().getDescription().length() * 1.25 < summary.length())) {
state.getCurrentItem().setDescription(summary);
} else if (NSRSS20.CHANNEL.equals(second) && state.getFeed() != null) {
state.getFeed().setDescription(summary);
state.getTempObjects().put(DURATION, durationMs);
} catch (NumberFormatException e) {
Log.e(NSTAG, "Duration \"" + durationStr + "\" could not be parsed");
}
}
private void parseSubtitle(HandlerState state) {
String subtitle = state.getContentBuf().toString();
if (TextUtils.isEmpty(subtitle)) {
return;
}
if (state.getCurrentItem() != null) {
if (TextUtils.isEmpty(state.getCurrentItem().getDescription())) {
state.getCurrentItem().setDescription(subtitle);
}
} else {
if (state.getFeed() != null && TextUtils.isEmpty(state.getFeed().getDescription())) {
state.getFeed().setDescription(subtitle);
}
}
}
private void parseSummary(HandlerState state, String secondElementName) {
String summary = state.getContentBuf().toString();
if (TextUtils.isEmpty(summary)) {
return;
}
FeedItem currentItem = state.getCurrentItem();
String description = getDescription(currentItem);
if (currentItem != null && description.length() * 1.25 < summary.length()) {
currentItem.setDescription(summary);
} else if (NSRSS20.CHANNEL.equals(secondElementName) && state.getFeed() != null) {
state.getFeed().setDescription(summary);
}
}
private String getDescription(FeedItem item) {
return (item != null && item.getDescription() != null) ? item.getDescription() : "";
}
}