mirror of
https://github.com/KUlishevgeniy/c22712.git
synced 2026-09-24 08:00:22 +00:00
Телеграм Бот и Функции с ошибками
This commit is contained in:
@@ -49,12 +49,15 @@ for i in range(len(productst)):
|
||||
u.append(features[10*i+j].text)
|
||||
wget.download(url, filename)
|
||||
t = pricest[i].text.replace("\xa0", " ")
|
||||
insert = f"""INSERT INTO public.laptops(
|
||||
Product, Price, Diagonal, Resolution, CPU, RAM, Graphics_Controller, Volume, src)
|
||||
VALUES
|
||||
('{productst[i].text.strip()}', '{t.strip()}', '{u[0][:u[0].find("/")]}', '{u[0][u[0].find("/")+1:]}',
|
||||
'{u[2]}', '{u[4]+" "+u[5]}', '{u[6]}', '{u[8]}', '{filename}');"""
|
||||
cursor.execute(insert)
|
||||
connection.commit()
|
||||
try:
|
||||
insert = f"""INSERT INTO public.laptops(
|
||||
Product, Price, Diagonal, Resolution, CPU, RAM, Graphics_Controller, Volume, src)
|
||||
VALUES
|
||||
('{productst[i].text.strip()}', '{t.strip()}', '{u[0][:u[0].find("/")]}', '{u[0][u[0].find("/")+1:]}',
|
||||
'{u[2]}', '{u[4]+" "+u[5]}', '{u[6]}', '{u[8]}', '{filename}');"""
|
||||
cursor.execute(insert)
|
||||
connection.commit()
|
||||
except:
|
||||
connection.rollback()
|
||||
cursor.close()
|
||||
connection.close()
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
import psycopg2
|
||||
import config
|
||||
def edit(request):
|
||||
try:
|
||||
connection = psycopg2.connect(host=config.host, dbname=config.dbname, user=config.user, password=config.password)
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(request)
|
||||
print("Done")
|
||||
except Exception as error:
|
||||
print(f"Что-то не так. Ошибка:{error}")
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
def select(request):
|
||||
try:
|
||||
connection = psycopg2.connect(host=config.host, dbname=config.dbname, user=config.user,
|
||||
password=config.password)
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(request)
|
||||
result = cursor.fetchall()
|
||||
except Exception as error:
|
||||
result = f"Ошибка: {error}"
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
return result
|
||||
@@ -1,15 +1,23 @@
|
||||
import telebot
|
||||
import psycopg2
|
||||
import config
|
||||
def execute_request(request):
|
||||
connection = psycopg2.connect(host=config.host, dbname=config.dbname, user=config.user, password=config.password)
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(request)
|
||||
result = cursor.fetchall()
|
||||
cursor.close()
|
||||
connection.close()
|
||||
return result
|
||||
max_id = int(execute_request("select count(*) from laptops")[0][0])
|
||||
bot = telebot.TeleBot(config.bot_token)
|
||||
|
||||
def select(request):
|
||||
try:
|
||||
connection = psycopg2.connect(host=config.host, dbname=config.dbname, user=config.user,
|
||||
password=config.password)
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(request)
|
||||
result = cursor.fetchall()
|
||||
except:
|
||||
result = "Ошибка"
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
return result
|
||||
|
||||
max_id = int(select("select max(id) from laptops")[0][0])
|
||||
bot = telebot.TeleBot(config.bot_token)
|
||||
@bot.message_handler(commands=["start"])
|
||||
def start(m, res=False):
|
||||
@@ -26,12 +34,21 @@ def from_bd(message):
|
||||
bot.send_message(message.chat.id, "ID больше, чем количество товаров. Попробуйте снова:")
|
||||
return
|
||||
else:
|
||||
data = execute_request(f"select * from laptops where id = {current_id}")[0][1:-1]
|
||||
names_of_columns = ["Товар", "Цена", "Диагональ", "Разрешение", "Процессор", "Оперативная память", "Графический контроллер", "Объём диска"]
|
||||
everydata = []
|
||||
for data_name, column_name in zip(data, names_of_columns):
|
||||
everydata.append(column_name.capitalize() + ": " + data_name)
|
||||
bot.send_message(message.chat.id, "\n\n".join(everydata))
|
||||
try:
|
||||
data = select(f"select * from laptops where id = {current_id}")[0][1:]
|
||||
if type(data) == str:
|
||||
bot.send_message(message.chat.id, "Ошибка на сервере")
|
||||
return
|
||||
s = data[:-1]
|
||||
names_of_columns = ["Товар", "Цена", "Диагональ", "Разрешение", "Процессор", "Оперативная память",
|
||||
"Графический контроллер", "Объём диска"]
|
||||
everydata = []
|
||||
for data_name, column_name in zip(s, names_of_columns):
|
||||
everydata.append(column_name.capitalize() + ": " + data_name)
|
||||
bot.send_message(message.chat.id, "\n\n".join(everydata))
|
||||
bot.send_photo(message.chat.id, open(data[-1], 'rb'))
|
||||
except:
|
||||
bot.send_message(message.chat.id, "Ошибка на сервере")
|
||||
bot.send_message(message.chat.id, f"Введите следующий ID от 1 до {max_id}:")
|
||||
bot.polling(none_stop=True, interval=0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user