Add support for alternative tags author and dc:date. Fix date parsing bug

This commit is contained in:
Shinokuni 2019-02-21 21:00:28 +00:00
parent 3290c55e65
commit 6a05cd6bbf
3 changed files with 72 additions and 29 deletions

View File

@ -181,12 +181,14 @@ public class Item {
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { if (newItem.getPubDate() == null) {
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.RSS_2_DATE_FORMAT)); try {
} catch (ParseException e) { newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.RSS_2_DATE_FORMAT));
e.printStackTrace(); } catch (ParseException e) {
} finally { e.printStackTrace();
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.ATOM_JSON_DATE_FORMAT)); } finally {
newItem.setPubDate(DateUtils.stringToDateTime(item.getDate(), DateUtils.ATOM_JSON_DATE_FORMAT));
}
} }
} }
} }
@ -196,7 +198,7 @@ public class Item {
if (item.getMediaContents() != null && item.getMediaContents().size() > 0) { if (item.getMediaContents() != null && item.getMediaContents().size() > 0) {
for (RSSMediaContent mediaContent : item.getMediaContents()) { for (RSSMediaContent mediaContent : item.getMediaContents()) {
if (Utils.isTypeImage(mediaContent.getMedium())) { if (mediaContent.getMedium() != null && Utils.isTypeImage(mediaContent.getMedium())) {
newItem.setImageLink(mediaContent.getUrl()); newItem.setImageLink(mediaContent.getUrl());
break; break;
} }
@ -204,7 +206,7 @@ public class Item {
} else { } else {
if (item.getEnclosures() != null) { if (item.getEnclosures() != null) {
for (RSSEnclosure enclosure : item.getEnclosures()) { for (RSSEnclosure enclosure : item.getEnclosures()) {
if (Utils.isTypeImage(enclosure.getType())) { if (enclosure.getType() != null && Utils.isTypeImage(enclosure.getType())) {
newItem.setImageLink(enclosure.getUrl()); newItem.setImageLink(enclosure.getUrl());
break; break;
} }

View File

@ -15,26 +15,26 @@ import static org.junit.Assert.*;
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/ */
public class ExampleUnitTest { public class ExampleUnitTest {
@Test @Test
public void addition_isCorrect() { public void rssDateTest() {
assertEquals(4, 2 + 2); try {
LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46);
assertEquals(0, dateTime.compareTo(DateUtils.stringToDateTime("Fri, 04 Jan 2019 22:21:46 +0000", DateUtils.RSS_2_DATE_FORMAT)));
assertEquals(0, dateTime.compareTo(DateUtils.stringToDateTime("Fri, 04 Jan 2019 22:21:46 GMT", DateUtils.RSS_2_DATE_FORMAT_2)));
} catch (ParseException e) {
e.printStackTrace();
}
} }
@Test @Test
public void stringToDateTest() { public void atomJsonDateTest() {
LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46);
try { try {
// RSS LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46);
assertTrue(dateTime.compareTo(DateUtils.stringToDateTime("Fri, 04 Jan 2019 22:21:46 +0000", DateUtils.RSS_2_DATE_FORMAT)) == 0);
// ATOM
assertTrue(dateTime.compareTo(DateUtils.stringToDateTime("2019-01-04T22:21:46+00:00", DateUtils.ATOM_JSON_DATE_FORMAT)) == 0);
// JSON
assertTrue(dateTime.compareTo(DateUtils.stringToDateTime("2019-01-04T22:21:46-0000", DateUtils.ATOM_JSON_DATE_FORMAT)) == 0);
assertEquals(0, dateTime.compareTo(DateUtils.stringToDateTime("2019-01-04T22:21:46+00:00", DateUtils.ATOM_JSON_DATE_FORMAT)));
assertEquals(0, dateTime.compareTo(DateUtils.stringToDateTime("2019-01-04T22:21:46-0000", DateUtils.ATOM_JSON_DATE_FORMAT)));
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -15,12 +15,13 @@ public class RSSItem extends AItem {
@Element(name = "title", required = false) @Element(name = "title", required = false)
private String title; private String title;
@Element(name = "description", required = false, data = true)
private String description;
@Element(name = "link", required = false) @Element(name = "link", required = false)
private String link; private String link;
@ElementList(name = "link", inline = true, required = false)
@Namespace(prefix = "atom")
private List<String> otherLinks;
@Element(name = "imageLink", required = false) @Element(name = "imageLink", required = false)
private String imageLink; private String imageLink;
@ -33,11 +34,21 @@ public class RSSItem extends AItem {
@Element(name = "creator", required = false) @Element(name = "creator", required = false)
@Namespace(prefix = "dc", reference = "http://purl.org/dc/elements/1.1/") @Namespace(prefix = "dc", reference = "http://purl.org/dc/elements/1.1/")
private String creator;
@Element(required = false)
private String author; private String author;
@Element(name = "pubDate", required = false) @Element(name = "pubDate", required = false)
private String pubDate; private String pubDate;
@Element(name = "date", required = false)
@Namespace(prefix = "dc")
private String date;
@Element(name = "description", required = false)
private String description;
@Element(name = "encoded", required = false) @Element(name = "encoded", required = false)
@Namespace(prefix = "content") @Namespace(prefix = "content")
private String content; private String content;
@ -77,12 +88,12 @@ public class RSSItem extends AItem {
this.imageLink = imageLink; this.imageLink = imageLink;
} }
public String getAuthor() { public String getCreator() {
return author; return creator;
} }
public void setAuthor(String author) { public void setCreator(String creator) {
this.author = author; this.creator = creator;
} }
public String getPubDate() { public String getPubDate() {
@ -124,4 +135,34 @@ public class RSSItem extends AItem {
public void setEnclosures(List<RSSEnclosure> enclosures) { public void setEnclosures(List<RSSEnclosure> enclosures) {
this.enclosures = enclosures; this.enclosures = enclosures;
} }
public String getDate() {
if (pubDate != null)
return pubDate;
else
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAuthor() {
if (creator != null)
return creator;
else
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public List<String> getOtherLinks() {
return otherLinks;
}
public void setOtherLinks(List<String> otherLinks) {
this.otherLinks = otherLinks;
}
} }