# dipesh-ghimire/auto-nepse-stock-scrapers architecture

> Architecture diagram of the GitHub repository dipesh-ghimire/auto-nepse-stock-scrapers, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dipesh-ghimire/auto-nepse-stock-scrapers
- Repository: https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers

## Overview

##### Project type and purpose
- From the file tree and README.md, this is a monolithic Django‐based full-stack web application with:
- A standard Django MVC structure (models, views, templates)
- Static assets (CSS/JS) for a Bootstrap 5-powered responsive frontend
- Celery for scheduled/async scraping tasks
- Selenium-based scrapers (“merolagani_scraper.py”, etc.) to fetch live stock prices from external sites
- A time-series forecasting component (ARIMA) for future price prediction

##### File structure
- Top-level “stockmarket” folder: Django project settings, ASGI/WSGI entrypoints, Celery setup
- “stocks” app: models.py (Company, PriceHistory, CompanyNews, Floorsheet), views, forms, templates, URLs, utility functions, and Celery tasks
- “stocks/scrapers”: three site-specific scraper implementations inheriting from base_scraper.py
- “stocks/tasks.py”: Celery tasks that schedule and invoke the scrapers, save results to the database, trigger forecasting
- Configuration files: requirements.txt, manage.py, .gitignore

##### README for additional insights
- Features list confirms CRUD for companies, news, price history; real-time scraping; ARIMA forecasting
- Mentions RESTful-style URL patterns and responsive frontend

##### High-level architecture components
a. Frontend (Browser)
– Static assets (CSS/JS) + Django templates served via HTTP
– User interactions (forms, list/detail views)
b. Backend Web Server
– Django application (ASGI/WSGI) handling HTTP requests
– URL routing → views → ORM → database
c. Database
– Relational datastore (e.g., PostgreSQL or SQLite) for Company, PriceHistory, CompanyNews, etc.
d. Celery Infrastructure
– Message broker (RabbitMQ or Redis) – not in file tree but implied by celery.py
– Celery beat scheduler (celerybeat-schedule files)
– Celery worker processes (execute tasks.py)
e. Scraper Subsystem
– Selenium-based scrapers that fetch live data from external services (Merolagani, Nepstock, Sharesansar)
– BaseScraper abstraction + site-specific implementations
f. Forecasting Module
– ARIMA model invoked within tasks or views for time-series prediction
g. External Services
– Third-party websites (Merolagani, Nepstock, Sharesansar) as data sources

##### Relationships and interactions
- Browser → HTTP → Django (front controller) → ORM → Database
- Celery beat → schedules tasks → Celery broker → Celery worker → scraper code → external sites → parsed data → back to Database
- User triggers manual scrape via Django view → Django enqueues Celery task → same flow
- Forecasting: Celery task or view loads historical prices from Database → runs ARIMA → writes predictions back or returns data to user

##### Architectural patterns & design principles
- MVC (Django’s MTV) for clear separation of data models, templates, and request handling
- Asynchronous task processing with Celery for non-blocking scrapes and long-running forecasts
- Modular scrapers via inheritance (base_scraper.py) to support multiple sources
- RESTful URL design for CRUD operations
- Single repository monolith, but logically separated into web/UI vs background processing

##### Step-by-step for the principal software engineer
3. Celery architecture: beat scheduler writing to broker, workers reading tasks.
– HTTP GET/POST from Browser → Django
– ORM read/write between Django → Database
– AMQP or Redis messages between Scheduler/Worker
– Data fetch from External sites → Worker
– Blue = user-facing flow,
– Orange = async/background flow,
– Green = data storage,
– Grey = external services

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (Browser)" 
        direction TB
        CSS["CSS\nstockmarket/static/css/style.css"]:::blue
        JS["JS\nstockmarket/static/js/style.js"]:::blue
        Templates["Templates\nstockmarket/stocks/templates/"]:::blue
        Browser["Web Browser"]:::blue
    end

    %% Backend Web Server
    subgraph "Django App" 
        direction TB
        subgraph "Project Entry & Configuration"
            manage["manage.py"]:::blue
            asgi["asgi.py"]:::blue
            wsgi["wsgi.py"]:::blue
            settings["settings.py"]:::blue
            rooturls["urls.py"]:::blue
        end
        subgraph "Stocks App (stocks)"
            models["Models\nstocks/models.py"]:::blue
            views["URLs & Views\nstocks/views.py"]:::blue
            urls["URL Routing\nstocks/urls.py"]:::blue
            forms["Forms\nstocks/forms.py"]:::blue
            admin["Admin\nstocks/admin.py"]:::blue
            tasks_node["Tasks\nstocks/tasks.py"]:::blue
        end
    end

    %% Database
    Database["Database (PostgreSQL/SQLite)"]:::green

    %% Celery Infrastructure
    subgraph "Celery Subsystem"
        direction TB
        celerycfg["Celery Config\nstockmarket/stockmarket/celery.py"]:::orange
        Scheduler["Beat Scheduler"]:::orange
        Broker["Broker\n(Redis/RabbitMQ)"]:::orange
        Worker["Worker Processes"]:::orange
        subgraph "Scraper Subsystem"
            direction TB
            base["BaseScraper\nstocks/scrapers/base_scraper.py"]:::orange
            mero["Merolagani Scraper\nstocks/scrapers/merolagani_scraper.py"]:::orange
            nep["Nepstock Scraper\nstocks/scrapers/nepstock_scraper.py"]:::orange
            share["Sharesansar Scraper\nstocks/scrapers/sharesansar_scraper.py"]:::orange
        end
        Forecast["Forecasting (ARIMA)\nstocks/utility.py"]:::orange
    end

    %% External Services
    subgraph "External Services"
        direction TB
        ExtMero["Merolagani Site"]:::grey
        ExtNep["Nepstock Site"]:::grey
        ExtShare["Sharesansar Site"]:::grey
    end

    %% Connections - User Flow
    CSS --> Browser
    JS --> Browser
    Browser -->|"HTTP GET/POST"| views
    views -->|"ORM Read/Write"| Database
    Database -->|"Data for Rendering"| views

    %% Connections - Async Flow
    celerycfg --> Scheduler
    Scheduler -->|"AMQP"| Broker
    views -->|"Enqueue Task"| Broker
    Broker -->|"AMQP"| Worker
    Worker --> tasks_node
    tasks_node --> base
    base --> mero
    base --> nep
    base --> share
    mero -->|"HTTP"| ExtMero
    nep -->|"HTTP"| ExtNep
    share -->|"HTTP"| ExtShare
    mero -->|"Parsed Data"| Database
    nep -->|"Parsed Data"| Database
    share -->|"Parsed Data"| Database
    Worker --> Forecast
    Forecast -->|"Predictions"| Database

    %% Click Events
    click CSS "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/static/css/style.css"
    click JS "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/static/js/style.js"
    click Templates "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/tree/main/stockmarket/stocks/templates/"
    click manage "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/manage.py"
    click asgi "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stockmarket/asgi.py"
    click wsgi "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stockmarket/wsgi.py"
    click settings "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stockmarket/settings.py"
    click rooturls "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stockmarket/urls.py"
    click models "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/models.py"
    click views "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/views.py"
    click urls "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/urls.py"
    click forms "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/forms.py"
    click admin "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/admin.py"
    click tasks_node "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/tasks.py"
    click celerycfg "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stockmarket/celery.py"
    click base "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/scrapers/base_scraper.py"
    click mero "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/scrapers/merolagani_scraper.py"
    click nep "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/scrapers/nepstock_scraper.py"
    click share "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/scrapers/sharesansar_scraper.py"
    click Forecast "https://github.com/dipesh-ghimire/auto-nepse-stock-scrapers/blob/main/stockmarket/stocks/utility.py"

    %% Styles
    classDef blue fill:#E0F7FA,stroke:#0288D1,color:#01579B;
    classDef orange fill:#FFE0B2,stroke:#FB8C00,color:#E65100;
    classDef green fill:#C8E6C9,stroke:#388E3C,color:#1B5E20;
    classDef grey fill:#ECEFF1,stroke:#90A4AE,color:#37474F;
```
