summaryrefslogtreecommitdiff
path: root/ATRI/plugins/plugin_pixiv/__init__.py
blob: 3bb2ae03c3c83dc07d13073f289d19834ec23c13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   __init__.py
@Time    :   2020/11/07 14:31:30
@Author  :   Kyomotoi
@Contact :   [email protected]
@Github  :   https://github.com/Kyomotoi
@License :   Copyright © 2018-2020 Kyomotoi, All Rights Reserved.
'''
__author__ = 'kyomotoi'

import re
import json

from nonebot.plugin import on_command
from nonebot.typing import Bot, Event

from ATRI.utils.utils_error import errorRepo
from ATRI.utils.utils_img import aio_download_pics
from ATRI.utils.utils_request import aio_get_bytes
from ATRI.utils.utils_rule import check_banlist, check_switch

plugin_name_0 = "pixiv-pic-search"
pixivSearchIMG = on_command('p站搜图',
                            rule=check_banlist() & check_switch(plugin_name_0, True))


@pixivSearchIMG.handle()
async def _(bot: Bot, event: Event, state: dict) -> None:
    user = str(event.user_id)
    group = str(event.group_id)

    state["user"] = user
    state["group"] = group

    pid = str(event.message).strip()

    if pid:
        state["pid"] = pid


@pixivSearchIMG.got("pid", prompt="请发送目标PID码")
async def _(bot: Bot, event: Event, state: dict) -> None:
    pid = state["pid"]
    pid = re.findall(r"\d+", pid)

    if len(pid):
        pass
    else:
        await pixivSearchIMG.reject("请发送纯阿拉伯数字的pid")

    await bot.send(event, "别急!在搜索了!")

    URL = f"https://api.imjad.cn/pixiv/v1/?type=illust&id={pid[0]}"
    data = {}

    try:
        data = json.loads(await aio_get_bytes(URL))
    except:
        await pixivSearchIMG.finish(errorRepo("请求数据失败"))

    IMG = data["response"][0]["image_urls"]["large"]
    IMG = IMG.replace("i.pximg.net", "i.pixiv.cat")

    msg0 = f'[CQ:at,qq={state["user"]}]\n'
    msg0 += "Search result:\n"
    msg0 += f"Pid: {pid[0]}\n"
    msg0 += f'Title {data["response"][0]["title"]}\n'
    msg0 += f'W&H: {data["response"][0]["width"]}x{data["response"][0]["height"]}\n'
    msg0 += f'Tags: {data["response"][0]["tags"]}\n'
    msg0 += f'Account Name: {data["response"][0]["user"]["account"]}\n'
    msg0 += f'Author Name: {data["response"][0]["user"]["name"]}\n'
    msg0 += f'Link: https://www.pixiv.net/users/{data["response"][0]["user"]["id"]}\n'
    msg0 += IMG.replace('https://', '')

    await pixivSearchIMG.finish(msg0)


plugin_name_1 = "pixiv-author-search"
pixivSearchAuthor = on_command("p站画师",
                               rule=check_banlist()
                               & check_switch(plugin_name_1, True))


@pixivSearchAuthor.handle()
async def _(bot: Bot, event: Event, state: dict) -> None:
    user = str(event.user_id)
    group = str(event.group_id)

    state["user"] = user
    state["group"] = group

    author_id = str(event.message).strip()

    if author_id:
        state["author_id"] = author_id


@pixivSearchAuthor.got("author_id", prompt="请发送目标画师id")
async def _(bot: Bot, event: Event, state: dict) -> None:
    author_id = state["author_id"]
    author_id = re.findall(r"\d+", author_id)

    if len(author_id):
        pass
    else:
        await pixivSearchAuthor.reject("请发送纯阿拉伯数字的画师id")

    await bot.send(event, f"别急!在搜索了!\n将展示画师[{author_id[0]}]的前三项作品")

    URL = f"https://api.imjad.cn/pixiv/v1/?type=member_illust&id={author_id[0]}"
    data = {}

    try:
        data = json.loads(await aio_get_bytes(URL))
    except:
        await pixivSearchAuthor.finish(errorRepo("请求网络失败"))

    d = {}

    for i in range(0, 3):
        pid = data["response"][i]["id"]
        title = data["response"][i]["title"]
        IMG = data["response"][i]["image_urls"]["large"]
        IMG = IMG.replace("i.pximg.net", "i.pixiv.cat")
        d[i] = [f"{pid}", f"{title}", f"{IMG}"]

    msg = f'[CQ:at,qq={state["user"]}]'

    result = sorted(d.items(), key=lambda x: x[1], reverse=True)

    t = 0

    for i in result:
        t += 1
        msg += "\n————————————\n"
        msg += f"({t})\n"
        msg += f"Title: {i[1][1]}\n"
        msg += f"Pid: {i[1][0]}\n"
        msg += f"{i[1][2].replace('https://', '')}"

    await pixivSearchAuthor.finish(msg)


plugin_name_2 = "pixiv-rank"
pixivRank = on_command("p站排行榜",
                       rule=check_banlist() & check_switch(plugin_name_2, True))


@pixivRank.handle()
async def _(bot: Bot, event: Event, state: dict) -> None:
    user = str(event.user_id)

    await bot.send(event, "正在获取P站每日排行榜前三作品")

    URL = "https://api.imjad.cn/pixiv/v1/?type=rank"
    data = {}

    try:
        data = json.loads(await aio_get_bytes(URL))
    except:
        await pixivRank.finish(errorRepo("网络请求失败"))

    d = {}
    for i in range(0, 3):
        pid = data["response"][0]["works"][i]["work"]["id"]
        title = data["response"][0]["works"][i]["work"]["title"]
        IMG = data["response"][0]["works"][i]["work"]["image_urls"]["large"]
        IMG = IMG.replace("i.pximg.net", "i.pixiv.cat")
        d[i] = [f"{pid}", f"{title}", f"{IMG}"]

    msg = f"[CQ:at,qq={user}]"

    result = sorted(d.items(), key=lambda x: x[1], reverse=True)

    t = 0

    for i in result:
        t += 1
        msg += "\n————————————\n"
        msg += f"({t})\n"
        msg += f"Title: {i[1][1]}\n"
        msg += f"Pid: {i[1][0]}\n"
        msg += f"{i[1][2].replace('https://', '')}"

    print(msg)
    await pixivRank.finish(msg)