mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-06-06 01:47:26 +08:00
新增对微博博客内照片获取的支持 文件存放路径data/weibo/images
This commit is contained in:
@@ -8,13 +8,14 @@ from typing import List
|
||||
import config
|
||||
|
||||
from .weibo_store_impl import *
|
||||
from .weibo_store_image import *
|
||||
|
||||
|
||||
class WeibostoreFactory:
|
||||
STORES = {
|
||||
"csv": WeiboCsvStoreImplement,
|
||||
"db": WeiboDbStoreImplement,
|
||||
"json": WeiboJsonStoreImplement
|
||||
"json": WeiboJsonStoreImplement,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
@@ -86,3 +87,6 @@ async def update_weibo_note_comment(note_id: str, comment_item: Dict):
|
||||
utils.logger.info(
|
||||
f"[store.weibo.update_weibo_note_comment] Weibo note comment: {comment_id}, content: {save_comment_item.get('content', '')[:24]} ...")
|
||||
await WeibostoreFactory.create_store().store_comment(comment_item=save_comment_item)
|
||||
|
||||
async def update_weibo_note_image(picid: str, pic_content, extension_file_name):
|
||||
await WeiboStoreImage().store_image({"pic_id": picid, "pic_content": pic_content, "extension_file_name": extension_file_name})
|
||||
51
store/weibo/weibo_store_image.py
Normal file
51
store/weibo/weibo_store_image.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Author : Erm
|
||||
# @Time : 2024/4/9 17:35
|
||||
# @Desc : 微博保存图片类
|
||||
import pathlib
|
||||
|
||||
from tools import utils
|
||||
from base.base_crawler import AbstractStoreImage
|
||||
import aiofiles
|
||||
from typing import Dict
|
||||
|
||||
class WeiboStoreImage(AbstractStoreImage):
|
||||
image_store_path: str = "data/weibo/images"
|
||||
|
||||
async def store_image(self, image_content_item: Dict):
|
||||
"""
|
||||
store content
|
||||
Args:
|
||||
content_item:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
await self.save_image(image_content_item.get("pic_id"), image_content_item.get("pic_content"), image_content_item.get("extension_file_name"))
|
||||
|
||||
def make_save_file_name(self, picid: str, extension_file_name: str) -> str:
|
||||
"""
|
||||
make save file name by store type
|
||||
Args:
|
||||
picid: image id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return f"{self.image_store_path}/{picid}.{extension_file_name}"
|
||||
|
||||
async def save_image(self, picid: str, pic_content: str, extension_file_name="jpg"):
|
||||
"""
|
||||
save image to local
|
||||
Args:
|
||||
picid: image id
|
||||
pic_content: image content
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
pathlib.Path(self.image_store_path).mkdir(parents=True, exist_ok=True)
|
||||
save_file_name = self.make_save_file_name(picid, extension_file_name)
|
||||
async with aiofiles.open(save_file_name, 'wb') as f:
|
||||
await f.write(pic_content)
|
||||
utils.logger.info(f"[WeiboImageStoreImplement.save_image] save image {save_file_name} success ...")
|
||||
Reference in New Issue
Block a user