XZZ/worker/rss.py

110 lines
3.4 KiB
Python
Raw Normal View History

2020-12-26 20:09:56 +08:00
from zzcore import StdAns
2020-12-26 20:36:04 +08:00
import feedparser, json, time
2020-12-26 20:09:56 +08:00
2020-12-26 20:33:25 +08:00
allowCMD = ['sub','unsub','list','disable', 'enable']
2020-12-26 20:09:56 +08:00
2020-12-26 20:17:57 +08:00
# allSub = {
# 'https://farseerfc.me/feeds/atom.xml' : {
# 'url' : 'https://farseerfc.me/feeds/atom.xml',
# 'title' : 'Farseerfc的小窩',
# 'lastUpdated' : 'Sat, 12 Dec 2020 22:55:52 +0800',
# }
# }
2020-12-26 20:09:56 +08:00
class Ans(StdAns):
def push(self,title,posttitle,posturl):
msg = f'{title}\n{posttitle} {posturl}'
self.sendmsg(msg)
def GETMSG(self):
if len(self.parms) < 2:
return '不加参数是坏文明!'
gid = str(self.gid)
cmd = self.parms[1]
if cmd not in allowCMD:
return f'咱不知道 {cmd} 是什么东西 ●﹏●'
try:
nowdata = json.loads(self.DATAGET()[gid])
except:
nowdata ={
'allSub' : {},
'status' : False,
}
if cmd == 'sub':
if len(self.parms) < 3:
return '汝想订阅什么呢?'
suburl = self.parms[2]
try:
d = feedparser.parse(suburl)
title = d.feed.title
2020-12-26 20:31:53 +08:00
lastUpdated = d.entries[0].published
2020-12-26 20:09:56 +08:00
except:
return "咱好像没能成功订阅 (╥_╥)"
newsub = {
'url': suburl,
'title': title,
'lastUpdated' : lastUpdated,
}
nowdata['allSub'][suburl] = newsub
msg = f'{title}》订阅成功'
elif cmd == 'unsub':
if len(self.parms) < 3:
return '汝想退订什么呢?'
suburl = self.parms[2]
if suburl not in nowdata['allSub'].keys():
return '汝没有订阅过这个!'
unsub = nowdata['allSub'].pop(suburl)
2020-12-26 20:21:39 +08:00
msg = f"{unsub['title']}》已退订"
2020-12-26 20:09:56 +08:00
elif cmd == 'list':
msg = '订阅列表'
2020-12-26 20:25:34 +08:00
for sub in nowdata['allSub'].values():
2020-12-26 20:17:57 +08:00
msg += f"\n{sub['title']} {sub['url']}"
2020-12-26 20:09:56 +08:00
elif cmd == 'enable':
nowdata['status'] = True
self.DATASET({gid:json.dumps(nowdata)})
2020-12-26 20:36:04 +08:00
self.sendmsg("订阅已启用,咱会每隔五分钟抓取订阅,有新内容就会推送哦")
2020-12-26 20:09:56 +08:00
while(nowdata['status']):
nowdata = json.loads(self.DATAGET()[gid])
2020-12-26 20:25:34 +08:00
for sub in nowdata['allSub'].values():
2020-12-26 20:09:56 +08:00
try:
d = feedparser.parse(sub['url'])
except:
self.sendmsg(f"咱抓取不到 《{sub['title']}》!")
continue
2020-12-26 20:31:53 +08:00
if d.entries[0].published == sub['lastUpdated']:
2020-12-26 20:09:56 +08:00
continue
newfeedtitle = d.entries[0].title
newfeedlink = d.entries[0].link
self.push(sub['title'], newfeedtitle, newfeedlink)
2020-12-26 20:31:53 +08:00
nowdata['allSub'][sub['url']]['lastUpdated'] = d.entries[0].published
2020-12-26 20:09:56 +08:00
self.DATASET({gid:json.dumps(nowdata)})
2020-12-26 21:56:08 +08:00
time.sleep(5000)
2020-12-26 20:09:56 +08:00
2020-12-26 20:47:52 +08:00
return '订阅已经真的停了!'
2020-12-26 20:09:56 +08:00
elif cmd == 'disable':
nowdata['status'] = False
msg = '咱不会再推送了!'
self.DATASET({gid:json.dumps(nowdata)})
return msg