Skip to content

Commit

Permalink
Add pure Kotlin automix parser and tests. (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanisidrowu authored Jan 15, 2021
1 parent 7cda2ca commit 42b3754
Show file tree
Hide file tree
Showing 9 changed files with 568 additions and 161 deletions.
43 changes: 11 additions & 32 deletions android/src/main/java/tw/ktrssreader/parser/AndroidAutoMixParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package tw.ktrssreader.parser

import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import tw.ktrssreader.kotlin.constant.ParserConst
import tw.ktrssreader.kotlin.constant.ParserConst.AUTHOR
import tw.ktrssreader.kotlin.constant.ParserConst.BLOCK
import tw.ktrssreader.kotlin.constant.ParserConst.CATEGORY
import tw.ktrssreader.kotlin.constant.ParserConst.CHANNEL
import tw.ktrssreader.kotlin.constant.ParserConst.CLOUD
import tw.ktrssreader.kotlin.constant.ParserConst.COMMENTS
import tw.ktrssreader.kotlin.constant.ParserConst.COPYRIGHT
Expand Down Expand Up @@ -79,6 +79,8 @@ import tw.ktrssreader.kotlin.constant.ParserConst.TYPE
import tw.ktrssreader.kotlin.constant.ParserConst.URL
import tw.ktrssreader.kotlin.constant.ParserConst.WEB_MASTER
import tw.ktrssreader.kotlin.constant.ParserConst.WIDTH
import tw.ktrssreader.kotlin.extension.hrefToImage
import tw.ktrssreader.kotlin.extension.replaceInvalidUrlByPriority
import tw.ktrssreader.kotlin.model.channel.*
import tw.ktrssreader.kotlin.model.item.*
import tw.ktrssreader.utils.logD
Expand All @@ -93,6 +95,10 @@ class AndroidAutoMixParser : AndroidParserBase<AutoMixChannelData>() {
override val logTag: String = this::class.java.simpleName

override fun parse(xml: String): AutoMixChannelData {
rssStandardMap.clear()
iTunesMap.clear()
googlePlayMap.clear()

parseTag(xml)

return AutoMixChannelData(
Expand Down Expand Up @@ -139,7 +145,7 @@ class AndroidAutoMixParser : AndroidParserBase<AutoMixChannelData>() {

@Throws(IOException::class, XmlPullParserException::class)
private fun parseTag(xml: String) = parseChannel<Unit>(xml = xml) {
require(XmlPullParser.START_TAG, null, ParserConst.CHANNEL)
require(XmlPullParser.START_TAG, null, CHANNEL)

var title: String? = null
var description: String? = null
Expand Down Expand Up @@ -232,7 +238,7 @@ class AndroidAutoMixParser : AndroidParserBase<AutoMixChannelData>() {
else -> skip()
}
}
require(XmlPullParser.END_TAG, null, ParserConst.CHANNEL)
require(XmlPullParser.END_TAG, null, CHANNEL)

rssStandardMap.run {
put(TITLE, title)
Expand All @@ -257,7 +263,7 @@ class AndroidAutoMixParser : AndroidParserBase<AutoMixChannelData>() {
put(ITEM, items.takeIf { it.isNotEmpty() })
}
iTunesMap.run {
put(IMAGE, hrefToImage(iTunesImageHref))
put(IMAGE, iTunesImageHref.hrefToImage())
put(CATEGORY, iTunesCategories?.takeIf { it.isNotEmpty() })
put(ITUNES_TITLE, iTunesSimpleTitle)
put(EXPLICIT, iTunesExplicit)
Expand All @@ -271,7 +277,7 @@ class AndroidAutoMixParser : AndroidParserBase<AutoMixChannelData>() {
}
googlePlayMap.run {
put(DESCRIPTION, googleDescription)
put(IMAGE, hrefToImage(googleImageHref))
put(IMAGE, googleImageHref.hrefToImage())
put(CATEGORY, googleCategories?.takeIf { it.isNotEmpty() })
put(EXPLICIT, googleExplicit)
put(EMAIL, googleEmail)
Expand Down Expand Up @@ -473,31 +479,4 @@ class AndroidAutoMixParser : AndroidParserBase<AutoMixChannelData>() {
block = iTunesBlock ?: googleBlock
)
}

private fun Image?.replaceInvalidUrlByPriority(vararg priorityHref: String?): Image? {
if (this == null || url != null) return this
val href = priorityHref.firstOrNull { null != it } ?: return this

return Image(
link = link,
title = title,
url = href,
description = description,
height = height,
width = width
)
}

private fun hrefToImage(href: String?): Image? {
href ?: return null

return Image(
link = null,
title = null,
url = href,
description = null,
height = null,
width = null
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2020 Feng Hsien Hsu, Siao Syuan Yang, Wei-Qi Wang, Ya-Han Tsai, Yu Hao Wu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package tw.ktrssreader.kotlin.extension

import tw.ktrssreader.kotlin.model.channel.Image

fun Image?.replaceInvalidUrlByPriority(vararg priorityHref: String?): Image? {
if (this == null || url != null) return this
val href = priorityHref.firstOrNull { null != it } ?: return this

return Image(
link = link,
title = title,
url = href,
description = description,
height = height,
width = width
)
}

fun String?.hrefToImage(): Image? {
this ?: return null

return Image(
link = null,
title = null,
url = this,
description = null,
height = null,
width = null
)
}
Loading

0 comments on commit 42b3754

Please sign in to comment.