shindan_cli

shindan-cli

CI Release Package PyPI PyPI - Python Version

ShindanMaker (診断メーカー, https://shindanmaker.com) CLI + Library

Install

pip install shindan-cli

Usage

Supported types of diagnosis:

CLI

$ shindan -h
usage: shindan [-h] [-w] [-H] [-l] [-V] ID NAME

ShindanMaker (https://shindanmaker.com) CLI

positional arguments:
  ID             shindan page id
  NAME           shindan name

optional arguments:
  -h, --help     show this help message and exit
  -w, --wait     insert random wait
  -H, --hashtag  add hashtag `#shindanmaker`
  -l, --link     add link to last of output
  -V, --version  show program's version number and exit

$ shindan 1036646 hoge
ねこって、むしだ。

𝙐𝙉𝙄𝙌𝙇𝙊

$ shindan 1036646 huga -l
ねこって、むしだ。

𝙉𝙄𝙎𝙎𝙄𝙉
https://shindanmaker.com/1036646

$ shindan 1036646 huga -l -H
ねこって、むしだ。

𝙁𝙐𝙅𝙄𝙏𝙎𝙐
#shindanmaker
https://shindanmaker.com/1036646

Library

from shindan_cli import shindan
# type: (int, str, optional[bool]) -> ShindanResults
shindan(1036646, 'hoge', wait=False)

Returns:

{
  'results': ['ねこって、むしだ。', '', '𝙏𝙊𝙆𝙔𝙊 𝙈𝙀𝙏𝙍𝙊'],
  'hashtags': ['#shindanmaker'],
  'shindan_url': 'https://shindanmaker.com/1036646'
}

License

MIT


Similar Imprementations

 1""".. include:: ../README.md"""  # noqa: D400, D415
 2
 3from __future__ import annotations
 4
 5import importlib.metadata
 6
 7from .shindan import ShindanError, ShindanResult, shindan
 8
 9try:
10    __version__ = importlib.metadata.version(__name__)
11except importlib.metadata.PackageNotFoundError:
12    __version__ = "0.0.0"
13
14__all__ = ("ShindanError", "ShindanResult", "shindan")
class ShindanError(builtins.Exception):
27class ShindanError(Exception):
28    """Error class for shindan-cli."""

Error class for shindan-cli.

class ShindanResult(typing.TypedDict):
 9class ShindanResult(TypedDict):
10    """TypedDict Class for result of shindan."""
11
12    results: list[str]
13    hashtags: list[str]
14    shindan_url: str

TypedDict Class for result of shindan.

results: list[str]
hashtags: list[str]
shindan_url: str
def shindan( page_id: int, shindan_name: str, *, wait: bool | None = False) -> ShindanResult:
 68def shindan(
 69    page_id: int,
 70    shindan_name: str,
 71    *,
 72    wait: bool | None = False,
 73) -> ShindanResult:
 74    """Get the shindan result from <https://shindanmaker.com>.
 75
 76    Args:
 77        page_id : int
 78            shindan page id (e.g. `1036646`)
 79        shindan_name : str
 80            shindan name (e.g. your name)
 81        wait : bool | None, optional
 82            enable random waits while fetching shindan data, by default False
 83
 84    Returns:
 85        ShindanResult: the returned result from <https://shindanmaker.com>
 86
 87    Raises:
 88        ShindanError
 89
 90    """
 91    if not isinstance(page_id, int) or page_id < 0:
 92        msg = f"invalid page id: {page_id}"
 93        raise ShindanError(msg)
 94    shindan_url = f"{BASE_URL}/{page_id}"
 95
 96    session = create_session()
 97
 98    shindan_page = request_with_retry(session, "GET", shindan_url, headers=HEADERS)
 99    if shindan_page.status_code != 200:  # noqa: PLR2004
100        raise ShindanError(shindan_page.status_code)
101
102    source = BeautifulSoup(shindan_page.text, features="lxml")
103
104    shindan_type_input = source.select_one('input[name="type"]')
105    if not shindan_type_input or not shindan_type_input.get("value"):
106        msg = "Cannot detect shindan type!"
107        raise ValueError(msg)
108    shindan_type = str(shindan_type_input.get("value"))
109    params = cast(
110        "Params",
111        {
112            key: key_input.get("value")
113            for key in TARGET_KEYS_BY_TYPE[shindan_type]
114            if (key_input := source.select_one(f'input[name="{key}"]'))
115        },
116    )
117    # overwrite randname (old: shindanName)
118    params["randname"] = shindan_name
119    params["_token"] = params["_token"] or __get_csrf_token(session, source, shindan_url=shindan_url)
120
121    if wait:
122        _http.random_wait()
123
124    if params["type"] == "ai":
125        hashtag_title = source.select_one("h1#shindanTitle")
126        if not hashtag_title or not isinstance(
127            hashtag := hashtag_title.get("data-shindan_hashtag"),
128            str,
129        ):
130            hashtag = None
131        return get_result_by_ai(
132            session,
133            params,
134            user_inputs=get_user_inputs(source, shindan_name),
135            hashtag=hashtag,
136            csrf_token=params["_token"],
137            shindan_url=shindan_url,
138        )
139    if params["type"] == "branch":
140        params["rbr"] = get_rbr(source)
141        return get_result_by_branch(
142            session,
143            params,
144            shindan_url=shindan_url,
145        )
146    if params["type"] == "check":
147        return get_result_by_check(
148            session,
149            params,
150            user_choices=get_choices(source),
151            shindan_url=shindan_url,
152        )
153    if params["type"] == "name":
154        return get_result_by_name(
155            session,
156            params,
157            shindan_url=shindan_url,
158        )
159    raise NotImplementedError(shindan_type)

Get the shindan result from https://shindanmaker.com.

Arguments:
  • page_id : int shindan page id (e.g. 1036646)
  • shindan_name : str shindan name (e.g. your name)
  • wait : bool | None, optional enable random waits while fetching shindan data, by default False
Returns:

ShindanResult: the returned result from https://shindanmaker.com

Raises:
  • ShindanError