Fixing atom parsing problem and rss type discovery when the mime-type is not enough specific (application/xml, text/xml). Note that is a workaround and I definitely have to improve this.

This commit is contained in:
Shinokuni 2019-02-04 22:45:13 +00:00
parent c8680af7ca
commit 0836d45217
5 changed files with 46 additions and 22 deletions

View File

@ -4,9 +4,6 @@ import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.ColorInt;
import android.support.v7.graphics.Palette;
import android.util.Log;
@ -20,21 +17,15 @@ import com.readrops.readropslibrary.ParsingResult;
import com.readrops.readropslibrary.QueryCallback;
import com.readrops.readropslibrary.localfeed.AFeed;
import com.readrops.readropslibrary.localfeed.RSSNetwork;
import com.readrops.readropslibrary.localfeed.atom.ATOMEntry;
import com.readrops.readropslibrary.localfeed.atom.ATOMFeed;
import com.readrops.readropslibrary.localfeed.json.JSONFeed;
import com.readrops.readropslibrary.localfeed.json.JSONItem;
import com.readrops.readropslibrary.localfeed.rss.RSSFeed;
import com.readrops.readropslibrary.localfeed.rss.RSSItem;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@ -142,7 +133,7 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
private void parseATOMItems(ATOMFeed feed) {
try {
Feed dbFeed = database.feedDao().getFeedByUrl(feed.getLink().getHref());
Feed dbFeed = database.feedDao().getFeedByUrl(feed.getUrl());
if (dbFeed == null) {
dbFeed = Feed.feedFromATOM(feed);
@ -156,8 +147,6 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
} catch (Exception e) {
failureCallBackInMainThread(e);
}
}
private void parseJSONItems(JSONFeed feed) {

View File

@ -121,7 +121,7 @@ public class Feed {
feed.setName(atomFeed.getTitle());
feed.setDescription(atomFeed.getSubtitle());
feed.setUrl(atomFeed.getLink().getHref());
feed.setUrl(atomFeed.getUrl());
feed.setSiteUrl(atomFeed.getWebSiteUrl());
feed.setDescription(atomFeed.getSubtitle());
feed.setLastUpdated(atomFeed.getUpdated());

View File

@ -73,6 +73,17 @@ public class RSSNetwork {
String xml = Utils.inputStreamToString(stream);
Serializer serializer = new Persister();
if (type == RSSType.RSS_UNKNOWN) {
if (xml.contains("rss version=\"2.0\""))
type = RSSType.RSS_2;
else if (xml.contains("<feed xmlns=\"http://www.w3.org/2005/Atom\">"))
type = RSSType.RSS_ATOM;
else {
callback.onSyncFailure(new Exception("Unknown xml format"));
return;
}
}
switch (type) {
case RSS_2:
RSSFeed rssFeed = serializer.read(RSSFeed.class, xml);
@ -103,9 +114,9 @@ public class RSSNetwork {
case Utils.RSS_DEFAULT_CONTENT_TYPE:
return RSSType.RSS_2;
case Utils.RSS_TEXT_CONTENT_TYPE:
return RSSType.RSS_2;
return RSSType.RSS_UNKNOWN;
case Utils.RSS_APPLICATION_CONTENT_TYPE:
return RSSType.RSS_2;
return RSSType.RSS_UNKNOWN;
case Utils.ATOM_CONTENT_TYPE:
return RSSType.RSS_ATOM;
case Utils.JSON_CONTENT_TYPE:
@ -120,7 +131,8 @@ public class RSSNetwork {
public enum RSSType {
RSS_2("rss"),
RSS_ATOM("atom"),
RSS_JSON("json");
RSS_JSON("json"),
RSS_UNKNOWN("");
private String value;

View File

@ -14,8 +14,8 @@ public class ATOMFeed extends AFeed {
@Element(required = false)
private String title;
@Element(name = "link", required = false)
private ATOMLink link;
@ElementList(name = "link", inline = true, required = false)
private List<ATOMLink> links;
@Element(required = false)
private String id;
@ -40,12 +40,12 @@ public class ATOMFeed extends AFeed {
this.title = title;
}
public ATOMLink getLink() {
return link;
public List<ATOMLink> getLinks() {
return links;
}
public void setLink(ATOMLink link) {
this.link = link;
public void setLinks(List<ATOMLink> links) {
this.links = links;
}
public String getSubtitle() {
@ -91,4 +91,14 @@ public class ATOMFeed extends AFeed {
public String getWebSiteUrl() {
return id;
}
public String getUrl() {
if (links.size() > 1) {
if (links.get(0).getRel() != null)
return links.get(0).getHref();
else
return links.get(1).getHref();
} else
return null;
}
}

View File

@ -9,9 +9,22 @@ public class ATOMLink {
@Attribute(name = "href", required = false)
private String href;
@Attribute(name = "rel", required = false)
private String rel;
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String getRel() {
return rel;
}
public void setRel(String rel) {
this.rel = rel;
}
}