This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
169 lines (152 loc) · 5.49 KB
/
__init__.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
import asyncio
import urllib.parse
from datetime import datetime, timedelta
from io import BytesIO
import qrcode
from aiohttp import ClientSession
from graia.ariadne import Ariadne
from graia.ariadne.event.message import GroupMessage, FriendMessage, MessageEvent
from graia.ariadne.message.chain import MessageChain
from graia.ariadne.message.element import Plain, Image, Forward, ForwardNode
from graia.ariadne.message.parser.twilight import (
Twilight,
FullMatch,
ParamMatch,
RegexResult,
)
from graia.saya import Saya, Channel
from graia.saya.builtins.broadcast import ListenerSchema
from pydantic import BaseModel, root_validator
from library import config, prefix_match
from library.depend import Switch, FunctionCall, Blacklist
saya = Saya.current()
channel = Channel.current()
channel.name("FurryGroupSearch")
channel.author("nullqwertyuiop")
channel.description("")
class CityGroupLoc(BaseModel):
id: int
province: str
city: str | None
class CityGroup(BaseModel):
location: CityGroupLoc
group: int
level: int
name: str
url: str | None
valid: int
@root_validator(pre=True, allow_reuse=True)
def furry_group_search_validator(cls, values: dict):
if "valid" not in values:
values["valid"] = values.get("vaild")
return values
@channel.use(
ListenerSchema(
listening_events=[GroupMessage, FriendMessage],
inline_dispatchers=[
Twilight(prefix_match(), FullMatch("同城群"), ParamMatch() @ "city")
],
decorators=[
Switch.check(channel.module),
Blacklist.check(),
FunctionCall.record(channel.module),
],
)
)
async def furry_group_search(app: Ariadne, event: MessageEvent, city: RegexResult):
city = city.result.display
async with ClientSession() as session:
async with session.get(
f"https://api.fursuitguide.yooofur.com:26364/cityGroup/"
f"search?type=3&keyword={urllib.parse.quote(city)}"
) as resp:
data = await resp.json()
if data.get("code", 0) != 100:
return await app.send_message(
event.sender.group if isinstance(event, GroupMessage) else event.sender,
MessageChain(
[
Plain(f"无法找到 {city} 的同城群\n"),
Plain("==========\n"),
Plain("可扫码提交收录同城群信息"),
Image(
data_bytes=await async_generate_qr(
"https://docs.qq.com/form/page/DQWVod0VFa1VsQkFL?_w_tencentdocx_form=1"
)
),
]
),
)
msg = await generate_forward(data.get("data"))
await app.send_message(
event.sender.group if isinstance(event, GroupMessage) else event.sender, msg
)
async def generate_forward(full_data: list) -> MessageChain:
order = ["市级群", "省级群", "省内其他市级"]
offset = 0
nodes = []
for index, data in enumerate(full_data):
if not data:
continue
nodes.append(
ForwardNode(
target=config.account,
name=f"{config.name}#{config.num}",
time=datetime.now() + timedelta(seconds=5 * offset),
message=MessageChain(f"====={order[index]}====="),
)
)
offset += 1
groups = [CityGroup(**item) for item in data]
for group_index, group in enumerate(groups):
nodes.append(
ForwardNode(
target=config.account,
name=f"{config.name}#{config.num}",
time=datetime.now() + timedelta(seconds=5 * offset),
message=MessageChain(
[
Plain(f"{group_index + 1}. {group.name}\n"),
Plain(
f"地区:{group.location.province}{group.location.city or ''}\n"
),
Plain(f"群号:{group.group}\n")
if group.valid
else Plain(f"进入该群需先添加:{group.group}"),
Image(data_bytes=await async_generate_qr(group.url))
if group.valid
else Plain(""),
]
),
)
)
offset += 1
nodes.append(
ForwardNode(
target=config.account,
name=f"{config.name}#{config.num}",
time=datetime.now() + timedelta(seconds=5 * offset),
message=MessageChain(
[
Plain("==========\n"),
Plain("可扫码提交收录同城群信息\n"),
Image(
data_bytes=await async_generate_qr(
"https://docs.qq.com/form/page/DQWVod0VFa1VsQkFL?_w_tencentdocx_form=1"
)
),
]
),
)
)
return MessageChain([Forward(nodes)])
def generate_qr(content: str):
qr = qrcode.QRCode(border=0)
qr.add_data(content)
qr.make(fit=True)
output = BytesIO()
qr.make_image(fill_color=(0, 0, 0)).save(output)
return output.getvalue()
async def async_generate_qr(content: str):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, generate_qr, content)