diff --git a/Задания/task1/Lahin/postgres.py b/Задания/task1/Lahin/postgres.py index fe8a029..c0620eb 100644 --- a/Задания/task1/Lahin/postgres.py +++ b/Задания/task1/Lahin/postgres.py @@ -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() \ No newline at end of file diff --git a/Задания/task1/Lahin/select_and_edit'.py b/Задания/task1/Lahin/select_and_edit'.py new file mode 100644 index 0000000..298ef0b --- /dev/null +++ b/Задания/task1/Lahin/select_and_edit'.py @@ -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 \ No newline at end of file diff --git a/Задания/task1/Lahin/telegrambot.py b/Задания/task1/Lahin/telegrambot.py index 3031c38..57d6121 100644 --- a/Задания/task1/Lahin/telegrambot.py +++ b/Задания/task1/Lahin/telegrambot.py @@ -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)