Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new feature: weibo poller #63

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions config_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@
# 'setu',
'translate',
# 'twitter',
# 'weibo'
}
61 changes: 61 additions & 0 deletions hoshino/modules/weibo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from .weibo import WeiboSpider
from hoshino.service import Service, Privilege as Priv
from hoshino.res import R
from hoshino import util

sv = Service('weibo-poller', use_priv=Priv.ADMIN, manage_priv=Priv.SUPERUSER, visible=False)
user_configs = util.load_config(__file__)
'''
sample config.json

[{
"user_id": "6603867494",
"service_name": "bcr-weibo",
"filter": true
}]
'''

subr_dic = {}

for config in user_configs:
sv.logger.debug(config)
wb_spider = WeiboSpider(config)
service_name = config["service_name"]

if service_name not in subr_dic:
subService = Service(service_name, enable_on_default=True)
subr_dic[service_name] = {"service": subService, "spiders": [wb_spider]}
else:
subr_dic[service_name]["spiders"].append(wb_spider)

def wb_to_message(wb):
msg = f'@{wb["screen_name"]}:\n{wb["text"]}'
if sv.bot.config.IS_CQPRO and len(wb["pics"]) > 0:
images_url = wb["pics"]
msg = f'{msg}\n'
res_imgs = [R.remote_img(url).cqcode for url in images_url]
for img in res_imgs:
msg = f'{msg}{img}'
if len(wb["video_url"]) > 0:
videos = wb["video_url"]
res_videos = ';'.join(videos)
msg = f'{msg}\n视频链接:{res_videos}'
return msg

@sv.scheduled_job('interval', seconds=20*60)
async def weibo_poller():
for sv_name, serviceObj in subr_dic.items():
weibos = []
ssv = serviceObj["service"]
spiders = serviceObj["spiders"]
for spider in spiders:
latest_weibos = await spider.get_latest_weibos()
formatted_weibos = [wb_to_message(wb) for wb in latest_weibos]

if l := len(formatted_weibos):
sv.logger.info(f"成功获取@{spider.get_username()}的新微博{l}条")
else:
sv.logger.info(f"未检测到@{spider.get_username()}的新微博")

weibos.extend(formatted_weibos)
await ssv.broadcast(weibos, ssv.name, 0.5)
21 changes: 21 additions & 0 deletions hoshino/modules/weibo/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class WeiboError(Exception):
def __init__(self, msg, *msgs):
self._msgs = [msg, *msgs]

def __str__(self):
return '\n'.join(self._msgs)

@property
def message(self):
return str(self)

def append(self, msg:str):
self._msgs.append(msg)


class ParseError(WeiboError):
pass


class NotFoundError(WeiboError):
pass
Loading