maybe last version

This commit is contained in:
Harutyun
2023-04-26 23:03:38 +03:00
parent 0c5e7acf4f
commit e4f0ef9af2
3 changed files with 19 additions and 25 deletions
+3 -4
View File
@@ -14,15 +14,14 @@ Image = soup.find_all(attrs={"class": "holder-img"})
connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4') connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4')
cursor = connection.cursor() cursor = connection.cursor()
create_q = '''CREATE TABLE Food create_q = '''CREATE TABLE Food (ID serial primary key, Name varchar(500), Price varchar(60), src varchar(100))'''
(ID serial primary key, Name varchar(500), Price varchar(60), src varchar(100))'''
cursor.execute(create_q) cursor.execute(create_q)
connection.commit() connection.commit()
#https://smartomato.ams3.cdn.digitaloceanspaces.com/uploads/media/photo/769221/dish_large__1.jpg #https://smartomato.ams3.cdn.digitaloceanspaces.com/uploads/media/photo/769221/dish_large__1.jpg
for j in range(15): for j in range(19):
url = Image[j].find('img').attrs['src'] url = Image[j].find('img').attrs['src']
tempf = f"C:\\Users\\Harutyun\\Desktop\\prog\\food{j}.jpg" tempf = f"img\\food{j}.jpg"
wget.download(url, tempf) wget.download(url, tempf)
insert_query = f'''INSERT into public.Food(Name, Price, src) values ('{Name[j].text}', '{Price[j].text}', '{tempf}') ''' insert_query = f'''INSERT into public.Food(Name, Price, src) values ('{Name[j].text}', '{Price[j].text}', '{tempf}') '''
cursor.execute(insert_query) cursor.execute(insert_query)
+4 -6
View File
@@ -3,21 +3,19 @@ import wget
from Задания.task1.Garanyan import parser from Задания.task1.Garanyan import parser
connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4')
password='Q1w2e3r4')
cursor = connection.cursor() cursor = connection.cursor()
create_q = '''CREATE TABLE Food create_q = '''CREATE TABLE Food (ID serial primary key, Name varchar(500), Price varchar(60), src varchar(100))'''
(ID serial primary key, Name varchar(500), Price varchar(60), src varchar(100))'''
try: try:
cursor.execute(create_q) cursor.execute(create_q)
connection.commit() connection.commit()
except psycopg2.errors.DuplicateTable: except psycopg2.errors.DuplicateTable:
print("s") print("duplicate error")
for j in range(15): for j in range(15):
url = parser.Image[j].find('img').attrs['src'] url = parser.Image[j].find('img').attrs['src']
tempf = f"C:\\Users\\Harutyun\\Desktop\\prog\\food{j}.jpg" tempf = f"img\\food{j}.jpg"
wget.download(url, tempf) wget.download(url, tempf)
try: try:
insert_query = f'''INSERT into public.Food(Name, Price, src) values ('{parser.Name[j].text}', '{parser.Price[j].text}', '{tempf}') ''' insert_query = f'''INSERT into public.Food(Name, Price, src) values ('{parser.Name[j].text}', '{parser.Price[j].text}', '{tempf}') '''
+12 -15
View File
@@ -3,8 +3,7 @@ import psycopg2
from telebot import types from telebot import types
import random import random
connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', connection = psycopg2.connect(host='localhost', dbname='dbdata', user='postgres', password='Q1w2e3r4')
password='Q1w2e3r4')
cursor = connection.cursor() cursor = connection.cursor()
try: try:
sel_query = """SELECT * FROM public.food""" sel_query = """SELECT * FROM public.food"""
@@ -16,32 +15,30 @@ except psycopg2.errors.UndefinedTable:
cursor.execute(sel_query) cursor.execute(sel_query)
array = list(cursor.fetchall()) array = list(cursor.fetchall())
bot = telebot.TeleBot('841097550:AAFc5MoFRivTEfv-gOSJctqH53NfYMTKpCc') bot = telebot.TeleBot('841097550:AAFc5MoFRivTEfv-gOSJctqH53NfYMTKpCc')
@bot.message_handler(commands=["start"]) @bot.message_handler(commands=["start"])
def start(m, res=False): def start(m, res=False):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True) markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton("Хот-доги и соусы!") item1 = types.KeyboardButton("Хот-доги и не только!")
item2 = types.KeyboardButton("Случайная фотография нашего товара!") item2 = types.KeyboardButton("Цена товара")
markup.add(item1) markup.add(item1)
markup.add(item2) markup.add(item2)
bot.send_message(m.chat.id, bot.send_message(m.chat.id,
'"Хот-доги и соусы!" - для получения случайного товара из нашей хотдожной\n' '"Хот-доги и не только!" - для получения случайного товара из нашей хотдожной\n'
' "Случайная фотография нашего товара!" - для вывода фотографий', ' "Цена товара" - для вывода цены', reply_markup=markup)
reply_markup=markup)
i = random.choice(array)
@bot.message_handler(content_types=["text"]) @bot.message_handler(content_types=["text"])
def handle_text(message): def handle_text(message):
if (message.text.strip() == 'Хот-доги и соусы!'): global i
if (message.text.strip() == 'Хот-доги и не только!'):
i = random.choice(array) i = random.choice(array)
ans = f'Название: {i[1]}\nЦена: {i[2]}\n' ans = f'Название: {i[1]}\n'
bot.send_message(message.chat.id, ans) bot.send_message(message.chat.id, ans)
bot.send_photo(message.chat.id, open(i[3], 'rb')) bot.send_photo(message.chat.id, open(i[3], 'rb'))
elif (message.text.strip() == 'Случайная фотография нашего товара!'): elif (message.text.strip() == 'Цена товара'):
i = random.choice(array) ans = f'Цена: {i[2]}\n'
bot.send_photo(message.chat.id, open(i[3], 'rb')) bot.send_message(message.chat.id, ans)
bot.polling(none_stop=True, interval=0) bot.polling(none_stop=True, interval=0)