diff --git a/Python/TeleBot/FuncForBot.py b/Python/TeleBot/FuncForBot.py new file mode 100644 index 0000000..12ad380 --- /dev/null +++ b/Python/TeleBot/FuncForBot.py @@ -0,0 +1,14 @@ +def sel(cursor, tabl): + query = f"""SELECT * FROM public.{tabl}""" + cursor.execute(query) + array = list(cursor.fetchall()) + return array + + +def upd(conn, cursor, tabl): + import parser + for i in range(15): + up_query = f"""UPDATE {tabl} SET page_name = '{parser.name[i].text}', price = '{parser.price[i].text}', priceDis = '{parser.priceDis[i].text}', mark = '{parser.mark[i].text}' WHERE id = {i+1}""" + cursor.execute(up_query) + conn.commit() + diff --git a/Python/TeleBot/__pycache__/FuncForBot.cpython-311.pyc b/Python/TeleBot/__pycache__/FuncForBot.cpython-311.pyc new file mode 100644 index 0000000..749672e Binary files /dev/null and b/Python/TeleBot/__pycache__/FuncForBot.cpython-311.pyc differ diff --git a/Python/TeleBot/__pycache__/parser.cpython-311.pyc b/Python/TeleBot/__pycache__/parser.cpython-311.pyc index 0e879ce..e83e42f 100644 Binary files a/Python/TeleBot/__pycache__/parser.cpython-311.pyc and b/Python/TeleBot/__pycache__/parser.cpython-311.pyc differ diff --git a/Python/TeleBot/parser.py b/Python/TeleBot/parser.py index b2bb715..5da6f9e 100644 --- a/Python/TeleBot/parser.py +++ b/Python/TeleBot/parser.py @@ -14,5 +14,3 @@ price = b_soup.find_all('span', class_="middle_price") priceDis = b_soup.find_all('span', class_="baseoldprice") mark = b_soup.find_all('span', class_="product-rating__rating") pictures = b_soup.find_all('div', class_="catalog-list-item__img-wrapper") - - diff --git a/Python/TeleBot/tableCreator.py b/Python/TeleBot/tableCreator.py index baf8903..b4f67df 100644 --- a/Python/TeleBot/tableCreator.py +++ b/Python/TeleBot/tableCreator.py @@ -4,12 +4,12 @@ import parser import config -connection = psycopg2.connect(host=config.hostname, dbname=config.databname, user=config.username, password=config.passw) +connection = psycopg2.connect( + host=config.hostname, dbname=config.databname, user=config.username, password=config.passw) cursor = connection.cursor() creat_qwery = """ create table Parser (id serial primary key, page_name varchar(100), price varchar(10), priceDis varchar(30), mark varchar(10), scr varchar(100))""" -# cursor.execute(creat_qwery) -# connection.commit() + try: cursor.execute(creat_qwery) connection.commit() @@ -19,7 +19,8 @@ except psycopg2.errors.DuplicateTable: for i in range(15): print(i) - url = 'https://amwine.ru' + parser.pictures[i].find('a').find('img').attrs['data-src'] + url = 'https://amwine.ru' + \ + parser.pictures[i].find('a').find('img').attrs['data-src'] filename = f"Python\\TeleBot\img\{i}.jpg" wget.download(url, filename) try: @@ -29,6 +30,4 @@ for i in range(15): except: connection.rollback() -cursor.close() -connection.close() \ No newline at end of file diff --git a/Python/TeleBot/test.py b/Python/TeleBot/test.py index 8d41f23..11d67b6 100644 --- a/Python/TeleBot/test.py +++ b/Python/TeleBot/test.py @@ -1,9 +1,14 @@ -def f(): - a = [1,2,3] - b = [3,4,5] - c = ["1", "2", "3"] - return a,b,c +import psycopg2 +import wget +import config -a,b,c = f() -print(a, b, c) \ No newline at end of file +connection = psycopg2.connect(host=config.hostname, dbname=config.databname, user=config.username, password=config.passw) +cursor = connection.cursor() + +def sel(a): + s_query = f"""SELECT * FROM public.parser WHERE id = {a}""" + print(cursor.execute(s_query)) + return cursor.fetchone() + +print(sel(10)) \ No newline at end of file diff --git a/Python/TeleBot/tgBot.py b/Python/TeleBot/tgBot.py index 868766b..f36db68 100644 --- a/Python/TeleBot/tgBot.py +++ b/Python/TeleBot/tgBot.py @@ -3,42 +3,45 @@ import psycopg2 import config from telebot import types import random +import FuncForBot -connection = psycopg2.connect(host=config.hostname, dbname=config.databname, user=config.username, password=config.passw) +connection = psycopg2.connect( + host=config.hostname, dbname=config.databname, user=config.username, password=config.passw) cursor = connection.cursor() try: - sel_query = """SELECT * FROM public.parser""" - cursor.execute(sel_query) + array = FuncForBot.sel(cursor, 'parser') except psycopg2.errors.UndefinedTable: connection.rollback() import tableCreator - sel_query = """SELECT * FROM public.parser""" - cursor.execute(sel_query) - -array = list(cursor.fetchall()) + array = FuncForBot.sel(cursor, 'parser') +finally: + FuncForBot.upd(connection, cursor, 'parser') + cursor.close() + connection.close() bot = telebot.TeleBot(config.api) -@bot.message_handler(commands=["start"]) + +@bot.message_handler(commands=["start"]) def start(m, res=False): - markup=types.ReplyKeyboardMarkup(resize_keyboard=True) - item1=types.KeyboardButton("Рандомное вино!") - item2=types.KeyboardButton("Повтори!") - markup.add(item1) - markup.add(item2) - bot.send_message(m.chat.id, '"Рандомное вино!" - для получения карточки товара\n "Повтори!" - для повторения сообщений.', reply_markup=markup) + markup = types.ReplyKeyboardMarkup(resize_keyboard=True) + item1 = types.KeyboardButton("Рандомное вино!") + item2 = types.KeyboardButton("Повтори!") + markup.add(item1) + markup.add(item2) + bot.send_message( + m.chat.id, '"Рандомное вино!" - для получения карточки товара\n "Повтори!" - для повторения сообщений.', reply_markup=markup) + @bot.message_handler(content_types=["text"]) def handle_text(message): - if(message.text.strip() == 'Рандомное вино!'): + if (message.text.strip() == 'Рандомное вино!'): i = random.choice(array) ans = f'Название: {i[1]}\nЦена: {i[2]}\nЦена без скидки: {i[3]}\nОценка: {i[4]}\n' bot.send_message(message.chat.id, ans) bot.send_photo(message.chat.id, open(i[5], 'rb')) - elif(message.text.strip() == 'Повтори!'): - bot.send_message(message.chat.id, 'Ввод: '+ message.text) - - + elif (message.text.strip() == 'Повтори!'): + bot.send_message(message.chat.id, 'Ввод: ' + message.text) + + bot.polling(none_stop=True, interval=0) - -