Drkcore

27 12 2010 Python Flask Tweet

feedgeneratorを使ってFlaskでRSSを出力する

FlaskでRSSを出力するのにテンプレートを使っていたのだけど、zenbackの関連記事が一向に反映されなくて、Feed Validation Serviceにかけたらvalidじゃないとか言われたのでおそらくこれだろうと。

で、Feed::XMLみたいなのないかなぁと探したらfeedgeneratorってのがあったのでこれを使ってみたら便利。★10と行きたいところであったが、RSSのタイムゾーンではまったので★9くらいで。

結局、dateutil を使ったタイムゾーン管理 / Twisted Mindを参考にしてreplaceで変更した。

tz    = gettz('Asia/Tokyo')
title = u"Drkcore"
link  = u"http://blog.kzfmix.com/rss"

feed = feedgenerator.Rss201rev2Feed(
    title       = title,
    link        = link,
    feed_url    = u"http://blog.kzfmix.com/rss",
    description = u"Programming, Music, Snowboarding",
    language    = u"ja"
    )

for entry in entries:
    categories = [tag.name for tag in entry.tags]
    feed.add_item(
        title       = entry.title,
        link        = u"http://blog.kzfmix.com/entry/" + entry.perma,
        description = entry.htmlized_content,
        pubdate     = entry.pubdate.replace(tzinfo=tz),
        categories  = categories
        )

response = make_response(feed.writeString('utf-8'))
response.headers['Content-Type'] = 'application/xml; charset=utf-8'
return response

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021