Skip to main content

Production-Ready MongoDB & Web Scraping Toolkit

Project description

Command Center - Production-Ready MongoDB & Web Scraping Toolkit

Version: 0.1.0 Author: Dharmik Vadher A comprehensive module for distributed web scraping with MongoDB state tracking.

Overview

Command Center is a powerful Python toolkit designed for building robust, scalable, and production-ready web scraping solutions. It provides a cohesive set of components to manage distributed workers, track the state of scraped items in MongoDB, handle HTTP requests with intelligence, and ensure data quality.

This module is born out of the need for a standardized, reliable framework for web scraping projects that can handle the complexities of distributed systems, data persistence, and error handling. It's built to be both powerful and easy to use, allowing developers to focus on the scraping logic rather than the underlying infrastructure.

Key Features

  • Distributed Worker Coordination: The Commander class allows for the seamless coordination of multiple scraping workers. It uses a heartbeat mechanism to track active workers and dynamically distributes the workload, ensuring efficient and balanced scraping.
  • MongoDB State Management: The Record class acts as an intelligent wrapper for MongoDB documents. It provides a simple API for tracking the state of each item (e.g., scraping, processing) with timestamps and metadata. A suite of query_helpers simplifies the process of finding items in specific states.
  • Resilient HTTP Client: The fetch() function is a sophisticated HTTP client built on top of cloudscraper. It includes features like caching to local files, response validation, automatic retries with backoff, and proxy support.
  • Intelligent Logging: CustomLogger provides an exception-aware logging solution with colored console output for improved readability. It automatically formats exceptions, making debugging faster and more efficient.
  • Data Validation and Sanitization: With validate_text() and clean_dict(), you can enforce data quality at the source. Validate responses against a set of rules and sanitize dictionaries to ensure clean, consistent data enters your database.

Installation

To use this module in your project, you can clone the repository and install the dependencies.

git clone <repository-url>
cd command-center
pip install -r requirements.txt

(Note: A requirements.txt file would need to be created for a real-world scenario, listing dependencies like pymongo, cloudscraper, etc.)

Usage

Here's a brief overview of how to use the core components of Command Center.

CustomLogger

The log object is a global instance of CustomLogger, ready to be used throughout your project.

from command_center import log

log.info("This is an informational message.")
log.warning("Something might be wrong.")

try:
    x = 1 / 0
except Exception as e:
    log.error(e) # Automatically formats the exception

Commander: Distributed Worker Coordination

Coordinate multiple scraper instances working on the same dataset.

from pymongo import MongoClient
from command_center import Commander

# Connect to your MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client['my_database']
workers_collection = db['workers']

# Initialize the Commander
commander = Commander(workers_collection)

# In your worker script
worker_ip = Commander.get_local_ip()
config = commander.start_worker('my_feed_name', worker_ip)

# Use the config to distribute work
# For example, in a pymongo query:
documents_to_process = db['data'].find({}).limit(config['threads_limit']).skip(config['threads_skip'])

Record: MongoDB Document Wrapper

The Record class simplifies interaction with your MongoDB documents and their state.

from pymongo import MongoClient
from command_center import Record

client = MongoClient('mongodb://localhost:27017/')
db = client['my_database']
data_collection = db['data']

# Fetch a document
doc = data_collection.find_one({'_id': 'some_id'})

if doc:
    # Wrap it in a Record object
    record = Record(doc, data_collection)

    # Access data using dot notation
    print(record.product_name)

    # Mark the 'scraping' state as successful with metadata
    record.mark_done('scraping', url="http://example.com", status_code=200)

    # Mark the 'processing' state as failed
    record.mark_fail('processing', error="Could not parse data")

    # Check the state
    if record.is_state_success('scraping'):
        print("Scraping was successful.")

Query Helpers

Use the query helpers to easily find documents based on their state.

from command_center import query_unprocessed, query_failed

# Find all documents that haven't been scraped yet
unprocessed_docs = data_collection.find(query_unprocessed('scraping'))

# Find all documents where processing failed
failed_docs = data_collection.find(query_failed('processing'))```

### fetch(): HTTP Client

A robust function for making HTTP requests.

```python
from command_center import fetch

url = "http://example.com"
save_path = "/path/to/cache"
filename = "example.html"

# Define validation rules
rules = {
    "required": ["</html>"],
    "forbidden": ["Access Denied"]
}

# Fetch the URL
response = fetch(url, save_dir=save_path, filename=filename, valid_rules=rules, max_retries=3)

if not isinstance(response, Exception):
    print("Successfully fetched and validated the page.")
    print(response.text)
else:
    print(f"Failed to fetch the page: {response}")

Utilities

validate_text()

Ensure the text you receive meets your criteria.

from command_center import validate_text

html_content = "<html><body><h1>Welcome</h1></body></html>"
rules = {"required": ["<h1>"], "must_start_with": "<html>"}

text, errors = validate_text(html_content, rules)

if errors:
    print(f"Validation failed: {errors}")
else:
    print("Validation successful.")

clean_dict()

Sanitize dictionaries before inserting them into your database.

from command_center import clean_dict

dirty_data = {
    "Product Name ": "  My Awesome Product\u2122 ",
    "Price_$_": 19.99,
    "Features": [" Feature 1 ", "Feature 2  "]
}

clean_data = clean_dict(dirty_data)
# {'product_name': 'My Awesome Product', 'price_': 19.99, 'features': ['Feature 1', 'Feature 2']}
print(clean_data)

Contributing

As this is a project from the Production Team, contributions are handled internally. Please refer to the team's development guidelines for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mycommandcenter-0.1.0.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mycommandcenter-0.1.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file mycommandcenter-0.1.0.tar.gz.

File metadata

  • Download URL: mycommandcenter-0.1.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for mycommandcenter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 11bcbce439003adc908bf46c1f19272e0641abbb0dc15277645d70bdfa8f20d0
MD5 709e42932e45c16468fb41660468d650
BLAKE2b-256 11af93048d3c4faac0508ba0266c9d413d1fa2b0c2b9ac38653f78de956f85a6

See more details on using hashes here.

File details

Details for the file mycommandcenter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mycommandcenter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9228a0b37a1d3a26787a2a30a9c9fc480b97fd1e4ce736fa09b4bc40858b7a85
MD5 f0f071d9159cb30e850eb24d16698d1f
BLAKE2b-256 cbebec7888f4c6c71ab5ef4dba59936d489c2348122762bd4c40422e548805b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page