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:
parent
c8680af7ca
commit
0836d45217
@ -4,9 +4,6 @@ import android.app.Application;
|
|||||||
import android.arch.lifecycle.LiveData;
|
import android.arch.lifecycle.LiveData;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.Looper;
|
|
||||||
import android.support.annotation.ColorInt;
|
import android.support.annotation.ColorInt;
|
||||||
import android.support.v7.graphics.Palette;
|
import android.support.v7.graphics.Palette;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@ -20,21 +17,15 @@ import com.readrops.readropslibrary.ParsingResult;
|
|||||||
import com.readrops.readropslibrary.QueryCallback;
|
import com.readrops.readropslibrary.QueryCallback;
|
||||||
import com.readrops.readropslibrary.localfeed.AFeed;
|
import com.readrops.readropslibrary.localfeed.AFeed;
|
||||||
import com.readrops.readropslibrary.localfeed.RSSNetwork;
|
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.atom.ATOMFeed;
|
||||||
import com.readrops.readropslibrary.localfeed.json.JSONFeed;
|
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.RSSFeed;
|
||||||
import com.readrops.readropslibrary.localfeed.rss.RSSItem;
|
|
||||||
|
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
@ -142,7 +133,7 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
|
|||||||
|
|
||||||
private void parseATOMItems(ATOMFeed feed) {
|
private void parseATOMItems(ATOMFeed feed) {
|
||||||
try {
|
try {
|
||||||
Feed dbFeed = database.feedDao().getFeedByUrl(feed.getLink().getHref());
|
Feed dbFeed = database.feedDao().getFeedByUrl(feed.getUrl());
|
||||||
if (dbFeed == null) {
|
if (dbFeed == null) {
|
||||||
dbFeed = Feed.feedFromATOM(feed);
|
dbFeed = Feed.feedFromATOM(feed);
|
||||||
|
|
||||||
@ -156,8 +147,6 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
failureCallBackInMainThread(e);
|
failureCallBackInMainThread(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseJSONItems(JSONFeed feed) {
|
private void parseJSONItems(JSONFeed feed) {
|
||||||
|
@ -121,7 +121,7 @@ public class Feed {
|
|||||||
|
|
||||||
feed.setName(atomFeed.getTitle());
|
feed.setName(atomFeed.getTitle());
|
||||||
feed.setDescription(atomFeed.getSubtitle());
|
feed.setDescription(atomFeed.getSubtitle());
|
||||||
feed.setUrl(atomFeed.getLink().getHref());
|
feed.setUrl(atomFeed.getUrl());
|
||||||
feed.setSiteUrl(atomFeed.getWebSiteUrl());
|
feed.setSiteUrl(atomFeed.getWebSiteUrl());
|
||||||
feed.setDescription(atomFeed.getSubtitle());
|
feed.setDescription(atomFeed.getSubtitle());
|
||||||
feed.setLastUpdated(atomFeed.getUpdated());
|
feed.setLastUpdated(atomFeed.getUpdated());
|
||||||
|
@ -73,6 +73,17 @@ public class RSSNetwork {
|
|||||||
String xml = Utils.inputStreamToString(stream);
|
String xml = Utils.inputStreamToString(stream);
|
||||||
Serializer serializer = new Persister();
|
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) {
|
switch (type) {
|
||||||
case RSS_2:
|
case RSS_2:
|
||||||
RSSFeed rssFeed = serializer.read(RSSFeed.class, xml);
|
RSSFeed rssFeed = serializer.read(RSSFeed.class, xml);
|
||||||
@ -103,9 +114,9 @@ public class RSSNetwork {
|
|||||||
case Utils.RSS_DEFAULT_CONTENT_TYPE:
|
case Utils.RSS_DEFAULT_CONTENT_TYPE:
|
||||||
return RSSType.RSS_2;
|
return RSSType.RSS_2;
|
||||||
case Utils.RSS_TEXT_CONTENT_TYPE:
|
case Utils.RSS_TEXT_CONTENT_TYPE:
|
||||||
return RSSType.RSS_2;
|
return RSSType.RSS_UNKNOWN;
|
||||||
case Utils.RSS_APPLICATION_CONTENT_TYPE:
|
case Utils.RSS_APPLICATION_CONTENT_TYPE:
|
||||||
return RSSType.RSS_2;
|
return RSSType.RSS_UNKNOWN;
|
||||||
case Utils.ATOM_CONTENT_TYPE:
|
case Utils.ATOM_CONTENT_TYPE:
|
||||||
return RSSType.RSS_ATOM;
|
return RSSType.RSS_ATOM;
|
||||||
case Utils.JSON_CONTENT_TYPE:
|
case Utils.JSON_CONTENT_TYPE:
|
||||||
@ -120,7 +131,8 @@ public class RSSNetwork {
|
|||||||
public enum RSSType {
|
public enum RSSType {
|
||||||
RSS_2("rss"),
|
RSS_2("rss"),
|
||||||
RSS_ATOM("atom"),
|
RSS_ATOM("atom"),
|
||||||
RSS_JSON("json");
|
RSS_JSON("json"),
|
||||||
|
RSS_UNKNOWN("");
|
||||||
|
|
||||||
private String value;
|
private String value;
|
||||||
|
|
||||||
|
@ -14,8 +14,8 @@ public class ATOMFeed extends AFeed {
|
|||||||
@Element(required = false)
|
@Element(required = false)
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
@Element(name = "link", required = false)
|
@ElementList(name = "link", inline = true, required = false)
|
||||||
private ATOMLink link;
|
private List<ATOMLink> links;
|
||||||
|
|
||||||
@Element(required = false)
|
@Element(required = false)
|
||||||
private String id;
|
private String id;
|
||||||
@ -40,12 +40,12 @@ public class ATOMFeed extends AFeed {
|
|||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ATOMLink getLink() {
|
public List<ATOMLink> getLinks() {
|
||||||
return link;
|
return links;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLink(ATOMLink link) {
|
public void setLinks(List<ATOMLink> links) {
|
||||||
this.link = link;
|
this.links = links;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSubtitle() {
|
public String getSubtitle() {
|
||||||
@ -91,4 +91,14 @@ public class ATOMFeed extends AFeed {
|
|||||||
public String getWebSiteUrl() {
|
public String getWebSiteUrl() {
|
||||||
return id;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,9 +9,22 @@ public class ATOMLink {
|
|||||||
@Attribute(name = "href", required = false)
|
@Attribute(name = "href", required = false)
|
||||||
private String href;
|
private String href;
|
||||||
|
|
||||||
|
@Attribute(name = "rel", required = false)
|
||||||
|
private String rel;
|
||||||
|
|
||||||
public String getHref() {
|
public String getHref() {
|
||||||
return href;
|
return href;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setHref(String href) {
|
||||||
|
this.href = href;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRel() {
|
||||||
|
return rel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRel(String rel) {
|
||||||
|
this.rel = rel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user