Skip to content

Instantly share code, notes, and snippets.

@amane-katagiri
Last active June 2, 2023 14:07
Show Gist options
  • Save amane-katagiri/7402ed1ddabf9050868c013a45690087 to your computer and use it in GitHub Desktop.
Save amane-katagiri/7402ed1ddabf9050868c013a45690087 to your computer and use it in GitHub Desktop.
CSVっぽいテキストからAtomフィードを生成する
#!/usr/bin/env python3
# ##### つかいかた #####
#
# 実行前に `pip install feedgenerator` でライブラリをインストールしてください
#
# 例1: ./feedgen.py
# => サンプル入力からフィードを生成して標準出力に表示します
# 例2: ./feedgen.py input.txt
# => input.txtの内容からフィードを生成して標準出力に表示します
# 例3: ./feedgen.py input.txt output.txt
# => input.txtの内容からフィードを生成してoutput.txtに書き出します
from datetime import datetime
import sys
import xml.dom.minidom
from feedgenerator import Atom1Feed, get_tag_uri
def _load_config(config: str) -> [dict, dict]:
site_metadata, items = config.strip().split("\n\n", 1)
site_metadata = dict(
map(
lambda x: x.split(": "), site_metadata.split("\n")
)
)
header, items = items.split("\n", 1)
header = header.split(",")
items = list(
map(
lambda x: dict(
zip(
header,
map(lambda x: x.strip('"'), x.split('","'))
)
), items.split("\n")
)
)
return site_metadata, items
def generate_feed(config: str) -> str:
site_metadata, items = _load_config(config)
feed = Atom1Feed(
title=site_metadata["TITLE"],
link=site_metadata["LINK"],
feed_url=site_metadata["FEED_URL"],
description=site_metadata["DESCRIPTION"]
)
for item in reversed(sorted(items, key=lambda x: x["date"])):
pubdate = datetime.fromisoformat(item["date"])
feed.add_item(
title=item["title"],
link=item["link"],
unique_id=get_tag_uri(item["link"], pubdate),
description=item["description"],
author_name=item["author"],
pubdate=pubdate
)
return xml.dom.minidom.parseString(
feed.writeString("utf-8")
).toprettyxml()
if __name__ == "__main__":
if len(sys.argv) > 1:
with open(sys.argv[1], encoding="utf-8") as f:
result = generate_feed(f.read())
else:
result = generate_feed(
"""
TITLE: サイトタイトル
LINK: https://example.com/
FEED_URL: https://example.com/feed.xml
DESCRIPTION: サイトの説明
date,title,author,link,description
"2021-01-01 00:00:00+09:00","タイトル1","作者名","https://example.com/post/1","面白い記事です"
"2022-12-31 12:34:56+09:00","タイトル2","作者名","https://example.com/post/2","楽しい記事です"
"2023-05-27 01:23:45+09:00","タイトル3","作者名","https://example.com/post/3","最新の記事です"
"""
)
if len(sys.argv) > 2:
with open(sys.argv[2], "w", encoding="utf-8") as f:
f.write(result)
else:
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment