pokemans/scraper/webhook.py
2024-03-05 17:03:40 -05:00

48 lines
1.6 KiB
Python

import requests
from app_log import LoggingManager
class WebhookNotifier:
def __init__(self, webhook_url, disable_webhook=False):
self.webhook_url = webhook_url
self.disable_webhook = disable_webhook
self.log_manager = LoggingManager("scraper.log")
def send_notification(self, post):
title = post.title
url = post.url
permalink = post.permalink
selftext = post.selftext
content = f"""
**New Deal!**
**Title:** {title}
**URL:** {url}
**Permalink:** https://old.reddit.com{permalink}
**Selftext:** {selftext}"""
if not self.disable_webhook:
self.log_manager.log(f"Sending notification to {self.webhook_url}")
try:
requests.post(self.webhook_url, data={"content": content})
except Exception as e:
self.log_manager.error(f"Failed to send notification: {e}")
def costco_notification(self, data):
for product in data:
sku = product.get("sku")
name = product.get("name")
price = product.get("price")
img_url = product.get("img_url")
product_link = product.get("product_link")
content = f"""
**Costco has a new item!**
**Name:** {name}
**Price:** {price}
**Link:** {product_link}
{img_url}"""
if not self.disable_webhook:
self.log_manager.log(f"Sending notification to {self.webhook_url}")
try:
requests.post(self.webhook_url, data={"content": content})
except Exception as e:
self.log_manager.error(f"Failed to send notification: {e}")