-
Notifications
You must be signed in to change notification settings - Fork 34
/
bot.py
360 lines (310 loc) · 14.6 KB
/
bot.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
# -*- coding: utf-8 -*-
import re
import requests
import base64
import time
import datetime
from urllib import parse
from bs4 import BeautifulSoup
import telebot
import os
# Example of your code beginning
# Config vars
token = os.environ['TELEGRAM_TOKEN']
session = requests.session()
bot = telebot.TeleBot(token)
@bot.message_handler(commands=['saucenao'])
def send_saucenao(message):
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_saucenao)
def get_saucenao(message):
print(message)
if message.text=="/cancel":
bot.send_message(chat_id=message.chat.id, text="已退出搜图模式", parse_mode="MarkdownV2")
return
if message.content_type!="photo":
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_ascii2d)
return
else:
url = bot.get_file_url(message.photo[-1].file_id)
print(url)
saucenao(url, message.chat.id)
bot.send_message(chat_id=message.chat.id, text="搜索完成", parse_mode="MarkdownV2")
@bot.message_handler(commands=['ascii2d'])
def send_ascii2d(message):
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_ascii2d)
def get_ascii2d(message):
print(message)
if message.text=="/cancel":
bot.send_message(chat_id=message.chat.id, text="已退出搜图模式", parse_mode="MarkdownV2")
return
if message.content_type!="photo":
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_ascii2d)
return
else:
url = bot.get_file_url(message.photo[-1].file_id)
print(url)
ascii2d(url, message.chat.id)
bot.send_message(chat_id=message.chat.id, text="搜索完成", parse_mode="MarkdownV2")
@bot.message_handler(commands=['anime'])
def send_anime(message):
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_anime)
def get_anime(message):
print(message)
if message.text=="/cancel":
bot.send_message(chat_id=message.chat.id, text="已退出搜图模式", parse_mode="MarkdownV2")
return
if message.content_type!="photo":
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_ascii2d)
return
else:
url = bot.get_file_url(message.photo[-1].file_id)
print(url)
anime(url, message.chat.id)
bot.send_message(chat_id=message.chat.id, text="搜索完成", parse_mode="MarkdownV2")
@bot.message_handler(commands=['iqdb'])
def send_iqdb(message):
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_iqdb)
def get_iqdb(message):
print(message)
if message.text=="/cancel":
bot.send_message(chat_id=message.chat.id, text="已退出搜图模式", parse_mode="MarkdownV2")
return
if message.content_type!="photo":
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_ascii2d)
return
else:
url = bot.get_file_url(message.photo[-1].file_id)
print(url)
iqdb(url, message.chat.id)
bot.send_message(chat_id=message.chat.id, text="搜索完成", parse_mode="MarkdownV2")
@bot.message_handler(commands=['all'])
def send_all(message):
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_all)
def get_all(message):
print(message)
if message.text=="/cancel":
bot.send_message(chat_id=message.chat.id, text="已退出搜图模式", parse_mode="MarkdownV2")
return
if message.content_type!="photo":
bot.send_message(chat_id=message.chat.id, text="请发送图片,或输入 /cancel 取消",parse_mode="MarkdownV2")
bot.register_next_step_handler(message, get_ascii2d)
return
else:
url = bot.get_file_url(message.photo[-1].file_id)
print(url)
saucenao(url, message.chat.id)
ascii2d(url, message.chat.id)
anime(url, message.chat.id)
iqdb(url, message.chat.id)
bot.send_message(chat_id=message.chat.id, text="搜索完成", parse_mode="MarkdownV2")
def saucenao(photo_url,chat_id):
try:
url="https://saucenao.com/search.php"
#url = "https://saucenao.com"
Header = {
'Host': 'saucenao.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
'Accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, * / *;q = 0.8',
'Accept - Language': 'zh - CN, zh;q = 0.8, zh - TW;q = 0.7, zh - HK;q = 0.5, en - US;q = 0.3, en;q = 0.2',
'Accept - Encoding': 'gzip, deflate, br',
'Connection': 'keep - alive',
}
payloaddata = {
'frame': 1,
'hide': 0,
'database': 999,
}
#files = {"file": "file": ('saber.jpg', open("saber.jpg", "rb", , "image/png")}
photo_file=requests.get(photo_url)
files = {"file": (
"saucenao.jpg", photo_file.content, "image/png")}
bot.send_message(chat_id=chat_id,text="正在搜索saucenao")
r = session.post(url=url, headers=Header, data=payloaddata,files=files)
#r = session .get(url=url,headers=Header)
soup = BeautifulSoup(r.text, 'html.parser')
#print(soup.prettify())
result=0
choice=0
for img in soup.find_all('div', attrs={'class': 'result'}): # 找到class="wrap"的div里面的所有<img>标签
#print(img)
if('hidden' in str(img['class']))==False:
try:
name=img.find("div",attrs={'class': 'resulttitle'}).get_text()
img_url=str(img.img['src'])
describe_list=img.find("div",attrs={'class': 'resultcontentcolumn'})
url_list = img.find("div", attrs={'class': 'resultcontentcolumn'}).find_all("a", attrs={'class': 'linkify'})
similarity = str(img.find("div", attrs={'class': 'resultsimilarityinfo'}).get_text())
print(name)
except:
continue
try:
describe = str(url_list[0].previous_sibling.string)
describe_id = str(url_list[0].string)
describe_url = str(url_list[0]['href'])
auther_url = str(url_list[1]['href'])
auther = str(url_list[1].previous_sibling.string)
auther_id = str(url_list[1].string)
'''print(name)
print(img_url)
print(describe)
print(describe_id)
print(similarity)
print(auther)
print(auther_id)
print(describe_url)'''
text = f"{name}\n{describe}[{describe_id}]({describe_url})\n{auther}:[{auther_id}]({auther_url})\n相似度{similarity}"
except:
'''print(describe_list.get_text())
print(describe_list.strong.string)
print(describe_list.strong.next_sibling.string)
print(describe_list.small.string)
print(describe_list.small.next_sibling.next_sibling.string)'''
auther = str(describe_list.strong.string)
auther_id = str(describe_list.strong.next_sibling.string)
describe = str(describe_list.small.string) + "\n" + str(describe_list.small.next_sibling.next_sibling.string)
text = f"{name}\n{auther}:{auther_id}\n{describe}\n相似度{similarity}"
photo_file = session.get(img_url)
bot.send_photo(chat_id=chat_id,photo=photo_file.content,parse_mode='Markdown',caption=text)
result=1
if result==0:
bot.send_message(chat_id=chat_id, text="saucenao无结果")
except:
print("saucenao")
def ascii2d(photo_url,chat_id):
try:
url = "https://ascii2d.net/"
# url = "https://saucenao.com"
Header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0',
}
html = session.get(url, headers=Header)
print(html)
authenticity_token = re.findall("<input type=\"hidden\" name=\"authenticity_token\" value=\"(.*?)\" />", html.text, re.S)[0]
payloaddata = {
'authenticity_token': authenticity_token,
'utf8': "✓",
}
# files = {"file": "file": ('saber.jpg', open("saber.jpg", "rb", , "image/png")}
bot.send_message(chat_id=chat_id, text="正在搜索ascii2d")
photo_file = requests.get(photo_url)
files = {"file": (
"saucenao.jpg", photo_file.content, "image/png")}
url = "https://ascii2d.net/search/multi"
r = session.post(url=url, headers=Header, data=payloaddata, files=files)
soup = BeautifulSoup(r.text, 'html.parser')
# print(soup.prettify())
pan = 0
for img in soup.find_all('div', attrs={'class': 'row item-box'}): # 找到class="wrap"的div里面的所有<img>标签
# print(img)
if pan != 0:
img_url = "https://ascii2d.net" + str(img.img['src'])
the_list = img.find_all('a')
title = str(the_list[0].get_text())
title_url = str(the_list[0]["href"])
auther = str(the_list[1].get_text())
auther_url = str(the_list[1]["href"])
photo_file = session.get(img_url)
text=f"titile:[{title}]({title_url})\nauther:[{auther}]({auther_url})"
bot.send_photo(chat_id=chat_id, caption=text, parse_mode='Markdown',photo=photo_file.content)
pan = pan + 1
if pan == 3:
break
except:
print("ascii2d faild")
def anime(photo_url,chat_id):
try:
url = "https://trace.moe/api/search"
# url = "https://saucenao.com"
photo_file = requests.get(photo_url)
ls_f = base64.b64encode(photo_file.content)
data = {
"image": ls_f,
}
# files = {"file": "file": ('saber.jpg', open("saber.jpg", "rb", , "image/png")}
r = session.post(url=url, data=data)
# r = session .get(url=url,headers=Header)
bot.send_message(chat_id=chat_id,text="正在搜索 trace.moe")
information = r.json()
anilist_id = information['docs'][0]["anilist_id"]
filename = information['docs'][0]['filename']
tokenthumb = information['docs'][0]['tokenthumb']
at = information['docs'][0]['at']
limit = information['limit'] # 剩余搜索次数
limit_ttl = information['limit_ttl'] # 剩余重置时间
title = information['docs'][0]['title_chinese']
episode = information['docs'][0]['episode']
quota = information['quota']
quota_ttl = information['quota_ttl']
similarity=information['docs'][0]['similarity']
similarity_num="%.2f%%" % (similarity * 100)
img_url = f"https://trace.moe/thumbnail.php?anilist_id={anilist_id}&file={parse.quote(filename)}&t={at}&token={tokenthumb}"
print(img_url)
video_url = f"https://trace.moe/preview.php?anilist_id={anilist_id}&file={parse.quote(filename)}&t={at}&token={tokenthumb}"
print(video_url)
video = f"https://media.trace.moe/video/{anilist_id}/{parse.quote(filename)}?t={at}&token={tokenthumb}"
print(video)
more_url = f"https://anilist.co/anime/{anilist_id}"
text = f"{similarity_num}\nTitle:{title}\n集数:{episode}\n时间:{datetime.timedelta(seconds=int(at))}\n来源:{filename}\n[更多信息]({more_url})\n分钟剩余搜索次数:{limit}\n分钟剩余次数重置时间:{limit_ttl}s\n24小时剩余搜索次数:{quota}\n24小时剩余次数重置时间:{datetime.timedelta(seconds=int(quota_ttl))}"
print(text)
photo_file = session.get(img_url)
bot.send_photo(chat_id=chat_id, photo=photo_file.content, parse_mode='Markdown', caption=text)
photo_file = session.get(video)
bot.send_video(chat_id=chat_id,data=photo_file.content)
except:
print("anime faild")
def iqdb(photo_url,chat_id):
try:
bot.send_message(chat_id=chat_id, text="正在搜索 iqdb", parse_mode="MarkdownV2")
url = "http://iqdb.org/"
# url = "https://saucenao.com"
photo_file = requests.get(photo_url)
files = {"file": (
"iqdb.jpg", photo_file.content, "image/png")}
# files = {"file": "file": ('saber.jpg', open("saber.jpg", "rb", , "image/png")}
r = requests.post(url=url, files=files)
#print(r.text)
soup = BeautifulSoup(r.text, 'html.parser')
a=1
for img in soup.find_all('td', attrs={'class': 'image'}): # 找到class="wrap"的div里面的所有<img>标签
#print(img)
if a==7:
break
try:
#print(img.a.get('href'))
img_html=img.a.get('href')
if "http:" not in img_html and "https:" not in img_html:
img_html="https:"+img_html
img_url="http://iqdb.org"+img.img.get('src')
text=f"[图片详情]({img_html})"
photo_file = session.get(img_url)
bot.send_photo(chat_id=chat_id, photo=photo_file.content, parse_mode='Markdown', caption=text)
a=a+1
except:
None
except:
None
'''@bot.message_handler(content_types=['photo'])
def get_photo(message):
#print(message)
#print(message.photo[-1].file_id)
#bot.get_file_url
url=bot.get_file_url(message.photo[-1].file_id)
print(url)
saucenao(url,message.chat.id)
ascii2d(url, message.chat.id)
anime(url, message.chat.id)'''
if __name__ == '__main__':
while True:
try:
bot.polling(none_stop=True)
except Exception as e:
time.sleep(2)