# andythefactory/newspaper4k architecture

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

- Interactive diagram: https://gitdiagram.com/andythefactory/newspaper4k
- Repository: https://github.com/andythefactory/newspaper4k

## Overview

##### Project type and purpose
- Newspaper4k is a Python library and CLI tool for scraping, parsing, and curating news articles from arbitrary websites and Google News.
- It provides a Command-Line Interface (CLI) entrypoint and a Python API (Article, Source, GoogleNewsSource classes).
- Core features: multilingual support, multi-threaded download, extractors for body/metadata/images/videos, NLP summarization/keywords, output formatters (JSON/CSV/Text).

##### File structure
- Top-level package “newspaper/” contains the core modules:
– __main__.py & cli.py: CLI entrypoint and argument parsing
– api.py, article.py, source.py, google_news.py: main API classes
– configuration.py: global settings (threads, language, timeouts)
– network.py: HTTP fetching
– cleaners.py & parsers.py: HTML cleanup and parsing
– extractors/: modular extractors for title, body, authors, images, metadata, video, categories
– nlp.py: keyword extraction and summarization
– outputformatters.py: formatting pipeline results
– languages/, resources/: language-specific stopwords, source lists, user-agents
– mthreading.py: threading helper for concurrent downloads
– utils/: support classes & helpers
- Project root contains CI configs (.github/, .travis.yml, ReadTheDocs), docs/, tests/, setup.py/pyproject.toml, requirements, license.

##### README
- Confirms the layering: CLI → API → build/download → parse/extract → NLP → output.
- Notes Google News integration via GNewsSource (requires external Gnews package).
- Highlights multi-language, multi-thread, plugin-style extractors.

##### Main components & interactions
a. User Interface layer
– User/Developer invokes either:
- CLI (python -m newspaper …) → newspaper/__main__.py → cli.py
- Python API (import newspaper; newspaper.article() or newspaper.build())
b. Configuration
– Read from Configuration class or CLI flags (language, threads, output format)
c. Download & Networking
– Source.download_articles() or Article.download() uses network.py (+ mthreading.py)
– Fetch HTML via requests/urllib (and optionally cloudscraper for JS-heavy sites)
d. Parsing & Cleaning
– cleaners.py strips unwanted tags; parsers.py uses lxml/BeautifulSoup to build DOM
e. Extraction pipeline
– extractors/*.py run in sequence on cleaned HTML:
- MetadataExtractor, TitleExtractor, AuthorsExtractor, PubDateExtractor, CategoriesExtractor
- ContentExtractor (article body), ImageExtractor, VideosExtractor
f. NLP & Summarization
– nlp.py tokenizes, extracts keywords, builds summary
– uses language resources (stopword lists in resources/text)
g. Output Formatting
– outputformatters.py serializes results to JSON/CSV/Text
h. External integrations
– GoogleNewsSource (in google_news.py) uses the Gnews package
– feedparser for RSS, PIL/Lxml for image parsing

##### Architectural patterns & design principles
- Layered architecture: UI → API → Services (download, parse, extract, NLP) → Data/resources → External
- Pipeline design: each step (cleaners → parsers → extractors → NLP → output) is a pluggable stage
- Configuration-driven: behavior (threads, language) is centralized in Configuration
- Concurrency: ThreadPoolExecutor for multi-threaded downloads
- Extensibility: extractors and output formatters can be extended or swapped out

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Interface Layer
    subgraph "User Interface" 
        direction TB
        CLI_MAIN["__main__.py"]:::entry
        CLI_PARSER["cli.py"]:::entry
        API["API Classes"]:::entry
        CLI_MAIN -->|invokes| CLI_PARSER
        CLI_PARSER -->|calls| API
    end

    %% Configuration Layer
    CONFIG["Configuration"]:::data

    API -->|reads settings| CONFIG
    CLI_PARSER -->|parses flags| CONFIG

    %% Download & Networking
    subgraph "Download & Networking" 
        direction TB
        NETWORK["network.py"]:::core
        THREADS["mthreading.py"]:::core
        NETWORK -->|HTTP fetch| THREADS
    end

    CONFIG -->|configures| NETWORK
    CONFIG -->|configures| THREADS

    %% Parsing Layer
    subgraph "Parsing Pipeline"
        direction TB
        CLEANER["cleaners.py"]:::core
        PARSER["parsers.py"]:::core
        NETWORK -->|raw HTML| CLEANER
        CLEANER -->|clean HTML| PARSER
    end

    %% Extraction Pipeline
    subgraph "Extraction Pipeline"
        direction TB
        META["MetadataExtractor"]:::core
        TITLE["TitleExtractor"]:::core
        AUTHORS["AuthorsExtractor"]:::core
        PUBDATE["PubDateExtractor"]:::core
        CATS["CategoriesExtractor"]:::core
        CONTENT["ContentExtractor"]:::core
        IMAGES["ImageExtractor"]:::core
        VIDEOS["VideosExtractor"]:::core

        PARSER --> META
        META --> TITLE
        TITLE --> AUTHORS
        AUTHORS --> PUBDATE
        PUBDATE --> CATS
        CATS --> CONTENT
        CONTENT --> IMAGES
        IMAGES --> VIDEOS
    end

    %% NLP & Summarization
    NLP["NLP Engine"]:::core
    VIDEOS --> NLP
    CONFIG -->|language resources| NLP

    %% Output Formatting
    OUTPUT["Output Formatter"]:::core
    NLP --> OUTPUT

    %% Data & Resources
    subgraph "Data & Resources"
        direction TB
        LANG["languages/*.py"]:::data
        STOPWORDS["stopwords-*.txt"]:::data
        MISC_RES["misc resources"]:::data
        VERSION["version.py"]:::data
        URLS["urls.py"]:::data
        EXCEPT["exceptions.py"]:::data
        CONFIG
        LANG --> STOPWORDS
        LANG --> MISC_RES
    end

    OUTPUT -->|writes| LANG

    %% External Services
    subgraph "External Services"
        direction TB
        INTERNET["Internet (Web)"]:::external
        GNEWS["Gnews Service"]:::external
    end

    NETWORK -->|fetches from| INTERNET
    META -->|optional RSS| INTERNET

    %% Google News Integration
    GNS["GoogleNewsSource"]:::core
    API -->|uses| GNS
    GNS -->|calls| GNEWS-.->|via Gnews package| GNEWS

    %% Click events
    click CLI_MAIN "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/__main__.py"
    click CLI_PARSER "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/cli.py"
    click API "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/api.py"
    click META "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/extractors/metadata_extractor.py"
    click TITLE "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/extractors/title_extractor.py"
    click AUTHORS "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/extractors/authors_extractor.py"
    click PUBDATE "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/extractors/pubdate_extractor.py"
    click CATS "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/extractors/categories_extractor.py"
    click CONTENT "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/extractors/content_extractor.py"
    click IMAGES "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/extractors/image_extractor.py"
    click VIDEOS "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/extractors/videos_extractor.py"
    click NETWORK "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/network.py"
    click THREADS "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/mthreading.py"
    click CLEANER "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/cleaners.py"
    click PARSER "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/parsers.py"
    click NLP "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/nlp.py"
    click STOPWORDS "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/resources/text/stopwords-*.txt"
    click OUTPUT "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/outputformatters.py"
    click LANG "https://github.com/andythefactory/newspaper4k/tree/master/newspaper/languages/"
    click MISC_RES "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/resources/misc/useragents.txt"
    click GNS "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/google_news.py"
    click CONFIG "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/configuration.py"
    click VERSION "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/version.py"
    click URLS "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/urls.py"
    click EXCEPT "https://github.com/andythefactory/newspaper4k/blob/master/newspaper/exceptions.py"

    %% Styles
    classDef entry fill:#ccffcc,stroke:#2f6627,color:#000;
    classDef core fill:#cce5ff,stroke:#2f4f76,color:#000;
    classDef data fill:#fff2cc,stroke:#7a4f01,color:#000;
    classDef external fill:#f2cccc,stroke:#8c1c13,color:#000;
```
