Just coded to retrieve RSS feeds

Really nice to get RSS Feeds and insert the new records in a dataset full of news and then I was able to alter the slides with data from the datasets. Really nice.


All images, texts and links are retrieved through an RSS Feed and then stored in my news dataset.

Could you please elaborate and let me know how you did it?
I also want to do the same.

1 Like

You want the code or video how I did it?

1 Like

If you have both, I need them!
Thank You.

1 Like

The code you can get asap, the teaching video is under production and will be a part of WIX Show at wixshow.com.

2 Likes

Code in backend module serviceModule.jsw

export function getRSSTitle(indexNr) {
  const url = "https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Fwww.sn.se%2Ffeed%2F";
  
 return fetch(url, {method: 'get'})
    .then(response => response.json())
    .then(json => json.items[indexNr]);
    
}

The url for the actual RSS you will have to change.

The code below is raw and not for any training use :slight_smile:

import {getRSSTitle} from 'backend/serviceModule';
getRSSTitle(0)
	.then(news => {
		$w("#newsHeadline").text = news.title;
		$w("#newsDescription").text = "Denna nyhet publicerades på sn.se (RSS)" + news.pubDate + " i kategorin " + news.categories[0];
		$w("#newsImage").src = news.enclosure["link"];
		$w("#slideshow1").slides[0].background.src = news.enclosure["link"];
	// does this records exists in the dataset?
	wixData.query("news")
	.eq("title", news.title)
	.limit(1)
   .find()
   .then( (results) => {
   	
     if (results.totalCount === 1)
     {
     	console.log(results.items);
     } else if (results.totalCount === 0) {
     	let toInsert = {
  			"title":        news.title,
  			"newsImage":   	news.enclosure["link"],
  			"categoryName":		news.categories[0],
  			"datePublished":	news.pubDate,
  			"sourceUrl":		news.link
		};
		console.log("skapat objekt för insert");
		let options = {
  			"suppressAuth": true,
  			"suppressHooks": true
		};

		wixData.insert("news", toInsert, options)
			.then( (results) => {
			let item = results; //see item below
				console.log("sparat ny post i databsen");
				$w("#newsdataset").refresh();
			} )
			.catch( (err) => {
				let errorMsg = err;
			});
     	}
   	}).catch( (err) => {
      console.log("error " + err.code);
    });

getRSSTitle(0) where the zero is the index item you get from the RSS Feed. Happy coding

3 Likes

Thanks! When video is live, just shoutout!

1 Like

Hey Now I want to add this, could you help me in achieving it?
I mean step by step!

1 Like

Can you please share? your website is no longer live, so nobody can see how you did it