pokemans/scraper/webhook.py
2024-03-05 21:52:36 -05:00

47 lines
1.5 KiB
Python

import requests
from app_log import LoggingManager
from models import Product, Post
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, product : Product):
name = product.name
price = product.price
product_link = product.product_link
img_url = product.img_url
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}")