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)
|
u.append(features[10*i+j].text)
|
||||||
wget.download(url, filename)
|
wget.download(url, filename)
|
||||||
t = pricest[i].text.replace("\xa0", " ")
|
t = pricest[i].text.replace("\xa0", " ")
|
||||||
insert = f"""INSERT INTO public.laptops(
|
try:
|
||||||
Product, Price, Diagonal, Resolution, CPU, RAM, Graphics_Controller, Volume, src)
|
insert = f"""INSERT INTO public.laptops(
|
||||||
VALUES
|
Product, Price, Diagonal, Resolution, CPU, RAM, Graphics_Controller, Volume, src)
|
||||||
('{productst[i].text.strip()}', '{t.strip()}', '{u[0][:u[0].find("/")]}', '{u[0][u[0].find("/")+1:]}',
|
VALUES
|
||||||
'{u[2]}', '{u[4]+" "+u[5]}', '{u[6]}', '{u[8]}', '{filename}');"""
|
('{productst[i].text.strip()}', '{t.strip()}', '{u[0][:u[0].find("/")]}', '{u[0][u[0].find("/")+1:]}',
|
||||||
cursor.execute(insert)
|
'{u[2]}', '{u[4]+" "+u[5]}', '{u[6]}', '{u[8]}', '{filename}');"""
|
||||||
connection.commit()
|
cursor.execute(insert)
|
||||||
|
connection.commit()
|
||||||
|
except:
|
||||||
|
connection.rollback()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
connection.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 telebot
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import config
|
import config
|
||||||
def execute_request(request):
|
bot = telebot.TeleBot(config.bot_token)
|
||||||
connection = psycopg2.connect(host=config.host, dbname=config.dbname, user=config.user, password=config.password)
|
|
||||||
cursor = connection.cursor()
|
def select(request):
|
||||||
cursor.execute(request)
|
try:
|
||||||
result = cursor.fetchall()
|
connection = psycopg2.connect(host=config.host, dbname=config.dbname, user=config.user,
|
||||||
cursor.close()
|
password=config.password)
|
||||||
connection.close()
|
cursor = connection.cursor()
|
||||||
return result
|
cursor.execute(request)
|
||||||
max_id = int(execute_request("select count(*) from laptops")[0][0])
|
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 = telebot.TeleBot(config.bot_token)
|
||||||
@bot.message_handler(commands=["start"])
|
@bot.message_handler(commands=["start"])
|
||||||
def start(m, res=False):
|
def start(m, res=False):
|
||||||
@@ -26,12 +34,21 @@ def from_bd(message):
|
|||||||
bot.send_message(message.chat.id, "ID больше, чем количество товаров. Попробуйте снова:")
|
bot.send_message(message.chat.id, "ID больше, чем количество товаров. Попробуйте снова:")
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
data = execute_request(f"select * from laptops where id = {current_id}")[0][1:-1]
|
try:
|
||||||
names_of_columns = ["Товар", "Цена", "Диагональ", "Разрешение", "Процессор", "Оперативная память", "Графический контроллер", "Объём диска"]
|
data = select(f"select * from laptops where id = {current_id}")[0][1:]
|
||||||
everydata = []
|
if type(data) == str:
|
||||||
for data_name, column_name in zip(data, names_of_columns):
|
bot.send_message(message.chat.id, "Ошибка на сервере")
|
||||||
everydata.append(column_name.capitalize() + ": " + data_name)
|
return
|
||||||
bot.send_message(message.chat.id, "\n\n".join(everydata))
|
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.send_message(message.chat.id, f"Введите следующий ID от 1 до {max_id}:")
|
||||||
bot.polling(none_stop=True, interval=0)
|
bot.polling(none_stop=True, interval=0)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user