Use LocalRSSDataSource isUrlRSSResource
This commit is contained in:
parent
581de2e1dd
commit
6a1ddaeabb
@ -1,18 +1,14 @@
|
|||||||
package com.readrops.api.localfeed
|
package com.readrops.api.localfeed
|
||||||
|
|
||||||
import com.readrops.api.utils.UnknownFormatException
|
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
object LocalRSSHelper {
|
object LocalRSSHelper {
|
||||||
|
|
||||||
private const val RSS_DEFAULT_CONTENT_TYPE = "application/rss+xml"
|
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 ATOM_CONTENT_TYPE = "application/atom+xml"
|
||||||
private const val JSONFEED_CONTENT_TYPE = "application/feed+json"
|
private const val JSONFEED_CONTENT_TYPE = "application/feed+json"
|
||||||
private const val JSON_CONTENT_TYPE = "application/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\""
|
private const val RSS_2_REGEX = "rss.*version=\"2.0\""
|
||||||
|
|
||||||
@ -26,8 +22,7 @@ object LocalRSSHelper {
|
|||||||
RSS_DEFAULT_CONTENT_TYPE -> RSSType.RSS_2
|
RSS_DEFAULT_CONTENT_TYPE -> RSSType.RSS_2
|
||||||
ATOM_CONTENT_TYPE -> RSSType.ATOM
|
ATOM_CONTENT_TYPE -> RSSType.ATOM
|
||||||
JSON_CONTENT_TYPE, JSONFEED_CONTENT_TYPE -> RSSType.JSONFEED
|
JSON_CONTENT_TYPE, JSONFEED_CONTENT_TYPE -> RSSType.JSONFEED
|
||||||
RSS_TEXT_CONTENT_TYPE, RSS_APPLICATION_CONTENT_TYPE, HTML_CONTENT_TYPE -> RSSType.UNKNOWN
|
else -> RSSType.UNKNOWN
|
||||||
else -> throw UnknownFormatException("Unknown content type : $contentType")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,27 +32,21 @@ object LocalRSSHelper {
|
|||||||
fun getRSSContentType(content: InputStream): RSSType {
|
fun getRSSContentType(content: InputStream): RSSType {
|
||||||
val stringBuffer = StringBuffer()
|
val stringBuffer = StringBuffer()
|
||||||
val reader = content.bufferedReader()
|
val reader = content.bufferedReader()
|
||||||
|
var type = RSSType.UNKNOWN
|
||||||
|
|
||||||
var currentLine = reader.readLine()
|
// we get the first 10 lines which should be sufficient to get the type,
|
||||||
while (currentLine != null) {
|
// otherwise iterating over the whole file could be too slow
|
||||||
stringBuffer.append(currentLine)
|
for (i in 0..9) stringBuffer.append(reader.readLine())
|
||||||
|
|
||||||
if (Pattern.compile(RSS_2_REGEX).matcher(stringBuffer.toString()).find()) {
|
if (Pattern.compile(RSS_2_REGEX).matcher(stringBuffer.toString()).find()) {
|
||||||
reader.close()
|
type = RSSType.RSS_2
|
||||||
content.close()
|
|
||||||
|
|
||||||
return RSSType.RSS_2
|
|
||||||
} else if (Pattern.compile(ATOM_REGEX).matcher(stringBuffer.toString()).find()) {
|
} else if (Pattern.compile(ATOM_REGEX).matcher(stringBuffer.toString()).find()) {
|
||||||
|
type = RSSType.ATOM
|
||||||
|
}
|
||||||
|
|
||||||
reader.close()
|
reader.close()
|
||||||
content.close()
|
content.close()
|
||||||
|
return type
|
||||||
return RSSType.ATOM
|
|
||||||
}
|
|
||||||
|
|
||||||
currentLine = reader.readLine()
|
|
||||||
}
|
|
||||||
|
|
||||||
return RSSType.UNKNOWN
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class RSSType {
|
enum class RSSType {
|
||||||
|
@ -7,13 +7,14 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.lifecycle.AndroidViewModel;
|
import androidx.lifecycle.AndroidViewModel;
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
|
|
||||||
import com.readrops.db.Database;
|
import com.readrops.api.localfeed.LocalRSSDataSource;
|
||||||
import com.readrops.db.entities.account.Account;
|
import com.readrops.api.utils.HttpManager;
|
||||||
import com.readrops.app.repositories.ARepository;
|
import com.readrops.app.repositories.ARepository;
|
||||||
import com.readrops.app.utils.FeedInsertionResult;
|
import com.readrops.app.utils.FeedInsertionResult;
|
||||||
import com.readrops.app.utils.HtmlParser;
|
import com.readrops.app.utils.HtmlParser;
|
||||||
import com.readrops.app.utils.ParsingResult;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -47,15 +48,15 @@ public class AddFeedsViewModel extends AndroidViewModel {
|
|||||||
|
|
||||||
public Single<List<ParsingResult>> parseUrl(String url) {
|
public Single<List<ParsingResult>> parseUrl(String url) {
|
||||||
return Single.create(emitter -> {
|
return Single.create(emitter -> {
|
||||||
RSSQuery rssApi = new RSSQuery();
|
LocalRSSDataSource dataSource = new LocalRSSDataSource(HttpManager.getInstance().getOkHttpClient());
|
||||||
List<ParsingResult> results = new ArrayList<>();
|
List<ParsingResult> results = new ArrayList<>();
|
||||||
|
|
||||||
if (rssApi.isUrlFeedLink(url)) {
|
if (dataSource.isUrlRSSResource(url)) {
|
||||||
ParsingResult parsingResult = new ParsingResult(url, null);
|
ParsingResult parsingResult = new ParsingResult(url, null);
|
||||||
results.add(parsingResult);
|
results.add(parsingResult);
|
||||||
|
} else {
|
||||||
} else
|
|
||||||
results.addAll(HtmlParser.getFeedLink(url));
|
results.addAll(HtmlParser.getFeedLink(url));
|
||||||
|
}
|
||||||
|
|
||||||
emitter.onSuccess(results);
|
emitter.onSuccess(results);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user