0

Need help please. I am trying to build an rss-based feed in django. But I am finding media:content (url, medium, height and width) to be tricky.

I have looked and looked, and ended up with this:


class CustomFeed(Rss201rev2Feed):
    def add_item_elements(self, handler, item):
        super().add_item_elements(handler, item)
        handler.addQuickElement("image", item["image"])

class Rss(Feed):
    feed_type = CustomFeed
    title = "#Title of the item"
    link = "/feeds/"
    description = "#Description of the item"

    def item_extra_kwargs(self, item):
        img_url = item.image.medium.url
        request_url = self.request.build_absolute_uri('/')[:-1]
        image_url_abs = f"{request_url}{img_url}"

        return {
            'image': image_url_abs
        }

But this gives me the image as standalone in the rss feed:

<image>https://www.url.com/image.jpg</image>

i badly need the code to return this:

<media:content url="https://www.url.com/image.jpg" medium="image" height="640" width="362"/> 

please help.

2 Answers2

0
class CustomFeed(Rss201rev2Feed):
    def root_attributes(self):
        attrs = super(CustomFeed, self).root_attributes()
        # attrs['xmlns:dc'] = "http://purl.org/dc/elements/1.1/"
        attrs['xmlns:media'] ='http://search.yahoo.com/mrss/'
        return attrs

    def add_root_elements(self, handler):
        super(CustomFeed, self).add_root_elements(handler)
        thumbnail = dict(url=self.feed['image_url'])
        thumbnail['medium'] = str(self.feed['medium'])
        thumbnail['height'] = str(self.feed['height'])
        thumbnail['width'] = str(self.feed['width'])
        handler.addQuickElement('media:content', '', thumbnail)
sodimel
  • 864
  • 2
  • 11
  • 24
  • This would be a better answer if you explained how the code you provided answers the question. – pppery Apr 13 '22 at 18:32
-1
class Rss(Feed):

    link = 'http://www.example.com'
    feed_type = CustomFeed
    title = 'LatestItems'
    description = 'Latest Items'


def feed_extra_kwargs(self, obj):
    return {
        'image_url': self.link + '/image.jpg',
        'height': 433,
        'medium': 'image',
        'width': 500,
        }
sodimel
  • 864
  • 2
  • 11
  • 24
  • Thank you. But the problem with this solution is that /image.jpg is static text.. but i have an image field name item.image.medium.url – Ryan Rodrigues Aug 25 '20 at 15:11
  • 1
    This would be a better answer if you explained how the code you provided answers the question. – pppery Apr 13 '22 at 18:40