惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
L
LINUX DO - 最新话题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
宝玉的分享
宝玉的分享
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyberwarzone
Cyberwarzone
S
Securelist
博客园_首页
Know Your Adversary
Know Your Adversary
S
Schneier on Security
雷峰网
雷峰网
L
LINUX DO - 热门话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Last Week in AI
Last Week in AI
P
Privacy & Cybersecurity Law Blog
Scott Helme
Scott Helme
Schneier on Security
Schneier on Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
AI
AI
K
Kaspersky official blog
爱范儿
爱范儿
H
Heimdal Security Blog
S
Secure Thoughts
T
Threatpost
B
Blog RSS Feed
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
V2EX - 技术
V2EX - 技术
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
The Cloudflare Blog
T
Troy Hunt's Blog
Stack Overflow Blog
Stack Overflow Blog
MyScale Blog
MyScale Blog
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security

John Bokma's Hacking and Hiking

Failed to verify signature archive-contents.sig in Emacs Aquamacs 3.6 Hangs When Saving An Encrypted File PAR trouble How To Create a Bootable LibreELEC Installation using Mac OS Running pdflatex Using the Alpine Pandoc LaTeX Docker Image A Docker Image for Sass Timezones in Alpine Docker Containers A Tale of Three Docker Images Debugging a Perl Docker container Perl Time::Piece Unicode Issue Giving Docker Desktop for macOS a Second Chance Flashing another TP-Link TL-WDR4300 with OpenWrt firmware Rehousing two tarantulas Getting started with the Perl version of tumblelog on Ubuntu 18.04 LTS A visit to Avonturia De Vogelkelder Flashing a TP-Link TL-WDR4300 with OpenWrt firmware Mounting a VDI File in a Different VirtualBox Guest Wireless Headless Raspberry Pi - John Bokma A Matter of Time - John Bokma RFC #822 and RFC #3339 dates in Perl RFC #822 and RFC #3339 dates in Python Hand coding an RSS 2.0 feed in Perl Nav Element with no Heading Rewriting CommonMark Nodes in Perl "right" this time
Hand coding an RSS 2.0 feed in Python
John Bokma · 2019-10-10 · via John Bokma's Hacking and Hiking

October 9, 2019

One requirement I have for tumblelog is that everything the Python version generates is identical to everything the Perl version generates. This means that sometimes I have to hand code a function that is available in a library for, say Python, but works differently in a Perl library.

When I was working on adding an RSS feed to Python version I decided to use lxml.etree as this would give me some control over the output. But, alas, not enough to match the output of the Perl version of tumblelog. Moreover, I was not really happy with the generated XML. So I decided to hand code both the Python and the Perl version.

def create_rss_feed(days, config):

    items = []
    todo = config['days']

    for day in days:
        (url, title, description) = get_url_title_description(day, config)

        # RFC #822 in USA locale
        ctime = end_of_day.ctime()
        pub_date = (f'{ctime[0:3]}, {end_of_day.day:02d} {ctime[4:7]}'
                        + end_of_day.strftime(' %Y %H:%M:%S %z'))

        items.append(
            ''.join([
                '<item>'
                '<title>', escape(title), '</title>'
                '<link>', escape(url), '</link>'
                '<guid isPermaLink="true">', escape(url), '</guid>'
                '<pubDate>', escape(pub_date), '</pubDate>'
                '<description>', escape(description), '</description>'
                '</item>'
            ])
        )
        todo -= 1
        if not todo:
            break


    xml = ''.join([
        '<?xml version="1.0" encoding="UTF-8"?>'
        '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">'
        '<channel>'
        '<title>', escape(config['name']), '</title>'
        '<link>', escape(config['blog-url']), '</link>'
        '<description>', escape(config['description']),'</description>'
        '<atom:link href="', escape(config['rss-feed-url']),
        '" rel="self" type="application/rss+xml" />',
        *items,
        '</channel>'
        '</rss>'
    ])
    feed_path = config['rss-path']
    p = Path(config['output-dir']).joinpath(feed_path)
    with p.open(mode='w', encoding='utf-8') as f:
        print(xml, file=f)

    if not config['quiet']:
        print(f"Created '{feed_path}'")

Note: if you think there are commas missing in the above code recall that in Python the compiler glues strings together if there is no comma between them.

Note: '<title>', escape(title), '</title>' can also be written as: 'f<title>{escape(title)}</title>' but I was afraid that this would make the code harder to read.

The code is quite straightforward. The calculation of the publication date is explained in RFC #822 and RFC #3339 dates in Python.

The escape function is imported from the html module.

If you are interested in the rest of the source code you can download it from GitHub.

Related