Skip to content

Commit

Permalink
Add info about image into rss item. (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
logart authored Jun 13, 2024
1 parent e9bbc74 commit 459290e
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* MIT License
*
* Copyright (c) 2022, Apptastic Software
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.apptasticsoftware.rssreader.module.mediarss;

import com.apptasticsoftware.rssreader.DateTimeParser;
import com.apptasticsoftware.rssreader.Item;

import java.util.Objects;
import java.util.Optional;

/**
* Class representing the media rss item.
*/
public class MediaRssItem extends Item {
private MediaThumbnail mediaThumbnail;

/**
* Constructor
*
* @param dateTimeParser timestamp parser
*/
public MediaRssItem(DateTimeParser dateTimeParser) {
super(dateTimeParser);
}

/**
* Get the media thumbnail
*
* @return media thumbnail
*/
public Optional<MediaThumbnail> getMediaThumbnail() {
return Optional.ofNullable(mediaThumbnail);
}

/**
* Set the media thumbnail
*
* @param mediaThumbnail media thumbnail
*/
public void setMediaThumbnail(MediaThumbnail mediaThumbnail) {
this.mediaThumbnail = mediaThumbnail;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
MediaRssItem that = (MediaRssItem) o;
return Objects.equals(getMediaThumbnail(), that.getMediaThumbnail());
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getMediaThumbnail());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* MIT License
*
* Copyright (c) 2022, Apptastic Software
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.apptasticsoftware.rssreader.module.mediarss;

import com.apptasticsoftware.rssreader.AbstractRssReader;
import com.apptasticsoftware.rssreader.Channel;
import com.apptasticsoftware.rssreader.DateTimeParser;

import java.util.function.BiConsumer;

/**
* Class for reading media rss feeds.
*/
public class MediaRssReader extends AbstractRssReader<Channel, MediaRssItem> {

@Override
protected Channel createChannel(DateTimeParser dateTimeParser) {
return new Channel(dateTimeParser);
}

@Override
protected MediaRssItem createItem(DateTimeParser dateTimeParser) {
return new MediaRssItem(dateTimeParser);
}

@Override
protected void registerItemAttributes() {
super.registerItemAttributes();
super.addItemExtension("media:thumbnail", "url", mediaThumbnailSetterTemplateBuilder(MediaThumbnail::setUrl));
super.addItemExtension("media:thumbnail", "height", mediaThumbnailSetterTemplateBuilder(
(mediaThumbnail, height) -> mediaThumbnail.setHeight(Integer.parseInt(height))
));
super.addItemExtension("media:thumbnail", "width", mediaThumbnailSetterTemplateBuilder(
(mediaThumbnail, width) -> mediaThumbnail.setWidth(Integer.parseInt(width))
));
}

private BiConsumer<MediaRssItem, String> mediaThumbnailSetterTemplateBuilder(BiConsumer<MediaThumbnail, String> setter) {
return (mediaRssItem, value) -> {
var mediaThumbnail = mediaRssItem.getMediaThumbnail().orElse(new MediaThumbnail());
setter.accept(mediaThumbnail, value);
mediaRssItem.setMediaThumbnail(mediaThumbnail);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.apptasticsoftware.rssreader.module.mediarss;

import java.util.Optional;

/**
* Class representing the media thumbnail from the media rss spec.
* See <a href="https://www.rssboard.org/media-rss#media-thumbnails">for details</a>.
*/
public class MediaThumbnail {
private String url;
private Integer width;
private Integer height;
private String time;

/**
* Get the url of the thumbnail
*
* @return url
*/
public String getUrl() {
return url;
}

/**
* Set the url of the thumbnail
*
* @param url url
*/
public void setUrl(String url) {
this.url = url;
}

/**
* Get the width of the thumbnail
*
* @return width
*/
public Optional<Integer> getWidth() {
return Optional.ofNullable(width);
}

/**
* Set the width of the thumbnail
*
* @param width width
*/
public void setWidth(Integer width) {
this.width = width;
}

/**
* Get the height of the thumbnail
*
* @return height
*/
public Optional<Integer> getHeight() {
return Optional.ofNullable(height);
}

/**
* Set the height of the thumbnail
*
* @param height height
*/
public void setHeight(Integer height) {
this.height = height;
}

/**
* Get the time of the thumbnail
*
* @return time
*/
public Optional<String> getTime() {
return Optional.ofNullable(time);
}

/**
* Set the time of the thumbnail
*
* @param time time
*/
public void setTime(String time) {
this.time = time;
}
}
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
exports com.apptasticsoftware.rssreader;
exports com.apptasticsoftware.rssreader.util;
exports com.apptasticsoftware.rssreader.module.itunes;
exports com.apptasticsoftware.rssreader.module.mediarss;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.apptasticsoftware.rssreader.module.mediarss;

import com.apptasticsoftware.rssreader.util.ItemComparator;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

import java.util.stream.Collectors;

import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAnd;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;

class MediaRssReaderTest {

@Test
void readMediaRssFeed() {
var res = new MediaRssReader().read(MediaRssReader.class.getClassLoader().getResourceAsStream("media-rss.xml"))
.sorted(ItemComparator.oldestItemFirst())
.collect(Collectors.toList());

assertEquals(10, res.size());
}

@Test
void readMediaRssFeedItemTitle() {
var res = new MediaRssReader().read(MediaRssReader.class.getClassLoader().getResourceAsStream("media-rss.xml"))
.sorted(ItemComparator.oldestItemFirst())
.collect(Collectors.toList());

MediaRssItem item = res.get(0);
assertThat(item.getTitle(), isPresentAnd(equalTo("Ignitis_wind")));
}

@Test
void readMediaRssFeedItemPubDate() {
var res = new MediaRssReader().read(MediaRssReader.class.getClassLoader().getResourceAsStream("media-rss.xml"))
.sorted(ItemComparator.oldestItemFirst())
.collect(Collectors.toList());

MediaRssItem item = res.get(0);
assertThat(item.getPubDate(), isPresentAnd(equalTo("Mon, 07 Nov 2022 14:51:45 -0500")));
}

@Test
void readMediaRssFeedItemLink() {
var res = new MediaRssReader().read(MediaRssReader.class.getClassLoader().getResourceAsStream("media-rss.xml"))
.sorted(ItemComparator.oldestItemFirst())
.collect(Collectors.toList());

MediaRssItem item = res.get(0);
assertThat(item.getLink(), isPresentAnd(equalTo("https://vimeo.com/768251452")));
}

@Test
void readMediaRssFeedDescription() {
var res = new MediaRssReader().read(MediaRssReader.class.getClassLoader().getResourceAsStream("media-rss.xml"))
.sorted(ItemComparator.oldestItemFirst())
.collect(Collectors.toList());

MediaRssItem item = res.get(0);
assertThat(item.getDescription(), isPresentAnd(equalTo("This is &quot;Ignitis_wind&quot; by pvz.lt on Vimeo, the home for high quality videos and the people who love them.")));
}

@Test
void readMediaRssFeedGuid() {
var res = new MediaRssReader().read(MediaRssReader.class.getClassLoader().getResourceAsStream("media-rss.xml"))
.sorted(ItemComparator.oldestItemFirst())
.collect(Collectors.toList());

MediaRssItem item = res.get(0);
assertThat(item.getGuid(), isPresentAnd(equalTo("tag:vimeo,2022-11-07:clip768251452")));
}

@Test
void readMediaRssFeedIsPermaLink() {
var res = new MediaRssReader().read(MediaRssReader.class.getClassLoader().getResourceAsStream("media-rss.xml"))
.sorted(ItemComparator.oldestItemFirst())
.collect(Collectors.toList());

MediaRssItem item = res.get(0);
assertThat(item.getIsPermaLink(), isPresentAnd(equalTo(false)));
}

@Test
void readMediaRssFeedThumbnail() {
var res = new MediaRssReader().read(MediaRssReader.class.getClassLoader().getResourceAsStream("media-rss.xml"))
.sorted(ItemComparator.oldestItemFirst())
.collect(Collectors.toList());

MediaRssItem item = res.get(0);
MediaThumbnail mediaThumbnail = item.getMediaThumbnail().get();
assertEquals("https://i.vimeocdn.com/video/1542457228-31ab55501fdd5316663c63781ae1a37932abc4b314bcc619e3377c0ca85b859d-d_960", mediaThumbnail.getUrl());
assertThat(mediaThumbnail.getHeight(), isPresentAnd(equalTo(540)));
assertThat(mediaThumbnail.getWidth(), isPresentAnd(equalTo(960)));
assertThat(mediaThumbnail.getTime(), isEmpty());
}

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(MediaRssItem.class).withIgnoredFields("defaultComparator").withIgnoredFields("dateTimeParser").withIgnoredFields("category").withNonnullFields("categories").withIgnoredFields("enclosure").withNonnullFields("enclosures").verify();
}
}
Loading

0 comments on commit 459290e

Please sign in to comment.