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
39 changes: 23 additions & 16 deletions hoshino/modules/weibo/weibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@
from datetime import date, datetime, timedelta
from time import sleep

import requests
import httpx as requests
from lxml import etree
from hoshino.service import Service, Privilege as Priv
from hoshino import util, logger
from hoshino.res import R

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": "pcr-weibo",
Ice9Coffee marked this conversation as resolved.
Show resolved Hide resolved
"filter": true
}]
'''

class Weibo(object):
def __init__(self, config):
Expand Down Expand Up @@ -83,20 +93,18 @@ def get_user_info(self, user_id):

def validate_config(self, config):
Ice9Coffee marked this conversation as resolved.
Show resolved Hide resolved
"""验证配置是否正确"""
exist_argument_list = ['user_id', 'service_name']
true_false_argument_list = ['filter']

# 验证filter、original_pic_download、retweet_pic_download、original_video_download、retweet_video_download
argument_list = [
'filter'
]
for argument in argument_list:
if config[argument] != 0 and config[argument] != 1:
logger.error(u'%s值应为0或1,请重新输入' % config[argument])
for argument in true_false_argument_list:
if argument not in config:
logger.error(f'请填写 {argument}')
if config[argument] != True and config[argument] != False:
logger.error(f'{argument} 值应为 True 或 False,请重新输入')

# 验证user_id_list
if "user_id" not in config:
logger.error(u'请填写用户 id')
if "service_name" not in config:
logger.error(u'请填写所属服务名')
for argument in exist_argument_list:
if argument not in config:
logger.error(f'请填写 {argument}')

def get_pics(self, weibo_info):
"""获取微博原始图片url"""
Expand Down Expand Up @@ -409,11 +417,10 @@ def get_latest_weibos(self):
return []


user_configs = util.load_config(__file__)
subr_dic = {}

for config in user_configs:
print(config)
sv.logger.debug(config)
wb = Weibo(config)
service_name = config["service_name"]

Expand All @@ -437,7 +444,7 @@ def wb_to_message(wb):
msg = f'{msg}\n视频链接:{res_videos}'
return msg

@sv.scheduled_job('interval', seconds=20*60)
@sv.scheduled_job('interval', seconds=10)
async def weibo_poller():
for sv_name, serviceObj in subr_dic.items():
weibos = []
Expand Down
3 changes: 2 additions & 1 deletion hoshino/res.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import asyncio
from PIL import Image
import requests
import httpx as requests
Ice9Coffee marked this conversation as resolved.
Show resolved Hide resolved
from io import BytesIO
from urllib.request import pathname2url
from urllib.parse import urljoin
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ zhconv>=1.4.0
Pillow>=6.2.1
TwitterAPI>=2.5.10
matplotlib>=3.2.0
numpy>=1.18.0
numpy>=1.18.0
httpx>=0.12.1