kone
소미소프트
소미소프트

히토미 다운로더 케모노

유틸
29,180 views · 13 points

hitomi downloader kemono 입니다.

이전에 작성했었는데 사이트가 바뀌었고, 몇몇 동작하지 않는다고 해서 가져왔습니다.

[그록 ㄳㄳ]

밑에 부분 복붙 후 .hds 으로 저장

‘ 옵션 > 설정 > 플러그인 > + > 해당 .hds 파일 클릭 > 히토미다운로더 재시작 ‘

하면 적용 됩니다.

#coding: utf8
#title: Pawchive
#version: 1.0.0
#about: Hitomi Downloader script for pawchive.pw / pawchive.st
# Compatible with Hitomi Downloader user scripts (.hds / .py)
# Tools -> Import script... (Alt+S)

import json
import os
import time

import downloader
from utils import Downloader, Session, clean_title, get_print, get_ext


HOSTS = ('pawchive.pw', 'pawchive.st')
PAGE_SIZE = 50
REQUEST_GAP = 0.35


def _host_from_url(url):
    url = (url or '').lower()
    for host in HOSTS:
        if host in url:
            return host
    return HOSTS[0]


def _parse_url(url):
    """
    Accepts:
      https://pawchive.pw/fanbox/user/123
      https://pawchive.pw/fanbox/user/123/post/456
      https://www.pawchive.st/patreon/user/123/post/456
    Returns dict: host, service, user, post or None
    """
    s = (url or '').strip()
    if not s:
        raise Exception('Empty URL')

    s = s.split('#')[0].split('?')[0]
    s = s.replace('http://', 'https://')
    if s.startswith('www.'):
        s = 'https://' + s
    if not s.startswith('http'):
        s = 'https://' + s

    host = _host_from_url(s)
    path = s.split(host, 1)[-1].strip('/')
    parts = [p for p in path.split('/') if p]

    # drop leading 'www' leftovers
    if parts and parts[0] in ('www',):
        parts = parts[1:]

    service = parts[0] if parts else ''
    user = ''
    post = None

    if len(parts) >= 3 and parts[1] == 'user':
        user = parts[2]
        if len(parts) >= 5 and parts[3] == 'post':
            post = parts[4]
    elif len(parts) >= 2:
        # fallback: /service/userid
        user = parts[1]

    if not service or not user:
        raise Exception('Unsupported Pawchive URL: %s' % url)

    return {
        'host': host,
        'service': service,
        'user': user,
        'post': post,
        'origin': 'https://%s' % host,
        'file_origin': 'https://file.%s' % host,
    }


def _safe_name(name, fallback='file'):
    name = clean_title(name or fallback) or fallback
    name = name.strip('. ')
    return name or fallback


def _file_url(info, path):
    if not path:
        return None
    path = str(path).strip()
    if path.startswith('http://') or path.startswith('https://'):
        return path
    if not path.startswith('/'):
        path = '/' + path
    # gallery-dl / official clients: https://file.pawchive.pw/data{path}
    if path.startswith('/data/'):
        return info['file_origin'] + path
    return info['file_origin'] + '/data' + path


def _iter_files(post):
    seen = set()
    items = []

    main = post.get('file') or {}
    if isinstance(main, dict) and (main.get('path') or main.get('name')):
        items.append(main)

    atts = post.get('attachments') or []
    if isinstance(atts, list):
        items.extend([a for a in atts if isinstance(a, dict)])

    for item in items:
        path = item.get('path') or item.get('server_path') or ''
        name = item.get('name') or os.path.basename(str(path).split('?')[0]) or 'file'
        key = (str(path), str(name))
        if not path or key in seen:
            continue
        seen.add(key)
        yield name, path


def _api_json(session, url, cw=None, referer=None):
    print_ = get_print(cw)
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'en-US,en;q=0.9',
    }
    if referer:
        headers['Referer'] = referer
    html = downloader.read_html(url, session=session, headers=headers)
    html = (html or '').strip()
    if not html:
        raise Exception('Empty API response: %s' % url)
    if html[:1] not in '[{':
        print_('Non-JSON response from %s' % url)
        raise Exception('Pawchive API did not return JSON')
    return json.loads(html)


@Downloader.register
class Downloader_pawchive(Downloader):
    type = 'pawchive'
    URLS = ['pawchive.pw', 'pawchive.st']
    display_name = 'Pawchive'
    MAX_CORE = 4
    MAX_PARALLEL = 1
    ACCEPT_COOKIES = [r'(.*\.)?pawchive\.(pw|st)']

    @classmethod
    def fix_url(cls, url):
        info = _parse_url(url)
        base = '%s/%s/user/%s' % (info['origin'], info['service'], info['user'])
        if info['post']:
            return '%s/post/%s' % (base, info['post'])
        return base

    @classmethod
    def key_id(cls, url):
        info = _parse_url(url)
        key = '%s_%s_%s' % (info['service'], info['user'], info['post'] or 'all')
        return key

    def init(self):
        self.session = Session()
        self.session.headers.update({
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
        })

    def _profile_name(self, info):
        print_ = get_print(self.cw)
        url = '%s/api/v1/%s/user/%s/profile' % (
            info['origin'], info['service'], info['user'])
        try:
            data = _api_json(self.session, url, self.cw, referer=info['origin'] + '/')
            name = data.get('name') or data.get('creator_name') or info['user']
            return _safe_name(name, info['user'])
        except Exception as e:
            print_('profile lookup failed: %s' % e)
            return info['user']

    def _read_post(self, info, post_id):
        api = '%s/api/v1/%s/user/%s/post/%s' % (
            info['origin'], info['service'], info['user'], post_id)
        referer = '%s/%s/user/%s/post/%s' % (
            info['origin'], info['service'], info['user'], post_id)
        return _api_json(self.session, api, self.cw, referer=referer)

    def _iter_creator_posts(self, info):
        print_ = get_print(self.cw)
        endpoints = [
            '%s/api/v1/%s/user/%s/posts' % (info['origin'], info['service'], info['user']),
            '%s/api/v1/%s/user/%s' % (info['origin'], info['service'], info['user']),
        ]
        referer = '%s/%s/user/%s' % (info['origin'], info['service'], info['user'])
        offset = 0
        chosen = None
        while True:
            data = None
            last_err = None
            urls_try = [chosen] if chosen else endpoints
            for api in urls_try:
                if not api:
                    continue
                url = api + ('?o=%d' % offset)
                try:
                    data = _api_json(self.session, url, self.cw, referer=referer)
                    chosen = api
                    break
                except Exception as e:
                    last_err = e
                    print_('list fail %s : %s' % (url, e))
            if data is None:
                if offset == 0:
                    raise Exception('Creator post list failed: %s' % last_err)
                break
            if isinstance(data, dict):
                data = data.get('posts') or data.get('data') or []
            if not isinstance(data, list) or not data:
                break
            for post in data:
                if isinstance(post, dict):
                    yield post
            if len(data) < PAGE_SIZE:
                break
            offset += PAGE_SIZE
            time.sleep(REQUEST_GAP)

    def _push_post_files(self, info, post, creator, index_base=0, prefix=True):
        print_ = get_print(self.cw)
        pid = str(post.get('id') or '')
        title = _safe_name(post.get('title') or ('post_%s' % pid), 'post')
        published = str(post.get('published') or post.get('added') or '')[:10]
        files = list(_iter_files(post))

        if not files:
            print_('no files in post %s (%s)' % (pid, title))
            return 0

        if post.get('has_full') is False:
            print_('post %s may be preview-only (has_full=false)' % pid)

        n = 0
        used = set()
        for name, path in files:
            url = _file_url(info, path)
            if not url:
                continue
            ext = get_ext(name) or get_ext(url.split('?')[0]) or ''
            base = os.path.splitext(_safe_name(name, 'file'))[0]
            if prefix:
                bits = [pid or '0']
                if published:
                    bits.insert(0, published)
                fname = '%s_%02d_%s%s' % ('_'.join(bits), index_base + n + 1, base, ext)
            else:
                fname = '%s%s' % (base, ext)
            # avoid overwrite inside one task
            key = fname.lower()
            k = 2
            while key in used:
                root, e = os.path.splitext(fname)
                fname = '%s_%d%s' % (root, k, e)
                key = fname.lower()
                k += 1
            used.add(key)
            self.urls.append(url)
            self.filenames[url] = fname
            n += 1
        return n

    def read(self):
        print_ = get_print(self.cw)
        info = _parse_url(self.url)
        creator = self._profile_name(info)
        self.artist = creator

        if info['post']:
            post = self._read_post(info, info['post'])
            title = _safe_name(post.get('title') or info['post'], info['post'])
            self.title = clean_title('[%s] %s - %s [%s]' % (
                info['service'], creator, title, info['post']))
            count = self._push_post_files(info, post, creator, prefix=False)
            if count == 0:
                raise Exception('No downloadable files in this post')
            print_('queued %d file(s) from post %s' % (count, info['post']))
            return

        self.title = clean_title('[%s] %s [%s]' % (
            info['service'], creator, info['user']))
        total_posts = 0
        total_files = 0
        for post in self._iter_creator_posts(info):
            total_posts += 1
            total_files += self._push_post_files(info, post, creator, prefix=True)
            if total_posts % 10 == 0:
                print_('scanned %d posts / %d files...' % (total_posts, total_files))
            if self.cw and getattr(self.cw, 'alive', True) is False:
                break

        if total_files == 0:
            raise Exception('No downloadable files for this creator')
        print_('queued %d file(s) from %d post(s)' % (total_files, total_posts))

13
13 comments
(edited )
hds 할때 메모장 txt에서 hds로 변환하면 되나요?
넹
👍
일부 다운이 되지 않는 post가 있는데 무슨 문제일까요? 대표적으로 horosuke가 다 안됩니다...
https://pawchive.pw/ 에서 진행해주세요
Fail_403뜨는데 왜 그런지 아시나요?
기존 케모노는 업데이트 안되서, https://pawchive.pw/ 에서 해주셔야 합니다.
크리에이터 링크 자체를 넣으면 반응이 없어요. 개별 게시물에만 반응하는데, 문제는 게시물의 날짜가 폴더명에 들어가지 않아서 그냥 다운로드 받으면 문제가 큽니다. 크리에이터 링크 지원이 안되면 게시물의 폴더명 설정이 가능하도록 하면 좋을 것 같습니다. 날짜 없는 거 치명적이에요.
https://pawchive.pw/fanbox/user/123 https://pawchive.pw/fanbox/user/123/post/456 https://www.pawchive.st/patreon/user/123/post/456 코드를 보면 알겟지만, 다 되는데 왜 안되징 grok 한테 다시 부탁해봄
작가 하나당 2000장이 한계던데 어쩔수 없는건가요?
이거 포스트에 있는 이미지 순서랑 다운받은 이미지랑 순서가 다르게 다운받아지는데 따로 뭘 설정해줘야 되는거임?
All
Title
Body