pull/34/head
kunfei 5 years ago
parent 8171c938a0
commit bf77172bbc
  1. 97
      app/src/main/java/io/legado/app/model/rss/RssParser.kt

@ -33,75 +33,48 @@ object RssParser {
// Start parsing the item // Start parsing the item
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
when { when {
xmlPullParser.name.equals( xmlPullParser.name
RSSKeywords.RSS_ITEM, .equals(RSSKeywords.RSS_ITEM, true) -> insideItem = true
ignoreCase = true xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_TITLE, true) ->
) -> insideItem = true if (insideItem) currentArticle.title = xmlPullParser.nextText().trim()
xmlPullParser.name.equals( xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_LINK, true) ->
RSSKeywords.RSS_ITEM_TITLE, if (insideItem) currentArticle.link = xmlPullParser.nextText().trim()
ignoreCase = true xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_AUTHOR, true) ->
) -> if (insideItem) { if (insideItem) currentArticle.author = xmlPullParser.nextText().trim()
currentArticle.title = xmlPullParser.nextText().trim() xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_CATEGORY, true) ->
} if (insideItem) currentArticle.categories.add(xmlPullParser.nextText().trim())
xmlPullParser.name.equals( xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_THUMBNAIL, true) ->
RSSKeywords.RSS_ITEM_LINK, if (insideItem) currentArticle.image =
ignoreCase = true
) -> if (insideItem) {
currentArticle.link = xmlPullParser.nextText().trim()
}
xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_AUTHOR,
ignoreCase = true
) -> if (insideItem) {
currentArticle.author = xmlPullParser.nextText().trim()
}
xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_CATEGORY,
ignoreCase = true
) -> if (insideItem) {
currentArticle.categories.add(xmlPullParser.nextText().trim())
}
xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_THUMBNAIL,
ignoreCase = true
) -> if (insideItem) {
currentArticle.image =
xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL) xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_URL)
} xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_ENCLOSURE, true) ->
xmlPullParser.name.equals( if (insideItem) {
RSSKeywords.RSS_ITEM_ENCLOSURE, val type =
ignoreCase = true xmlPullParser.getAttributeValue(null, RSSKeywords.RSS_ITEM_TYPE)
) -> if (insideItem) {
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( xmlPullParser.name
RSSKeywords.RSS_ITEM_DESCRIPTION, .equals(RSSKeywords.RSS_ITEM_DESCRIPTION, 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( xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_CONTENT, true) ->
RSSKeywords.RSS_ITEM_CONTENT, if (insideItem) {
ignoreCase = true
) -> 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( xmlPullParser.name
RSSKeywords.RSS_ITEM_PUB_DATE, .equals(RSSKeywords.RSS_ITEM_PUB_DATE, 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()
@ -109,23 +82,13 @@ object RssParser {
// Skip to be able to find date inside 'tag' tag // Skip to be able to find date inside 'tag' tag
continue@loop continue@loop
} }
xmlPullParser.name.equals( xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_TIME, true) ->
RSSKeywords.RSS_ITEM_TIME, if (insideItem) currentArticle.pubDate = xmlPullParser.nextText()
ignoreCase = true xmlPullParser.name.equals(RSSKeywords.RSS_ITEM_GUID, true) ->
) -> if (insideItem) { if (insideItem) currentArticle.guid = xmlPullParser.nextText().trim()
currentArticle.pubDate = xmlPullParser.nextText()
}
xmlPullParser.name.equals(
RSSKeywords.RSS_ITEM_GUID,
ignoreCase = true
) -> if (insideItem) {
currentArticle.guid = xmlPullParser.nextText().trim()
}
} }
} else if (eventType == XmlPullParser.END_TAG && xmlPullParser.name.equals( } else if (eventType == XmlPullParser.END_TAG
"item", && xmlPullParser.name.equals("item", true)
ignoreCase = true
)
) { ) {
// The item is correctly parsed // The item is correctly parsed
insideItem = false insideItem = false

Loading…
Cancel
Save