Closed aswzen closed 1 year ago
You need to extend the Item class and extend the AbstractRssReader class.
Override AbstractRssReader.createItem()
method to create objects of the extended Item class.
Override AbstractRssReader.registerItemTags()
and AbstractRssReader.registerItemAttributes()
methods to register custom tags and tag attributes.
Take a look at the iTunes module it extends the Item class.
ItunesItem extends the Item
class.
ItunesRssReader extends the AbstractRssReader
class and overrides AbstractRssReader.createItem()
and AbstractRssReader.registerItemTags()
methods and registers custom tags.
If It its only a few custom tags and/or tag attributes you need to access then it is possible to reuse fields in the existing Item class and re-map those to other tags or tag attributes.
Example:
List<Item> items = new RssReader()
.addItemExtension("dc:creator", Item::setAuthor)
.addItemExtension("dc:date", Item::setPubDate)
.read("https://lwn.net/headlines/rss")
.collect(Collectors.toList());
This is the RSS response from server
<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss">
<channel>
<title><![CDATA[Genflix]]></title>
<description><![CDATA[Film Pilihan untuk MyIM3]]></description>
<link>https://stg.genflix.co.id</link>
<generator>rss</generator>
<lastBuildDate>Tue, 27 Dec 2022 10:50:51 GMT</lastBuildDate>
<atom:link href="https://s3.ap-southeast-1.amazonaws.com/rss.genflix.co.id/myim3tv-stg.xml" rel="self" type="application/rss+xml"/>
<language>ID</language>
<item>
<title><![CDATA[The Raid : Redemption]]></title>
<description><![CDATA[The Raid : Redemption description is not completed yet. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute ir]]></description>
<guid isPermaLink="false">c476dec1-dfb4-4800-a97b-201bdd2aeef3</guid>
<pubDate>Tue, 27 Dec 2022 10:50:51 GMT</pubDate>
<streamType>movie</streamType>
<movieTitle>The Raid : Redemption</movieTitle>
<movieSynopsis>The Raid : Redemption description is not completed yet. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute ir</movieSynopsis>
<movieDirector/>
<movieImageHomeModule>https://genflix-stg-uploads.s3.ap-southeast-1.amazonaws.com/c476dec1-dfb4-4800-a97b-201bdd2aeef3_ioh_movie_image_home</movieImageHomeModule>
<movieImage>https://genflix-stg-uploads.s3.ap-southeast-1.amazonaws.com/c476dec1-dfb4-4800-a97b-201bdd2aeef3_ioh_movie_image</movieImage>
<movieTag>Film baru,Trending,Top 10 weekly</movieTag>
<movieTagTheme>1</movieTagTheme>
<movieCategory>Action</movieCategory>
<movieDuration>100</movieDuration>
<movieUrl>https://stg.genflix.co.id/mobile/ioh/movie/c476dec1-dfb4-4800-a97b-201bdd2aeef3?trailer=2ca7462c-4720-4a82-a664-7b9d9ed3bf84&jwt_token=!JWTTOKEN!&jwt_client=Genflix</movieUrl>
<trailerTitle>Trailer Film</trailerTitle>
<isOtherCategory>false</isOtherCategory>
<premium>true</premium>
<ageRating>NC-17</ageRating>
<media:player href="https://stg.genflix.co.id/movie/c476dec1-dfb4-4800-a97b-201bdd2aeef3"/>
</item>
</channel>
</rss>
when i try to take the movieCategory using .addItemExtension("movieCategory", Item::setComments), then item.getComments().get() i got empty content
It looks correct to me. Are you using the latest version? 3.3.0
Try if this works:
new RssReader().addItemExtension("movieCategory", Item::setComments)
.read("https://s3.ap-southeast-1.amazonaws.com/rss.genflix.co.id/myim3tv-stg.xml")
.map(Item::getComments)
.forEach(System.out::println);
This is the output I get:
Optional[Action]
Optional[Horror]
Optional[Drama]
Optional[Animation]
Optional.empty
Optional.empty
Ok there are so many item extension, so ill back to the main topic.
Now its working perfectly.
GenFlixReader Reader Class
public class GenFlixReader extends AbstractRssReader<Channel, GenFlixItem> {
@Override
protected void registerChannelTags() {
super.registerChannelTags();
}
@Override
protected void registerChannelAttributes() {
super.registerChannelAttributes();
}
@Override
protected void registerItemTags() {
super.registerItemTags();
addItemExtension("movieTitle", GenFlixItem::setMovieTitle);
addItemExtension("movieDuration", GenFlixItem::setMovieDuration);
addItemExtension("movieCategory", GenFlixItem::setMovieCategory);
addItemExtension("movieUrl", GenFlixItem::setMovieUrl);
addItemExtension("movieImage", GenFlixItem::setMovieImage);
}
@Override
protected GenFlixItem createItem() {
return new GenFlixItem();
}
@Override
protected Channel createChannel() {
return new Channel();
}
}
GenFlixItem Item Class (Using lombok for getter setter)
@Data
public class GenFlixItem extends Item {
private String movieTitle;
private String movieCategory;
private String movieUrl;
private String movieDuration;
private String movieImage;
}
Fetching test
List<GenFlixItem> items = new GenFlixReader().read(rssSource.getRssFeedLink()).toList();
logger.debug("----------->> ");
for (GenFlixItem item : items) {
logger.debug("item getMovieTitle : "+ item.getMovieTitle());
logger.debug("item getMovieCategory : "+ item.getMovieCategory());
logger.debug("item getMovieImage : "+ item.getMovieImage());
}
logger.debug("----------->> ");
Results
----------->>
item getMovieTitle : The Raid : Redemption
item getMovieCategory : Action
item getMovieImage : https://genflix-stg-uploads.s3.ap-southeast-1.amazonaws.com/c476dec1-dfb4-4800-a97b-201bdd2aeef3_ioh_movie_image
item getMovieTitle : Temaram
item getMovieCategory : Horror
item getMovieImage : https://genflix-stg-uploads.s3.ap-southeast-1.amazonaws.com/423e04a9-85a7-4c20-8e59-21a4b96d6772_ioh_movie_image
item getMovieTitle : Ketua BEM
item getMovieCategory : Drama
item getMovieImage : https://genflix-stg-uploads.s3.ap-southeast-1.amazonaws.com/d7f82e5b-e447-4190-9996-2986f982b2d6_ioh_movie_image
item getMovieTitle : Astra Lost in Space
item getMovieCategory : Animation
item getMovieImage : https://genflix-stg-uploads.s3.ap-southeast-1.amazonaws.com/6bee8059-819f-4281-9e26-c267652b5a28_ioh_movie_image
----------->>
Thank you for your amazing library !
How to extend the class Item?