I was working on one telgram bot, and when finally i made everything i discovered that it is extremely slow. def s takes 5-7 seconds to get result and send it to user, i tried to optimise it with methods from other sites, but it didn't work. How can i optimise it?
import telebot
import pyscp
from googlesearch import search
scp = "scp-"
bot = telebot.TeleBot("NO")
def extract_arg(arg):
return arg.split()[1:]
@bot.message_handler(commands=['o'])
def o(message):
global status
status = extract_arg(message.text)
try:
object = status[0]
except Exception as e:
object ="7777"
l = scp + object
url = "scpfoundation.net/" + l
ru_wiki = pyscp.wikidot.Wiki('scpfoundation.net')
p = ru_wiki(l)
try:
k = ('{}'.format(p.title))
text = (f'<a href="{url}">{k}</a>')
except Exception as e:
text="Простите, этот номер не присвоен не одному из объектов"
bot.send_message(message.chat.id, text,parse_mode='HTML')
def extract_argument(argument):
return argument.split()[3:]
@bot.message_handler(commands=['s'])
def s(m):
status1 = m.text
status2 = status1.replace('/s', "")
f = open("base.txt", "r")
searchlines = [line.strip() for line in f. readlines() if line.strip()]
f.close()
out = []
out1=[]
try:
for i, line in enumerate(searchlines):
if status2.lower() in line.lower():
for l in searchlines[i : i + 1]:
out.append(l.split(maxsplit=1)[0])
out1.append(l.split(maxsplit=1)[1])
except Exception as e:
bot.send_message(m.chat.id, "Простите, не смог ничего найти.",parse_mode='HTML')
pass
finalout = list(set(out))
number = len(finalout)
g, nm, count, count1, gey =[], int("0"), int("0"), int("0"), []
while (nm<number):
url = 'http://scpfoundation.net/'
try:
ru_wiki = pyscp.wikidot.Wiki('scpfoundation.net')
p = ru_wiki(finalout[count])
k = ('{}'.format(p.title))
gey.append(k)
result = " ".join ([url, finalout[count]])
g.append(f'<a href="{result}">{k}</a>')
except Exception as e:
pass
count+=1
count1+=1
nm+=1
numbeer=int('0')
counter=int('0')
ka = search(f'{status2} site:scpfoundation.net', num_results=4)
while (numbeer<5):
try:
ru_wiki = pyscp.wikidot.Wiki('scpfoundation.net')
p = ru_wiki(ka[counter])
kj = ('{}'.format(p.title))
if (kj not in gey and "forum" not in ka[counter] and "draft" not in ka[counter] and "fragment" not in ka[counter]):
result = ka[counter]
g.append(f'<a href="{result}">{kj}</a>')
except Exception as e:
pass
numbeer+=1
counter+=1
story = '\n'.join(g)
try:
bot.send_message(m.chat.id, story,parse_mode='HTML')
except Exception as e:
bot.send_message(m.chat.id, "Простите, ничего не найдено.", parse_mode='HTML')
@bot.message_handler(commands=['help'])
def help(t):
bot.send_message(t.chat.id, "/o — поиск по номеру; /s — поиск по названию; /help — это сообщение; /join — присоеденится к сообществу; /faq — ответы на частые вопросы",parse_mode='HTML')
@bot.message_handler(commands=['join'])
def join(j):
joiner=(f'<a href="http://scpfoundation.net/system:join">Подай простую заявку!</a>')
bot.send_message(j.chat.id, joiner,parse_mode='HTML')
@bot.message_handler(commands=['faq'])
def faq(f):
faqer=(f'<a href="http://scpfoundation.net/faq">Читать тут.</a>')
bot.send_message(f.chat.id, faqer,parse_mode='HTML')
bot.polling()
The problem is with while (numbeer<5)
5 is number of urls i grab from google search, and less urls i grab less times it need.
I can't reduce the numbers of urls, so maybe i can optimise another part of code to reduce the time?