-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathapp_7.py
118 lines (99 loc) · 4.17 KB
/
app_7.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
import requests
from bs4 import BeautifulSoup
import time
import sqlite3
import pandas as pd
import asyncio
from telegram import Bot
import os
from dotenv import load_dotenv
load_dotenv()
# Configurações do bot do Telegram
TOKEN = os.getenv('TELEGRAM_TOKEN')
CHAT_ID = os.getenv('TELEGRAM_CHAT_ID')
bot = Bot(token=TOKEN)
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(db_name='iphone_prices.db'):
"""Cria uma conexão com o banco de dados SQLite."""
conn = sqlite3.connect(db_name)
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 INTEGER PRIMARY KEY AUTOINCREMENT,
product_name TEXT,
old_price INTEGER,
new_price INTEGER,
installment_price INTEGER,
timestamp TEXT
)
''')
conn.commit()
def save_to_database(conn, data):
"""Salva uma linha de dados no banco de dados SQLite usando pandas."""
df = pd.DataFrame([data]) # Converte o dicionário em um DataFrame de uma linha
df.to_sql('prices', conn, if_exists='append', index=False) # Salva no banco de dados
def get_max_price(conn):
"""Consulta o maior preço registrado até o momento."""
cursor = conn.cursor()
cursor.execute("SELECT MAX(new_price), timestamp FROM prices")
result = cursor.fetchone()
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 SQLite
save_to_database(conn, 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())