Dev.to WebDev 🛠 Dev 👁 1 📖 2 min read

Building a Scalable URL Monitoring System

Introduction Monitoring website content changes can be a crucial task for various use cases such as price tracking, job alerts, competitor website changes, and stock availability. In this article, we'll explore the tec

Introduction

Monitoring website content changes can be a crucial task for various use cases such as price tracking, job alerts, competitor website changes, and stock availability. In this article, we'll explore the technical aspects of building a scalable URL monitoring system.

Architecture Overview

A basic URL monitoring system consists of the following components:

  • URL Storage: A database to store the URLs to be monitored.
  • HTTP Client: A component responsible for sending HTTP requests to the stored URLs.
  • Change Detection: A module that compares the current website content with the previously stored content to detect changes.
  • Notification System: A component that sends notifications (e.g., email alerts) when content changes are detected.

URL Storage

To store URLs, we can use a relational database management system like MySQL or PostgreSQL. However, for a scalable system, a NoSQL database like MongoDB or Cassandra would be more suitable. We'll use MongoDB as an example.

from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017/')
db = client['url_monitor']
urls_collection = db['urls']

# Insert a new URL
url_doc = {'url': 'https://example.com', 'last_checked': None, 'content': None}
urls_collection.insert_one(url_doc)

HTTP Client

For sending HTTP requests, we can use the requests library in Python. To avoid overloading the target website, we should implement a retry mechanism with exponential backoff.

import requests
from time import sleep

def fetch_url(url):
    retries = 0
    while retries < 5:
        try:
            response = requests.get(url, timeout=10)
            return response.text
        except requests.exceptions.RequestException as e:
            retries += 1
            sleep(2 ** retries)
    return None

Change Detection

To detect content changes, we can use a simple string comparison. However, this approach may not work well for websites with dynamic content. A more advanced approach would be to use a library like BeautifulSoup to parse the HTML content and compare the relevant parts.

from bs4 import BeautifulSoup

def detect_changes(old_content, new_content):
    old_soup = BeautifulSoup(old_content, 'html.parser')
    new_soup = BeautifulSoup(new_content, 'html.parser')

    # Compare relevant parts of the HTML content
    if old_soup.find('div', {'class': 'price'}) != new_soup.find('div', {'class': 'price'}):
        return True
    return False

Notification System

For sending email alerts, we can use a library like smtplib. We should also implement a queue system to handle a large number of notifications.

import smtplib
from email.message import EmailMessage

def send_notification(to, subject, body):
    msg = EmailMessage()
    msg.set_content(body)
    msg['Subject'] = subject
    msg['From'] = '[email protected]'
    msg['To'] = to

    with smtplib.SMTP('your_smtp_server', 587) as smtp:
        smtp.starttls()
        smtp.login('[email protected]', 'your_password')
        smtp.send_message(msg)

Conclusion

Building a scalable URL monitoring system requires careful consideration of the architecture, URL storage, HTTP client, change detection, and notification system. By using the right technologies and implementing a robust system, you can create a reliable URL monitoring system. If you're interested in using a pre-built solution, you can try GetURLMonitor, which offers a free plan with 3 URLs and checks every 24 hours, as well as a Pro plan with unlimited URLs and hourly checks for just $5/month.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.