pull/34/head
kunfei 5 years ago
parent 79db2a7234
commit 9cc4a8083b
  1. 75
      app/src/main/java/io/legado/app/model/rss/RssParser.kt

@ -28,125 +28,108 @@ object RssParser {
var eventType = xmlPullParser.eventType var eventType = xmlPullParser.eventType
// Start parsing the xml // Start parsing the xml
while (eventType != XmlPullParser.END_DOCUMENT) { loop@ while (eventType != XmlPullParser.END_DOCUMENT) {
// Start parsing the item // Start parsing the item
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
if (xmlPullParser.name.equals(RSSKeywords.RSS_ITEM, ignoreCase = true)) { when {
insideItem = true xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM,
} else if (xmlPullParser.name.equals( ignoreCase = true
) -> insideItem = true
xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_TITLE, RSSKeywords.RSS_ITEM_TITLE,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
currentArticle.title = xmlPullParser.nextText().trim() currentArticle.title = xmlPullParser.nextText().trim()
} }
xmlPullParser.name.equals(
} else if (xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_LINK, RSSKeywords.RSS_ITEM_LINK,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
currentArticle.link = xmlPullParser.nextText().trim() currentArticle.link = xmlPullParser.nextText().trim()
} }
xmlPullParser.name.equals(
} else if (xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_AUTHOR, RSSKeywords.RSS_ITEM_AUTHOR,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
currentArticle.author = xmlPullParser.nextText().trim() currentArticle.author = xmlPullParser.nextText().trim()
} }
xmlPullParser.name.equals(
} else if (xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_CATEGORY, RSSKeywords.RSS_ITEM_CATEGORY,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
currentArticle.categories.add(xmlPullParser.nextText().trim()) currentArticle.categories.add(xmlPullParser.nextText().trim())
} }
xmlPullParser.name.equals(
} else if (xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_THUMBNAIL, RSSKeywords.RSS_ITEM_THUMBNAIL,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
currentArticle.image = currentArticle.image =
xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL) xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL)
} }
xmlPullParser.name.equals(
} else if (xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_ENCLOSURE, RSSKeywords.RSS_ITEM_ENCLOSURE,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
val type = xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_TYPE) val type = xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_TYPE)
if (type != null && type.contains("image/")) { if (type != null && type.contains("image/")) {
currentArticle.image = currentArticle.image =
xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL) xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL)
} }
} }
xmlPullParser.name.equals(
} else if (xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_DESCRIPTION, RSSKeywords.RSS_ITEM_DESCRIPTION,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
val description = xmlPullParser.nextText() val description = xmlPullParser.nextText()
currentArticle.description = description.trim() currentArticle.description = description.trim()
if (currentArticle.image == null) { if (currentArticle.image == null) {
currentArticle.image = getImageUrl(description) currentArticle.image = getImageUrl(description)
} }
} }
xmlPullParser.name.equals(
} else if (xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_CONTENT, RSSKeywords.RSS_ITEM_CONTENT,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
val content = xmlPullParser.nextText().trim() val content = xmlPullParser.nextText().trim()
currentArticle.content = content currentArticle.content = content
if (currentArticle.image == null) { if (currentArticle.image == null) {
currentArticle.image = getImageUrl(content) currentArticle.image = getImageUrl(content)
} }
} }
xmlPullParser.name.equals(
} else if (xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_PUB_DATE, RSSKeywords.RSS_ITEM_PUB_DATE,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
val nextTokenType = xmlPullParser.next() val nextTokenType = xmlPullParser.next()
if (nextTokenType == XmlPullParser.TEXT) { if (nextTokenType == XmlPullParser.TEXT) {
currentArticle.pubDate = xmlPullParser.text.trim() currentArticle.pubDate = xmlPullParser.text.trim()
} }
// Skip to be able to find date inside 'tag' tag // Skip to be able to find date inside 'tag' tag
continue continue@loop
} }
} else if (xmlPullParser.name.equals( xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_TIME, RSSKeywords.RSS_ITEM_TIME,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
currentArticle.pubDate = xmlPullParser.nextText() currentArticle.pubDate = xmlPullParser.nextText()
} }
} else if (xmlPullParser.name.equals( xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_GUID, RSSKeywords.RSS_ITEM_GUID,
ignoreCase = true ignoreCase = true
) )
) { -> if (insideItem) {
if (insideItem) {
currentArticle.guid = xmlPullParser.nextText().trim() currentArticle.guid = xmlPullParser.nextText().trim()
} }
} }

Loading…
Cancel
Save