Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 51 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.12"
- run: python -m pip install -e ".[dev]"
Expand All @@ -32,14 +32,14 @@ jobs:
python: "3.12"
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: ${{ matrix.python }}
- run: python -m pip install -e ".[dev]"
- name: Regressions, source archive and coverage
run: python -m pytest tests -m "not integration" --cov=meta_ads_collector --cov-branch --cov-report=xml --junitxml=ci-results/unit.xml
- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
if: always()
with:
name: tests-${{ matrix.os }}-${{ matrix.python }}
Expand All @@ -49,8 +49,8 @@ jobs:
distribution:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.12"
- run: python -m pip install -e ".[dev]"
Expand All @@ -60,34 +60,57 @@ jobs:
run: python scripts/check_distribution.py
- name: Exact minimum dependency regression suite
run: python scripts/check_distribution.py --minimum
- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
with:
name: release-distributions
path: dist/
- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
if: always()
with:
name: distribution-tests
path: ci-results/
live-proxy:
needs: [distribution]
runs-on: macos-latest
timeout-minutes: 10
env:
METAADS_CI_PROXY: ${{ secrets.METAADS_CI_PROXY }}
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.12"
- run: python -m pip install packaging
- uses: actions/download-artifact@v8
with:
name: release-distributions
path: dist/
- name: Actual Meta SOCKS modes with matched family sessions and safe metadata
run: python scripts/check_distribution.py --live --proxy-diagnostics
- uses: actions/upload-artifact@v7
if: always()
with:
name: live-proxy-tests
path: ci-results/
live-connectivity:
needs: [quality, test, distribution]
runs-on: macos-latest
timeout-minutes: 5
env:
METAADS_CI_PROXY: ${{ secrets.METAADS_CI_PROXY }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.12"
- run: python -m pip install packaging
- uses: actions/download-artifact@v4
- uses: actions/download-artifact@v8
with:
name: release-distributions
path: dist/
- name: Bounded actual Meta connectivity and field check
run: python scripts/check_distribution.py --live --preflight
- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
if: always()
with:
name: live-connectivity-tests
Expand All @@ -103,14 +126,14 @@ jobs:
os: windows-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: ${{ matrix.python }}
- run: python -m pip install -e ".[dev,mcp]"
- name: MCP contracts, storage, jobs, recovery and actual stdio protocol
run: python -m pytest mcp/tests -m "not mcp_live" --cov=meta_ads_collector_mcp --cov-branch --junitxml=mcp/test-results/unit.xml
- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
if: always()
with:
name: mcp-${{ matrix.os }}-${{ matrix.python }}
Expand All @@ -119,17 +142,17 @@ jobs:
needs: [distribution, mcp-tests]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.12"
- uses: actions/download-artifact@v4
- uses: actions/download-artifact@v8
with:
name: release-distributions
path: dist/
- name: Installed optional extra outside checkout
run: python mcp/check_distribution.py
- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
if: always()
with:
name: mcp-wheel-tests
Expand All @@ -141,17 +164,17 @@ jobs:
env:
METAADS_CI_PROXY: ${{ secrets.METAADS_CI_PROXY }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.12"
- uses: actions/download-artifact@v4
- uses: actions/download-artifact@v8
with:
name: release-distributions
path: dist/
- name: Actual Meta through installed MCP extra, including worker crash recovery
run: python mcp/check_distribution.py --live
- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
if: always()
with:
name: mcp-live-tests
Expand All @@ -163,18 +186,18 @@ jobs:
env:
METAADS_CI_PROXY: ${{ secrets.METAADS_CI_PROXY }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.12"
- run: python -m pip install packaging
- uses: actions/download-artifact@v4
- uses: actions/download-artifact@v8
with:
name: release-distributions
path: dist/
- name: Full actual Meta integration suite
run: python scripts/check_distribution.py --live
- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
if: always()
with:
name: live-meta-tests
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.12"
- name: Install documentation tools
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
contents: read
id-token: write
steps:
- uses: actions/download-artifact@v4
- uses: actions/download-artifact@v8
with:
name: release-distributions
path: dist/
Expand All @@ -31,8 +31,8 @@ jobs:
env:
METAADS_CI_PROXY: ${{ secrets.METAADS_CI_PROXY }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.12"
- run: python -m pip install packaging
Expand All @@ -47,7 +47,7 @@ jobs:
run: |
python mcp/check_distribution.py --dist-dir published-dist
python mcp/check_distribution.py --dist-dir published-dist --live
- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
if: always()
with:
name: published-package-tests
Expand Down
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,46 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.7.0] - 2026-10-22

### Added
- `MetaAdsClient.get_ad_insights()` / `AsyncMetaAdsClient.get_ad_insights()`: the Ad Library "See ad details"
query (`AdLibraryV3AdDetailsQuery`). `get_ad_details()` and `enrich_ad()` now attach its payload under
`ad_details`, so enriched ads carry EU/UK/BR transparency (age, gender and location audience, total reach,
per-country age/gender reach breakdown), US political demographic and regional insights, payer/beneficiary
data, advertiser page info (verification, category, Instagram handle and followers, about text, cover photo),
page spend by disclaimer, verified-voice context and violation types.
- New normalized `Ad` fields: `page_categories`, `impressions_text`, `impressions_index`, `extra_texts`,
`extra_links`, `extra_images`, `extra_videos`, `ad_details`, `eu_transparency`, `uk_transparency`,
`br_transparency`, `eu_total_reach`, `targets_eu`, `payer_beneficiary_data`, `aaa_info`, `insights`,
`page_spend`, `violation_types`, `verified_voice_context`, `is_violating_eu_siep`. `targeting`,
`age_gender_distribution`, `region_distribution` and `beneficiary_payers` are filled from the details payload.
- New `AdCreative` fields: `original_image_url`, `resized_image_url`, `watermarked_image_url`,
`watermarked_video_hd_url`, `watermarked_video_sd_url`, `image_crops`.
- New `PageInfo` fields: `verification`, `category`, `alias`, `entity_type`, `is_deleted`, `is_restricted`,
`is_profile_page`, `about`, `cover_photo_url`, `ig_username`, `ig_followers`, `ig_verified`.
- CSV exports share one schema (`meta_ads_collector.exports.CSV_COLUMNS`) and add `creative_video_url`,
`cta_text`, `cta_type`, `display_format`, `page_like_count`, `page_categories`, `categories`, `bylines`,
`impressions_text`, `reach_lower`, `reach_upper`, `eu_total_reach`, `payer_beneficiary`, `targeted_countries`,
`collation_id` and `collation_count` columns before `collected_at` and `api_fields`.

### Changed
- `page.likes` and `page.is_deleted` are populated from search rows; `funding_entity` and `disclaimer`
mapping is unchanged.
- Media downloads alternate between the creative's regional `*.fbcdn.net` edge and Meta's generic
`scontent.xx.fbcdn.net` / `video.xx.fbcdn.net` host across the existing retry budget, so one unreachable edge
(for example behind a proxy) no longer fails the download. Non-Meta URLs are unchanged.
- CI: the bounded live connectivity check allows three bootstrap attempts and GitHub Actions are pinned to
their current Node 24 majors. Actual Meta invocations retry transport failures on the private proxy leg
(curl timeouts, upstream `CONNECT` 590, rate limits) up to twice; data and behaviour assertions still fail
on the first attempt. The local test gateways retry one stalled upstream attempt per client request.

## [1.6.0] - 2026-10-01

### Added
- Optional MCP extension (`pip install "meta-ads-collector[mcp]"`) with durable jobs, monitoring and the
`meta-ads-mcp` command; beginner quick starts in the README.

## [1.5.1] - 2026-10-01

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ with MetaAdsCollector() as collector:
print(ad.id, advertiser)
```

The library also supports pagination, async collection, filters, exports, media downloads, and HTTP/HTTPS/SOCKS proxies. Fields supplied by Meta are preserved in `ad.api_fields`; individual ads may lack spend, impressions, media, or audience details. JSON/JSONL exports retain `api_fields`; CSV includes it as a JSON column.
The library also supports pagination, async collection, filters, exports, media downloads, and HTTP/HTTPS/SOCKS proxies. Fields supplied by Meta are preserved in `ad.api_fields`; individual ads may lack spend, impressions, media, or audience details. JSON/JSONL exports retain `api_fields`; CSV includes it as a JSON column. Pass `--enrich` (or call `collector.enrich_ad(ad)`) to add the Ad Library "See ad details" payload: EU/UK transparency audience and reach, US political demographics, payer/beneficiary data and advertiser page details.

MetaAdsCollector uses internal Meta endpoints. Meta can change or restrict access, so network, proxy, verification, or rate-limit failures can occur. An empty query result does not establish that no matching ads exist. Keep result limits small when starting, and check errors before treating a collection as complete.

Expand Down
32 changes: 29 additions & 3 deletions docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Download media files for a single ad.

#### `enrich_ad(ad) -> Ad`

Fetch additional detail data from the ad detail endpoint and merge into the ad object. Returns the original ad unchanged on failure.
Fetch the current search row and the "See ad details" payload for the ad (see `MetaAdsClient.get_ad_details`) and merge every missing field into a copy of the ad. The details payload is kept under `ad.ad_details` and inside `ad.api_fields`; EU/UK transparency, demographic insights, payer/beneficiary data and advertiser page info are normalized into `targeting`, `age_gender_distribution`, `region_distribution`, `beneficiary_payers`, `page` and the `eu_*`/`payer_beneficiary_data` fields. Returns the original ad unchanged on failure.

#### `stream(query, ...) -> Iterator[tuple[str, dict[str, Any]]]`

Expand Down Expand Up @@ -239,9 +239,13 @@ Search for ads via the GraphQL API. Returns `(response_data, next_cursor)`.

Search for pages using the typeahead endpoint. Returns a list of page dicts.

#### `get_ad_details(ad_archive_id, page_id) -> dict`
#### `get_ad_details(ad_archive_id, page_id=None, country="ALL") -> dict`

Fetch detailed ad data. Tries the detail page first, then a page-scoped search.
Fetch detailed ad data. With `page_id`, runs a page-scoped search to obtain the current search row, then `get_ad_insights()` and attaches its payload under the `ad_details` key. Without `page_id`, only the (currently JS-challenged) HTML detail page is attempted. Raises `NotImplementedError` when nothing is available.

#### `get_ad_insights(ad_archive_id, page_id, country="ALL") -> dict`

Run the Ad Library "See ad details" query (`AdLibraryV3AdDetailsQuery`) and return its `ad_details` object: `advertiser` (page info, about text, page spend by disclaimer), `insights` (US political age/gender and regional reach), `transparency_by_location` (`eu_transparency`, `uk_transparency`, `br_transparency` with age, gender and location audience, total reach and per-country age/gender breakdown), `aaa_info` (payer/beneficiary data), `verified_voice_context`, `violation_types` and `is_violating_eu_siep`. Pass the result as the `ad_details` key of a search row to `Ad.from_graphql_response()`. Raises `MetaAdsError` when Meta returns no details and `RateLimitError` on rate limits.

#### `close() -> None`

Expand Down Expand Up @@ -308,6 +312,20 @@ Normalized ad schema for the fields parsed by this package. Meta may omit fields
| `page_is_deleted`, `contains_sensitive_content`, `contains_digital_created_media`, `is_aaa_eligible` | `bool \| None` | Meta eligibility and content flags |
| `has_user_reported`, `report_count`, `hide_data_status`, `gated_type` | `Any` | Reporting and visibility fields as returned |
| `branded_content`, `event`, `menu_items`, `is_reshared`, `root_reshared_post`, `fev_info` | `Any` | Additional response fields where present |
| `page_categories` | `list[str]` | Advertiser page categories from the search row |
| `impressions_text`, `impressions_index` | `str \| None`, `int \| None` | Meta's impression bucket label (for example `>1M`) and index |
| `extra_texts`, `extra_links`, `extra_images`, `extra_videos` | `Any` | Additional creative variants as returned |
| `ad_details` | `dict \| None` | Lossless "See ad details" payload (set by `enrich_ad()` / `get_ad_details()`) |
| `eu_transparency`, `uk_transparency`, `br_transparency` | `dict \| None` | Regional transparency blocks: audience, reach and per-country breakdown |
| `eu_total_reach` | `int \| None` | EU total reach from `eu_transparency` |
| `targets_eu` | `bool \| None` | Whether the ad targets the EU |
| `payer_beneficiary_data` | `list[dict]` | `{"payer", "beneficiary"}` pairs from `aaa_info` |
| `aaa_info` | `dict \| None` | Payer/beneficiary block including takedown and violation flags |
| `insights` | `dict \| None` | US political `age_gender_data` and `location_data` |
| `page_spend` | `dict \| None` | Advertiser lifetime and weekly spend by disclaimer |
| `violation_types` | `list` | Violation types reported by Meta |
| `verified_voice_context` | `Any` | Verified-voice and financial-services payer/beneficiary context |
| `is_violating_eu_siep` | `bool \| None` | EU SIEP violation flag |
| `collation_id` | `str \| None` | Collation group ID |
| `collation_count` | `int \| None` | Number of collated variants |
| `api_fields` | `dict \| None` | Deep copy of the complete API row, including unrecognized and nested fields; included in exports by default |
Expand Down Expand Up @@ -339,6 +357,9 @@ Normalized ad schema for the fields parsed by this package. Meta may omit fields
| `thumbnail_url` | `str \| None` | Video thumbnail URL |
| `cta_text` | `str \| None` | Call-to-action text |
| `cta_type` | `str \| None` | CTA type identifier |
| `original_image_url`, `resized_image_url`, `watermarked_image_url` | `str \| None` | Image variants as returned |
| `watermarked_video_hd_url`, `watermarked_video_sd_url` | `str \| None` | Watermarked video variants |
| `image_crops` | `Any` | Crop metadata as returned |

### class PageInfo

Expand All @@ -350,6 +371,11 @@ Normalized ad schema for the fields parsed by this package. Meta may omit fields
| `page_url` | `str \| None` | `None` | Page URL |
| `likes` | `int \| None` | `None` | Like count |
| `verified` | `bool` | `False` | Verification status |
| `verification` | `str \| None` | `None` | `BLUE_VERIFIED`, `GRAY_VERIFIED` or `NOT_VERIFIED` (details query) |
| `category`, `alias`, `entity_type` | `str \| None` | `None` | Page category, vanity alias and entity type (details query) |
| `is_deleted`, `is_restricted`, `is_profile_page` | `bool \| None` | `None` | Page state flags |
| `about`, `cover_photo_url` | `str \| None` | `None` | About text and cover photo (details query) |
| `ig_username`, `ig_followers`, `ig_verified` | `str \| None`, `int \| None`, `bool \| None` | `None` | Linked Instagram account (details query) |

### class PageSearchResult

Expand Down
Loading
Loading