Skip to content

Commit

Permalink
Send weibo images; fix subservice bug
Browse files Browse the repository at this point in the history
  • Loading branch information
王见思 committed May 8, 2020
1 parent 4eef604 commit 31821aa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
38 changes: 30 additions & 8 deletions hoshino/modules/weibo/weibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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)

Expand All @@ -26,6 +27,8 @@ def __init__(self, config):
self.weibo = [] # 存储爬取到的所有微博信息
self.weibo_id_list = [] # 存储爬取到的所有微博id

self.__recent = False

def get_json(self, params):
"""获取网页中json数据"""
url = 'https://m.weibo.cn/api/container/getIndex?'
Expand Down Expand Up @@ -103,10 +106,9 @@ def get_pics(self, weibo_info):
if weibo_info.get('pics'):
pic_info = weibo_info['pics']
pic_list = [pic['large']['url'] for pic in pic_info]
pics = ','.join(pic_list)
else:
pics = ''
return pics
pic_list = []
return pic_list

def get_live_photo(self, weibo_info):
"""获取live photo中的视频url"""
Expand Down Expand Up @@ -142,7 +144,7 @@ def get_video_url(self, weibo_info):
live_photo_list = self.get_live_photo(weibo_info)
if live_photo_list:
video_url_list += live_photo_list
return ';'.join(video_url_list)
return video_url_list

def get_location(self, selector):
"""获取微博发布位置"""
Expand Down Expand Up @@ -205,20 +207,25 @@ def standardize_date(self, created_at):
"""标准化微博发布时间"""
if u"刚刚" in created_at:
created_at = datetime.now().strftime("%Y-%m-%d")
self.__recent = True
elif u"分钟" in created_at:
minute = created_at[:created_at.find(u"分钟")]
minute = timedelta(minutes=int(minute))
created_at = (datetime.now() - minute).strftime("%Y-%m-%d")
self.__recent = True
elif u"小时" in created_at:
hour = created_at[:created_at.find(u"小时")]
hour = timedelta(hours=int(hour))
created_at = (datetime.now() - hour).strftime("%Y-%m-%d")
self.__recent = False
elif u"昨天" in created_at:
day = timedelta(days=1)
created_at = (datetime.now() - day).strftime("%Y-%m-%d")
self.__recent = False
elif created_at.count('-') == 1:
year = datetime.now().strftime("%Y")
created_at = year + "-" + created_at
self.__recent = False
return created_at

def standardize_info(self, weibo):
Expand Down Expand Up @@ -380,6 +387,7 @@ def get_one_weibo(self, info):
return weibo
except Exception as e:
logger.exception(e)
self.__recent = False

def get_latest_weibos(self):
try:
Expand All @@ -391,7 +399,7 @@ def get_latest_weibos(self):
if w['card_type'] == 9:
wb = self.get_one_weibo(w)
if wb:
if wb['created_at'] != str(date.today()):
if not self.__recent:
continue
if wb['id'] in self.weibo_id_list:
continue
Expand All @@ -416,22 +424,36 @@ def get_latest_weibos(self):
print(config)
wb = Weibo(config)
service_name = config["service_name"]
subService = Service(service_name, enable_on_default=True)

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

@sv.scheduled_job('interval', seconds=60 * 20)
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 = spider.get_latest_weibos()
formatted_weibos = [wb["text"] for wb in 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}条")
Expand Down
22 changes: 22 additions & 0 deletions hoshino/res.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from PIL import Image
import requests
from io import BytesIO
from urllib.request import pathname2url
from urllib.parse import urljoin

Expand All @@ -19,7 +21,27 @@ def get(path, *paths):
def img(path, *paths):
return ResImg(os.path.join('img', path, *paths))

@staticmethod
def remote_img(url):
return RemoteResImg(url)

class RemoteResObj:
def __init__(self, url):
self.__path = url

@property
def url(self):
return self.__path

class RemoteResImg(RemoteResObj):
@property
def cqcode(self) -> MessageSegment:
return MessageSegment.image(self.url)

def open(self) -> Image:
response = requests.get(self.url)
return Image.open(BytesIO(response))


class ResObj:

Expand Down

0 comments on commit 31821aa

Please sign in to comment.