Fixing some atom and html parsing problems, improving favicon link retrieval. The ATOM parsing should be now at the same level as the RSS one

This commit is contained in:
Shinokuni 2019-02-03 19:41:53 +00:00
parent c5cca8d822
commit bb9d79a9da
9 changed files with 58 additions and 23 deletions

View File

@ -10,6 +10,7 @@ import android.os.Looper;
import android.support.annotation.ColorInt;
import android.support.v7.graphics.Palette;
import android.util.Log;
import android.util.Patterns;
import com.readrops.app.database.ItemWithFeed;
import com.readrops.app.database.entities.Feed;
@ -33,6 +34,7 @@ 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;
@ -140,7 +142,7 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
private void parseATOMItems(ATOMFeed feed) {
try {
Feed dbFeed = database.feedDao().getFeedByUrl(feed.getLink());
Feed dbFeed = database.feedDao().getFeedByUrl(feed.getLink().getHref());
if (dbFeed == null) {
dbFeed = Feed.feedFromATOM(feed);
@ -179,8 +181,10 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
private void insertItems(List<Item> items) {
for (Item dbItem : items) {
if (!Boolean.valueOf(database.itemDao().guidExist(dbItem.getGuid()))) {
dbItem.setImageLink(HtmlParser.getDescImageLink(dbItem.getDescription()));
dbItem.setDescription(Jsoup.parse(dbItem.getDescription()).text());
if (dbItem.getDescription() != null) {
dbItem.setImageLink(HtmlParser.getDescImageLink(dbItem.getDescription()));
dbItem.setDescription(Jsoup.parse(dbItem.getDescription()).text());
}
database.itemDao().insert(dbItem);
Log.d(TAG, "adding " + dbItem.getTitle());
@ -190,7 +194,7 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
private void setFavIconUtils(Feed feed) throws IOException {
String favUrl = HtmlParser.getFaviconLink(feed.getSiteUrl());
if (favUrl != null) {
if (favUrl != null && Patterns.WEB_URL.matcher(favUrl).matches()) {
feed.setIconUrl(favUrl);
feed.setColor(getFaviconColor(favUrl));
}

View File

@ -56,8 +56,7 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
Item item = itemWithFeed.getItem();
Item item1 = t1.getItem();
return item.getTitle().equals(item1.getTitle()) &&
item.getDescription().equals(item1.getDescription());
return item.getTitle().equals(item1.getTitle());
}
};
@ -174,7 +173,12 @@ public class MainItemListAdapter extends ListAdapter<ItemWithFeed, MainItemListA
itemTitle.setText(item.getTitle());
date.setText(DateUtils.formatedDateByLocal(item.getPubDate()));
feedName.setText(itemWithFeed.getFeedName());
itemDescription.setText(item.getDescription());
if (item.getDescription() != null) {
itemDescription.setVisibility(View.VISIBLE);
itemDescription.setText(item.getDescription());
} else
itemDescription.setVisibility(View.GONE);
}
public ImageView getItemImage() {

View File

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

View File

@ -168,6 +168,8 @@ public class Item {
dbItem.setLink(item.getLink().getHref());
dbItem.setFeedId(feed.getId());
dbItems.add(dbItem);
}
return dbItems;
@ -190,6 +192,8 @@ public class Item {
dbItem.setLink(item.getUrl());
dbItem.setFeedId(feed.getId());
dbItems.add(dbItem);
}
return dbItems;

View File

@ -77,12 +77,12 @@ public final class HtmlParser {
String favUrl = null;
String head = getHTMLHeadFromUrl(url);
Document document = Jsoup.parse(head);
Document document = Jsoup.parse(head, url);
Elements elements = document.select("link");
for (Element element : elements) {
if (element.attributes().get("rel").contains("icon")) {
favUrl = element.attributes().get("href");
favUrl = element.absUrl("href");
break;
}
}

View File

@ -78,12 +78,12 @@ public class RSSNetwork {
callback.onSyncSuccess(rssFeed, type);
break;
case RSS_ATOM:
ATOMFeed atomFeed = serializer.read(ATOMFeed.class, stream);
ATOMFeed atomFeed = serializer.read(ATOMFeed.class, xml);
callback.onSyncSuccess(atomFeed, type);
break;
case RSS_JSON:
Gson gson = new Gson();
JSONFeed feed = gson.fromJson(Utils.inputStreamToString(stream), JSONFeed.class);
JSONFeed feed = gson.fromJson(xml, JSONFeed.class);
callback.onSyncSuccess(feed, type);
break;
}

View File

@ -14,8 +14,11 @@ public class ATOMFeed extends AFeed {
@Element(required = false)
private String title;
@Element(name = "href", required = false)
private String link;
@Element(name = "link", required = false)
private ATOMLink link;
@Element(required = false)
private String id;
@Element(required = false)
private String subtitle;
@ -37,11 +40,11 @@ public class ATOMFeed extends AFeed {
this.title = title;
}
public String getLink() {
public ATOMLink getLink() {
return link;
}
public void setLink(String link) {
public void setLink(ATOMLink link) {
this.link = link;
}
@ -76,4 +79,16 @@ public class ATOMFeed extends AFeed {
public void setEntries(List<ATOMEntry> entries) {
this.entries = entries;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getWebSiteUrl() {
return id;
}
}

View File

@ -3,7 +3,7 @@ package com.readrops.readropslibrary.localfeed.atom;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;
@Root(name = "href", strict = false)
@Root(name = "link", strict = false)
public class ATOMLink {
@Attribute(name = "href", required = false)

View File

@ -77,16 +77,22 @@ public class RSSChannel {
}
public String getFeedUrl() {
if (links.size() > 0)
return links.get(0).getHref();
else
if (links.size() > 1) {
if (links.get(0).getHref() != null)
return links.get(0).getHref();
else
return links.get(1).getHref();
} else
return null;
}
public String getUrl() {
if (links.size() > 1)
return links.get(1).getText();
else
if (links.size() > 1) {
if (links.get(1).getText() != null)
return links.get(1).getText();
else
return links.get(0).getText();
} else
return null;
}
}