# kenser/pornhubbot architecture

> Architecture diagram of the GitHub repository kenser/pornhubbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kenser/pornhubbot
- Repository: https://github.com/kenser/pornhubbot

## Overview

#### Purpose and Entry Point
This is a Python 2.7 Scrapy crawler for collecting PornHub video metadata: title, source-page URL, thumbnail URL, duration, and 480p MP4 URL. It persists results to MongoDB. Runtime starts through `PornHub/quickstart.py`, with Scrapy project configuration in `PornHub/scrapy.cfg`.

#### Crawl Orchestration
The primary spider initiates concurrent requests across several PornHub categories, follows paginated listing pages, and schedules discovered video detail pages. The crawl logic is centered in `PornHub/PornHub/spiders/pornHubSpider.py`; project-level crawler behavior is configured by `PornHub/PornHub/settings.py`.

#### Request Identity and Access Boundary
Requests receive randomized cookie and User-Agent identity data to vary outbound traffic characteristics. User-Agent inventory resides in `PornHub/PornHub/user_agents.py`; request/response interception and header assignment are expected through `PornHub/PornHub/middlewares.py`. This is the boundary between Scrapy’s scheduler/downloader and the external PornHub site.

#### Extraction and Domain Model
The spider converts listing/detail-page HTML into structured crawl records containing the fields described in the README. The Scrapy item schema is defined in `PornHub/PornHub/items.py`, while PornHub-specific classification or record types are separated in `PornHub/PornHub/pornhub_type.py`.

#### Processing and Persistence
Scrapy emits extracted items through its pipeline stage. `PornHub/PornHub/pipelines.py` is the persistence boundary and is responsible for writing results to MongoDB, including the `PhRes` collection/table described by the project. MongoDB is the material external dependency and must be installed and running before execution.

#### Runtime Composition
`PornHub/quickstart.py` launches the crawler; `PornHub/PornHub/settings.py` controls concurrency, delays, pipelines, middleware, and related Scrapy runtime behavior. The package root `PornHub/PornHub/__init__.py` establishes the importable Scrapy project, and `PornHub/PornHub/spiders/__init__.py` registers the spider package.

#### Primary Flow
Startup script → Scrapy engine/settings → spider category requests → downloader middleware applies request identity → category and pagination parsing → video detail extraction → item creation → pipeline → MongoDB `PhRes` storage. The system’s throughput is primarily governed by Scrapy concurrency/network settings and MongoDB write capacity.

Main entry points: [`PornHub/PornHub/spiders/pornHubSpider.py`](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/spiders/pornHubSpider.py) (PornHub spider), [`PornHub/PornHub/settings.py`](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/settings.py) (Scrapy settings), [`PornHub/PornHub/middlewares.py`](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/middlewares.py) (Downloader middleware), [`PornHub/PornHub/pipelines.py`](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/pipelines.py) (Mongo pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Scrapy Runtime"]
  node_quickstart["Crawler launcher<br/>Python entry point<br/>[quickstart.py]"]
  node_settings["Scrapy settings<br/>runtime configuration<br/>[settings.py]"]
  node_project_package["Project package<br/>Python package<br/>[__init__.py]"]
  node_scrapy_engine{{"Scrapy engine<br/>crawler runtime"}}
end

subgraph group_crawl["Crawl &amp; Extraction"]
  node_spider_package["Spider package<br/>Python package<br/>[__init__.py]"]
  node_pornhub_spider{{"PornHub spider<br/>Scrapy spider<br/>[pornHubSpider.py]"}}
  node_listing_pages["Category &amp; listing pages<br/>external HTML"]
  node_detail_pages["Video detail pages<br/>external HTML"]
  node_item_schema["Video item schema<br/>Scrapy item<br/>[items.py]"]
  node_pornhub_types["PornHub record types<br/>domain model<br/>[pornhub_type.py]"]
end

subgraph group_access["Request Access Boundary"]
  node_request_middleware["Downloader middleware<br/>Scrapy middleware<br/>[middlewares.py]"]
  node_user_agents["User-Agent inventory<br/>identity data<br/>[user_agents.py]"]
  node_pornhub_site{{"PornHub site<br/>external service"}}
end

subgraph group_storage["Persistence"]
  node_pipeline{{"Mongo pipeline<br/>Scrapy item pipeline<br/>[pipelines.py]"}}
  node_mongodb[("MongoDB PhRes<br/>database")]
end

node_quickstart -->|"launches"| node_scrapy_engine
node_quickstart -->|"loads"| node_settings
node_settings -->|"configures"| node_scrapy_engine
node_project_package -->|"provides project"| node_scrapy_engine
node_spider_package -->|"contains"| node_pornhub_spider
node_scrapy_engine -->|"runs"| node_pornhub_spider
node_pornhub_spider -->|"requests categories and pages"| node_listing_pages
node_listing_pages -->|"video links and pagination"| node_pornhub_spider
node_pornhub_spider -->|"schedules video pages"| node_detail_pages
node_detail_pages -->|"video metadata HTML"| node_pornhub_spider
node_scrapy_engine -->|"outbound requests"| node_request_middleware
node_request_middleware -->|"selects identity"| node_user_agents
node_request_middleware -->|"applies headers and cookies"| node_pornhub_site
node_pornhub_site -->|"serves"| node_listing_pages
node_pornhub_site -->|"serves"| node_detail_pages
node_pornhub_spider -->|"creates items"| node_item_schema
node_pornhub_spider -->|"uses"| node_pornhub_types
node_item_schema -->|"emits items"| node_pipeline
node_settings -->|"enables"| node_pipeline
node_pipeline -->|"writes PhRes records"| node_mongodb

click node_quickstart "https://github.com/kenser/pornhubbot/blob/master/PornHub/quickstart.py"
click node_settings "https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/settings.py"
click node_project_package "https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/__init__.py"
click node_spider_package "https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/spiders/__init__.py"
click node_pornhub_spider "https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/spiders/pornHubSpider.py"
click node_request_middleware "https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/middlewares.py"
click node_user_agents "https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/user_agents.py"
click node_item_schema "https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/items.py"
click node_pornhub_types "https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/pornhub_type.py"
click node_pipeline "https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/pipelines.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_quickstart,node_settings,node_project_package,node_scrapy_engine toneBlue
class node_spider_package,node_pornhub_spider,node_listing_pages,node_detail_pages,node_item_schema,node_pornhub_types toneAmber
class node_request_middleware,node_user_agents,node_pornhub_site toneMint
class node_pipeline,node_mongodb toneRose
```

## Components

### Scrapy Runtime

- [Crawler launcher](https://github.com/kenser/pornhubbot/blob/master/PornHub/quickstart.py): Python entry point · Starts the Scrapy crawler. · `PornHub/quickstart.py`
- [Scrapy settings](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/settings.py): runtime configuration · Configures concurrency, middleware, and pipelines. · `PornHub/PornHub/settings.py`
- [Project package](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/__init__.py): Python package · `PornHub/PornHub/__init__.py`
- **Scrapy engine**: crawler runtime · Schedules requests and routes responses and items.

### Crawl & Extraction

- [Spider package](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/spiders/__init__.py): Python package · `PornHub/PornHub/spiders/__init__.py`
- [PornHub spider](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/spiders/pornHubSpider.py): Scrapy spider · Requests categories and pagination, then schedules video detail pages. · `PornHub/PornHub/spiders/pornHubSpider.py`
- **Category & listing pages**: external HTML · Provide video links and pagination.
- **Video detail pages**: external HTML · Provide video metadata and the 480p MP4 URL.
- [Video item schema](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/items.py): Scrapy item · Defines title, source URL, thumbnail, duration, and MP4 fields. · `PornHub/PornHub/items.py`
- [PornHub record types](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/pornhub_type.py): domain model · `PornHub/PornHub/pornhub_type.py`

### Request Access Boundary

- [Downloader middleware](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/middlewares.py): Scrapy middleware · Intercepts requests and assigns headers. · `PornHub/PornHub/middlewares.py`
- [User-Agent inventory](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/user_agents.py): identity data · Supplies randomized User-Agent values. · `PornHub/PornHub/user_agents.py`
- **PornHub site**: external service

### Persistence

- [Mongo pipeline](https://github.com/kenser/pornhubbot/blob/master/PornHub/PornHub/pipelines.py): Scrapy item pipeline · Writes extracted records to the PhRes collection. · `PornHub/PornHub/pipelines.py`
- **MongoDB PhRes**: database · Persistent crawl-result storage.

## Connections

- Crawler launcher → Scrapy engine: launches
- Crawler launcher → Scrapy settings: loads
- Scrapy settings → Scrapy engine: configures
- Project package → Scrapy engine: provides project
- Spider package → PornHub spider: contains
- Scrapy engine → PornHub spider: runs
- PornHub spider → Category & listing pages: requests categories and pages
- Category & listing pages → PornHub spider: video links and pagination
- PornHub spider → Video detail pages: schedules video pages
- Video detail pages → PornHub spider: video metadata HTML
- Scrapy engine → Downloader middleware: outbound requests
- Downloader middleware → User-Agent inventory: selects identity
- Downloader middleware → PornHub site: applies headers and cookies
- PornHub site → Category & listing pages: serves
- PornHub site → Video detail pages: serves
- PornHub spider → Video item schema: creates items
- PornHub spider → PornHub record types: uses
- Video item schema → Mongo pipeline: emits items
- Scrapy settings → Mongo pipeline: enables
- Mongo pipeline → MongoDB PhRes: writes PhRes records
