RSS stands for Really Simple Syndication. It refers to files called XML files that are easily read by a computer that automatically updates information. This frequently updated information is fetched by users’ computers and is shown in a user-convenient manner. Without performing any complex operations, RSS feeds allow you to obtain the latest news or blogpost simply by referring to the Feed URLs. Hence RSS Feeds make one of the most simple and effective ways of keeping in touch with the latest information, i.e., the latest news can be added to your website without having to check for new updates frequently.
RSS uses standard web formats to publish ever-changing information such as blog posts, news, audio, video, etc. RSS documents, commonly known as feeds, contain text and metadata, such as the time and name of the author.
RSS Feeds can be also used on the Odoo website, which helps to upload the latest news updates on your Odoo Website. In this blog, we can see how to extract contents from an RSS Feed using python and display it on the Website. A python library Feedparser is used for parsing RSS Feed.
First, we can create a custom module RSS Feeds for managing Feeds from the website. Then add a field in the Configuration Settings for adding the RSS Feed(URL) and add it to the view. You can refer to the blog New Res Config Settings Option in Odoo15 to see how to add a new option under Res Config Settings. Also, add a button for updating the RSS Feeds.
Then create a model for storing all feeds with basic fields.
class NewsFeeds(models.Model):
"""Feeds"""
_name = "news.feeds"
_inherit = ['mail.thread', 'mail.activity.mixin']
_description = 'News Feeds
_order = 'name'
name = fields.Text(string="Title", required=True, store=True)
url = fields.Char(string="Website URL")
author = fields.Char(string="Author")
datetime_published = fields.Datetime(string="Published")
date_published = fields.Date(string="Date Published")
Now, let’s see how to parse feeds from the RSS Feed. Basically, this will be a URL that returns feeds in a standard format. Here is an example of an RSS feed provided by Times of India Top Stories. This contains feeds with metadata like Publish date and Author.
When the Feed is set under the configuration settings and after clicking the button Update, all feeds will be updated in the model news.feeds after deleting all current feeds. So let’s see the method for the button click. Define a function with the same name as the button added in the view. In this example, the name for the button is given as update_feed. Hence define a new function update_feed() in the Res Config Settings model. In the button click a method is called which will parse all feeds from the URL and return required values as a dictionary. Using the dictionary values records are created in the model news.feeds.The URL is passed as a parameter for the method _get_feeds_details(). Before that, it is necessary to import the feed parser library.
def _get_feeds_details(self, url=None):
"""
Take link of rss feed as argument
"""
if url is not None:
feed = feedparser.parse(url)
posts = feed.entries
if len(feed.feed) > 0:
posts_details = {"Blog title": feed.feed.title,
"Blog link": feed.feed.link}
post_list = []
for post in posts:
print(post)
temp = dict()
try:
temp["title"] = post.title
temp["link"] = post.link
temp["author"] = post.author
temp["time_published"] = post.published
temp["tags"] = [tag.term for tag in post.tags]
temp["authors"] = [author.name for author in post.authors]
temp["summary"] = post.summary
except:
pass
post_list.append(temp)
posts_details["posts"] = post_list
return posts_details
else:
return None
else:
return None
def update_feed(self):
self.env['news.feeds].sudo().search([]).unlink()
if self.feed_url:
data = self._get_feeds_details(self.feed_url)
if data:
feeds = data['posts']
for feed in feeds:
datetime_obj = False
if 'time_published' in feed:
x = re.findall("d{2} w{3} d{4}",
feed['time_published'])
datetime_obj = datetime.datetime.strptime(x[0],
'%d %b %Y')
self.env['news.feeds].sudo().create({
'name': feed['title'],
'url': feed['link'],
'author': feed['author'] if 'author' in feed else False,
'datetime_published': datetime_obj if datetime_obj else False,
'date_published': datetime_obj.date() if datetime_obj else False
})
Now on the button click, all feeds will be created in the Feeds model. RSS Feed will update content frequently. So you can update the feed according to your need. This method will delete all current feeds and create new ones. List view for Feeds will show all the feeds.
Hence the feature of RSS Feeds can be used in Odoo according to the business need. You can get updated news or website content using this feature. This can be used to update your customers regarding the news or any other content using Odoo website-like platforms.