Use LocalRSSDataSource isUrlRSSResource

This commit is contained in:
Shinokuni 2020-09-20 18:12:05 +02:00
parent 581de2e1dd
commit 6a1ddaeabb
2 changed files with 20 additions and 30 deletions

View File

@ -1,18 +1,14 @@
package com.readrops.api.localfeed
import com.readrops.api.utils.UnknownFormatException
import java.io.InputStream
import java.util.regex.Pattern
object LocalRSSHelper {
private const val RSS_DEFAULT_CONTENT_TYPE = "application/rss+xml"
private const val RSS_TEXT_CONTENT_TYPE = "text/xml"
private const val RSS_APPLICATION_CONTENT_TYPE = "application/xml"
private const val ATOM_CONTENT_TYPE = "application/atom+xml"
private const val JSONFEED_CONTENT_TYPE = "application/feed+json"
private const val JSON_CONTENT_TYPE = "application/json"
private const val HTML_CONTENT_TYPE = "text/html"
private const val RSS_2_REGEX = "rss.*version=\"2.0\""
@ -26,8 +22,7 @@ object LocalRSSHelper {
RSS_DEFAULT_CONTENT_TYPE -> RSSType.RSS_2
ATOM_CONTENT_TYPE -> RSSType.ATOM
JSON_CONTENT_TYPE, JSONFEED_CONTENT_TYPE -> RSSType.JSONFEED
RSS_TEXT_CONTENT_TYPE, RSS_APPLICATION_CONTENT_TYPE, HTML_CONTENT_TYPE -> RSSType.UNKNOWN
else -> throw UnknownFormatException("Unknown content type : $contentType")
else -> RSSType.UNKNOWN
}
}
@ -37,27 +32,21 @@ object LocalRSSHelper {
fun getRSSContentType(content: InputStream): RSSType {
val stringBuffer = StringBuffer()
val reader = content.bufferedReader()
var type = RSSType.UNKNOWN
var currentLine = reader.readLine()
while (currentLine != null) {
stringBuffer.append(currentLine)
// we get the first 10 lines which should be sufficient to get the type,
// otherwise iterating over the whole file could be too slow
for (i in 0..9) stringBuffer.append(reader.readLine())
if (Pattern.compile(RSS_2_REGEX).matcher(stringBuffer.toString()).find()) {
reader.close()
content.close()
return RSSType.RSS_2
type = RSSType.RSS_2
} else if (Pattern.compile(ATOM_REGEX).matcher(stringBuffer.toString()).find()) {
type = RSSType.ATOM
}
reader.close()
content.close()
return RSSType.ATOM
}
currentLine = reader.readLine()
}
return RSSType.UNKNOWN
return type
}
enum class RSSType {

View File

@ -7,13 +7,14 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.readrops.db.Database;
import com.readrops.db.entities.account.Account;
import com.readrops.api.localfeed.LocalRSSDataSource;
import com.readrops.api.utils.HttpManager;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.HtmlParser;
import com.readrops.app.utils.ParsingResult;
import com.readrops.api.localfeed.RSSQuery;
import com.readrops.db.Database;
import com.readrops.db.entities.account.Account;
import java.util.ArrayList;
import java.util.List;
@ -47,15 +48,15 @@ public class AddFeedsViewModel extends AndroidViewModel {
public Single<List<ParsingResult>> parseUrl(String url) {
return Single.create(emitter -> {
RSSQuery rssApi = new RSSQuery();
LocalRSSDataSource dataSource = new LocalRSSDataSource(HttpManager.getInstance().getOkHttpClient());
List<ParsingResult> results = new ArrayList<>();
if (rssApi.isUrlFeedLink(url)) {
if (dataSource.isUrlRSSResource(url)) {
ParsingResult parsingResult = new ParsingResult(url, null);
results.add(parsingResult);
} else
} else {
results.addAll(HtmlParser.getFeedLink(url));
}
emitter.onSuccess(results);
});