Skip to content

chang-zy/xiaohongshu-skill

xiaohongshu-skills

Verified

小红书(RedNote / Xiaohongshu / XHS / RED / Little Red Book)浏览器自动化技能集合。 支持认证登录、内容发布、搜索发现、账号与评论分析、社交互动和复合运营。 当用户提到小红书、红书、RedNote、Xiaohongshu、XHS、RED、Little Red Book, 或要求发笔记、搜笔记、分析博主、评论、登录、点赞、收藏时触发。

scripts/image_downloader.py

View on GitHub
"""媒体下载(SHA256 缓存),对应 Go pkg/downloader/images.go。"""

from __future__ import annotations

import hashlib
import logging
import os
import time
from urllib.parse import urlparse, urlunparse

import requests

logger = logging.getLogger(__name__)

_USER_AGENT = (
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
    "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)

# 已知图片扩展名
_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".svg"}
_XHS_CDN_SUFFIX = ".xhscdn.com"
_XHS_REFERER = "https://www.xiaohongshu.com/"


def is_image_url(path: str) -> bool:
    """判断字符串是否为图片/媒体 URL。"""
    return path.lower().startswith(("http://", "https://"))


class ImageDownloader:
    """图片下载器(带 SHA256 缓存)。"""

    def __init__(self, save_path: str) -> None:
        self.save_path = save_path
        os.makedirs(save_path, exist_ok=True)
        self._session = requests.Session()
        self._session.timeout = 30

    def download_image(self, image_url: str) -> str:
        """下载单张图片,返回本地文件路径。

        如果文件已存在(通过 URL hash 判断),直接返回路径。

        Raises:
            ValueError: URL 格式无效。
            RuntimeError: 下载失败。
        """
        if not is_image_url(image_url):
            raise ValueError(f"无效的图片 URL: {image_url}")

        # 生成文件名
        url_hash = hashlib.sha256(image_url.encode()).hexdigest()[:16]
        ext = self._detect_extension(image_url)
        filename = f"img_{url_hash}_{int(time.time())}{ext}"
        filepath = os.path.join(self.save_path, filename)

        # 检查是否已有同 hash 的文件
        existing = self._find_existing(url_hash)
        if existing:
            return existing

        # 小红书 CDN 的 http 图片链接会被拒绝;保持原路径和签名,仅升级为 https。
        request_url = self._secure_xhs_cdn_url(image_url)
        parsed = urlparse(request_url)
        headers = {
            "User-Agent": _USER_AGENT,
            "Referer": _XHS_REFERER
            if parsed.hostname and parsed.hostname.endswith(_XHS_CDN_SUFFIX)
            else f"{parsed.scheme}://{parsed.hostname}/",
        }

        resp = self._session.get(request_url, headers=headers, timeout=30)
        if resp.status_code != 200:
            raise RuntimeError(f"下载失败 (status={resp.status_code}): {request_url}")

        # 保存
        with open(filepath, "wb") as f:
            f.write(resp.content)

        logger.info("下载完成: %s -> %s", image_url, filepath)
        return filepath

    @staticmethod
    def _secure_xhs_cdn_url(image_url: str) -> str:
        """仅将小红书 CDN 的 http 链接升级为 https,避免改变 URL 签名。"""
        parsed = urlparse(image_url)
        if parsed.scheme == "http" and parsed.hostname and parsed.hostname.endswith(_XHS_CDN_SUFFIX):
            return urlunparse(parsed._replace(scheme="https"))
        return image_url

    def download_images(self, image_urls: list[str]) -> list[str]:
        """批量下载图片。"""
        paths = []
        for url in image_urls:
            try:
                path = self.download_image(url)
                paths.append(path)
            except Exception as e:
                logger.error("下载失败 %s: %s", url, e)
        return paths

    def _detect_extension(self, url: str) -> str:
        """从 URL 推断文件扩展名。"""
        parsed = urlparse(url)
        path = parsed.path.lower()
        for ext in _IMAGE_EXTENSIONS:
            if path.endswith(ext):
                return ext
        return ".jpg"  # 默认

    def _find_existing(self, url_hash: str) -> str | None:
        """查找已有同 hash 的文件。"""
        prefix = f"img_{url_hash}_"
        for filename in os.listdir(self.save_path):
            if filename.startswith(prefix):
                return os.path.join(self.save_path, filename)
        return None


def process_images(images: list[str], save_dir: str | None = None) -> list[str]:
    """处理图片列表(URL 下载,本地路径直接返回)。"""
    if not save_dir:
        save_dir = os.path.join(os.path.expanduser("~"), ".xhs", "images")

    downloader = ImageDownloader(save_dir)
    result = []

    for img in images:
        if is_image_url(img):
            path = downloader.download_image(img)
            result.append(path)
        else:
            # 本地路径
            if os.path.exists(img):
                result.append(os.path.abspath(img))
            else:
                logger.warning("文件不存在: %s", img)

    return result
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 323kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 323kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 323kUpdated Claude CodeCodexVerifiedInstall