Skip to content

Instantly share code, notes, and snippets.

@Hootrix Hootrix/tianfu.py
Last active Jan 7, 2020

Embed
What would you like to do?
天府市民云APP的消息RSS生成
import datetime
import time
import PyRSS2Gen
import requests, pytz
class NoOutput:
def __init__(self):
pass
def publish(self, handler):
pass
class IPhoneRSS2(PyRSS2Gen.RSSItem):
"""给标签内容添加CDATA标记
"""
def __init__(self, **kwargs):
PyRSS2Gen.RSSItem.__init__(self, **kwargs)
# 需要处理的标签
# Tags that need to be processed
self._handle_tag = ['description','title']
def publish(self, handler):
for i in self._handle_tag:
if getattr(self,i):
setattr(self,'_%s'%i,getattr(self,i)) # self['_%s'%i] = self[i]
setattr(self,i,NoOutput()) # This disables the Py2GenRSS "Automatic" output of the description, which would be escaped.
PyRSS2Gen.RSSItem.publish(self, handler)
def publish_extensions(self, handler):
from xml.sax import saxutils
for i in self._handle_tag:
if getattr(self,i):
handler.startElement(i, self.element_attrs)
# handler._write('<![CDATA[%s]]>' % saxutils.escape(getattr(self,'_%s' % i))) #写入源数据 手动编码处理
handler._write('<![CDATA[%s]]>' % getattr(self,'_%s' % i)) #写入源数据 避免PyRSS2Gen中handler.characters(obj)的编码处理
handler.endElement(i)
url = 'https://tfsmy.chengdu.gov.cn/appnews/api/headline?page=1&size=10'
headers = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 iOS/12.2 Brand/iPhone Display/320.000000*568.000000 tfsmy/1.6.2'
}
response = requests.get(url, headers=headers)
content = response.json()
tz = pytz.utc
if content['code'] == 1:
items = []
for item in content['result']['list']:
url = "http://tfsmy.chengdu.gov.cn/cms/share/news?id=%s" % item['id']
time_tuple = time.strptime(item['updateTime'], "%Y-%m-%d %H:%M:%S")
pubData = datetime.datetime.fromtimestamp(int(time.mktime(time_tuple)), tz=pytz.utc)#转换为datetime.datetime对象 默认string会输出GMT时间字符串
items.append(IPhoneRSS2(
title=item['title'],
link=url, # 打开预览的网址
description='%s'%(item['title']),
guid=PyRSS2Gen.Guid(item['image'], isPermaLink=1), # 设置为封面图
pubDate=pubData
))
rss = PyRSS2Gen.RSS2(
title="天府市民云APP feed",
link="https://www.tfsmy.com/download/tianfu.html",
description="用于获取app内「成都头条」的RSS feeds",
lastBuildDate=datetime.datetime.now(tz=tz),
items=items,
)
# rss.write_xml(open("./tianfu.xml", "w", encoding='utf-8'), encoding='utf-8')
rss.write_xml(open("/home/wwwroot/rss.hhtjim.com/tianfu.xml", "w", encoding='utf-8'), encoding='utf-8')
@Hootrix

This comment has been minimized.

Copy link
Owner Author

Hootrix commented Jan 7, 2020

修改PyRSS2Gen不能处理CDATA的问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.