Metadata-Version: 2.3
Name: radimbig-pinchana-tiktok
Version: 1.0.0
Summary: Unofficial PyPI distribution of the Pinchana TikTok scraper maintained by radimbig
Keywords: pinchana,tiktok,scraping,media,fork,unofficial
Author: Pinchana
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: radimbig-pinchana-core
Requires-Dist: yt-dlp>=2026.7.4
Requires-Dist: curl-cffi>=0.16.0
Requires-Dist: fastapi>=0.141.1
Requires-Dist: httpx>=0.28.1
Requires-Dist: pydantic>=2.13.4
Requires-Dist: uvicorn>=0.52.1
Maintainer: radimbig
Requires-Python: >=3.13
Project-URL: Homepage, https://github.com/radimbig2/pinchana-tiktok
Project-URL: Repository, https://github.com/radimbig2/pinchana-tiktok
Project-URL: Issues, https://github.com/radimbig2/pinchana-tiktok/issues
Project-URL: Upstream, https://github.com/Pinchana/pinchana-tiktok
Description-Content-Type: text/markdown

# Pinchana TikTok

`radimbig-pinchana-tiktok` is an unofficial PyPI distribution of the Pinchana
TikTok scraper maintained by `radimbig`. The Python import remains
`pinchana_tiktok`; the upstream project is
https://github.com/Pinchana/pinchana-tiktok.

This FastAPI module and importable Python library extract supported TikTok posts
through a dedicated yt-dlp workflow. It handles ordinary videos and photo
slideshows, including slideshow background audio when available.

## Install and library usage

```sh
pip install radimbig-pinchana-tiktok
```

```python
from pinchana_tiktok import download_tiktok_post_assets

result = download_tiktok_post_assets(
    post_url="https://www.tiktok.com/@creator/video/1234567890",
    temp_directory="./temp_videos",
    user_id=42,
    request_id="abc123",
)
```

The result contains `caption`, `author_username`, and ordered `files`. Each file
has a `media_type` of `video`, `photo`, or `audio`. Call this synchronous API
from a worker thread when the host application already runs an asyncio loop.

## Processing flow

1. Resolve supported canonical and short TikTok URLs.
2. Extract media metadata with the project extractor.
3. Reuse anonymous cookies, pace upstream work globally, and retry temporary failures within a bounded policy.
4. When the deployment-wide VPN is enabled, rotate Gluetun once after a confirmed platform block.
5. Download ordered media to `/app/cache/tiktok/{post_id}` in containers.

The gateway's API v1 response represents slideshow images as ordered `content` assets and their audio as a `soundtrack` asset.

## API

- `POST /scrape` accepts `{"url":"https://www.tiktok.com/@account/video/POST_ID"}`.
- `GET /health` reports module and VPN readiness.

External clients should call the gateway's authenticated `POST /v1/scrape` route.

## Configuration

| Variable | Default | Purpose |
| --- | --- | --- |
| `CACHE_PATH` | `./cache` | Base media cache path |
| `CACHE_MAX_SIZE_GB` | `10.0` | Maximum cache size before eviction |
| `TIKTOK_REQUEST_INTERVAL_SECONDS` | `0.5` | Delay between upstream requests to reduce burst-rate throttling |
| `TIKTOK_PROXY_URL` | empty | Optional HTTP/SOCKS proxy applied to extraction and every media download |
| `TIKTOK_COOKIES_BASE64` | empty | Base64-encoded Netscape cookies; highest priority |
| `TIKTOK_COOKIES` | empty | Raw Netscape cookie text, including escaped newlines |
| `TIKTOK_COOKIES_FILE` | empty | Path to a Netscape cookies file |
| `GLUETUN_CONTROL_URL` | `http://localhost:8000` | Private Gluetun control endpoint |

Cookies are passed to extraction, short-link resolution, and every media
download request. Never commit or log their values; use a separate,
age-verified TikTok account and refresh the export when TikTok rejects it.

## Development

```sh
uv sync --frozen
uv run uvicorn pinchana_tiktok.main:app --host 0.0.0.0 --port 8081 --reload
```

```sh
# Run from the parent pinchana-api directory.
docker build --file pinchana-tiktok/Dockerfile --tag pinchana-tiktok:local .
```

Development Compose keeps `VPN_ENABLED=false`. VPN operation is controlled only
by the deployment-wide `VPN_ENABLED` setting; there is no TikTok-specific VPN
toggle. The scraper prefers anonymous public access but accepts operator-supplied
account cookies for posts requiring login or audience confirmation. It does not
use fabricated mobile identifiers or CAPTCHA-solving services.

Live tests are opt-in. Set `PINCHANA_TIKTOK_LIVE=1` and provide the four
`TIKTOK_LIVE_*_URL` values documented in `.env.example` before running pytest.

## Publishing

Pushing a `v*` tag builds wheel/sdist artifacts and publishes them to PyPI using
the repository secret `PYPI_API_TOKEN`.
