Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import os
import pprint
from datetime import datetime, timedelta
import pytz
# https://pypi.python.org/pypi/feedgenerator
import feedgenerator
rss_max = 10
base_url = 'http://kamekokamekame.net/'
root_path = 'source/'
def get_rss_files(base_path):
dirs = []
for x in os.listdir(base_path):
dd = os.path.join(base_path, x)
if os.path.isdir(dd):
if x[0] != '_':
dirs.append(dd)
dirs.sort(reverse=True)
# print(dirs)
rss = []
num = 0
for d in dirs:
for ff in os.listdir(d):
if ff[-3:] == '.md':
rss.append(os.path.join(d, ff))
num += 1
if num > rss_max:
break
if num > rss_max:
break
rss.sort(reverse=True)
# pprint.pprint(rss)
return rss
def create_item_data(f_path):
"""itemタグ用の情報を取得."""
item = dict()
# link
item['link'] = base_url + f_path.replace(root_path, '')[:-3] + '.html'
item['unique_id'] = item['link']
# pubDate
b_name = os.path.basename(f_path)[:-3]
jst = pytz.timezone('Asia/Tokyo')
dt = datetime.strptime(b_name, '%Y_%m_%d').replace(tzinfo=jst)
item['pubdate'] = dt + timedelta(hours=9)
str_description = ''
with open(f_path) as f:
for idx, row in enumerate(f):
if idx == 0:
item['title'] = row.rstrip().replace('#','')
elif idx == 1 and row[0] == '_':
item['category'] = row.rstrip().replace('_', '')
else:
str_description += row.strip().replace('#','')
item['description'] = str_description[:100]
return item
def create_feed():
title = "鉄は熱いうちに打て"
link = base_url
feed_url = "kamekokamekame.net/rss20.xml"
description = "亀の歩みエンジニア"
# フィードを生成
return feedgenerator.Rss201rev2Feed(title=title,link=link,feed_url=feed_url,description=description,language="ja")
def main():
feed = create_feed()
files = get_rss_files(root_path)
for ff in files:
item_data = create_item_data(ff)
feed.add_item(**item_data)
with open('build/html/rss20.xml', 'w') as rf:
rf.write(feed.writeString("utf-8"))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment