summaryrefslogtreecommitdiff
path: root/ATRI/plugins/pixiv.py
blob: 58a22030a21cfa1bd35abc081df80e57280936f5 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import time
import json
from datetime import datetime
from random import choice
from pathlib import Path
import nonebot
from nonebot import on_command, CommandSession

from ATRI.modules import response # type: ignore


bot = nonebot.get_bot()
master = bot.config.SUPERUSERS

URL_1 = 'https://api.imjad.cn/pixiv/v1/?type=illust&id=' #单图搜索
URL_2 = 'https://api.imjad.cn/pixiv/v1/?type=member_illust&id=' #画师作品搜索
URL_3 = 'https://api.imjad.cn/pixiv/v1/?type=rank' #每日排行榜


IMG_SEACH_REPLY = """[CQ:at,qq={user}]
搜索结果如下:
Pid: {pid}
Title: {title}
宽高: {width}x{height}
Tags: {tags}
账号名称: {account}
名称: {name}
Link: {user_link}
{img}
---------------
完成时间:{time}s"""


def now_time():
    now_ = datetime.now()
    hour = now_.hour
    minute = now_.minute
    now = hour + minute / 60
    return now


@on_command('pixiv_seach_img', aliases = ['p站搜图', 'P站搜图', '批站搜图'], only_to_me = False)
async def _(session: CommandSession):
    user = session.event.user_id
    group = session.event.group_id
    try:
        with open(Path('.') / 'ATRI' / 'plugins' / 'noobList' / 'noobGroup.json', 'r') as f:
            data = json.load(f)
    except:
        data = {}
    try:
        with open(Path('.') / 'ATRI' / 'plugins' / 'noobList' / 'noobList.json', 'r') as f:
            data1 = json.load(f)
    except:
        data1 = {}

    if str(group) in data.keys():
        pass
    else:
        if str(user) in data1.keys():
            pass
        else:
            if 0 <= now_time() < 5.5:
                await session.send(
                    choice(
                        [
                            'zzzz......',
                            'zzzzzzzz......',
                            'zzz...好涩哦..zzz....',
                            '别...不要..zzz..那..zzz..',
                            '嘻嘻..zzz..呐~..zzzz..'
                        ]
                    )
                )
            else:
                with open(Path('.') / 'ATRI' / 'plugins' / 'switch' / 'switch.json', 'r') as f:
                    data = json.load(f)

                if data["pixiv_seach_img"] == "on":
                    pid = session.current_arg.strip()

                    if not pid:
                        pid = session.get('message', prompt = '请告诉ATRI需要查询的Pid码')
                    
                    start =time.perf_counter()
                    await session.send('开始P站搜图\n如搜索时间过长或许为图片过大上传较慢')

                    URL = URL_1 + pid

                    dc = json.loads(response.request_api(URL))

                    if not dc:
                        session.finish('ATRI在网络上走散了...请重试...')

                    img = f'https://pixiv.cat/{pid}.jpg'
                    

                    end = time.perf_counter()

                    await session.send(
                        IMG_SEACH_REPLY.format(
                            user = user,
                            pid = pid,
                            title = dc["response"][0]["title"],
                            width = dc["response"][0]["width"],
                            height = dc["response"][0]["height"],
                            tags = dc["response"][0]["tags"],
                            account = dc["response"][0]["user"]["account"],
                            name = dc["response"][0]["user"]["name"],
                            user_link = f'https://www.pixiv.net/users/' + f'{dc["response"][0]["user"]["id"]}',
                            img = img,
                            time = round(end - start, 3)
                        )
                    )
                
                else:
                    await session.send('该功能已被禁用...')


@on_command('pixiv_seach_author', aliases = ['画师', '搜索画师', '画师搜索'], only_to_me = False)
async def _(session: CommandSession):
    user = session.event.user_id
    group = session.event.group_id
    try:
        with open(Path('.') / 'ATRI' / 'plugins' / 'noobList' / 'noobGroup.json', 'r') as f:
            data = json.load(f)
    except:
        data = {}
    try:
        with open(Path('.') / 'ATRI' / 'plugins' / 'noobList' / 'noobList.json', 'r') as f:
            data1 = json.load(f)
    except:
        data1 = {}

    if str(group) in data.keys():
        pass
    else:
        if str(user) in data1.keys():
            pass
        else:
            if 0 <= now_time() < 5.5:
                await session.send(
                    choice(
                        [
                            'zzzz......',
                            'zzzzzzzz......',
                            'zzz...好涩哦..zzz....',
                            '别...不要..zzz..那..zzz..',
                            '嘻嘻..zzz..呐~..zzzz..'
                        ]
                    )
                )
            else:
                with open(Path('.') / 'ATRI' / 'plugins' / 'switch' / 'switch.json', 'r') as f:
                    data = json.load(f)
                    
                if data["pixiv_seach_author"] == "on":
                    author_id = session.current_arg.strip()

                    if not author_id:
                        author_id = session.get('message', prompt = '请告诉ATRI需要查询的画师ID')
                    
                    start =time.perf_counter()
                    await session.send(f'开始获取画师{author_id}的前三项作品\n如获取时间过长或许为图片过大上传较慢')

                    URL = URL_2 + author_id

                    dc = json.loads(response.request_api(URL))

                    if not dc:
                        session.finish('ATRI在网络上走散了...请重试...')

                    d ={}

                    for i in range(0,3):
                        pid = dc["response"][i]["id"]
                        pidURL = f'https://pixiv.cat/{pid}.jpg'
                        d[i] = [f'{pid}',f'{pidURL}']
                    
                    msg0 = (f'[CQ:at,qq={user}]\n画师id:{author_id},接下来展示前三作品')

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

                    t = 0

                    for i in result:
                        t += 1
                        msg = (f'\n---------------\n({t})\nPid: {i[1][0]}\n{i[1][1]}')
                        msg0 += msg
                    end = time.perf_counter()

                    msg1 = (f'\n---------------\n完成时间:{round(end - start, 3)}s')
                    msg0 += msg1
                    
                    await session.send(msg0)
                
                else:
                    await session.send('该功能已被禁用...')


@on_command('pixiv_daily_rank', aliases = ['P站排行榜', '批站排行榜', 'p站排行榜'], only_to_me = False)
async def _(session: CommandSession):
    user = session.event.user_id
    group = session.event.group_id
    try:
        with open(Path('.') / 'ATRI' / 'plugins' / 'noobList' / 'noobGroup.json', 'r') as f:
            data = json.load(f)
    except:
        data = {}
    try:
        with open(Path('.') / 'ATRI' / 'plugins' / 'noobList' / 'noobList.json', 'r') as f:
            data1 = json.load(f)
    except:
        data1 = {}

    if str(group) in data.keys():
        pass
    else:
        if str(user) in data1.keys():
            pass
        else:
            if 0 <= now_time() < 5.5:
                await session.send(
                    choice(
                        [
                            'zzzz......',
                            'zzzzzzzz......',
                            'zzz...好涩哦..zzz....',
                            '别...不要..zzz..那..zzz..',
                            '嘻嘻..zzz..呐~..zzzz..'
                        ]
                    )
                )
            else:
                with open(Path('.') / 'ATRI' / 'plugins' / 'switch' / 'switch.json', 'r') as f:
                    data = json.load(f)
                
                if data["pixiv_daily_rank"] == "on":

                    await session.send('ATRI正在获取P站每日排行榜前五作品...')

                    start =time.perf_counter()
                    dc = json.loads(response.request_api(URL_3))

                    d = {}

                    for i in range(0,5):
                        pid = dc["response"][0]["works"][i]["work"]["id"]
                        pidURL = f'https://pixiv.cat/{pid}.jpg'
                        d[i] = [f'{pid}',f'{pidURL}']

                    msg0 = (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 = (f'\n---------------\n({t})\nPid: {i[1][0]}\n{i[1][1]}')
                        msg0 += msg
                    end = time.perf_counter()

                    msg1 = (f'\n---------------\n完成时间:{round(end - start, 3)}s')
                    msg0 += msg1

                    await session.send(msg0)
                
                else:
                    await session.send('该功能已被禁用...')