-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathapp_8_postgres.py
143 lines (122 loc) · 4.96 KB
/
app_8_postgres.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import requests
from bs4 import BeautifulSoup
import time
import pandas as pd
import asyncio
from telegram import Bot
import os
from dotenv import load_dotenv
import psycopg2
from sqlalchemy import create_engine
load_dotenv()
# Configurações do bot do Telegram
TOKEN = os.getenv('TELEGRAM_TOKEN')
CHAT_ID = os.getenv('TELEGRAM_CHAT_ID')
bot = Bot(token=TOKEN)
# Configurações do banco de dados PostgreSQL
POSTGRES_DB = os.getenv("POSTGRES_DB")
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
POSTGRES_HOST = os.getenv("POSTGRES_HOST")
POSTGRES_PORT = os.getenv("POSTGRES_PORT")
# Cria o engine do SQLAlchemy para o PostgreSQL
DATABASE_URL = f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DB}"
engine = create_engine(DATABASE_URL)
def fetch_page():
url = 'https://www.mercadolivre.com.br/apple-iphone-16-pro-1-tb-titnio-preto-distribuidor-autorizado/p/MLB1040287851#polycard_client=search-nordic&wid=MLB5054621110&sid=search&searchVariation=MLB1040287851&position=6&search_layout=stack&type=product&tracking_id=92c2ddf6-f70e-475b-b41e-fe2742459774'
response = requests.get(url)
return response.text
def parse_page(html):
soup = BeautifulSoup(html, 'html.parser')
product_name = soup.find('h1', class_='ui-pdp-title').get_text(strip=True)
prices = soup.find_all('span', class_='andes-money-amount__fraction')
old_price = int(prices[0].get_text(strip=True).replace('.', ''))
new_price = int(prices[1].get_text(strip=True).replace('.', ''))
installment_price = int(prices[2].get_text(strip=True).replace('.', ''))
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
return {
'product_name': product_name,
'old_price': old_price,
'new_price': new_price,
'installment_price': installment_price,
'timestamp': timestamp
}
def create_connection():
"""Cria uma conexão com o banco de dados PostgreSQL."""
conn = psycopg2.connect(
dbname=POSTGRES_DB,
user=POSTGRES_USER,
password=POSTGRES_PASSWORD,
host=POSTGRES_HOST,
port=POSTGRES_PORT
)
return conn
def setup_database(conn):
"""Cria a tabela de preços se ela não existir."""
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS prices (
id SERIAL PRIMARY KEY,
product_name TEXT,
old_price INTEGER,
new_price INTEGER,
installment_price INTEGER,
timestamp TIMESTAMP
)
''')
conn.commit()
cursor.close()
def save_to_database(data, table_name='prices'):
"""Salva uma linha de dados no banco de dados PostgreSQL usando pandas e SQLAlchemy."""
df = pd.DataFrame([data])
# Usa SQLAlchemy para salvar os dados no PostgreSQL
df.to_sql(table_name, engine, if_exists='append', index=False)
def get_max_price(conn):
"""Consulta o maior preço registrado até o momento com o timestamp correspondente."""
cursor = conn.cursor()
cursor.execute("""
SELECT new_price, timestamp
FROM prices
WHERE new_price = (SELECT MAX(new_price) FROM prices);
""")
result = cursor.fetchone()
cursor.close()
if result and result[0] is not None:
return result[0], result[1]
return None, None
async def send_telegram_message(text):
"""Envia uma mensagem para o Telegram."""
await bot.send_message(chat_id=CHAT_ID, text=text)
async def main():
conn = create_connection()
setup_database(conn)
try:
while True:
# Faz a requisição e parseia a página
page_content = fetch_page()
product_info = parse_page(page_content)
current_price = product_info['new_price']
# Obtém o maior preço já salvo
max_price, max_price_timestamp = get_max_price(conn)
# Comparação de preços
if max_price is None or current_price > max_price:
message = f"Novo preço maior detectado: {current_price}"
print(message)
await send_telegram_message(message)
max_price = current_price
max_price_timestamp = product_info['timestamp']
else:
message = f"O maior preço registrado é {max_price} em {max_price_timestamp}"
print(message)
await send_telegram_message(message)
# Salva os dados no banco de dados PostgreSQL
save_to_database(product_info)
print("Dados salvos no banco:", product_info)
# Aguarda 10 segundos antes da próxima execução
await asyncio.sleep(10)
except KeyboardInterrupt:
print("Parando a execução...")
finally:
conn.close()
# Executa o loop assíncrono
asyncio.run(main())