# sukhcha-in/dart_web_scraper architecture

> Architecture diagram of the GitHub repository sukhcha-in/dart_web_scraper, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sukhcha-in/dart_web_scraper
- Repository: https://github.com/sukhcha-in/dart_web_scraper

## Overview

##### Project Type and Purpose
- This is a Dart library providing a configurable and reusable web scraper. It allows users to scrape web pages or APIs through a key/value based configuration (configMap) and built‐in parsing logic.
- The library supports multiple scraping features (HTTP requests, parsing HTML through CSS selectors, JSON parsing, data cleaning, concurrency, etc.), so the design must emphasize its modular and configuration-driven nature.

##### Main Components
a. Library API / Entry Points:
- The primary entry is the WebScraper class (as shown in lib/dart_web_scraper.dart and related files) which exposes the scrape() method.
- That consumers import and use this class.

b. Configuration Management:
- ConfigMap structure which holds:
– Domain keys (e.g., "quotes.toscrape.com", "example.com")
– Under each domain:
▪ A list of Config objects.
▪ Each Config contains two main aspects:
- A Map of Parsers keyed by urltarget name.
- A list of UrlTargets.
- This configuration component is critical as it directs how different parts of the scraping process are executed.

c. URL Targeting:
- The UrlTarget component (and the associated UrlCleaner) determines which portions of a website or API should be scraped.
- How UrlTargets direct traffic to specific parsing logic based on URL matching ('where' list).

d. Parsing Components:
- Detail the various Parser types (e.g., element, attribute, text, table, json, etc.). Each parser is defined in its own module under lib/dart_web_scraper/parsers.
– Indicate that parsers are the heart of the extraction logic.
– Include connections that show how a Parser is triggered as part of a UrlTarget’s execution.
- Incorporate the Optional logic modules (under lib/dart_web_scraper/optional) which provide additional operations such as appending, cropping, regex matching, and more.
- Mention data injection via the <slot> mechanism for dynamic configuration of selectors and URLs.

e. Common Utilities and Models:
- Core shared functionalities and data types are maintained in lib/common.
– Models: Represent different data structures (e.g., ConfigModel, UrltargetModel, ParserModel, etc.).
– Enums: Defined in common/enums.dart to standardize parser types and other constants.
– Utility functions: For cookie handling, debug logging, HTTP processing, random number utilities, etc.
- These utilities support the consistency and reusability of the core scraping logic.

f. External Interactions:
- Data cleaning and post-processing: Represent how data is cleaned before it becomes the final output.

##### Relationships and Data Flow
- From WebScraper, illustrate a flow branching into two separate processes:
– If the URL matches one of the UrlTargets defined in the config: direct to the appropriate parsing workflow.
- That each Config object organizes:
– A set of UrlTargets that filter URLs.
– A mapping to Parsers that extract data from the fetched HTML/JSON.
– Data goes from WebScraper to URL targeting.
– Then data flows into the parsers.
– Utilities are used as supporting modules throughout the parsing and data refining stages.

##### Architectural Patterns & Design Principles
- Modular design: Each directory (config, parsers, optional functions, and common utilities) is a self-contained module.
- Separation of concerns: Distinct components for scraping, parsing, and configuration (e.g., the clear separation between Config, UrlTarget, and Parser classes).
- Extensibility: The design uses plug-in style optional methods (e.g., Optional.any, Optional.http) that allow for dynamic behaviors without modifying the core.

## Diagram (Mermaid)

```mermaid
flowchart TD
    CLIENT["Client Code"]:::client

    subgraph "Public API"
        WS["WebScraper"] 
    end

    CLIENT -->|"calls"| WS

    subgraph "Configuration Layer"
        CONFIG["Configuration Management"]
        URLT["URL Target"]
    end

    WS -->|"usesConfig"| CONFIG
    CONFIG -->|"definesTargets"| URLT

    subgraph "Parser Engine"
        direction TB
        subgraph "Built-in Parsers"
            EP["Element Parser"]:::parser
            AP["Attribute Parser"]:::parser
            TP["Text Parser"]:::parser
            TBL["Table Parser"]:::parser
            JP["JSON Parser"]:::parser
            J5P["JSON5Decode Parser"]:::parser
            JTP["JSON Table Parser"]:::parser
            JLP["JSONLD Parser"]:::parser
            HTP["HTTP Parser"]:::parser
        end
        URLT -->|"triggers"| EP
        URLT -->|"triggers"| AP
        URLT -->|"triggers"| TP
        URLT -->|"triggers"| TBL
        URLT -->|"triggers"| JP
        URLT -->|"triggers"| J5P
        URLT -->|"triggers"| JTP
        URLT -->|"triggers"| JLP
        URLT -->|"triggers"| HTP
    end

    subgraph "Optional Modules"
        direction TB
        APP["Append"]:::optional
        APL["Apply"]:::optional
        CEND["Crop End"]:::optional
        CSTR["Crop Start"]:::optional
        MAT["Match"]:::optional
        NTH["Nth"]:::optional
        PRE["Prepend"]:::optional
        RMT["Regex Match"]:::optional
        RRT["Regex Replace"]:::optional
        REP["Replace"]:::optional
        SPB["Split By"]:::optional
        EXP["Exports"]:::optional
    end

    %% Optional enhancements linked from various parsers to optional modules
    HTP -.->|"optEnhance"| APP
    EP -.->|"optEnhance"| APL
    TP -.->|"optEnhance"| CEND
    JP -.->|"optEnhance"| CSTR
    TBL -.->|"optEnhance"| MAT
    AP -.->|"optEnhance"| NTH
    AP -.->|"optEnhance"| PRE
    AP -.->|"optEnhance"| RMT
    TP -.->|"optEnhance"| RRT
    TP -.->|"optEnhance"| REP
    TP -.->|"optEnhance"| SPB
    TP -.->|"optEnhance"| EXP

    %% External HTTP Module path
    HTTPM["HTTP Module"]:::http
    WS -->|"performs"| HTTPM
    HTTPM -->|"fetches"| URLT

    %% Common Utilities support across modules
    UTIL["Common Utilities"]:::util
    UTIL ---|"supports"| CONFIG
    UTIL ---|"supports"| URLT
    UTIL ---|"supports"| EP

    %% Click Events for components
    click WS "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/web_scraper.dart"
    click WS "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper.dart"
    click CONFIG "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/common/models/config_model.dart"
    click URLT "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/common/models/urltarget_model.dart"
    click EP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/parsers/element_parser.dart"
    click AP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/parsers/attribute_parser.dart"
    click TP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/parsers/text_parser.dart"
    click TBL "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/parsers/table_parser.dart"
    click JP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/parsers/json_parser.dart"
    click J5P "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/parsers/json5decode_parser.dart"
    click JTP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/parsers/json_table_parser.dart"
    click JLP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/parsers/jsonld_parser.dart"
    click HTP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/parsers/http_parser.dart"
    click APP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/append.dart"
    click APL "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/apply.dart"
    click CEND "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/crop_end.dart"
    click CSTR "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/crop_start.dart"
    click MAT "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/match.dart"
    click NTH "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/nth.dart"
    click PRE "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/prepend.dart"
    click RMT "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/regex_match.dart"
    click RRT "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/regex_replace.dart"
    click REP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/replace.dart"
    click SPB "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/split_by.dart"
    click EXP "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/dart_web_scraper/optional/exports.dart"
    click HTTPM "https://github.com/sukhcha-in/dart_web_scraper/blob/main/lib/common/utils/http.dart"

    %% Styles
    classDef client fill:#cce,stroke:#333,stroke-width:2px;
    classDef api fill:#f9f,stroke:#333,stroke-width:2px;
    classDef config fill:#bbf,stroke:#333,stroke-width:2px;
    classDef parser fill:#ff9,stroke:#333,stroke-width:2px;
    classDef optional fill:#cfc,stroke:#333,stroke-width:2px;
    classDef http fill:#fcc,stroke:#333,stroke-width:2px;
    classDef util fill:#def,stroke:#333,stroke-width:2px;

    class WS api;
    class CONFIG config;
    class URLT config;
    class EP,AP,TP,TBL,JP,J5P,JTP,JLP,HTP parser;
    class APP,APL,CEND,CSTR,MAT,NTH,PRE,RMT,RRT,REP,SPB,EXP optional;
    class HTTPM http;
    class UTIL util;
```
