Move Item and Feed match methods to ItemMatcher and FeedMatcher

This commit is contained in:
Shinokuni 2019-08-21 22:16:21 +02:00
parent 1507e55249
commit 6a1039f87e
5 changed files with 192 additions and 190 deletions

View File

@ -12,12 +12,6 @@ import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import com.readrops.app.database.entities.account.Account;
import com.readrops.readropslibrary.localfeed.atom.ATOMFeed;
import com.readrops.readropslibrary.localfeed.json.JSONFeed;
import com.readrops.readropslibrary.localfeed.rss.RSSChannel;
import com.readrops.readropslibrary.localfeed.rss.RSSFeed;
import org.jsoup.Jsoup;
@Entity(foreignKeys = {@ForeignKey(entity = Folder.class, parentColumns = "id",
childColumns = "folder_id", onDelete = ForeignKey.SET_NULL),
@ -240,60 +234,6 @@ public class Feed implements Parcelable {
this.remoteFolderId = remoteFolderId;
}
public static Feed feedFromRSS(RSSFeed rssFeed) {
Feed feed = new Feed();
RSSChannel channel = rssFeed.getChannel();
feed.setName(Jsoup.parse(channel.getTitle()).text());
feed.setUrl(channel.getFeedUrl());
feed.setSiteUrl(channel.getUrl());
feed.setDescription(channel.getDescription());
feed.setLastUpdated(channel.getLastUpdated());
feed.setEtag(rssFeed.getEtag());
feed.setLastModified(rssFeed.getLastModified());
feed.setFolderId(null);
return feed;
}
public static Feed feedFromATOM(ATOMFeed atomFeed) {
Feed feed = new Feed();
feed.setName(atomFeed.getTitle());
feed.setDescription(atomFeed.getSubtitle());
feed.setUrl(atomFeed.getUrl());
feed.setSiteUrl(atomFeed.getWebsiteUrl());
feed.setDescription(atomFeed.getSubtitle());
feed.setLastUpdated(atomFeed.getUpdated());
feed.setEtag(atomFeed.getEtag());
feed.setLastModified(atomFeed.getLastModified());
feed.setFolderId(null);
return feed;
}
public static Feed feedFromJSON(JSONFeed jsonFeed) {
Feed feed = new Feed();
feed.setName(jsonFeed.getTitle());
feed.setUrl(jsonFeed.getFeedUrl());
feed.setSiteUrl(jsonFeed.getHomePageUrl());
feed.setDescription(jsonFeed.getDescription());
feed.setEtag(jsonFeed.getEtag());
feed.setLastModified(jsonFeed.getLastModified());
feed.setIconUrl(jsonFeed.getFaviconUrl());
feed.setFolderId(null);
return feed;
}
@Override
public int describeContents() {
return 0;

View File

@ -1,22 +1,11 @@
package com.readrops.app.database.entities;
import androidx.room.*;
import com.readrops.app.utils.DateUtils;
import com.readrops.app.utils.Utils;
import com.readrops.readropslibrary.localfeed.atom.ATOMEntry;
import com.readrops.readropslibrary.localfeed.json.JSONItem;
import com.readrops.readropslibrary.localfeed.rss.RSSEnclosure;
import com.readrops.readropslibrary.localfeed.rss.RSSItem;
import com.readrops.readropslibrary.localfeed.rss.RSSMediaContent;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.PrimaryKey;
import org.joda.time.LocalDateTime;
import org.jsoup.Jsoup;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import static androidx.room.ForeignKey.CASCADE;
@ -205,115 +194,6 @@ public class Item implements Comparable<Item> {
this.remoteId = remoteId;
}
public static List<Item> itemsFromRSS(List<RSSItem> items, Feed feed) throws ParseException {
List<Item> dbItems = new ArrayList<>();
for(RSSItem item : items) {
Item newItem = new Item();
newItem.setAuthor(item.getCreator());
newItem.setContent(item.getContent());
newItem.setDescription(item.getDescription());
newItem.setGuid(item.getGuid());
newItem.setTitle(Jsoup.parse(item.getTitle()).text());
// I wish I hadn't done that...
if (Pattern.compile(DateUtils.RSS_ALTERNATIVE_DATE_FORMAT_REGEX).matcher(item.getDate()).matches())
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.RSS_2_DATE_FORMAT_3));
else {
try {
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.RSS_2_DATE_FORMAT_2));
} catch (ParseException e) {
e.printStackTrace();
} finally {
if (newItem.getPubDate() == null) {
try {
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.RSS_2_DATE_FORMAT));
} catch (ParseException e) {
e.printStackTrace();
} finally {
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.ATOM_JSON_DATE_FORMAT));
}
}
}
}
newItem.setLink(item.getLink());
newItem.setFeedId(feed.getId());
if (item.getMediaContents() != null && item.getMediaContents().size() > 0) {
for (RSSMediaContent mediaContent : item.getMediaContents()) {
if (mediaContent.getMedium() != null && Utils.isTypeImage(mediaContent.getMedium())) {
newItem.setImageLink(mediaContent.getUrl());
break;
}
}
} else {
if (item.getEnclosures() != null) {
for (RSSEnclosure enclosure : item.getEnclosures()) {
if (enclosure.getType() != null && Utils.isTypeImage(enclosure.getType())) {
newItem.setImageLink(enclosure.getUrl());
break;
}
}
}
}
dbItems.add(newItem);
}
return dbItems;
}
public static List<Item> itemsFromATOM(List<ATOMEntry> items, Feed feed) throws ParseException {
List<Item> dbItems = new ArrayList<>();
for (ATOMEntry item : items) {
Item dbItem = new Item();
dbItem.setContent(item.getContent());
dbItem.setDescription(item.getSummary());
dbItem.setGuid(item.getId());
dbItem.setTitle(item.getTitle());
dbItem.setPubDate(DateUtils.stringToDateTime(item.getUpdated(), DateUtils.ATOM_JSON_DATE_FORMAT));
dbItem.setLink(item.getUrl());
dbItem.setFeedId(feed.getId());
dbItems.add(dbItem);
}
return dbItems;
}
public static List<Item> itemsFromJSON(List<JSONItem> items, Feed feed) throws ParseException {
List<Item> dbItems = new ArrayList<>();
for (JSONItem item : items) {
Item dbItem = new Item();
if (item.getAuthor() != null)
dbItem.setAuthor(item.getAuthor().getName());
dbItem.setContent(item.getContent());
dbItem.setDescription(item.getSummary());
dbItem.setGuid(item.getId());
dbItem.setTitle(item.getTitle());
dbItem.setPubDate(DateUtils.stringToDateTime(item.getPubDate(), DateUtils.ATOM_JSON_DATE_FORMAT));
dbItem.setLink(item.getUrl());
dbItem.setFeedId(feed.getId());
dbItems.add(dbItem);
}
return dbItems;
}
@Override
public int compareTo(Item o) {
return this.pubDate.compareTo(o.getPubDate());

View File

@ -10,7 +10,9 @@ import com.readrops.app.database.entities.account.Account;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Item;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.FeedMatcher;
import com.readrops.app.utils.HtmlParser;
import com.readrops.app.utils.ItemMatcher;
import com.readrops.app.utils.ParsingResult;
import com.readrops.app.utils.Utils;
import com.readrops.readropslibrary.localfeed.AFeed;
@ -150,15 +152,15 @@ public class LocalFeedRepository extends ARepository {
switch (type) {
case RSS_2:
dbFeed = database.feedDao().getFeedByUrl(((RSSFeed) feed).getChannel().getFeedUrl());
items = Item.itemsFromRSS(((RSSFeed) feed).getChannel().getItems(), dbFeed);
items = ItemMatcher.itemsFromRSS(((RSSFeed) feed).getChannel().getItems(), dbFeed);
break;
case RSS_ATOM:
dbFeed = database.feedDao().getFeedByUrl(((ATOMFeed) feed).getUrl());
items = Item.itemsFromATOM(((ATOMFeed) feed).getEntries(), dbFeed);
items = ItemMatcher.itemsFromATOM(((ATOMFeed) feed).getEntries(), dbFeed);
break;
case RSS_JSON:
dbFeed = database.feedDao().getFeedByUrl(((JSONFeed) feed).getFeedUrl());
items = Item.itemsFromJSON(((JSONFeed) feed).getItems(), dbFeed);
items = ItemMatcher.itemsFromJSON(((JSONFeed) feed).getItems(), dbFeed);
break;
}
@ -172,13 +174,13 @@ public class LocalFeedRepository extends ARepository {
Feed dbFeed = null;
switch (type) {
case RSS_2:
dbFeed = Feed.feedFromRSS((RSSFeed) feed);
dbFeed = FeedMatcher.feedFromRSS((RSSFeed) feed);
break;
case RSS_ATOM:
dbFeed = Feed.feedFromATOM((ATOMFeed) feed);
dbFeed = FeedMatcher.feedFromATOM((ATOMFeed) feed);
break;
case RSS_JSON:
dbFeed = Feed.feedFromJSON((JSONFeed) feed);
dbFeed = FeedMatcher.feedFromJSON((JSONFeed) feed);
break;
}

View File

@ -2,9 +2,15 @@ package com.readrops.app.utils;
import com.readrops.app.database.entities.account.Account;
import com.readrops.app.database.entities.Feed;
import com.readrops.readropslibrary.localfeed.atom.ATOMFeed;
import com.readrops.readropslibrary.localfeed.json.JSONFeed;
import com.readrops.readropslibrary.localfeed.rss.RSSChannel;
import com.readrops.readropslibrary.localfeed.rss.RSSFeed;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSFeed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed;
import org.jsoup.Jsoup;
public final class FeedMatcher {
public static Feed nextNewsFeedToFeed(NextNewsFeed feed, Account account) {
@ -41,4 +47,57 @@ public final class FeedMatcher {
return newFeed;
}
public static Feed feedFromRSS(RSSFeed rssFeed) {
Feed feed = new Feed();
RSSChannel channel = rssFeed.getChannel();
feed.setName(Jsoup.parse(channel.getTitle()).text());
feed.setUrl(channel.getFeedUrl());
feed.setSiteUrl(channel.getUrl());
feed.setDescription(channel.getDescription());
feed.setLastUpdated(channel.getLastUpdated());
feed.setEtag(rssFeed.getEtag());
feed.setLastModified(rssFeed.getLastModified());
feed.setFolderId(null);
return feed;
}
public static Feed feedFromATOM(ATOMFeed atomFeed) {
Feed feed = new Feed();
feed.setName(atomFeed.getTitle());
feed.setDescription(atomFeed.getSubtitle());
feed.setUrl(atomFeed.getUrl());
feed.setSiteUrl(atomFeed.getWebsiteUrl());
feed.setDescription(atomFeed.getSubtitle());
feed.setLastUpdated(atomFeed.getUpdated());
feed.setEtag(atomFeed.getEtag());
feed.setLastModified(atomFeed.getLastModified());
feed.setFolderId(null);
return feed;
}
public static Feed feedFromJSON(JSONFeed jsonFeed) {
Feed feed = new Feed();
feed.setName(jsonFeed.getTitle());
feed.setUrl(jsonFeed.getFeedUrl());
feed.setSiteUrl(jsonFeed.getHomePageUrl());
feed.setDescription(jsonFeed.getDescription());
feed.setEtag(jsonFeed.getEtag());
feed.setLastModified(jsonFeed.getLastModified());
feed.setIconUrl(jsonFeed.getFaviconUrl());
feed.setFolderId(null);
return feed;
}
}

View File

@ -1,11 +1,23 @@
package com.readrops.app.utils;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Item;
import com.readrops.readropslibrary.localfeed.atom.ATOMEntry;
import com.readrops.readropslibrary.localfeed.json.JSONItem;
import com.readrops.readropslibrary.localfeed.rss.RSSEnclosure;
import com.readrops.readropslibrary.localfeed.rss.RSSItem;
import com.readrops.readropslibrary.localfeed.rss.RSSMediaContent;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSItem;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsItem;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.jsoup.Jsoup;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public final class ItemMatcher {
@ -50,4 +62,113 @@ public final class ItemMatcher {
return newItem;
}
public static List<Item> itemsFromRSS(List<RSSItem> items, Feed feed) throws ParseException {
List<Item> dbItems = new ArrayList<>();
for(RSSItem item : items) {
Item newItem = new Item();
newItem.setAuthor(item.getCreator());
newItem.setContent(item.getContent());
newItem.setDescription(item.getDescription());
newItem.setGuid(item.getGuid());
newItem.setTitle(Jsoup.parse(item.getTitle()).text());
// I wish I hadn't done that...
if (Pattern.compile(DateUtils.RSS_ALTERNATIVE_DATE_FORMAT_REGEX).matcher(item.getDate()).matches())
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.RSS_2_DATE_FORMAT_3));
else {
try {
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.RSS_2_DATE_FORMAT_2));
} catch (ParseException e) {
e.printStackTrace();
} finally {
if (newItem.getPubDate() == null) {
try {
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.RSS_2_DATE_FORMAT));
} catch (ParseException e) {
e.printStackTrace();
} finally {
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.ATOM_JSON_DATE_FORMAT));
}
}
}
}
newItem.setLink(item.getLink());
newItem.setFeedId(feed.getId());
if (item.getMediaContents() != null && item.getMediaContents().size() > 0) {
for (RSSMediaContent mediaContent : item.getMediaContents()) {
if (mediaContent.getMedium() != null && Utils.isTypeImage(mediaContent.getMedium())) {
newItem.setImageLink(mediaContent.getUrl());
break;
}
}
} else {
if (item.getEnclosures() != null) {
for (RSSEnclosure enclosure : item.getEnclosures()) {
if (enclosure.getType() != null && Utils.isTypeImage(enclosure.getType())) {
newItem.setImageLink(enclosure.getUrl());
break;
}
}
}
}
dbItems.add(newItem);
}
return dbItems;
}
public static List<Item> itemsFromATOM(List<ATOMEntry> items, Feed feed) throws ParseException {
List<Item> dbItems = new ArrayList<>();
for (ATOMEntry item : items) {
Item dbItem = new Item();
dbItem.setContent(item.getContent());
dbItem.setDescription(item.getSummary());
dbItem.setGuid(item.getId());
dbItem.setTitle(item.getTitle());
dbItem.setPubDate(DateUtils.stringToDateTime(item.getUpdated(), DateUtils.ATOM_JSON_DATE_FORMAT));
dbItem.setLink(item.getUrl());
dbItem.setFeedId(feed.getId());
dbItems.add(dbItem);
}
return dbItems;
}
public static List<Item> itemsFromJSON(List<JSONItem> items, Feed feed) throws ParseException {
List<Item> dbItems = new ArrayList<>();
for (JSONItem item : items) {
Item dbItem = new Item();
if (item.getAuthor() != null)
dbItem.setAuthor(item.getAuthor().getName());
dbItem.setContent(item.getContent());
dbItem.setDescription(item.getSummary());
dbItem.setGuid(item.getId());
dbItem.setTitle(item.getTitle());
dbItem.setPubDate(DateUtils.stringToDateTime(item.getPubDate(), DateUtils.ATOM_JSON_DATE_FORMAT));
dbItem.setLink(item.getUrl());
dbItem.setFeedId(feed.getId());
dbItems.add(dbItem);
}
return dbItems;
}
}