2
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ What does the code do? Why does a "telegram bot" need to fetch 5 urls ? \$\endgroup\$
    – user985366
    Jun 4, 2021 at 14:27

1 Answer 1

1
\$\begingroup\$

There are a number of issues, but I'm guessing that the major slowdown in s() is due to the unnecessarily repeated calls to pyscp.wikidot.Wiki('scpfoundation.net').

This web request is done once for each match in searchlines and then done 5 more times again. I'm assuming this is the exact same data retrieved every time.

You should do this only once in s() at most in my opinion then use the saved result inside your while loops.

\$\endgroup\$
3
  • \$\begingroup\$ What are other issues? This answer helped me very much \$\endgroup\$ Jun 4, 2021 at 15:42
  • \$\begingroup\$ When you enumerate(searchlines) in s() for example, you have a nested for loop that returns you l but you already know l as line from the outer loop, So that inner for is not likely doing anything to help. (unless of course I have misinterpreted things) \$\endgroup\$
    – JonSG
    Jun 4, 2021 at 15:56
  • \$\begingroup\$ It helps me to split line in right way, but thx \$\endgroup\$ Jun 4, 2021 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.