-
Notifications
You must be signed in to change notification settings - Fork 33
/
vip.py
362 lines (329 loc) · 14.1 KB
/
vip.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import io
from redis.exceptions import ConnectionError
from requests import get
from os import remove
from telethon.tl.types import MessageMediaPhoto
from asyncio import TimeoutError
from pagermaid import bot, redis, redis_status, version
from pagermaid.listener import listener
from pagermaid.utils import obtain_message, alias_command
try:
import aiohttp, aiofiles
pixiv_import = True
except ImportError:
pixiv_import = False
p_headers = {
"Referer": 'https://www.pixiv.net',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 '
'Safari/537.36',
}
p_original = ['i.pixiv.cat', 'i.pximg.net']
@listener(is_plugin=True, outgoing=True, command=alias_command("duckduckgo"),
description="Duckduckgo 搜索",
parameters="<query>")
async def baidu(context):
await context.edit("获取中 . . .")
try:
message = await obtain_message(context)
except ValueError:
await context.edit("出错了呜呜呜 ~ 无效的参数。")
return
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message('/duckduckgo ' + message)
chat_response = await conversation.get_response()
await bot.send_read_acknowledge(conversation.chat_id)
duckduckgo_text = chat_response.text
await context.edit(duckduckgo_text)
@listener(is_plugin=True, outgoing=True, command=alias_command("baidu"),
description="百度搜索",
parameters="<query>")
async def baidu(context):
await context.edit("获取中 . . .")
try:
message = await obtain_message(context)
except ValueError:
await context.edit("出错了呜呜呜 ~ 无效的参数。")
return
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message('/baidu ' + message)
chat_response = await conversation.get_response()
await bot.send_read_acknowledge(conversation.chat_id)
baidu_text = chat_response.text
await context.edit(baidu_text)
@listener(is_plugin=True, outgoing=True, command=alias_command("caiyun"),
description="彩云翻译",
parameters="<query>")
async def caiyun_translate(context):
await context.edit("获取中 . . .")
try:
message = await obtain_message(context)
except ValueError:
return await context.edit("出错了呜呜呜 ~ 无效的参数。")
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message('/translate ' + message)
chat_response = await conversation.get_response()
await bot.send_read_acknowledge(conversation.chat_id)
caiyun_text = chat_response.text
await context.edit(caiyun_text)
@listener(is_plugin=True, outgoing=True, command=alias_command("deepl"),
description="Deepl 翻译",
parameters="<query>")
async def deepl_translate(context):
await context.edit("获取中 . . .")
try:
message = await obtain_message(context)
except ValueError:
await context.edit("出错了呜呜呜 ~ 无效的参数。")
return
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message('/deepl ' + message)
chat_response = await conversation.get_response()
await bot.send_read_acknowledge(conversation.chat_id)
deepl_text = chat_response.text
await context.edit(deepl_text)
@listener(is_plugin=True, outgoing=True, command=alias_command("weather"),
description="使用彩云天气 api 查询国内实时天气。",
parameters="<位置>")
async def weather(context):
await context.edit("获取中 . . .")
try:
message = await obtain_message(context)
except ValueError:
await context.edit("出错了呜呜呜 ~ 无效的参数。")
return
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message('/weather_api ' + message)
chat_response = await conversation.get_response()
await bot.send_read_acknowledge(conversation.chat_id)
weather_text = chat_response.text
await context.edit(weather_text)
@listener(is_plugin=True, outgoing=True, command=alias_command("weather_pic"),
description="使用彩云天气 api 查询国内实时天气。",
parameters="<位置>")
async def weather_pic(context):
await context.edit("获取中 . . .")
reply = await context.get_reply_message()
try:
message = await obtain_message(context)
except ValueError:
await context.edit("出错了呜呜呜 ~ 无效的参数。")
return
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message('/weather ' + message)
chat_response = await conversation.get_response()
await bot.send_read_acknowledge(conversation.chat_id)
if reply:
await context.respond(chat_response, reply_to=reply)
else:
await context.respond(chat_response)
await context.delete()
@listener(is_plugin=True, outgoing=True, command=alias_command("weather_he"),
description="使用和风天气 api 查询国内省市实时天气。",
parameters="<位置>")
async def weather_he(context):
await context.edit("获取中 . . .")
reply = await context.get_reply_message()
try:
message = await obtain_message(context)
except ValueError:
await context.edit("出错了呜呜呜 ~ 无效的参数。")
return
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message('/weather_he ' + message)
chat_response = await conversation.get_response()
await bot.send_read_acknowledge(conversation.chat_id)
if reply:
await context.respond(chat_response, reply_to=reply)
else:
await context.respond(chat_response)
await context.delete()
@listener(is_plugin=True, outgoing=True, command=alias_command("pixiv"),
description="查询插画信息 (或者回复一条消息)。使用 set [num] 更改镜像源,序号 2 为官方源, 3 为凯露自建源。异步下载需要依赖库 "
"aiohttp[speedups] 、 aiofiles",
parameters="[<图片链接>] <图片序号>")
async def pixiv(context):
await context.edit("获取中 . . .")
if len(context.parameter) == 2:
if context.parameter[0] == 'set':
if not redis_status():
await context.edit('redis 数据库离线 无法更改镜像源。')
return
else:
try:
num = int(context.parameter[1])
except ValueError:
await context.edit('镜像源序号错误。')
return
if 0 < num < 3:
try:
redis.set("pixiv_num", num)
except ConnectionError:
await context.edit('redis 数据库离线 无法更改镜像源。')
return
await context.edit('镜像源已更改。')
return
else:
await context.edit('镜像源序号错误。')
return
else:
pass
if not redis_status():
num = 2
else:
try:
num = int(redis.get("pixiv_num").decode())
except AttributeError:
num = 2
except ConnectionError:
num = 2
try:
message = await obtain_message(context)
except ValueError:
await context.edit("出错了呜呜呜 ~ 无效的参数。")
return
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message('/pixiv_api ' + message)
chat_response = await conversation.get_response()
await bot.send_read_acknowledge(conversation.chat_id)
pixiv_text = chat_response.text
pixiv_text = pixiv_text.replace('i.pixiv.cat', p_original[num - 1])
pixiv_list = pixiv_text.split('|||||')
if len(pixiv_list) == 2:
pixiv_albums = pixiv_list[1].split('|||')
pixiv_album = []
if pixiv_import:
await context.edit("调用异步下载图片中 . . .")
else:
await context.edit("下载图片中 . . .")
if len(pixiv_albums) > 8:
await context.edit('获取的图片数大于 8 ,将只发送前8张图片,下载图片中 . . .')
for i in range(0, min(len(pixiv_albums), 8)):
if not pixiv_import:
r = get(pixiv_albums[i], headers=p_headers)
with open("pixiv." + str(i) + ".jpg", "wb") as code:
code.write(r.content)
else:
async with aiohttp.ClientSession(headers=p_headers) as session:
response = await session.get(pixiv_albums[i])
content = await response.read()
async with aiofiles.open("pixiv." + str(i) + ".jpg", mode='wb') as code:
await code.write(content)
await code.close()
pixiv_album.extend(["pixiv." + str(i) + ".jpg"])
await context.client.send_file(context.chat_id, pixiv_album,
caption=pixiv_list[0])
await context.delete()
for i in pixiv_album:
try:
remove(i)
except:
pass
else:
await context.edit(pixiv_text)
@listener(is_plugin=True, outgoing=True, command=alias_command("whatanime"),
description="通过图片查找相似动漫。(需要回复图片)")
async def whatanime(context):
reply = await context.get_reply_message()
if reply is not None:
if reply.media:
if not isinstance(reply.media, MessageMediaPhoto):
await context.edit("宁需要回复一张图片")
return
else:
await context.edit("宁需要回复一张图片")
return
else:
await context.edit("宁需要回复一张图片")
return
await context.edit("获取中。。。")
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message(message='/whatanime_api', file=reply.photo)
chat_response = await conversation.get_response()
await bot.send_read_acknowledge(conversation.chat_id)
whatanime_text = chat_response.text
whatanime_list = whatanime_text.split('|||')
if len(whatanime_list) == 1:
await context.edit(whatanime_text)
return
link = whatanime_list[0]
name = whatanime_list[1]
native = whatanime_list[2]
episode = whatanime_list[3]
alias = whatanime_list[4]
r18 = whatanime_list[5]
percent = whatanime_list[6]
img = whatanime_list[7]
video = whatanime_list[8]
text = f'[{name}]({link}) (`{native}`)\n'
if bool(episode):
text += f'\n**集数:** `{episode}`'
if bool(alias):
text += f'\n**别名:** `{alias}`'
if bool(r18):
text += f'\n**R-18**'
text += f'\n**相似度:** `{percent}`'
r = get(img)
photo = io.BytesIO(r.content)
photo.name = f'{name}.png'
msg = await context.client.send_file(context.chat_id,
file=photo,
caption=text,
reply_to=reply.id)
if not video == '':
video_list = video.split('||')
video = video_list[0]
title = video_list[1]
start = video_list[2]
end = video_list[3]
text = f'`{title}`\n\n`{start}` - `{end}`'
await context.client.send_file(context.chat_id,
video,
caption=text,
reply_to=msg.id)
await context.delete()
@listener(is_plugin=True, outgoing=True, command=alias_command("tts_nan"),
description="通过 Azure 文本到语音 基于字符串生成 简体男声 语音消息。",
parameters="<字符串>")
async def az_tts_nan(context):
await az_tts(context, "")
@listener(is_plugin=True, outgoing=True, command=alias_command("tts_nv"),
description="通过 Azure 文本到语音 基于字符串生成 简体女声 语音消息。",
parameters="<字符串>")
async def az_tts_nv(context):
await az_tts(context, "nv")
@listener(is_plugin=True, outgoing=True, command=alias_command("tts_tw"),
description="通过 Azure 文本到语音 基于字符串生成 繁体男声 语音消息。",
parameters="<字符串>")
async def az_tts_tw(context):
await az_tts(context, "tw")
@listener(is_plugin=True, outgoing=True, command=alias_command("tts_ne"),
description="通过 Azure 文本到语音 基于字符串生成 简体新闻男声 语音消息。",
parameters="<字符串>")
async def az_tts_ne(context):
await az_tts(context, "ne")
@listener(is_plugin=True, outgoing=True, command=alias_command("tts_en"),
description="通过 Azure 文本到语音 基于字符串生成 英文男声 语音消息。",
parameters="<字符串>")
async def az_tts_en(context):
await az_tts(context, "en")
async def az_tts(context, mode):
await context.edit("获取中 . . .")
reply = await context.get_reply_message()
try:
message = await obtain_message(context)
except ValueError:
await context.edit("出错了呜呜呜 ~ 无效的参数。")
return
async with bot.conversation('PagerMaid_Modify_bot') as conversation:
await conversation.send_message('/tts ' + message + mode)
try:
chat_response = await conversation.get_response()
except TimeoutError:
return await context.edit("未收到服务器回应。")
await bot.send_read_acknowledge(conversation.chat_id)
if reply:
await context.respond(chat_response, reply_to=reply)
else:
await context.respond(chat_response)
await context.delete()