Serializing a FeedParser object to Atom

家住魔仙堡 提交于 2019-12-11 03:17:10

问题


I use feedparser http://www.feedparser.org/ to parse Atom feeds and I do some manipulation on the resulting Python objetcs. After that, I would like to serialize the objects back to Atom. But feedparser does not seem to offer a way to do so?

I noticed other Atom libraries like gdata http://code.google.com/p/gdata-python-client/ or demokritos http://jtauber.com/demokritos/ but, to tell the truth, they seem very difficult for the beginner. I use feedparser precisely because of its extreme simplicity.

Folowing namsral's good response, I wrote a serializer with my favorite template language, SimpleTAL

import feedparser

from simpletal import simpleTAL, simpleTALES, simpleTALUtils

mytemplate = """
<feed xmlns="http://www.w3.org/2005/Atom">
  <title tal:condition="feed/title" tal:content="feed/title"/>
  <link tal:condition="feed/link" tal:content="feed/link"/>
  <updated tal:condition="feed/updated" tal:content="feed/updated"/>
  <id tal:condition="feed/id" tal:content="feed/id"/>
  <!-- TODO other feed variables -->
  <entry xmlns='http://www.w3.org/2005/Atom'
       xmlns:thr='http://purl.org/syndication/thread/1.0'
       tal:repeat="entry entries">
    <title tal:condition="entry/title" tal:content="entry/title"/>
    <summary tal:condition="entry/summary" tal:content="entry/summary"/>
    <content tal:condition="entry/content" tal:content="python: entry.content[0]['value']"/> <!-- TODO: metadata and the other items in content -->
    <id tal:condition="entry/id" tal:content="entry/id"/>
    <published tal:condition="entry/published" tal:content="entry/published"/>
    <updated tal:condition="entry/updated" tal:content="entry/updated"/>
    <!-- TODO other entry fields -->
  </entry>
</feed>
"""
context = simpleTALES.Context(allowPythonPath=True)
template = simpleTAL.compileXMLTemplate (mytemplate)

class FeedParserPlus(feedparser.FeedParserDict):

    def serialize(self):
        context.addGlobal ("feed", self.feed)
        context.addGlobal ("entries", self.entries)
        result = simpleTALUtils.FastStringOutput()
        template.expand (context, result)
        return result.getvalue()

    @classmethod
    def parse(klass, text):
        result = feedparser.parse(text)
        return FeedParserPlus(result)

回答1:


Generating feeds is fairly easy using a Python template library like Mako, Jinja or Django's.

An example using Bottle.py:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>{{! d['title'] }}</title>
    <subtitle>{{! d['subtitle'] }}</subtitle>
    <link rel="alternate" type="text/html" href="{{! d['site_url'] }}" />
    <link rel="self" type="application/atom+xml" href="{{! d['feed_url'] }}" />
    <id>{{! d['feed_url'] }}</id>
    <updated>{{! d['date_updated'] }}</updated>
    <rights>{{! d['copyright'] }}</rights>

    %for entry in entries:
    <entry>
        <title>{{! entry['title'] }}</title>
        <link rel="alternate" type="text/html" href="{{! entry['url'] }}" />
        <id>{{! entry['atom_id'] }}</id>
        <published>{{! entry['date_published'] }}</published>
        <updated>{{! entry['date_updated'] }}</updated>
        <author>
            <name>{{! d['author'] }}</name>
            <uri>{{! d['site_url'] }}</uri>
        </author>
        <content type="html" xml:base="{{! d['site_url'] }}" xml:lang="en">
            <![CDATA[{{! entry['body'] }}]]>
        </content>
    </entry>
    %end

</feed>

For more information about the use of Django and especially django-atompub: http://code.google.com/p/django-atompub/wiki/UserGuide



来源:https://stackoverflow.com/questions/5916375/serializing-a-feedparser-object-to-atom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!