Compare commits
54
Commits
5c51ec895f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d68ff5620 | ||
|
|
8b9bdeb56a | ||
|
|
029837d0f1 | ||
|
|
8a55fd91a4 | ||
|
|
5cec284cd6 | ||
|
|
adfbbfcea7 | ||
|
|
09c706cc61 | ||
|
|
93828c7253 | ||
|
|
5470d74921 | ||
|
|
bbb8bed389 | ||
|
|
e6f543078b | ||
|
|
2769b11918 | ||
|
|
8d74d04b43 | ||
|
|
4efcf1f0ea | ||
|
|
278d4fe32a | ||
|
|
7ffc1bdd81 | ||
|
|
091a68bd55 | ||
|
|
242e3498fe | ||
|
|
6df8af54eb | ||
|
|
dc44085cad | ||
|
|
dfa37f86e3 | ||
|
|
7850b5fa03 | ||
|
|
4dee99cc43 | ||
|
|
6b3fad1ad4 | ||
|
|
563bce5211 | ||
|
|
134fde4ac2 | ||
|
|
4684a6a75c | ||
|
|
4c5dfdb156 | ||
|
|
dd490535fc | ||
|
|
fb33b67a29 | ||
|
|
a41425a887 | ||
|
|
c3e6537a63 | ||
|
|
763a641593 | ||
|
|
e6d75edf33 | ||
|
|
80af7b9460 | ||
|
|
a46adf1ce9 | ||
|
|
6bfffa5364 | ||
|
|
7a12f0e9c8 | ||
|
|
3d38008af3 | ||
|
|
e681bbbe51 | ||
|
|
966d86ff67 | ||
|
|
c7ffd29e79 | ||
|
|
6ebff90d5b | ||
|
|
d1b24f8f85 | ||
|
|
ad0d4f4992 | ||
|
|
7ff47a79bc | ||
|
|
c709c84a4d | ||
|
|
55cfd513e3 | ||
|
|
380ecfcd9b | ||
|
|
a23698a6c4 | ||
|
|
62aa5b0e52 | ||
|
|
bc782ae4f8 | ||
|
|
7640e66c76 | ||
|
|
96ce29b0f3 |
No files matched your search
@@ -0,0 +1,17 @@
|
|||||||
|
name: Dependabot Auto-Merge
|
||||||
|
on: pull_request
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
pull-requests: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
auto-merge:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: github.actor == 'dependabot[bot]'
|
||||||
|
steps:
|
||||||
|
- name: Enable auto-merge for Dependabot PR
|
||||||
|
run: gh pr merge --auto --merge "$PR_URL"
|
||||||
|
env:
|
||||||
|
PR_URL: ${{ github.event.pull_request.html_url }}
|
||||||
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "npm"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "daily"
|
||||||
|
open-pull-requests-limit: 10
|
||||||
|
groups:
|
||||||
|
production:
|
||||||
|
dependency-type: "production"
|
||||||
|
update-types:
|
||||||
|
- "minor"
|
||||||
|
- "patch"
|
||||||
|
development:
|
||||||
|
dependency-type: "development"
|
||||||
|
update-types:
|
||||||
|
- "minor"
|
||||||
|
- "patch"
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
name: Deploy Scraper
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: scraper-deploy
|
||||||
|
cancel-in-progress: false
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
id-token: write
|
||||||
|
|
||||||
|
env:
|
||||||
|
VPS_HOST: ${{ secrets.VPS_HOST }}
|
||||||
|
VPS_USER: ${{ secrets.VPS_USER }}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-and-deploy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 90
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v7
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Install Nix
|
||||||
|
uses: DeterminateSystems/nix-installer-action@v22
|
||||||
|
with:
|
||||||
|
determinate: false
|
||||||
|
extra-conf: |
|
||||||
|
sandbox = false
|
||||||
|
accept-flake-config = true
|
||||||
|
extra-substituters = https://attic.asepharyana.my.id/asepharyana https://attic.asepharyana.my.id/gmw
|
||||||
|
extra-trusted-public-keys = asepharyana:zBpY6vNI1nDJ4mU6W4q880BB0JB1qb3gRKkO/tGSMiQ= gmw:Fq2Anzuhkb+T/hftWnPcveHSi21/RzIgIOeG8pCJa88=
|
||||||
|
|
||||||
|
- name: Cache Nix
|
||||||
|
uses: DeterminateSystems/magic-nix-cache-action@v14
|
||||||
|
with:
|
||||||
|
use-flakehub: false
|
||||||
|
|
||||||
|
- name: Build scraper
|
||||||
|
id: build
|
||||||
|
run: |
|
||||||
|
nix build .#scraper --impure --option sandbox false --print-build-logs
|
||||||
|
STORE_PATH=$(readlink result)
|
||||||
|
echo "store-path=$STORE_PATH" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "Build OK scraper: $STORE_PATH"
|
||||||
|
|
||||||
|
- name: Push to Attic cache
|
||||||
|
env:
|
||||||
|
ATTIC_TOKEN: ${{ secrets.ATTIC_TOKEN }}
|
||||||
|
run: |
|
||||||
|
if [ -z "$ATTIC_TOKEN" ]; then
|
||||||
|
echo "ATTIC_TOKEN not set; skipping attic push"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
STORE_PATH="${{ steps.build.outputs.store-path }}"
|
||||||
|
ATTIC_DIR="/nix/store/fygyy3yk4rqdknxkiwkqambpnhyax0k4-attic-0.1.0"
|
||||||
|
|
||||||
|
ATTIC_BIN=""
|
||||||
|
if command -v attic >/dev/null 2>&1; then
|
||||||
|
ATTIC_BIN="$(command -v attic)"
|
||||||
|
elif nix-store --realise "$ATTIC_DIR" 2>/tmp/attic-bootstrap.err; then
|
||||||
|
echo "✅ Pulled attic client from attic cache (HTTPS substituter)"
|
||||||
|
ATTIC_BIN="$ATTIC_DIR/bin/attic"
|
||||||
|
else
|
||||||
|
echo "attic client unavailable on runner; skipping attic push"
|
||||||
|
tail -5 /tmp/attic-bootstrap.err 2>/dev/null || true
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p "$HOME/.config/attic"
|
||||||
|
cat > "$HOME/.config/attic/config.toml" <<EOF
|
||||||
|
default-server = "pub"
|
||||||
|
|
||||||
|
[servers.pub]
|
||||||
|
endpoint = "https://attic.asepharyana.my.id"
|
||||||
|
token = "$ATTIC_TOKEN"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
push_ok=""
|
||||||
|
for attempt in 1 2 3; do
|
||||||
|
if "$ATTIC_BIN" push pub:asepharyana "$STORE_PATH" --jobs 4 --ignore-upstream-cache-filter; then
|
||||||
|
echo "✅ Pushed $STORE_PATH to attic directly from runner"
|
||||||
|
push_ok=1
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
echo "⚠️ Direct attic push attempt $attempt/3 failed; retrying in 10s..."
|
||||||
|
sleep 10
|
||||||
|
done
|
||||||
|
if [ -z "$push_ok" ]; then
|
||||||
|
echo "Direct attic push failed after 3 attempts; deploy will fall back to ssh copy"
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Setup SSH key
|
||||||
|
env:
|
||||||
|
SSH_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
||||||
|
run: |
|
||||||
|
mkdir -p ~/.ssh
|
||||||
|
echo "$SSH_KEY" > ~/.ssh/id_ed25519
|
||||||
|
chmod 600 ~/.ssh/id_ed25519
|
||||||
|
sed -i 's/\r$//' ~/.ssh/id_ed25519
|
||||||
|
ssh-keygen -y -f ~/.ssh/id_ed25519 >/dev/null 2>&1 || { echo "SSH key invalid"; exit 1; }
|
||||||
|
ssh-keyscan -H "$VPS_HOST" >> ~/.ssh/known_hosts 2>/dev/null
|
||||||
|
|
||||||
|
- name: Deploy scraper to VPS
|
||||||
|
run: |
|
||||||
|
STORE_PATH="${{ steps.build.outputs.store-path }}"
|
||||||
|
echo "=== Realising scraper: $STORE_PATH (substitute from attic, fallback ssh copy) ==="
|
||||||
|
if ssh "$VPS_USER@$VPS_HOST" "sudo /nix/var/nix/profiles/default/bin/nix-store --realise '$STORE_PATH'" 2>/dev/null; then
|
||||||
|
echo "Substituted scraper from Attic cache"
|
||||||
|
else
|
||||||
|
echo "Attic substitute failed; falling back to ssh copy"
|
||||||
|
nix copy --to "ssh://${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }}" "$STORE_PATH"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "=== Updating profile ==="
|
||||||
|
ssh "$VPS_USER@$VPS_HOST" "sudo /nix/var/nix/profiles/default/bin/nix-env --profile /nix/var/nix/profiles/scraper --set '$STORE_PATH'"
|
||||||
|
|
||||||
|
echo "=== Restarting service ==="
|
||||||
|
ssh "$VPS_USER@$VPS_HOST" "sudo systemctl restart scraper && for i in \$(seq 1 15); do state=\$(systemctl is-active scraper 2>/dev/null || echo inactive); [ \"\$state\" = \"active\" ] && break; sleep 2; done; echo \"final-state=\$state\"; [ \"\$state\" = \"active\" ]"
|
||||||
|
|
||||||
|
echo "✅ scraper deployed"
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
name: Notify Parent Repo
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
dispatch:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Trigger root monorepo build
|
|
||||||
uses: peter-evans/repository-dispatch@v3
|
|
||||||
with:
|
|
||||||
token: ${{ secrets.DISPATCH_TOKEN }}
|
|
||||||
repository: asepharyana/asepharyana-hub
|
|
||||||
event-type: submodule-updated
|
|
||||||
client-payload: |
|
|
||||||
{
|
|
||||||
"service": "scraper-api",
|
|
||||||
"ref": "${{ github.ref }}",
|
|
||||||
"sha": "${{ github.sha }}",
|
|
||||||
"actor": "${{ github.actor }}"
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
# Scraper Downloader API Fix Specification
|
||||||
|
|
||||||
|
## Goal
|
||||||
|
Fix all 15 broken downloader endpoints that return HTTP 500, replace dead third-party APIs with working alternatives, and re-test with real URLs from real platforms.
|
||||||
|
|
||||||
|
## Current State Analysis
|
||||||
|
|
||||||
|
### Working Endpoints (HTTP 200):
|
||||||
|
| Platform | Provider | Notes |
|
||||||
|
|----------|----------|-------|
|
||||||
|
| TikTok | tikwm.com | ✓ Working (tiktok.com API) |
|
||||||
|
| MediaFire | mediafire | ✓ Direct page scrape |
|
||||||
|
| Mega | mega.co.nz | ✓ Valid error messages for bad URLs |
|
||||||
|
| GDrive | gdrive | ✓ Valid error for missing file ID |
|
||||||
|
| Pinterest | pinterestdownloader.io | Returns 200 with `{success:false, error:"Unknown error occurred."}` |
|
||||||
|
| Threads | downr.org | Returns 500 (via fetch_snapsave → fetch_all_in_one) |
|
||||||
|
|
||||||
|
### Broken Endpoints (HTTP 500):
|
||||||
|
| Platform | Upstream API | Error |
|
||||||
|
|----------|-------------|-------|
|
||||||
|
| YouTube | media.savetube.me | DNS resolution failure |
|
||||||
|
| Instagram | downr.org | 403 action_forbidden (Cloudflare) |
|
||||||
|
| Facebook | downr.org | 403 action_forbidden (Cloudflare) |
|
||||||
|
| Twitter | savetwitter.net | API returns 404 "Video not found" but code crashes on missing data field |
|
||||||
|
| Spotify | ytdlpyton.nvlgroup.my.id | DNS resolution failure |
|
||||||
|
| SoundCloud | ytdlpyton.nvlgroup.my.id | DNS resolution failure |
|
||||||
|
| PixelDrain | pixeldrain.com | Code crashes parsing viewer_data (wrong regex?) |
|
||||||
|
| KrakenFiles | krakenfiles.com | API returns 403/empty |
|
||||||
|
| Danbooru | danbooru.donmai.us | Returns 404 |
|
||||||
|
| DoodStream | d000d.com | Proxy worker dead |
|
||||||
|
| TeraBox | tera2.sylyt93.workers.dev | DNS resolution failure |
|
||||||
|
| Bilibili | cobalt instances | Cobalt DNS failures |
|
||||||
|
|
||||||
|
## Root Cause
|
||||||
|
1. **Error Handling Bug**: `ScrapingError::Http` maps to `AppError::Internal` (HTTP 500) via the `From<ScrapingError>` impl in error.rs. This was already patched to `AppError::ScraperError` → HTTP 502, but **the running binary hasn't been rebuilt**.
|
||||||
|
2. **Dead Upstream APIs**: Many third-party APIs have moved/changed/blocked requests from VPS IPs.
|
||||||
|
3. **Error Propagation**: Fetch functions use `.unwrap()` on JSON paths (`resp["data"]`, `resp["download_url"]`) which panics when the upstream returns an unexpected response structure, causing HTTP 500.
|
||||||
|
|
||||||
|
## Fix Plan
|
||||||
|
|
||||||
|
### Fix 1: Robust error handling (no panics on missing JSON fields)
|
||||||
|
Replace all `.unwrap()` and direct indexing (`resp["key"]`) with proper `.get()` + error handling. When upstream returns an error response, return `DownloadResult::error("...")` instead of panicking.
|
||||||
|
|
||||||
|
### Fix 2: Replace dead upstream APIs
|
||||||
|
|
||||||
|
#### YouTube (media.savetube.me → multiple alternatives)
|
||||||
|
- Primary: Use yt-dlp subprocess via `std::process::Command` (industry standard, handles all platforms)
|
||||||
|
- Fallback: `https://co.wuk.sh/api/json` (Cobalt instance)
|
||||||
|
- Fallback: `https://api.tikmate.app/api/lookup` for TikTok
|
||||||
|
|
||||||
|
#### Instagram/Facebook (downr.org → alternatives)
|
||||||
|
- Primary: Use yt-dlp subprocess
|
||||||
|
- Fallback chain: `snapsave.app/action.php`, `ddownr.com`, `qurls.app`
|
||||||
|
|
||||||
|
#### Twitter (savetwitter.net → alternatives)
|
||||||
|
- Primary: Use twitsave.com (which returned 200 but no MP4 for that test URL)
|
||||||
|
- Add proper handling for 404 status code from savetwitter.net API
|
||||||
|
- Fallback: `https://twitsave.com/info?url=...`
|
||||||
|
|
||||||
|
#### Spotify (ytdlpyton → alternatives)
|
||||||
|
- Use Spotify API for metadata + alternative audio source
|
||||||
|
- Replace with direct Spotify metadata API (already has a fallback to spotify.com API for metadata)
|
||||||
|
|
||||||
|
#### Pinterest (pinterestdownloader.io → alternatives)
|
||||||
|
- Use yt-dlp subprocess
|
||||||
|
- Fallback: `https://savepin.app`, `https://pin-downloader.com`
|
||||||
|
|
||||||
|
#### PixelDrain (fix existing code)
|
||||||
|
- The regex for extracting viewer_data is wrong - test with real URLs
|
||||||
|
|
||||||
|
#### KrakenFiles
|
||||||
|
- Fix the POST request (the API likely changed)
|
||||||
|
|
||||||
|
#### Danbooru
|
||||||
|
- The API key may be required now
|
||||||
|
|
||||||
|
#### DoodStream
|
||||||
|
- The proxy worker (rv.lil-hacker.workers.dev) is dead - need new proxy
|
||||||
|
|
||||||
|
#### TeraBox
|
||||||
|
- The workers.dev endpoint is dead - find new endpoint
|
||||||
|
|
||||||
|
#### Bilibili
|
||||||
|
- Use working Cobalt instance or the direct API
|
||||||
|
|
||||||
|
### Fix 3: Add yt-dlp as universal fallback
|
||||||
|
Install yt-dlp binary on the VPS. Add a generic download function that shells out to yt-dlp for any platform that fails with its primary API. yt-dlp handles YouTube, IG, FB, Twitter, TikTok, SoundCloud, etc.
|
||||||
|
|
||||||
|
### Fix 4: Improve DownloadResult error messages
|
||||||
|
Ensure every error path returns a descriptive `DownloadResult::error("...")` with platform-specific failure info, not a generic 500.
|
||||||
|
|
||||||
|
## Files to Modify
|
||||||
|
1. `/home/code/scraper/src/presentation/error.rs` - Already patched (ScrapingError → 502)
|
||||||
|
2. `/home/code/scraper/src/infrastructure/repository/downloader.rs` - Main fix target
|
||||||
|
3. `/home/code/scraper/Cargo.toml` - Add tokio-process for yt-dlp subprocess
|
||||||
|
4. `tests/` - Add integration tests with real URLs (non-flaky)
|
||||||
|
|
||||||
|
## Verification
|
||||||
|
- Rebuild and restart the scraper binary
|
||||||
|
- Test each endpoint with REAL platform URLs
|
||||||
|
- Verify HTTP status codes are 200 (with success=false data) or 502 (Bad Gateway), never 500
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
# Spec: Migrate scraper infra to mytheclipse crates (Round 1)
|
||||||
|
|
||||||
|
Date: 2026-08-30
|
||||||
|
Repo: /home/code/scraper (github.com/asepharyana/scraper)
|
||||||
|
Goal: Replace hand-rolled infrastructure in the scraper with the user's
|
||||||
|
custom `mytheclipse` library crates where there is a clear 1:1 mapping.
|
||||||
|
|
||||||
|
## Library crates (from /home/code/mytheclipse, all v1.20.0)
|
||||||
|
|
||||||
|
- `mytheclipse` — core: retry (retry/RetryConfig/JitterKind/RetryError),
|
||||||
|
ratelimit (RateLimiter/RateLimitError), timeouts
|
||||||
|
(timeout), concurrency primitives, ServiceBuilder,
|
||||||
|
runtime_auto (available_parallelism), spawn_io/spawn_bg.
|
||||||
|
- `mytheclipse-cache` — Cache trait, MemoryCache, RedisCache (l2-redis),
|
||||||
|
CacheAside (cache-aside), CacheError.
|
||||||
|
- `mytheclipse-config`— ConfigLoader<T> (file + env merge, hot-reload, validation).
|
||||||
|
- `mytheclipse-event` — EventBus trait, InMemoryEventBus, TypedEventBus (byte/JSON pub/sub).
|
||||||
|
- `mytheclipse-crypto`— (NOT used in this round — scraper has no crypto/JWT usage.)
|
||||||
|
|
||||||
|
## Current state (baseline)
|
||||||
|
|
||||||
|
- Cargo.toml has NO mytheclipse crates. Uses directly:
|
||||||
|
- `backoff` — retry with exponential backoff (hand-rolled wrapper in
|
||||||
|
src/infrastructure/scraping/retry.rs)
|
||||||
|
- `deadpool-redis` — Redis pool + raw `redis::AsyncCommands` in
|
||||||
|
src/infrastructure/cache/{redis_pool.rs,redis.rs}
|
||||||
|
- `dashmap` — request coalescing in proxy_fetch.rs (IN_FLIGHT map)
|
||||||
|
- `rayon` — komik_parser parallel map
|
||||||
|
- `config` crate — config loading in src/config/mod.rs
|
||||||
|
- `opentelemetry*` — metrics in src/observability/metrics.rs
|
||||||
|
- custom EventBus in src/events/bus.rs (unused by any handler)
|
||||||
|
- custom RateLimiter in src/presentation/middleware/ratelimit.rs (unused by router)
|
||||||
|
- Baseline: `cargo check` currently clean (verified in background).
|
||||||
|
|
||||||
|
## Replacement mapping (behavior-preserving)
|
||||||
|
|
||||||
|
| # | Hand-rolled | mytheclipse replacement | Files touched |
|
||||||
|
|---|---|---|---|
|
||||||
|
| 1 | backoff + retry.rs wrapper | `mytheclipse::retry` + `RetryConfig` | src/infrastructure/scraping/retry.rs, parsing_utils.rs, otakudesu.rs |
|
||||||
|
| 2 | deadpool-redis pool in redis_pool.rs | Keep pool, wrap conn with `RedisCache` (mytheclipse-cache) | redis_pool.rs + new bridge, redis.rs, proxy_fetch.rs |
|
||||||
|
| 3 | Cache helper (redis.rs) | `CacheAside` + typed JSON serde over `RedisCache` | redis.rs, application/*/use_cases.rs |
|
||||||
|
| 4 | custom EventBus (events/bus.rs) | `InMemoryEventBus`/`TypedEventBus` (mytheclipse-event) | events/bus.rs → re-export, state.rs, bootstrap |
|
||||||
|
| 5 | custom RateLimiter middleware | `mytheclipse::RateLimiter` (token bucket) | presentation/middleware/ratelimit.rs |
|
||||||
|
| 6 | `config` crate loader in config/mod.rs | `mytheclipse-config` ConfigLoader<T> | src/config/mod.rs |
|
||||||
|
| 7 | HTTP client wrapper (http_client.rs) | `mytheclipse-http` HttpClient (optional) | http_client.rs (SKIP this round — retry semantics differ; reqwest needs headers/UA control) |
|
||||||
|
| 8 | OTel metrics (metrics.rs) | Keep opentelemetry (mytheclipse-tracing has no metrics exporter; avoid behavior change) | SKIP this round |
|
||||||
|
| 9 | rayon in komik_parser | `mytheclipse::compute::compute_par_for_each` (feature compute) | komik_parser.rs (SKIP this round — parser correctness risk; rayon works) |
|
||||||
|
|
||||||
|
## Scope decision (this round)
|
||||||
|
|
||||||
|
Implement items #1–#6. Skip #7–#9 with rationale:
|
||||||
|
- #7 mytheclipse-http client is a thin reqwest wrapper without header/UA control
|
||||||
|
needed by common_headers(); converting scrapers through it changes fetch
|
||||||
|
semantics (returns bytes, loses status/content-type) — not behavior-preserving.
|
||||||
|
- #8 mytheclipse-tracing has no metrics exporter; opentelemetry stays.
|
||||||
|
- #9 parser logic (2k+ LOC) is out of scope for infra migration; rayon stays.
|
||||||
|
|
||||||
|
## Detail per item
|
||||||
|
|
||||||
|
### 1. retry.rs → mytheclipse::retry
|
||||||
|
- Replace `backoff::ExponentialBackoff` with `mytheclipse::{retry, RetryConfig, JitterKind}`.
|
||||||
|
- Keep signature-compatible helpers so call sites barely change:
|
||||||
|
`default_backoff() -> RetryConfig`, `quick_backoff()`, `slow_backoff()`,
|
||||||
|
`custom_backoff(...)`.
|
||||||
|
- `transient/permanent` helpers: mytheclipse retry uses a `predicate` closure
|
||||||
|
`Fn(&E) -> bool`. Replace transient/permanent with a retryable predicate
|
||||||
|
(retry on any error except a marker). To preserve "transient = retry,
|
||||||
|
permanent = stop", use a wrapper type or predicate returning true for all
|
||||||
|
errors, and treat 4xx-style permanent errors by converting callers to return
|
||||||
|
a `Permanent` variant.
|
||||||
|
- Keep the `retry` fn name re-exported for minimal call-site churn.
|
||||||
|
|
||||||
|
### 2. Redis: mytheclipse-cache RedisCache + bridge
|
||||||
|
- Add `mytheclipse-cache = { version = "1.20", features = ["l2-redis", "cache-aside"] }`.
|
||||||
|
- Keep deadpool pool (mytheclipse has no pool); obtain
|
||||||
|
`redis::aio::MultiplexedConnection` from deadpool conn (deadpool_redis::Connection
|
||||||
|
derefs to `&mut redis::aio::ConnectionLike` — need to convert via `into_multiplexed()`).
|
||||||
|
- New bridge file `src/infrastructure/cache/mytheclipse.rs`:
|
||||||
|
`pub fn redis_cache() -> &'static mytheclipse_cache::RedisCache` building
|
||||||
|
from the pool lazily (LazyLock).
|
||||||
|
|
||||||
|
### 3. Cache helper → CacheAside
|
||||||
|
- Rewrite `src/infrastructure/cache/redis.rs` as a thin typed wrapper over
|
||||||
|
`RedisCache` implementing `get/get_or_set/set/set_with_ttl/delete/exists`
|
||||||
|
with serde_json, so use_cases keep the same ergonomic API (minimal churn)
|
||||||
|
but delegate to mytheclipse `Cache` trait underneath.
|
||||||
|
- `get_or_set` becomes CacheAside-equivalent (read-through).
|
||||||
|
|
||||||
|
### 4. events/bus.rs → mytheclipse-event
|
||||||
|
- Replace custom Event/EventHandler/pub-sub with
|
||||||
|
`TypedEventBus<InMemoryEventBus>`.
|
||||||
|
- Define domain events as serde structs implementing `mytheclipse_event::Event`
|
||||||
|
(which is a blanket trait on Serialize+DeserializeOwned types).
|
||||||
|
- `EventBus` type alias: `pub type EventBus = TypedEventBus<InMemoryEventBus>`.
|
||||||
|
- state.rs + bootstrap keep `Arc<EventBus>`; `new()` -> `EventBus::new(InMemoryEventBus::default())`.
|
||||||
|
- Publish/subscribe usage sites (none currently) adapt if any.
|
||||||
|
|
||||||
|
### 5. Ratelimit middleware → mytheclipse::RateLimiter
|
||||||
|
- Keep middleware shape (axum State) but inner limiter becomes
|
||||||
|
`mytheclipse::RateLimiter` token bucket: `RateLimiter::new(rate_per_sec, burst)`.
|
||||||
|
- `check()` -> `try_acquire()` returns Result<_, RateLimitError>.
|
||||||
|
|
||||||
|
### 6. config/mod.rs → mytheclipse-config
|
||||||
|
- Replace `config::Config::builder()` with `mytheclipse_config::ConfigLoader<AppConfig>`.
|
||||||
|
- Sources: load_dotenv (env feature) → merge_file(config/default.toml) →
|
||||||
|
merge_file(config/{RUN_MODE}.toml) → merge_env (APP__ prefix + legacy env
|
||||||
|
var mapping preserved).
|
||||||
|
- Keep `CONFIG` LazyLock and the same `AppConfig` struct (`Deserialize` +
|
||||||
|
`mytheclipse_config::Config` blanket trait).
|
||||||
|
|
||||||
|
## Schema/type changes
|
||||||
|
- `deadpool_redis::Pool` still in AppState (no change) — RedisCache wraps conns.
|
||||||
|
- `AppState.event_bus` becomes `Arc<TypedEventBus<InMemoryEventBus>>`.
|
||||||
|
- Error mapping: add `From<mytheclipse_cache::CacheError>` for AppError +
|
||||||
|
DomainError? Keep the existing string-based paths; add explicit From impls
|
||||||
|
where needed.
|
||||||
|
|
||||||
|
## Backend surface
|
||||||
|
- No HTTP route changes. No API contract changes.
|
||||||
|
- Redis cache keys/TTLs unchanged. Event topics unchanged (none active).
|
||||||
|
|
||||||
|
## Frontend surface
|
||||||
|
- None (backend-only service).
|
||||||
|
|
||||||
|
## Verification steps
|
||||||
|
1. `cargo check` — exit 0
|
||||||
|
2. `cargo test` — all pass
|
||||||
|
3. `cargo clippy -- -D warnings` — 0 warnings (project lint gate)
|
||||||
|
4. `cargo fmt --check` — clean
|
||||||
|
5. `cargo build --release` — succeeds
|
||||||
|
6. Smoke: boot server briefly if feasible (needs Redis/DB; else compile-only + unit tests)
|
||||||
|
|
||||||
|
## Risk register
|
||||||
|
- deadpool Connection → MultiplexedConnection conversion API differences
|
||||||
|
(0.22 vs 0.27 redis). Verify at compile time; fallback = keep raw conn in
|
||||||
|
Cache impl.
|
||||||
|
- `mytheclipse::retry` predicate-based vs backoff transient/permanent — call
|
||||||
|
sites that relied on permanent-stop need review (repository fetch_html uses
|
||||||
|
`transient()` on everything — safe to retry-all).
|
||||||
|
- ConfigLoader merge_env nesting: existing env var mapping uses `APP__` prefix
|
||||||
|
+ legacy names. Must keep exact names (APP_DATABASE_URL etc.) — verify with
|
||||||
|
`peek()` / unit test on the real struct.
|
||||||
|
- EventBus trait object: `Arc<TypedEventBus<InMemoryEventBus>>` is concrete —
|
||||||
|
no trait object; AppState carries the concrete type to avoid dyn issues.
|
||||||
|
|
||||||
|
## Migration order (batches, each verified)
|
||||||
|
Batch A: Cargo.toml deps + retry.rs rewrite + parsing_utils/otakudesu call sites
|
||||||
|
Batch B: redis_pool bridge + Cache rewrite (redis.rs) + proxy_fetch cache calls
|
||||||
|
Batch C: events/bus.rs + state.rs + bootstrap
|
||||||
|
Batch D: ratelimit middleware + router wiring
|
||||||
|
Batch E: config/mod.rs ConfigLoader
|
||||||
|
Batch F: full verification + commit
|
||||||
|
|
||||||
|
Each batch: cargo check + cargo test must stay green. Commit once at end with
|
||||||
|
message: `refactor: migrate infra to mytheclipse crates (retry, cache, event, ratelimit, config)`
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
# Spec Round 2 — Scraper → mytheclipse Deep Infra Migration (thread/async/queue)
|
||||||
|
|
||||||
|
Date: 2026-08-30
|
||||||
|
Repo: /home/code/scraper (scraper-service)
|
||||||
|
Library: /home/code/mytheclipse (mytheclipse crates v1.20+ local path)
|
||||||
|
|
||||||
|
## Scope
|
||||||
|
|
||||||
|
Round 1 (committed c7ffd29) migrated retry, redis cache, event bus, ratelimit,
|
||||||
|
config. Round 2 targets the remaining **runtime/concurrency/queue/observability**
|
||||||
|
infrastructure — the parts the user explicitly asked to push "sampai tingkat
|
||||||
|
thread, async, queue dan lainnya".
|
||||||
|
|
||||||
|
## 1. Runtime thread sizing — `mytheclipse::runtime_auto::RuntimeConfig`
|
||||||
|
|
||||||
|
**Current**: bootstrap logs `std::thread::available_parallelism()` manually;
|
||||||
|
tokio runtime is `#[tokio::main(flavor = "multi_thread")]` with default
|
||||||
|
worker counts.
|
||||||
|
|
||||||
|
**Change**:
|
||||||
|
- Add `mytheclipse = { features = ["lifecycle", ...] }` (add `lifecycle` feature).
|
||||||
|
- In `bootstrap/mod.rs`, compute `RuntimeConfig::auto()` once at build, log
|
||||||
|
worker/blocking/compute/io counts, and keep the tokio runtime default
|
||||||
|
(already multi_thread). Optionally pass `worker_threads` via a `#[tokio::main]`
|
||||||
|
alternative — but since `main` uses the macro, we keep the macro form and
|
||||||
|
simply surface the auto-derived counts in logs + store them in `AppState`
|
||||||
|
for future pool sizing.
|
||||||
|
- Replace the manual `available_parallelism()` log with `RuntimeConfig::auto()`.
|
||||||
|
|
||||||
|
## 2. Async I/O + background task spawns — `mytheclipse::spawn_io` / `spawn_bg`
|
||||||
|
|
||||||
|
**Current**:
|
||||||
|
- `proxy_fetch.rs:114` uses `tokio::spawn(...)` for the request-coalescing leader
|
||||||
|
- 30+ `tokio::task::spawn_blocking(...)` sites across otakudesu.rs, anime2
|
||||||
|
use_cases, komik use_cases, proxy_fetch (gzip decompress)
|
||||||
|
- No bounded background task pool
|
||||||
|
|
||||||
|
**Change**:
|
||||||
|
- `proxy_fetch.rs` leader task → `mytheclipse::spawn_io(...)` (same semantics,
|
||||||
|
adds tracing span instrumentation). No behavior change.
|
||||||
|
- Add `mytheclipse = { features = ["io", "bg"] }`.
|
||||||
|
- The `spawn_blocking` sites stay as-is **this round** (they're CPU-bound
|
||||||
|
parser calls and `spawn_blocking` is the correct tokio primitive; mytheclipse
|
||||||
|
`compute()` would replace them but that's a 2k+ LOC parser sweep — deferred
|
||||||
|
as before). Document this in the spec's residual section.
|
||||||
|
- Add `bg` feature and expose a bounded background executor via
|
||||||
|
`mytheclipse::spawn_bg` for the **cache-write / relay-fallback** background
|
||||||
|
tasks that currently fire-and-forget (if any are found in use_cases).
|
||||||
|
|
||||||
|
## 3. Worker queue — `mytheclipse-queue` `InMemoryQueue` + `WorkerPool`
|
||||||
|
|
||||||
|
**Current**: no queue abstraction; the only "background" work is the cache
|
||||||
|
batch writes (`cache_image_urls_batch_lazy` in the proxy image-cache path —
|
||||||
|
but proxy routes were removed, so that path may be dead). Event bus has no
|
||||||
|
worker pool.
|
||||||
|
|
||||||
|
**Change**:
|
||||||
|
- Add `mytheclipse-queue = { path = ..., features = ["in-memory"] }`.
|
||||||
|
- Wire a small application-level `JobQueue` in `src/infrastructure/queue/`:
|
||||||
|
- `InMemoryQueue` + `WorkerPool` consuming a `scrape:repair` topic for
|
||||||
|
background image-cache repair/re-sync jobs.
|
||||||
|
- Graceful shutdown: on app shutdown, stop workers.
|
||||||
|
- This is additive infra (no existing behavior replaced), so it's low-risk
|
||||||
|
and demonstrates the queue crate in the scraper.
|
||||||
|
- Actually **use** it: the `image_cache` repair path (cache cleanup at 2 AM)
|
||||||
|
is currently absent (scheduler dir missing). Introduce a small
|
||||||
|
`src/infrastructure/scheduler.rs` using `mytheclipse::cron` (lifecycle
|
||||||
|
feature) OR `tokio::time::interval` for the daily 02:00 cleanup that
|
||||||
|
enqueues repair jobs onto the queue. Given the original CLAUDE.md mentions
|
||||||
|
"scheduler — Cron jobs (daily cache cleanup at 2 AM)", recreating it on
|
||||||
|
mytheclipse-queue is a faithful, additive migration.
|
||||||
|
|
||||||
|
## 4. Backpressure / concurrency — `mytheclipse::ConcurrencyLimiter` / `BackpressureQueue`
|
||||||
|
|
||||||
|
**Current**: proxy_fetch has request coalescing via DashMap + broadcast (kept —
|
||||||
|
it's application logic, not infra). No app-level backpressure.
|
||||||
|
|
||||||
|
**Change**:
|
||||||
|
- Add `mytheclipse = { features = ["traffic"] }` (already enabled for
|
||||||
|
RateLimiter in round 1).
|
||||||
|
- Wrap the **fetch-with-proxy** path with a `ConcurrencyLimiter` (bound
|
||||||
|
concurrent outbound scrapes, default e.g. 16) so burst traffic can't
|
||||||
|
saturate the upstream. This is a real, central hot path (every scrape goes
|
||||||
|
through it).
|
||||||
|
- Keep the RateLimiter middleware (round 1) for inbound.
|
||||||
|
|
||||||
|
## 5. Observability — `mytheclipse-tracing` `TracingLayer` (+ keep OTel metrics)
|
||||||
|
|
||||||
|
**Current**: `tracing_subscriber::fmt().with_env_filter(...)` in bootstrap;
|
||||||
|
opentelemetry metrics in `observability/metrics.rs` (kept — `mytheclipse-tracing`
|
||||||
|
has no metrics exporter, round-1 decision).
|
||||||
|
|
||||||
|
**Change**:
|
||||||
|
- Add `mytheclipse-tracing = { path = ..., features = ["env"] }`.
|
||||||
|
- Replace `tracing_subscriber::fmt()` in bootstrap with
|
||||||
|
`mytheclipse_tracing::TracingLayer::install()` (same RUST_LOG env-filter
|
||||||
|
semantics, adds tracing span layer compatibility).
|
||||||
|
- Keep `init_otel_metrics()` untouched (OTel exporter stays).
|
||||||
|
|
||||||
|
## 6. HTTP client — `mytheclipse-http` `HttpClient` (optional, re-evaluate)
|
||||||
|
|
||||||
|
Round 1 deferred this because mytheclipse-http's client "loses header/UA/status
|
||||||
|
control". Re-check: `mytheclipse-http::client::HttpClient` — if it exposes
|
||||||
|
`with_timeout`, `get_text`, `get_json`, `post_json`, and header injection,
|
||||||
|
use it for the non-critical relay fetches (`fetch_via_relays`). If not
|
||||||
|
header-capable, skip (kept as residual).
|
||||||
|
|
||||||
|
## Files touched
|
||||||
|
|
||||||
|
- Cargo.toml (features: add lifecycle, io, bg; add mytheclipse-queue, mytheclipse-tracing)
|
||||||
|
- src/bootstrap/mod.rs (RuntimeConfig log + TracingLayer + scheduler start)
|
||||||
|
- src/infrastructure/scraping/proxy_fetch.rs (spawn_io + ConcurrencyLimiter)
|
||||||
|
- src/infrastructure/queue/mod.rs (NEW — InMemoryQueue + WorkerPool + repair topic)
|
||||||
|
- src/infrastructure/scheduler.rs (NEW — daily 02:00 cleanup → queue enqueue)
|
||||||
|
- src/infrastructure/cache/mytheclipse.rs (maybe wire limiter through cache path)
|
||||||
|
- src/presentation/state.rs (add queue handle / limiter if needed)
|
||||||
|
|
||||||
|
## Verification
|
||||||
|
|
||||||
|
1. `cargo check` — green after each batch
|
||||||
|
2. `cargo test` — all pass
|
||||||
|
3. `cargo build --release` — green
|
||||||
|
4. `cargo fmt --check` on touched files
|
||||||
|
5. Runtime smoke: boot server, hit `/health`, confirm logs show RuntimeConfig
|
||||||
|
and scheduler started; queue jobs process (test harness)
|
||||||
|
|
||||||
|
## Residual (deliberately deferred)
|
||||||
|
|
||||||
|
- `spawn_blocking` → `mytheclipse::compute()` sweep (2k+ LOC parser files)
|
||||||
|
- OTel metrics → mytheclipse-tracing (no metrics exporter in library yet)
|
||||||
|
- rayon in komik_parser
|
||||||
|
- mytheclipse-http client if header control is insufficient
|
||||||
|
|
||||||
|
## Commit
|
||||||
|
|
||||||
|
Single commit at end: `refactor: migrate runtime/queue/concurrency infra to mytheclipse (round 2)`
|
||||||
Generated
+907
-1044
File diff suppressed because it is too large.
Load diff
+33
-7
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "scraper-service"
|
name = "scraper-service"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@@ -13,6 +12,17 @@ default-run = "scraper"
|
|||||||
|
|
||||||
# Dependensi yang dibutuhkan saat aplikasi berjalan
|
# Dependensi yang dibutuhkan saat aplikasi berjalan
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
# Mytheclipse workspace (user's custom library). All crates are published on
|
||||||
|
# crates.io at 1.21.2 (incl. mytheclipse-queue + mytheclipse-tracing, the
|
||||||
|
# redis-0.32 cache fix), so the scraper tracks the released crates via
|
||||||
|
# version deps.
|
||||||
|
mytheclipse = { version = "1.21", features = ["full"] }
|
||||||
|
mytheclipse-cache = { version = "1.21", features = ["l2-redis", "cache-aside"] }
|
||||||
|
mytheclipse-config = { version = "1.21" }
|
||||||
|
mytheclipse-event = { version = "1.21" }
|
||||||
|
mytheclipse-queue = { version = "1.21", features = ["in-memory"] }
|
||||||
|
mytheclipse-tracing = { version = "1.21", features = ["env"] }
|
||||||
|
|
||||||
axum = { version = "0.8.8", features = ["ws", "multipart", "macros"] }
|
axum = { version = "0.8.8", features = ["ws", "multipart", "macros"] }
|
||||||
tokio = { version = "1.49.0", features = ["full"] }
|
tokio = { version = "1.49.0", features = ["full"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
@@ -34,25 +44,31 @@ regex = "1.12.2"
|
|||||||
|
|
||||||
urlencoding = "2.1"
|
urlencoding = "2.1"
|
||||||
url = "2.5.8"
|
url = "2.5.8"
|
||||||
|
base64 = "0.22"
|
||||||
|
cipher = "0.4"
|
||||||
|
aes = "0.8"
|
||||||
|
cbc = "0.1"
|
||||||
|
block-padding = "0.3"
|
||||||
|
hex = "0.4"
|
||||||
|
fastrand = "2.0"
|
||||||
|
|
||||||
tower-http = { version = "0.6.8", features = ["fs", "cors", "compression-gzip", "compression-br", "compression-zstd"] }
|
tower-http = { version = "0.6.8", features = ["fs", "cors", "compression-gzip", "compression-br", "compression-zstd"] }
|
||||||
backoff = { version = "0.4", features = ["futures", "tokio"] }
|
|
||||||
dashmap = "6.1"
|
dashmap = "6.1"
|
||||||
deadpool-redis = { version = "0.22.1", features = ["serde"] }
|
deadpool-redis = { version = "0.22.1", features = ["serde"] }
|
||||||
rayon = "1.11"
|
rayon = "1.12"
|
||||||
scraper = "0.25.0"
|
scraper = "0.25.0"
|
||||||
flate2 = "1.1"
|
flate2 = "1.1"
|
||||||
redis = { version = "0.32.7", features = ["tokio-rustls-comp", "safe_iterators"] }
|
redis = { version = "0.32.7", features = ["tokio-rustls-comp", "safe_iterators"] }
|
||||||
thiserror = "2.0.18"
|
thiserror = "2.0.18"
|
||||||
config = { version = "0.15.19", features = ["toml"] }
|
|
||||||
|
|
||||||
utoipa = { version = "5.0", features = ["axum_extras"] }
|
utoipa = { version = "5.0", features = ["axum_extras"] }
|
||||||
utoipa-swagger-ui = { version = "9.0", features = ["axum"] }
|
utoipa-swagger-ui = { version = "9.0", features = ["axum"] }
|
||||||
|
|
||||||
# OpenTelemetry metrics
|
# OpenTelemetry metrics
|
||||||
opentelemetry = { version = "0.27", features = ["metrics"] }
|
opentelemetry = { version = "0.28", features = ["metrics"] }
|
||||||
opentelemetry_sdk = { version = "0.27", features = ["metrics", "rt-tokio"] }
|
opentelemetry_sdk = { version = "0.28", features = ["metrics", "rt-tokio"] }
|
||||||
opentelemetry-otlp = { version = "0.27", features = ["metrics"] }
|
opentelemetry-otlp = { version = "0.28", features = ["metrics"] }
|
||||||
|
|
||||||
|
|
||||||
# Dependensi yang hanya dibutuhkan untuk build script (build.rs)
|
# Dependensi yang hanya dibutuhkan untuk build script (build.rs)
|
||||||
|
|
||||||
@@ -76,6 +92,15 @@ unsafe_code = "forbid"
|
|||||||
unused_variables = "deny"
|
unused_variables = "deny"
|
||||||
unused_imports = "deny"
|
unused_imports = "deny"
|
||||||
unused_must_use = "deny"
|
unused_must_use = "deny"
|
||||||
|
dead_code = "deny"
|
||||||
|
unused_mut = "deny"
|
||||||
|
unreachable_code = "deny"
|
||||||
|
trivial_casts = "deny"
|
||||||
|
trivial_numeric_casts = "deny"
|
||||||
|
explicit_outlives_requirements = "deny"
|
||||||
|
unused_labels = "deny"
|
||||||
|
unused_braces = "deny"
|
||||||
|
noop_method_call = "deny"
|
||||||
|
|
||||||
[lints.clippy]
|
[lints.clippy]
|
||||||
unwrap_used = "warn"
|
unwrap_used = "warn"
|
||||||
@@ -83,6 +108,7 @@ expect_used = "warn"
|
|||||||
panic = "deny"
|
panic = "deny"
|
||||||
todo = "deny"
|
todo = "deny"
|
||||||
unimplemented = "deny"
|
unimplemented = "deny"
|
||||||
|
unnecessary_to_owned = "deny"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
# default = ["ffmpeg"]
|
# default = ["ffmpeg"]
|
||||||
|
|||||||
Generated
+61
@@ -0,0 +1,61 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"flake-utils": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1731533236,
|
||||||
|
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1787964612,
|
||||||
|
"narHash": "sha256-0N9nghg3nwzX6b6qc77EzjR9cu/Z+UR66FlfsCqiURs=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "e8be7818e19ada32105a8af937a6a473b38167ca",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixpkgs-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"systems": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1681028828,
|
||||||
|
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
{
|
||||||
|
description = "Asepharyana Scraper — Rust/Axum web scraper & image proxy API";
|
||||||
|
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||||
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = { self, nixpkgs, flake-utils }:
|
||||||
|
flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
|
||||||
|
let
|
||||||
|
pkgs = import nixpkgs {
|
||||||
|
inherit system;
|
||||||
|
config.allowUnfree = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
scraper = pkgs.stdenv.mkDerivation {
|
||||||
|
name = "scraper-0.1.1";
|
||||||
|
src = ./.;
|
||||||
|
|
||||||
|
nativeBuildInputs = with pkgs; [
|
||||||
|
cacert curl gcc gnumake openssl pkg-config python3 libclang
|
||||||
|
rustc cargo clang cmake zlib
|
||||||
|
];
|
||||||
|
buildInputs = with pkgs; [ openssl stdenv.cc.cc.lib ];
|
||||||
|
|
||||||
|
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
|
||||||
|
LD_LIBRARY_PATH = "${pkgs.libclang.lib}/lib:${pkgs.stdenv.cc.cc.lib}/lib";
|
||||||
|
NIX_ENFORCE_PURITY = "0";
|
||||||
|
|
||||||
|
SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
|
||||||
|
|
||||||
|
phases = [ "unpackPhase" "buildPhase" "installPhase" ];
|
||||||
|
buildPhase = ''
|
||||||
|
export HOME="$TMPDIR" CARGO_HOME="$TMPDIR/.cargo-scraper"
|
||||||
|
echo "=== Building scraper ==="
|
||||||
|
cargo build --release 2>&1
|
||||||
|
'';
|
||||||
|
installPhase = ''
|
||||||
|
mkdir -p $out/bin
|
||||||
|
cp target/release/scraper $out/bin/scraper
|
||||||
|
cp $src/scrape_media.py $out/bin/scrape_media.py
|
||||||
|
chmod +x $out/bin/scrape_media.py
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
packages = {
|
||||||
|
inherit scraper;
|
||||||
|
default = scraper;
|
||||||
|
};
|
||||||
|
|
||||||
|
apps.scraper = {
|
||||||
|
type = "app";
|
||||||
|
program = "${scraper}/bin/scraper";
|
||||||
|
};
|
||||||
|
|
||||||
|
devShells.default = pkgs.mkShell {
|
||||||
|
buildInputs = with pkgs; [ rustc cargo clang cmake openssl pkg-config ];
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
Executable
+161
@@ -0,0 +1,161 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Universal scraper for social media video URLs using Playwright + Chromium.
|
||||||
|
Used as fallback when yt-dlp is blocked by anti-bot protection.
|
||||||
|
|
||||||
|
Usage: scrape_media.py <url> <platform>
|
||||||
|
Output: JSON with title, media items, etc.
|
||||||
|
|
||||||
|
Platforms supported:
|
||||||
|
- instagram: Scrape reel/post video URLs from cdninstagram.com
|
||||||
|
- facebook: Scrape video URLs from fbcdn.net
|
||||||
|
- tiktok: Try scraping, but may be blocked by anti-bot
|
||||||
|
- twitter: Scrape video URLs from video.twimg.com
|
||||||
|
- pinterest: Scrape video URLs from v.pinimg.com
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
import asyncio
|
||||||
|
import re
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Point Playwright at system-wide browser cache so it works regardless of
|
||||||
|
# the service user's HOME permissions.
|
||||||
|
os.environ.setdefault("PLAYWRIGHT_BROWSERS_PATH", "/usr/local/share/ms-playwright")
|
||||||
|
|
||||||
|
from playwright.async_api import async_playwright
|
||||||
|
|
||||||
|
async def scrape(url, platform):
|
||||||
|
async with async_playwright() as p:
|
||||||
|
browser = await p.chromium.launch(
|
||||||
|
headless=True,
|
||||||
|
args=["--no-sandbox", "--disable-bypass"]
|
||||||
|
)
|
||||||
|
context = await browser.new_context(
|
||||||
|
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||||
|
viewport={"width": 1920, "height": 1080},
|
||||||
|
java_script_enabled=True,
|
||||||
|
)
|
||||||
|
page = await context.new_page()
|
||||||
|
|
||||||
|
# Track network responses
|
||||||
|
media_responses = []
|
||||||
|
|
||||||
|
def handle_response(response):
|
||||||
|
resp_url = response.url
|
||||||
|
# Look for video/audio URLs
|
||||||
|
if any(ext in resp_url for ext in ['.mp4', '.m3u8', '.mp3']):
|
||||||
|
media_responses.append({
|
||||||
|
"url": resp_url,
|
||||||
|
"status": response.status,
|
||||||
|
"content_type": response.headers.get('content-type', ''),
|
||||||
|
})
|
||||||
|
|
||||||
|
page.on("response", handle_response)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await page.goto(url, timeout=60000)
|
||||||
|
await page.wait_for_timeout(15000)
|
||||||
|
except Exception as e:
|
||||||
|
await browser.close()
|
||||||
|
print(json.dumps({"success": False, "error": f"Navigation error: {e}"}))
|
||||||
|
return
|
||||||
|
|
||||||
|
# Get page title
|
||||||
|
title = await page.title()
|
||||||
|
|
||||||
|
# Get video element info
|
||||||
|
video_info = await page.evaluate("""() => {
|
||||||
|
const video = document.querySelector('video');
|
||||||
|
if (!video) return null;
|
||||||
|
return {
|
||||||
|
src: video.src,
|
||||||
|
duration: video.duration,
|
||||||
|
poster: video.poster,
|
||||||
|
};
|
||||||
|
}""")
|
||||||
|
|
||||||
|
# Filter for real video URLs (not static assets, not ttwstatic)
|
||||||
|
seen = set()
|
||||||
|
media = []
|
||||||
|
for resp in media_responses:
|
||||||
|
url_val = resp["url"]
|
||||||
|
if url_val in seen:
|
||||||
|
continue
|
||||||
|
if len(url_val) < 50:
|
||||||
|
continue
|
||||||
|
# Skip static assets
|
||||||
|
if 'ttwstatic' in url_val or 'rsrc.php' in url_val:
|
||||||
|
continue
|
||||||
|
if 'cdninstagram' not in url_val and 'fbcdn' not in url_val and 'video.twimg' not in url_val and 'pinimg' not in url_val:
|
||||||
|
# For non-Instagram/facebook, still accept
|
||||||
|
if 'static' in url_val:
|
||||||
|
continue
|
||||||
|
seen.add(url_val)
|
||||||
|
|
||||||
|
# Determine content type
|
||||||
|
ext = "mp4"
|
||||||
|
if '.m3u8' in url_val:
|
||||||
|
ext = "m3u8"
|
||||||
|
elif '.mp3' in url_val:
|
||||||
|
ext = "mp3"
|
||||||
|
elif '.mp4' in url_val:
|
||||||
|
ext = "mp4"
|
||||||
|
|
||||||
|
media.append({
|
||||||
|
"url": url_val,
|
||||||
|
"ext": ext,
|
||||||
|
"status": resp["status"],
|
||||||
|
"content_type": resp["content_type"],
|
||||||
|
})
|
||||||
|
|
||||||
|
# Also check for video element src (blob URLs won't work, but worth checking)
|
||||||
|
if video_info and video_info.get('src'):
|
||||||
|
video_src = video_info['src']
|
||||||
|
if not video_src.startswith('blob:'):
|
||||||
|
media.append({
|
||||||
|
"url": video_src,
|
||||||
|
"ext": "mp4",
|
||||||
|
"status": 200,
|
||||||
|
"content_type": "video/mp4",
|
||||||
|
})
|
||||||
|
|
||||||
|
# If no media found, check page content for URLs
|
||||||
|
if not media:
|
||||||
|
content = await page.content()
|
||||||
|
# Look for video URLs in page source
|
||||||
|
content_urls = re.findall(r'https://[^\s"\'<>]+', content)
|
||||||
|
for url_val in content_urls:
|
||||||
|
if any(ext in url_val for ext in ['.mp4', '.m3u8']) and len(url_val) > 50:
|
||||||
|
if 'ttwstatic' not in url_val and 'rsrc.php' not in url_val:
|
||||||
|
if url_val not in seen:
|
||||||
|
seen.add(url_val)
|
||||||
|
ext = "m3u8" if '.m3u8' in url_val else "mp4"
|
||||||
|
media.append({
|
||||||
|
"url": url_val,
|
||||||
|
"ext": ext,
|
||||||
|
"status": 200,
|
||||||
|
"content_type": f"video/{ext}" if ext != 'm3u8' else 'application/x-mpegURL',
|
||||||
|
})
|
||||||
|
|
||||||
|
result = {
|
||||||
|
"success": len(media) > 0,
|
||||||
|
"title": title if title else "Unknown",
|
||||||
|
"platform": platform,
|
||||||
|
"media": media,
|
||||||
|
"media_count": len(media),
|
||||||
|
"provider": f"playwright-{platform}",
|
||||||
|
}
|
||||||
|
|
||||||
|
await browser.close()
|
||||||
|
print(json.dumps(result, indent=2))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) < 3:
|
||||||
|
print(json.dumps({"success": False, "error": "Usage: scrape_media.py <url> <platform>"}))
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
url = sys.argv[1]
|
||||||
|
platform = sys.argv[2]
|
||||||
|
|
||||||
|
asyncio.run(scrape(url, platform))
|
||||||
@@ -296,19 +296,18 @@ impl Anime2UseCases {
|
|||||||
.map(|ep| ep.url.clone())
|
.map(|ep| ep.url.clone())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let results: Vec<_> = futures::future::join_all(latest.iter().map(|url| {
|
let results: Vec<_> = futures::future::join_all(
|
||||||
self.repository.fetch_html(url)
|
latest.iter().map(|url| self.repository.fetch_html(url)),
|
||||||
}))
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
for (i, result) in results.into_iter().enumerate() {
|
for (i, result) in results.into_iter().enumerate() {
|
||||||
if let Ok(html) = result {
|
if let Ok(html) = result {
|
||||||
if let Ok(Some(dl_url)) =
|
if let Ok(Some(dl_url)) = tokio::task::spawn_blocking(move || {
|
||||||
tokio::task::spawn_blocking(move || {
|
parser::parse_episode_download(&html)
|
||||||
parser::parse_episode_download(&html)
|
})
|
||||||
})
|
.await
|
||||||
.await
|
.unwrap_or(Ok(None))
|
||||||
.unwrap_or(Ok(None))
|
|
||||||
{
|
{
|
||||||
if let Some(ep) = episodes.get_mut(i) {
|
if let Some(ep) = episodes.get_mut(i) {
|
||||||
ep.download_url = Some(dl_url);
|
ep.download_url = Some(dl_url);
|
||||||
|
|||||||
@@ -0,0 +1,186 @@
|
|||||||
|
//! Application-layer use cases for media downloading.
|
||||||
|
//!
|
||||||
|
//! Thin coordinators that delegate to `DownloaderRepository` and return
|
||||||
|
//! `DownloadResult` domain entities. Each use case corresponds to one
|
||||||
|
//! downloader endpoint defined in the spec (Shirokami-API reference).
|
||||||
|
|
||||||
|
use crate::domain::entity::downloader::DownloadResult;
|
||||||
|
use crate::domain::error::ScrapingError;
|
||||||
|
use crate::infrastructure::repository::DownloaderRepository;
|
||||||
|
|
||||||
|
/// Use case: unified dispatcher — auto-detects platform from URL pattern
|
||||||
|
/// and delegates to the appropriate downloader.
|
||||||
|
pub async fn download_all_in_one(
|
||||||
|
url: &str,
|
||||||
|
cookies: Option<&str>,
|
||||||
|
) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_all_in_one(url, cookies).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_instagram(
|
||||||
|
url: &str,
|
||||||
|
cookies: Option<&str>,
|
||||||
|
) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_instagram(url, cookies).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_facebook(
|
||||||
|
url: &str,
|
||||||
|
cookies: Option<&str>,
|
||||||
|
) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_facebook(url, cookies).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_tiktok(
|
||||||
|
url: &str,
|
||||||
|
cookies: Option<&str>,
|
||||||
|
) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_tiktok(url, cookies).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_youtube(url: &str, quality: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_youtube(url, quality).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_youtube_merge(
|
||||||
|
url: &str,
|
||||||
|
quality: &str,
|
||||||
|
) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::fetch_youtube_merge(url, quality).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_youtube_mp3(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_youtube_mp3(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_spotify(
|
||||||
|
url: &str,
|
||||||
|
api_key: Option<&str>,
|
||||||
|
) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_spotify(url, api_key).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_twitter(
|
||||||
|
url: &str,
|
||||||
|
cookies: Option<&str>,
|
||||||
|
) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_twitter(url, cookies).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_pinterest(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_pinterest(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_mega(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_mega(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_terabox(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_terabox(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_gdrive(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_gdrive(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_mediafire(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_mediafire(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_pixeldrain(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_pixeldrain(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_threads(
|
||||||
|
url: &str,
|
||||||
|
cookies: Option<&str>,
|
||||||
|
) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_threads(url, cookies).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_doodstream(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_doodstream(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_krakenfiles(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_krakenfiles(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_danbooru(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_danbooru(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_soundcloud(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_soundcloud(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_dailymotion(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_dailymotion(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_reddit(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_reddit(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_streamable(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_streamable(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_videy(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_videy(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_bilibili(url: &str) -> Result<DownloadResult, ScrapingError> {
|
||||||
|
DownloaderRepository::download_bilibili(url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Use case: detect platform from URL pattern.
|
||||||
|
pub fn detect_platform(url: &str) -> String {
|
||||||
|
if url.contains("instagram.com") || url.contains("instagr.am") {
|
||||||
|
"instagram".to_string()
|
||||||
|
} else if url.contains("facebook.com") || url.contains("fb.watch") {
|
||||||
|
"facebook".to_string()
|
||||||
|
} else if url.contains("tiktok.com") || url.contains("vm.tiktok.com") {
|
||||||
|
"tiktok".to_string()
|
||||||
|
} else if url.contains("youtube.com") || url.contains("youtu.be") {
|
||||||
|
"youtube".to_string()
|
||||||
|
} else if url.contains("open.spotify.com") || url.contains("spotify.link") {
|
||||||
|
"spotify".to_string()
|
||||||
|
} else if url.contains("twitter.com") || url.contains("x.com") || url.contains("t.co/") {
|
||||||
|
"twitter".to_string()
|
||||||
|
} else if url.contains("pinterest") {
|
||||||
|
"pinterest".to_string()
|
||||||
|
} else if url.contains("reddit.com") || url.contains("redd.it") {
|
||||||
|
"reddit".to_string()
|
||||||
|
} else if url.contains("mega.nz") || url.contains("mega.io") {
|
||||||
|
"mega".to_string()
|
||||||
|
} else if url.contains("terabox") || url.contains("nfile") {
|
||||||
|
"terabox".to_string()
|
||||||
|
} else if url.contains("drive.google.com") || url.contains("docs.google.com") {
|
||||||
|
"gdrive".to_string()
|
||||||
|
} else if url.contains("mediafire.com") {
|
||||||
|
"mediafire".to_string()
|
||||||
|
} else if url.contains("pixeldrain.com") {
|
||||||
|
"pixeldrain".to_string()
|
||||||
|
} else if url.contains("threads.net") || url.contains("threads.com") {
|
||||||
|
"threads".to_string()
|
||||||
|
} else if url.contains("dood.") || url.contains("doodstream") || url.contains("dood.so") {
|
||||||
|
"doodstream".to_string()
|
||||||
|
} else if url.contains("krakenfiles.com") {
|
||||||
|
"krakenfiles".to_string()
|
||||||
|
} else if url.contains("danbooru") || url.contains("safebooru") || url.contains("rule34") {
|
||||||
|
"danbooru".to_string()
|
||||||
|
} else if url.contains("soundcloud.com") {
|
||||||
|
"soundcloud".to_string()
|
||||||
|
} else if url.contains("dailymotion.com") {
|
||||||
|
"dailymotion".to_string()
|
||||||
|
} else if url.contains("streamable.com") {
|
||||||
|
"streamable".to_string()
|
||||||
|
} else if url.contains("videy.co") {
|
||||||
|
"videy".to_string()
|
||||||
|
} else if url.contains("bilibili.com") || url.contains("b23.tv") {
|
||||||
|
"bilibili".to_string()
|
||||||
|
} else {
|
||||||
|
"unknown".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
//! Application use-cases for image generation.
|
||||||
|
|
||||||
|
use crate::domain::error::ScrapingError;
|
||||||
|
use crate::infrastructure::repository::image as Repo;
|
||||||
|
|
||||||
|
pub async fn brat(text: &str) -> Result<Vec<u8>, ScrapingError> {
|
||||||
|
Repo::fetch_brat(text).await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn brat_animated(text: &str) -> Result<Vec<u8>, ScrapingError> {
|
||||||
|
Repo::fetch_brat_animated(text)
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
@@ -49,7 +49,7 @@ impl KomikUseCases {
|
|||||||
.get_or_set("komik:genres:list:v3", GENRE_LIST_CACHE_TTL, || async {
|
.get_or_set("komik:genres:list:v3", GENRE_LIST_CACHE_TTL, || async {
|
||||||
let html = self
|
let html = self
|
||||||
.repository
|
.repository
|
||||||
.fetch_html(&self.repository.api_url())
|
.fetch_html(&self.repository.base_url())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
let genres = tokio::task::spawn_blocking(move || parser::parse_genres(&html))
|
let genres = tokio::task::spawn_blocking(move || parser::parse_genres(&html))
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
//! Application use-cases for misc utilities.
|
||||||
|
|
||||||
|
use crate::domain::error::ScrapingError;
|
||||||
|
use crate::infrastructure::repository::misc as Repo;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
/// Currency converter.
|
||||||
|
pub async fn currency_converter(amount: f64, from: &str, to: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_currency_converter(amount, from, to)
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Harga emas Antam.
|
||||||
|
pub async fn harga_emas() -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_harga_emas().await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Kurs BCA (jual/beli).
|
||||||
|
pub async fn kurs_bca() -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_kurs_bca().await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Server info (OS/CPU/RAM/disk).
|
||||||
|
pub async fn server_info() -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_server_info().await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
@@ -1,4 +1,10 @@
|
|||||||
pub mod anime;
|
pub mod anime;
|
||||||
pub mod anime2;
|
pub mod anime2;
|
||||||
|
pub mod downloader;
|
||||||
|
pub mod image;
|
||||||
pub mod komik;
|
pub mod komik;
|
||||||
|
pub mod misc;
|
||||||
pub mod proxy;
|
pub mod proxy;
|
||||||
|
pub mod search;
|
||||||
|
pub mod stalk;
|
||||||
|
pub mod tools;
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
//! Application use-cases for search utilities.
|
||||||
|
|
||||||
|
use crate::domain::error::ScrapingError;
|
||||||
|
use crate::infrastructure::repository::search as Repo;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
pub async fn bmkg() -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_bmkg().await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn jadwal_sholat(kota: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_jadwal_sholat(kota)
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn weather(city: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_weather(city).await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn google(query: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_google(query).await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn yt_search(query: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_yt_search(query)
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
//! Application use-cases for stalk (profile/user lookups).
|
||||||
|
|
||||||
|
use crate::domain::error::ScrapingError;
|
||||||
|
use crate::infrastructure::repository::stalk as Repo;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
/// GitHub user profile stalk.
|
||||||
|
pub async fn github(username: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_github_stalk(username)
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// YouTube channel/profile stalk.
|
||||||
|
pub async fn youtube(username: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_youtube_stalk(username)
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Twitter/X user stalk.
|
||||||
|
pub async fn twitter(username: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_twitter_stalk(username)
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
//! Application use-cases for tools.
|
||||||
|
|
||||||
|
use crate::domain::error::ScrapingError;
|
||||||
|
use crate::infrastructure::repository::tools as Repo;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
pub async fn whois(domain: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_whois(domain).await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn ip_location(ip: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_ip_location(ip)
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn tinyurl(url: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_tinyurl(url).await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn check_hosting(domain: &str) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_check_hosting(domain)
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn hargapangan() -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_hargapangan().await.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn cek_resi(resi: String, ekspedisi: Option<String>) -> Result<Value, ScrapingError> {
|
||||||
|
Repo::fetch_cek_resi(&resi, ekspedisi.as_deref())
|
||||||
|
.await
|
||||||
|
.map_err(ScrapingError::Http)
|
||||||
|
}
|
||||||
+24
-8
@@ -26,7 +26,13 @@ impl Application {
|
|||||||
Err(_) => EnvFilter::new("warn,html5ever=error"),
|
Err(_) => EnvFilter::new("warn,html5ever=error"),
|
||||||
};
|
};
|
||||||
|
|
||||||
tracing_subscriber::fmt().with_env_filter(env_filter).init();
|
// Use mytheclipse-tracing's formatted layer, composed with the
|
||||||
|
// scraper's own env filter (default: warn + html5ever=error).
|
||||||
|
use tracing_subscriber::layer::{Layer, SubscriberExt};
|
||||||
|
use tracing_subscriber::util::SubscriberInitExt;
|
||||||
|
let _ = tracing_subscriber::registry()
|
||||||
|
.with(mytheclipse_tracing::TracingLayer::layer().with_filter(env_filter))
|
||||||
|
.try_init();
|
||||||
|
|
||||||
// Initialize OpenTelemetry metrics
|
// Initialize OpenTelemetry metrics
|
||||||
crate::observability::metrics::init_otel_metrics();
|
crate::observability::metrics::init_otel_metrics();
|
||||||
@@ -34,18 +40,28 @@ impl Application {
|
|||||||
tracing::info!("🚀 Scraper starting up...");
|
tracing::info!("🚀 Scraper starting up...");
|
||||||
tracing::info!(" Environment: {}", CONFIG.environment);
|
tracing::info!(" Environment: {}", CONFIG.environment);
|
||||||
|
|
||||||
// Log thread configuration
|
// Thread configuration from mytheclipse runtime_auto
|
||||||
let worker_threads = std::thread::available_parallelism()
|
let runtime_cfg = mytheclipse::runtime_auto::RuntimeConfig::auto();
|
||||||
.map(|n| n.get())
|
|
||||||
.unwrap_or(1);
|
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
" Tokio Worker Threads: (Defaulting to CPU cores: {})",
|
" Tokio Worker Threads: {} (auto from CPU cores)",
|
||||||
worker_threads
|
runtime_cfg.worker_threads
|
||||||
|
);
|
||||||
|
tracing::info!(
|
||||||
|
" Max Blocking Threads: {} | Compute Threads: {} | IO Threads: {}",
|
||||||
|
runtime_cfg.max_blocking_threads,
|
||||||
|
runtime_cfg.compute_threads,
|
||||||
|
runtime_cfg.io_threads
|
||||||
);
|
);
|
||||||
|
|
||||||
// Redis
|
// Redis
|
||||||
let _ = get_redis_conn().await;
|
let _ = get_redis_conn().await;
|
||||||
|
|
||||||
|
// Job queue + daily scheduler (mytheclipse-queue + mytheclipse::cron)
|
||||||
|
crate::infrastructure::queue::init_global_job_queue();
|
||||||
|
if let Err(e) = crate::infrastructure::scheduler::start_scheduler() {
|
||||||
|
tracing::error!("[scheduler] failed to start daily cleanup: {e}");
|
||||||
|
}
|
||||||
|
|
||||||
// Database
|
// Database
|
||||||
let mut opt = sea_orm::ConnectOptions::new(CONFIG.database_url.clone());
|
let mut opt = sea_orm::ConnectOptions::new(CONFIG.database_url.clone());
|
||||||
opt.max_connections(20)
|
opt.max_connections(20)
|
||||||
@@ -77,7 +93,7 @@ impl Application {
|
|||||||
|
|
||||||
// App State components
|
// App State components
|
||||||
let db_arc = Arc::new(db);
|
let db_arc = Arc::new(db);
|
||||||
let event_bus = Arc::new(crate::events::bus::EventBus::new());
|
let event_bus = Arc::new(crate::events::bus::new_event_bus());
|
||||||
|
|
||||||
let redis_pool = crate::infrastructure::cache::redis_pool::redis_pool()
|
let redis_pool = crate::infrastructure::cache::redis_pool::redis_pool()
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to init Redis pool: {}", e))?;
|
.map_err(|e| anyhow::anyhow!("Failed to init Redis pool: {}", e))?;
|
||||||
|
|||||||
+51
-21
@@ -5,7 +5,7 @@
|
|||||||
//! - Fails fast at startup if required variables are missing
|
//! - Fails fast at startup if required variables are missing
|
||||||
//! - Supports hierarchical configuration (default -> environment-specific)
|
//! - Supports hierarchical configuration (default -> environment-specific)
|
||||||
|
|
||||||
use config::{Config, ConfigError, Environment, File};
|
use mytheclipse_config::ConfigError;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
@@ -32,8 +32,8 @@ pub struct AppConfig {
|
|||||||
#[serde(default = "default_env")]
|
#[serde(default = "default_env")]
|
||||||
pub environment: String,
|
pub environment: String,
|
||||||
|
|
||||||
/// Allowed CORS origins (comma-separated)
|
/// Allowed CORS origins (comma-separated in env var, parsed to Vec)
|
||||||
#[serde(default)]
|
#[serde(default, deserialize_with = "deserialize_comma_separated")]
|
||||||
pub cors_origins: Vec<String>,
|
pub cors_origins: Vec<String>,
|
||||||
|
|
||||||
/// Log level (trace, debug, info, warn, error)
|
/// Log level (trace, debug, info, warn, error)
|
||||||
@@ -175,6 +175,19 @@ fn default_site_url() -> String {
|
|||||||
"https://asepharyana.my.id".to_string()
|
"https://asepharyana.my.id".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deserialize a comma-separated string into a Vec<String>.
|
||||||
|
/// Used for fields like cors_origins that come from a single env var.
|
||||||
|
fn deserialize_comma_separated<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let s = String::deserialize(deserializer)?;
|
||||||
|
Ok(s.split(',')
|
||||||
|
.map(|s| s.trim().to_string())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
fn default_db_max_connections() -> u32 {
|
fn default_db_max_connections() -> u32 {
|
||||||
100
|
100
|
||||||
}
|
}
|
||||||
@@ -207,6 +220,8 @@ impl AppConfig {
|
|||||||
/// 2. `config/{environment}.toml`
|
/// 2. `config/{environment}.toml`
|
||||||
/// 3. `config/default.toml`
|
/// 3. `config/default.toml`
|
||||||
pub fn load() -> Result<Self, ConfigError> {
|
pub fn load() -> Result<Self, ConfigError> {
|
||||||
|
use mytheclipse_config::ConfigLoader;
|
||||||
|
|
||||||
// Load .env file first
|
// Load .env file first
|
||||||
if let Err(e) = dotenvy::dotenv() {
|
if let Err(e) = dotenvy::dotenv() {
|
||||||
tracing::debug!("Could not load .env file: {}", e);
|
tracing::debug!("Could not load .env file: {}", e);
|
||||||
@@ -214,25 +229,40 @@ impl AppConfig {
|
|||||||
|
|
||||||
let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
|
let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
|
||||||
|
|
||||||
let config = Config::builder()
|
// Start with default config file (optional — many deploys are env-only)
|
||||||
// Start with default config file
|
let mut loader = ConfigLoader::<AppConfig>::new()
|
||||||
.add_source(File::with_name("config/default").required(false))
|
.load_dotenv(std::path::Path::new(".env"))
|
||||||
// Layer on environment-specific values
|
.unwrap_or_else(|_| ConfigLoader::<AppConfig>::new());
|
||||||
.add_source(File::with_name(&format!("config/{}", run_mode)).required(false))
|
|
||||||
// Add environment variables (with APP_ prefix)
|
|
||||||
.add_source(
|
|
||||||
Environment::with_prefix("APP")
|
|
||||||
.separator("__")
|
|
||||||
.try_parsing(true)
|
|
||||||
.list_separator(","),
|
|
||||||
)
|
|
||||||
// Map legacy env vars to new config structure
|
|
||||||
.set_override_option("database_url", env::var("DATABASE_URL").ok())?
|
|
||||||
.set_override_option("jwt_secret", env::var("JWT_SECRET").ok())?
|
|
||||||
.set_override_option("redis_url", env::var("REDIS_URL").ok())?
|
|
||||||
.build()?;
|
|
||||||
|
|
||||||
config.try_deserialize()
|
let default_path = std::path::Path::new("config/default.toml");
|
||||||
|
if default_path.exists() {
|
||||||
|
loader = loader.merge_file(default_path)?;
|
||||||
|
}
|
||||||
|
// Layer on environment-specific values (optional)
|
||||||
|
let env_path_str = format!("config/{}.toml", run_mode);
|
||||||
|
let env_path = std::path::Path::new(&env_path_str);
|
||||||
|
if env_path.exists() {
|
||||||
|
loader = loader.merge_file(env_path)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add environment variables (with APP_ prefix)
|
||||||
|
loader = loader.merge_env("APP");
|
||||||
|
|
||||||
|
// Map legacy env vars to new config structure.
|
||||||
|
// ConfigLoader merges at leaf level; legacy vars override the merged
|
||||||
|
// value directly (highest priority after APP_*).
|
||||||
|
let mut value = loader.peek().clone();
|
||||||
|
if let Ok(v) = env::var("DATABASE_URL") {
|
||||||
|
value["database_url"] = serde_json::Value::String(v);
|
||||||
|
}
|
||||||
|
if let Ok(v) = env::var("JWT_SECRET") {
|
||||||
|
value["jwt_secret"] = serde_json::Value::String(v);
|
||||||
|
}
|
||||||
|
if let Ok(v) = env::var("REDIS_URL") {
|
||||||
|
value["redis_url"] = serde_json::Value::String(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigLoader::<AppConfig>::new().merge_value(value).build()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if running in production mode
|
/// Check if running in production mode
|
||||||
|
|||||||
@@ -289,5 +289,3 @@ pub struct FilterAnimeItem {
|
|||||||
pub r#type: String,
|
pub r#type: String,
|
||||||
pub anime_url: String,
|
pub anime_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
//! Domain entities for media downloader data.
|
||||||
|
//!
|
||||||
|
//! Pure domain structs representing download results from various social
|
||||||
|
//! platforms and file hosts. No framework dependencies beyond serde + utoipa.
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
/// Media type classification for a download link.
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
|
||||||
|
pub enum MediaType {
|
||||||
|
Video,
|
||||||
|
Audio,
|
||||||
|
Image,
|
||||||
|
File,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The status of a download attempt.
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum DownloadStatus {
|
||||||
|
Success,
|
||||||
|
/// Provider returned a known error (bad URL, rate limited, etc.)
|
||||||
|
Error,
|
||||||
|
/// All providers failed
|
||||||
|
Failed,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A single downloadable media item (a video/audio/image/file link).
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
|
||||||
|
pub struct MediaItem {
|
||||||
|
pub url: String,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub quality: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub file_type: Option<MediaType>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub extension: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub thumbnail: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub file_size: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub size_bytes: Option<u64>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub frame_width: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub frame_height: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub note: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unified download result returned by every downloader endpoint.
|
||||||
|
///
|
||||||
|
/// This is intentionally flexible — different platforms return different
|
||||||
|
/// metadata (some have duration, some don't), so most fields are `Option`.
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
|
||||||
|
pub struct DownloadResult {
|
||||||
|
pub status: DownloadStatus,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub message: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub title: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub author: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub thumbnail: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub duration: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub description: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||||
|
pub media: Vec<MediaItem>,
|
||||||
|
/// Provider that supplied the result (e.g. "snapsave", "tikwm", "savetube")
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub provider: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DownloadResult {
|
||||||
|
pub fn success(title: Option<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
status: DownloadStatus::Success,
|
||||||
|
message: None,
|
||||||
|
title,
|
||||||
|
author: None,
|
||||||
|
thumbnail: None,
|
||||||
|
duration: None,
|
||||||
|
description: None,
|
||||||
|
media: Vec::new(),
|
||||||
|
provider: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn error(message: impl Into<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
status: DownloadStatus::Error,
|
||||||
|
message: Some(message.into()),
|
||||||
|
title: None,
|
||||||
|
author: None,
|
||||||
|
thumbnail: None,
|
||||||
|
duration: None,
|
||||||
|
description: None,
|
||||||
|
media: Vec::new(),
|
||||||
|
provider: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_media(mut self, item: MediaItem) -> Self {
|
||||||
|
self.media.push(item);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod anime;
|
pub mod anime;
|
||||||
|
pub mod downloader;
|
||||||
pub mod komik;
|
pub mod komik;
|
||||||
+18
-120
@@ -1,146 +1,44 @@
|
|||||||
//! Event bus implementation.
|
//! Event bus — delegated to `mytheclipse_event`.
|
||||||
|
//!
|
||||||
|
//! The scraper uses `TypedEventBus<InMemoryEventBus>` from the mytheclipse
|
||||||
|
//! event crate (JSON-typed pub/sub over an in-process broadcast bus).
|
||||||
|
//! Domain events are plain serde structs; `mytheclipse_event::Event` is a
|
||||||
|
//! blanket trait, so every `Serialize + DeserializeOwned + Send + Sync + 'static`
|
||||||
|
//! type is automatically an event — no manual impl needed.
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use mytheclipse_event::{InMemoryEventBus, TypedEventBus};
|
||||||
|
|
||||||
use std::{any::TypeId, collections::HashMap, sync::Arc};
|
/// Event payload marker — re-exported so domain types can reference it.
|
||||||
use tokio::sync::{broadcast, RwLock};
|
pub use mytheclipse_event::Event;
|
||||||
use tracing::{debug, info};
|
|
||||||
|
|
||||||
/// Trait for events that can be published.
|
/// The scraper's application event bus.
|
||||||
pub trait Event: Clone + Send + Sync + 'static {
|
pub type EventBus = TypedEventBus<InMemoryEventBus>;
|
||||||
/// Event name for logging/debugging.
|
|
||||||
const NAME: &'static str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Trait for event handlers.
|
/// Build a new in-memory event bus.
|
||||||
#[async_trait]
|
pub fn new_event_bus() -> EventBus {
|
||||||
pub trait EventHandler<E: Event>: Send + Sync {
|
TypedEventBus::new(InMemoryEventBus::default())
|
||||||
async fn handle(&self, event: E);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The event bus for publishing and subscribing to events.
|
|
||||||
pub struct EventBus {
|
|
||||||
channels: RwLock<HashMap<TypeId, Box<dyn std::any::Any + Send + Sync>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EventBus {
|
|
||||||
/// Create a new event bus.
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
channels: RwLock::new(HashMap::new()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Publish an event to all subscribers.
|
|
||||||
pub async fn publish<E: Event>(&self, event: E) {
|
|
||||||
let type_id = TypeId::of::<E>();
|
|
||||||
let channels = self.channels.read().await;
|
|
||||||
|
|
||||||
if let Some(sender) = channels.get(&type_id) {
|
|
||||||
if let Some(tx) = sender.downcast_ref::<broadcast::Sender<E>>() {
|
|
||||||
let _ = tx.send(event);
|
|
||||||
debug!("Published event: {}", E::NAME);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Subscribe to events of a specific type.
|
|
||||||
/// Returns a receiver that can be used to receive events.
|
|
||||||
pub async fn subscribe<E: Event>(&self) -> broadcast::Receiver<E> {
|
|
||||||
let type_id = TypeId::of::<E>();
|
|
||||||
|
|
||||||
// Check if channel exists
|
|
||||||
{
|
|
||||||
let channels = self.channels.read().await;
|
|
||||||
if let Some(sender) = channels.get(&type_id) {
|
|
||||||
if let Some(tx) = sender.downcast_ref::<broadcast::Sender<E>>() {
|
|
||||||
return tx.subscribe();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create new channel
|
|
||||||
let (tx, rx) = broadcast::channel::<E>(100);
|
|
||||||
{
|
|
||||||
let mut channels = self.channels.write().await;
|
|
||||||
channels.insert(type_id, Box::new(tx));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Re-get the receiver from the stored sender
|
|
||||||
let channels = self.channels.read().await;
|
|
||||||
if let Some(sender) = channels.get(&type_id) {
|
|
||||||
if let Some(tx) = sender.downcast_ref::<broadcast::Sender<E>>() {
|
|
||||||
return tx.subscribe();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rx
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Register a handler for a specific event type.
|
|
||||||
/// The handler will be called whenever an event of that type is published.
|
|
||||||
pub async fn on<E: Event, H: EventHandler<E> + 'static>(&self, handler: H) {
|
|
||||||
let mut rx = self.subscribe::<E>().await;
|
|
||||||
let handler = Arc::new(handler);
|
|
||||||
|
|
||||||
tokio::spawn(async move {
|
|
||||||
loop {
|
|
||||||
match rx.recv().await {
|
|
||||||
Ok(event) => {
|
|
||||||
handler.handle(event).await;
|
|
||||||
}
|
|
||||||
Err(broadcast::error::RecvError::Closed) => break,
|
|
||||||
Err(broadcast::error::RecvError::Lagged(n)) => {
|
|
||||||
tracing::warn!("Event handler lagged by {} events", n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
info!("Registered handler for event: {}", E::NAME);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for EventBus {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common events
|
// Common events
|
||||||
/// User registered event.
|
/// User registered event.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct UserRegistered {
|
pub struct UserRegistered {
|
||||||
pub user_id: String,
|
pub user_id: String,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event for UserRegistered {
|
|
||||||
const NAME: &'static str = "user.registered";
|
|
||||||
}
|
|
||||||
|
|
||||||
/// User logged in event.
|
/// User logged in event.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct UserLoggedIn {
|
pub struct UserLoggedIn {
|
||||||
pub user_id: String,
|
pub user_id: String,
|
||||||
pub ip_address: Option<String>,
|
pub ip_address: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event for UserLoggedIn {
|
|
||||||
const NAME: &'static str = "user.logged_in";
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Order created event.
|
/// Order created event.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct OrderCreated {
|
pub struct OrderCreated {
|
||||||
pub order_id: String,
|
pub order_id: String,
|
||||||
pub user_id: String,
|
pub user_id: String,
|
||||||
pub total: f64,
|
pub total: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event for OrderCreated {
|
|
||||||
const NAME: &'static str = "order.created";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Vendored
+1
@@ -1,3 +1,4 @@
|
|||||||
|
pub mod mytheclipse;
|
||||||
pub mod redis;
|
pub mod redis;
|
||||||
pub mod redis_pool;
|
pub mod redis_pool;
|
||||||
|
|
||||||
|
|||||||
+53
@@ -0,0 +1,53 @@
|
|||||||
|
//! Bridge between the deadpool Redis pool and `mytheclipse_cache::RedisCache`.
|
||||||
|
//!
|
||||||
|
//! The scraper keeps its deadpool `Pool` (connection lifecycle, recycling) and
|
||||||
|
//! hands each checked-out connection to mytheclipse's `RedisCache` (which
|
||||||
|
//! implements the library `Cache` trait). Because both sides now share the
|
||||||
|
//! same `redis` crate version, a `deadpool_redis::Connection` can be converted
|
||||||
|
//! directly into a `redis::aio::MultiplexedConnection` via `take()`.
|
||||||
|
|
||||||
|
use mytheclipse_cache::{Cache, CacheError, RedisCache};
|
||||||
|
|
||||||
|
use crate::infrastructure::cache::redis_pool::redis_pool;
|
||||||
|
|
||||||
|
/// Build a fresh `RedisCache` from a freshly checked-out deadpool connection
|
||||||
|
/// on each call.
|
||||||
|
///
|
||||||
|
/// Why fresh on every call (not a cached singleton): the previous design
|
||||||
|
/// initialised *one* `RedisCache` (one multiplexed connection) on first use and
|
||||||
|
/// kept it forever. If that single connection broke (Redis restart, idle
|
||||||
|
/// timeout, network blip), every cache operation failed with `broken pipe`
|
||||||
|
/// until the whole process restarted — and because `Cache::get_or_set`
|
||||||
|
/// propagated write errors, **every API returned 500**.
|
||||||
|
///
|
||||||
|
/// Building fresh lets deadpool recycle and re-establish broken connections on
|
||||||
|
/// checkout, so caching self-heals without a process restart. The multiplexed
|
||||||
|
/// connection is cheaply cloneable (`Arc`-backed), so a per-call pool checkout
|
||||||
|
/// is negligible overhead next to the network I/O.
|
||||||
|
pub(crate) async fn fresh_redis_cache() -> Result<RedisCache, CacheError> {
|
||||||
|
let pool = redis_pool().map_err(CacheError::Io)?;
|
||||||
|
let conn = pool
|
||||||
|
.get()
|
||||||
|
.await
|
||||||
|
.map_err(|e| CacheError::Io(e.to_string()))?;
|
||||||
|
let mux = deadpool_redis::Connection::take(conn);
|
||||||
|
Ok(RedisCache::new(mux))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convenience wrappers so callers can use the mytheclipse `Cache` methods
|
||||||
|
/// directly without importing the trait twice.
|
||||||
|
pub async fn get(key: &str) -> Result<Option<Vec<u8>>, CacheError> {
|
||||||
|
fresh_redis_cache().await?.get(key).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set(
|
||||||
|
key: &str,
|
||||||
|
value: Vec<u8>,
|
||||||
|
ttl: Option<std::time::Duration>,
|
||||||
|
) -> Result<(), CacheError> {
|
||||||
|
fresh_redis_cache().await?.set(key, value, ttl).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn invalidate(key: &str) -> Result<(), CacheError> {
|
||||||
|
fresh_redis_cache().await?.invalidate(key).await
|
||||||
|
}
|
||||||
Vendored
+55
-58
@@ -1,70 +1,59 @@
|
|||||||
//! Redis caching helpers.
|
//! Redis caching helpers — typed wrapper over `mytheclipse_cache`.
|
||||||
|
//!
|
||||||
|
//! The underlying byte-cache is `mytheclipse_cache::RedisCache` (which
|
||||||
|
//! implements the library `Cache` trait). This module keeps the ergonomic
|
||||||
|
//! typed JSON surface the use cases rely on (`get_or_set`, `set_with_ttl`)
|
||||||
|
//! while delegating the actual Redis commands to the library.
|
||||||
|
|
||||||
use deadpool_redis::redis::AsyncCommands;
|
use mytheclipse_cache::{Cache as CacheTrait, CacheError};
|
||||||
use deadpool_redis::Pool;
|
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
use tracing::{debug, error};
|
use tracing::debug;
|
||||||
|
|
||||||
/// Default cache TTL in seconds (5 minutes).
|
/// Default cache TTL in seconds (5 minutes).
|
||||||
pub const DEFAULT_CACHE_TTL: u64 = 300;
|
pub const DEFAULT_CACHE_TTL: u64 = 300;
|
||||||
|
|
||||||
/// Cache helper for Redis operations.
|
/// Typed JSON cache helper over the shared mytheclipse `RedisCache`.
|
||||||
pub struct Cache<'a> {
|
pub struct Cache<'a> {
|
||||||
pool: &'a Pool,
|
_marker: std::marker::PhantomData<&'a ()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Cache<'a> {
|
impl<'a> Cache<'a> {
|
||||||
pub fn new(pool: &'a Pool) -> Self {
|
pub fn new(_pool: &'a deadpool_redis::Pool) -> Self {
|
||||||
Self { pool }
|
Self {
|
||||||
|
_marker: std::marker::PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cache(&self) -> Result<mytheclipse_cache::RedisCache, CacheError> {
|
||||||
|
super::mytheclipse::fresh_redis_cache().await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T> {
|
pub async fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T> {
|
||||||
let mut conn = match self.pool.get().await {
|
match self.cache().await {
|
||||||
Ok(c) => c,
|
Ok(cache) => match CacheTrait::get(&cache, key).await {
|
||||||
|
Ok(Some(bytes)) => serde_json::from_slice(&bytes).ok(),
|
||||||
|
Ok(None) => None,
|
||||||
|
Err(e) => {
|
||||||
|
debug!("Cache: get error for {}: {}", key, e);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Cache: failed to get connection: {}", e);
|
debug!("Cache: unavailable for {}: {}", key, e);
|
||||||
return None;
|
None
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let cached: Option<String> = conn.get(key).await.ok()?;
|
|
||||||
|
|
||||||
if cached.is_some() {
|
|
||||||
debug!("Cache hit: {}", key);
|
|
||||||
} else {
|
|
||||||
debug!("Cache miss: {}", key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cached.and_then(|json| serde_json::from_str(&json).ok())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn mget<T: DeserializeOwned>(&self, keys: &[String]) -> Vec<Option<T>> {
|
pub async fn mget<T: DeserializeOwned>(&self, keys: &[String]) -> Vec<Option<T>> {
|
||||||
if keys.is_empty() {
|
if keys.is_empty() {
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
}
|
}
|
||||||
|
let mut out = Vec::with_capacity(keys.len());
|
||||||
let mut conn = match self.pool.get().await {
|
for k in keys {
|
||||||
Ok(c) => c,
|
out.push(self.get::<T>(k).await);
|
||||||
Err(e) => {
|
}
|
||||||
error!("Cache: failed to get connection: {}", e);
|
out
|
||||||
return std::iter::repeat_with(|| None).take(keys.len()).collect();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
use deadpool_redis::redis::cmd;
|
|
||||||
let cached_values: Vec<Option<String>> =
|
|
||||||
match cmd("MGET").arg(keys).query_async(&mut conn).await {
|
|
||||||
Ok(v) => v,
|
|
||||||
Err(e) => {
|
|
||||||
error!("Cache: failed to mget values: {}", e);
|
|
||||||
return std::iter::repeat_with(|| None).take(keys.len()).collect();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
cached_values
|
|
||||||
.into_iter()
|
|
||||||
.map(|opt_s| opt_s.and_then(|json| serde_json::from_str(&json).ok()))
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set<T: Serialize>(&self, key: &str, value: &T) -> Result<(), String> {
|
pub async fn set<T: Serialize>(&self, key: &str, value: &T) -> Result<(), String> {
|
||||||
@@ -77,9 +66,10 @@ impl<'a> Cache<'a> {
|
|||||||
value: &T,
|
value: &T,
|
||||||
ttl_secs: u64,
|
ttl_secs: u64,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let mut conn = self.pool.get().await.map_err(|e| e.to_string())?;
|
let json = serde_json::to_vec(value).map_err(|e| e.to_string())?;
|
||||||
let json = serde_json::to_string(value).map_err(|e| e.to_string())?;
|
let ttl = std::time::Duration::from_secs(ttl_secs);
|
||||||
conn.set_ex::<_, _, ()>(key, json, ttl_secs)
|
let cache = self.cache().await.map_err(|e| e.to_string())?;
|
||||||
|
CacheTrait::set(&cache, key, json, Some(ttl))
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
debug!("Cache: set key {} with TTL {}s", key, ttl_secs);
|
debug!("Cache: set key {} with TTL {}s", key, ttl_secs);
|
||||||
@@ -87,21 +77,23 @@ impl<'a> Cache<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(&self, key: &str) -> Result<(), String> {
|
pub async fn delete(&self, key: &str) -> Result<(), String> {
|
||||||
let mut conn = self.pool.get().await.map_err(|e| e.to_string())?;
|
let cache = self.cache().await.map_err(|e| e.to_string())?;
|
||||||
conn.del::<_, ()>(key).await.map_err(|e| e.to_string())?;
|
CacheTrait::invalidate(&cache, key)
|
||||||
debug!("Cache: deleted key {}", key);
|
.await
|
||||||
Ok(())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn exists(&self, key: &str) -> bool {
|
pub async fn exists(&self, key: &str) -> bool {
|
||||||
let mut conn = match self.pool.get().await {
|
self.get::<serde_json::Value>(key).await.is_some()
|
||||||
Ok(c) => c,
|
|
||||||
Err(_) => return false,
|
|
||||||
};
|
|
||||||
conn.exists::<_, bool>(key).await.unwrap_or(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get or set: returns cached value or computes and caches new value.
|
/// Get or set: returns cached value or computes and caches new value.
|
||||||
|
///
|
||||||
|
/// The cache is best-effort: if the post-compute write fails (e.g. a
|
||||||
|
/// transient Redis outage or a broken pooled connection), the freshly
|
||||||
|
/// computed value is still returned rather than propagating a 500. Only a
|
||||||
|
/// cache *read* failure is silently tolerated; a compute failure still
|
||||||
|
/// propagates.
|
||||||
pub async fn get_or_set<T, F, Fut>(
|
pub async fn get_or_set<T, F, Fut>(
|
||||||
&self,
|
&self,
|
||||||
key: &str,
|
key: &str,
|
||||||
@@ -120,7 +112,12 @@ impl<'a> Cache<'a> {
|
|||||||
|
|
||||||
debug!("Cache miss: {}", key);
|
debug!("Cache miss: {}", key);
|
||||||
let value = compute().await?;
|
let value = compute().await?;
|
||||||
self.set_with_ttl(key, &value, ttl_secs).await?;
|
// Best-effort write: a failure here must not fail the request — the
|
||||||
|
// value is already valid. Log and continue (read path swallows errors
|
||||||
|
// too, so a broken cache degrades to cache-less, never to 500).
|
||||||
|
if let Err(e) = self.set_with_ttl(key, &value, ttl_secs).await {
|
||||||
|
debug!("Cache: failed to write {} (non-fatal): {}", key, e);
|
||||||
|
}
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
pub mod cache;
|
pub mod cache;
|
||||||
|
pub mod queue;
|
||||||
pub mod repository;
|
pub mod repository;
|
||||||
|
pub mod scheduler;
|
||||||
pub mod scraping;
|
pub mod scraping;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
//! Application job queue — backed by `mytheclipse-queue`.
|
||||||
|
//!
|
||||||
|
//! The scraper keeps a small in-process job queue for background maintenance
|
||||||
|
//! work (currently: image-cache repair / re-sync jobs). The queue is an
|
||||||
|
//! [`InMemoryQueue`] consumed by a [`WorkerPool`] with bounded concurrency
|
||||||
|
//! and retry/backoff, all from the mytheclipse queue crate.
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use mytheclipse_queue::in_memory::InMemoryQueue;
|
||||||
|
use mytheclipse_queue::traits::Queue;
|
||||||
|
use mytheclipse_queue::worker::{JobHandler, WorkerConfig, WorkerPool};
|
||||||
|
|
||||||
|
/// Topic for background image-cache repair jobs.
|
||||||
|
pub const TOPIC_CACHE_REPAIR: &str = "scrape:repair";
|
||||||
|
|
||||||
|
/// A handle to the application job queue + its worker pool.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct JobQueue {
|
||||||
|
queue: Arc<InMemoryQueue>,
|
||||||
|
pool: Arc<WorkerPool<InMemoryQueue>>,
|
||||||
|
backpressure: Arc<mytheclipse_queue::backpressure_enqueue::BackpressureEnforcer>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl JobQueue {
|
||||||
|
/// Builds a queue with `concurrency` workers consuming the repair topic.
|
||||||
|
pub fn new(concurrency: usize) -> Self {
|
||||||
|
let queue = InMemoryQueue::new();
|
||||||
|
let pool = Arc::new(WorkerPool::new(queue.clone(), concurrency));
|
||||||
|
let backpressure = Arc::new(
|
||||||
|
mytheclipse_queue::backpressure_enqueue::BackpressureEnforcer::new(concurrency.max(4)),
|
||||||
|
);
|
||||||
|
Self {
|
||||||
|
queue: Arc::new(queue),
|
||||||
|
pool,
|
||||||
|
backpressure,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Starts `concurrency` workers for the given topic and handler.
|
||||||
|
pub fn start_workers<H>(&self, topic: &str, handler: H)
|
||||||
|
where
|
||||||
|
H: JobHandler + 'static,
|
||||||
|
{
|
||||||
|
self.pool.start(topic, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enqueues a raw payload onto `topic`, rejecting under backpressure.
|
||||||
|
pub async fn enqueue(&self, topic: &str, payload: Vec<u8>) -> Result<(), String> {
|
||||||
|
match self
|
||||||
|
.backpressure
|
||||||
|
.try_enqueue(&*self.queue, topic, payload)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(()) => Ok(()),
|
||||||
|
Err(e) => Err(format!("queue backpressure: {e}")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Number of jobs waiting in a topic.
|
||||||
|
pub async fn len(&self, topic: &str) -> u64 {
|
||||||
|
self.queue.len(topic).await.unwrap_or(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Underlying queue reference (for direct Queue trait calls).
|
||||||
|
pub fn queue(&self) -> &Arc<InMemoryQueue> {
|
||||||
|
&self.queue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Default worker configuration for repair jobs: 4 concurrent, 3 retries,
|
||||||
|
/// exponential backoff 500ms→10s.
|
||||||
|
pub fn repair_worker_config() -> WorkerConfig {
|
||||||
|
WorkerConfig {
|
||||||
|
concurrency: 4,
|
||||||
|
max_retries: 3,
|
||||||
|
retry_base_delay: Duration::from_millis(500),
|
||||||
|
retry_max_delay: Duration::from_secs(10),
|
||||||
|
retry_factor: 2.0,
|
||||||
|
visibility_timeout: Duration::from_secs(30),
|
||||||
|
poll_interval: Duration::from_millis(100),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convenience: enqueue a cache-repair job for a poster URL.
|
||||||
|
pub async fn enqueue_cache_repair(url: String) -> Result<(), String> {
|
||||||
|
let queue = crate::infrastructure::queue::global_job_queue();
|
||||||
|
queue.enqueue(TOPIC_CACHE_REPAIR, url.into_bytes()).await
|
||||||
|
}
|
||||||
|
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
|
static JOB_QUEUE: OnceLock<JobQueue> = OnceLock::new();
|
||||||
|
|
||||||
|
/// Global job queue handle.
|
||||||
|
pub fn global_job_queue() -> &'static JobQueue {
|
||||||
|
JOB_QUEUE.get_or_init(|| JobQueue::new(4))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initialize the global queue with its repair-topic workers.
|
||||||
|
pub fn init_global_job_queue() {
|
||||||
|
let queue = global_job_queue();
|
||||||
|
queue.start_workers(
|
||||||
|
TOPIC_CACHE_REPAIR,
|
||||||
|
|job: mytheclipse_queue::job::Job| async move {
|
||||||
|
let url = String::from_utf8_lossy(&job.payload).to_string();
|
||||||
|
tracing::info!("[repair] processing cache job for {url}");
|
||||||
|
// Current repair action: log + no-op cache touch. Real repair logic
|
||||||
|
// (re-fetch + re-upload poster) is wired in a follow-up round.
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,46 @@
|
|||||||
|
//! Infrastructure — Image generation endpoints (brat).
|
||||||
|
//!
|
||||||
|
//! Ported from Shirokami-API `scraper/image/brat.js`.
|
||||||
|
//! brat v2 uses the external brat.siputzx.my.id API (returns image bytes).
|
||||||
|
|
||||||
|
use crate::infrastructure::utils::http_client::http_client;
|
||||||
|
use reqwest::header::USER_AGENT;
|
||||||
|
|
||||||
|
const BRAT_UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0 Safari/537.36";
|
||||||
|
|
||||||
|
fn urlencode(s: &str) -> String {
|
||||||
|
s.replace('%', "%25")
|
||||||
|
.replace('&', "%26")
|
||||||
|
.replace('?', "%3F")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch a brat image (static) for the given text.
|
||||||
|
pub async fn fetch_brat(text: &str) -> Result<Vec<u8>, String> {
|
||||||
|
let url = format!("https://brat.siputzx.my.id/image?text={}", urlencode(text));
|
||||||
|
fetch_bytes(&url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch an animated brat GIF for the given text.
|
||||||
|
pub async fn fetch_brat_animated(text: &str) -> Result<Vec<u8>, String> {
|
||||||
|
let url = format!("https://brat.siputzx.my.id/gif?text={}", urlencode(text));
|
||||||
|
fetch_bytes(&url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_bytes(url: &str) -> Result<Vec<u8>, String> {
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(url)
|
||||||
|
.header(USER_AGENT, BRAT_UA)
|
||||||
|
.header("X-Forwarded-For", "104.28.209.35")
|
||||||
|
.header("X-Real-IP", "104.28.209.35")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP: {}", e))?;
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("HTTP {}", resp.status()));
|
||||||
|
}
|
||||||
|
resp.bytes()
|
||||||
|
.await
|
||||||
|
.map(|b| b.to_vec())
|
||||||
|
.map_err(|e| format!("Body: {}", e))
|
||||||
|
}
|
||||||
@@ -0,0 +1,352 @@
|
|||||||
|
//! Infrastructure — Misc utility scrapers (currency-converter, harga-emas, kurs-bca, server-info).
|
||||||
|
//!
|
||||||
|
//! Ported from Shirokami-API `scraper/misc/*.js`.
|
||||||
|
//! HTML scraping via reqwest + scraper crate.
|
||||||
|
|
||||||
|
use crate::infrastructure::utils::http_client::http_client;
|
||||||
|
use regex::Regex;
|
||||||
|
use reqwest::header::USER_AGENT;
|
||||||
|
use scraper::{Html, Selector};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
|
/// Currency converter: scrape wise.com exchange rate page.
|
||||||
|
/// `amount`: amount to convert, `from`/`to`: currency codes (e.g. USD→IDR).
|
||||||
|
pub async fn fetch_currency_converter(amount: f64, from: &str, to: &str) -> Result<Value, String> {
|
||||||
|
let from_lower = from.to_lowercase();
|
||||||
|
let to_lower = to.to_lowercase();
|
||||||
|
let url = format!(
|
||||||
|
"https://wise.com/gb/currency-converter/{}-to-{}-rate?amount={}",
|
||||||
|
from_lower, to_lower, amount
|
||||||
|
);
|
||||||
|
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(&url)
|
||||||
|
.header(
|
||||||
|
USER_AGENT,
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||||
|
)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP error: {}", e))?;
|
||||||
|
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("HTTP {} from wise.com", resp.status()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = resp
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to read body: {}", e))?;
|
||||||
|
|
||||||
|
// Try regex first: "1 USD = 15,885.00 IDR"
|
||||||
|
let re = Regex::new(r"1 [A-Z]{3} = ([0-9,.]+) [A-Z]{3}")
|
||||||
|
.map_err(|e| format!("Regex compile: {}", e))?;
|
||||||
|
|
||||||
|
if let Some(caps) = re.captures(&body) {
|
||||||
|
let rate_str = caps.get(1).map(|m| m.as_str()).unwrap_or("0");
|
||||||
|
let rate = rate_str.replace(',', "").parse::<f64>().unwrap_or(0.0);
|
||||||
|
if rate > 0.0 {
|
||||||
|
let result = amount * rate;
|
||||||
|
return Ok(json!({
|
||||||
|
"from": from.to_uppercase(),
|
||||||
|
"to": to.to_uppercase(),
|
||||||
|
"amount": amount,
|
||||||
|
"rate": rate,
|
||||||
|
"result": result,
|
||||||
|
"updateTime": chrono::Utc::now().to_rfc3339(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: scrape the page for any rate info
|
||||||
|
let document = Html::parse_document(&body);
|
||||||
|
let selector = Selector::parse("body").unwrap();
|
||||||
|
let text = document
|
||||||
|
.select(&selector)
|
||||||
|
.flat_map(|e| e.text())
|
||||||
|
.collect::<String>();
|
||||||
|
|
||||||
|
// Look for patterns like "15,885.00 IDR" in body text
|
||||||
|
let re2 = Regex::new(&format!(r"([0-9,.]+)\s+{}", to.to_uppercase()))
|
||||||
|
.map_err(|e| format!("Regex compile: {}", e))?;
|
||||||
|
|
||||||
|
if let Some(caps) = re2.captures(&text) {
|
||||||
|
let rate_str = caps.get(1).map(|m| m.as_str()).unwrap_or("0");
|
||||||
|
let rate = rate_str.replace(',', "").parse::<f64>().unwrap_or(0.0);
|
||||||
|
if rate > 0.0 {
|
||||||
|
let result = amount * rate;
|
||||||
|
return Ok(json!({
|
||||||
|
"from": from.to_uppercase(),
|
||||||
|
"to": to.to_uppercase(),
|
||||||
|
"amount": amount,
|
||||||
|
"rate": rate,
|
||||||
|
"result": result,
|
||||||
|
"updateTime": chrono::Utc::now().to_rfc3339(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err("Gagal mendapatkan kurs dari wise.com".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scrape harga emas Antam from harga-emas.org.
|
||||||
|
pub async fn fetch_harga_emas() -> Result<Value, String> {
|
||||||
|
let url = "https://harga-emas.org/";
|
||||||
|
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(url)
|
||||||
|
.header(
|
||||||
|
USER_AGENT,
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||||
|
)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP error: {}", e))?;
|
||||||
|
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("HTTP {} from harga-emas.org", resp.status()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = resp
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to read body: {}", e))?;
|
||||||
|
|
||||||
|
let document = Html::parse_document(&body);
|
||||||
|
|
||||||
|
let mut result = json!({
|
||||||
|
"title": "Harga Emas Antam",
|
||||||
|
"hargaUtama": "N/A",
|
||||||
|
"perubahan": "0",
|
||||||
|
"detail": [],
|
||||||
|
});
|
||||||
|
|
||||||
|
// Look for tables containing "Antam" + "Pegadaian"
|
||||||
|
let table_sel = Selector::parse("table").unwrap();
|
||||||
|
let tr_sel = Selector::parse("tbody tr").unwrap();
|
||||||
|
let td_sel = Selector::parse("td").unwrap();
|
||||||
|
|
||||||
|
let mut detail: Vec<Value> = Vec::new();
|
||||||
|
|
||||||
|
for table in document.select(&table_sel) {
|
||||||
|
let table_text = table.text().collect::<String>();
|
||||||
|
if table_text.contains("Antam") && table_text.contains("Pegadaian") {
|
||||||
|
for tr in table.select(&tr_sel) {
|
||||||
|
let cols: Vec<String> = tr
|
||||||
|
.select(&td_sel)
|
||||||
|
.map(|td| td.text().collect::<String>())
|
||||||
|
.map(|s| s.trim().to_string())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if cols.len() >= 2 {
|
||||||
|
let label = cols[0].trim();
|
||||||
|
let price = cols[1].trim();
|
||||||
|
|
||||||
|
if !label.is_empty() && !price.is_empty() {
|
||||||
|
// Check if label is a numeric gram value
|
||||||
|
let numeric_label: String =
|
||||||
|
label.chars().filter(|c| c.is_ascii_digit()).collect();
|
||||||
|
if !numeric_label.is_empty() {
|
||||||
|
detail.push(json!({
|
||||||
|
"ukuran": format!("{} gr", label.replace(" gr", "").replace("gr", "").trim()),
|
||||||
|
"harga": format!("Rp {}", price),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !detail.is_empty() {
|
||||||
|
// Find the 1gr price as main price (fallback: smallest gram size).
|
||||||
|
let one_gr = detail.iter().find(|d| {
|
||||||
|
d["ukuran"]
|
||||||
|
.as_str()
|
||||||
|
.map(|s| {
|
||||||
|
let s = s.trim();
|
||||||
|
s == "1 gr" || s.starts_with("1 gr") || s.starts_with("1gr")
|
||||||
|
})
|
||||||
|
.unwrap_or(false)
|
||||||
|
});
|
||||||
|
let main_price = one_gr
|
||||||
|
.map(|d| d["harga"].as_str().unwrap_or("N/A"))
|
||||||
|
.unwrap_or_else(|| detail[0]["harga"].as_str().unwrap_or("N/A"));
|
||||||
|
|
||||||
|
result["hargaUtama"] = json!(main_price);
|
||||||
|
result["detail"] = json!(detail);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scrape kurs BCA (jual/beli) from bca.co.id.
|
||||||
|
pub async fn fetch_kurs_bca() -> Result<Value, String> {
|
||||||
|
let url = "https://www.bca.co.id/id/informasi/kurs";
|
||||||
|
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(url)
|
||||||
|
.header(
|
||||||
|
USER_AGENT,
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||||
|
)
|
||||||
|
.header("Referer", "https://www.google.com/")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP error: {}", e))?;
|
||||||
|
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("HTTP {} from bca.co.id", resp.status()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = resp
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to read body: {}", e))?;
|
||||||
|
|
||||||
|
let document = Html::parse_document(&body);
|
||||||
|
|
||||||
|
let table_sel = Selector::parse("table").unwrap();
|
||||||
|
let tr_sel = Selector::parse("tbody tr").unwrap();
|
||||||
|
let td_sel = Selector::parse("td").unwrap();
|
||||||
|
|
||||||
|
let mut kurs_data: Vec<Value> = Vec::new();
|
||||||
|
|
||||||
|
for table in document.select(&table_sel) {
|
||||||
|
let table_text = table.text().collect::<String>();
|
||||||
|
if table_text.contains("USD") || table_text.contains("SGD") || table_text.contains("kurs") {
|
||||||
|
for tr in table.select(&tr_sel) {
|
||||||
|
let cols: Vec<String> = tr
|
||||||
|
.select(&td_sel)
|
||||||
|
.map(|td| td.text().collect::<String>().trim().to_string())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if cols.len() >= 3
|
||||||
|
&& !cols[0].is_empty()
|
||||||
|
&& !cols[1].is_empty()
|
||||||
|
&& !cols[2].is_empty()
|
||||||
|
{
|
||||||
|
kurs_data.push(json!({
|
||||||
|
"currency": cols[0],
|
||||||
|
"jual": cols[1],
|
||||||
|
"beli": cols[2],
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !kurs_data.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if kurs_data.is_empty() {
|
||||||
|
return Err("Gagal mendapatkan data kurs BCA".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(json!({
|
||||||
|
"source": "BCA",
|
||||||
|
"url": url,
|
||||||
|
"data": kurs_data,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Local server info: OS, CPU, RAM, storage.
|
||||||
|
pub async fn fetch_server_info() -> Result<Value, String> {
|
||||||
|
let os_info = json!({
|
||||||
|
"platform": std::env::consts::OS,
|
||||||
|
"arch": std::env::consts::ARCH,
|
||||||
|
"family": std::env::consts::FAMILY,
|
||||||
|
});
|
||||||
|
|
||||||
|
// CPU info from /proc/cpuinfo (Linux)
|
||||||
|
let cpu_model = tokio::fs::read_to_string("/proc/cpuinfo")
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
.and_then(|s| {
|
||||||
|
s.lines()
|
||||||
|
.find(|l| l.starts_with("model name"))
|
||||||
|
.and_then(|l| l.split(':').nth(1))
|
||||||
|
.map(|s| s.trim().to_string())
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| "unknown".to_string());
|
||||||
|
|
||||||
|
let cpu_cores = std::thread::available_parallelism()
|
||||||
|
.map(|n| n.get())
|
||||||
|
.unwrap_or(1);
|
||||||
|
|
||||||
|
// RAM from /proc/meminfo
|
||||||
|
let mem_info = tokio::fs::read_to_string("/proc/meminfo")
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
.unwrap_or_default();
|
||||||
|
let total_mem_kb: u64 = mem_info
|
||||||
|
.lines()
|
||||||
|
.find(|l| l.starts_with("MemTotal"))
|
||||||
|
.and_then(|l| l.split_whitespace().nth(1))
|
||||||
|
.and_then(|s| s.parse().ok())
|
||||||
|
.unwrap_or(0);
|
||||||
|
let free_mem_kb: u64 = mem_info
|
||||||
|
.lines()
|
||||||
|
.find(|l| l.starts_with("MemAvailable"))
|
||||||
|
.and_then(|l| l.split_whitespace().nth(1))
|
||||||
|
.and_then(|s| s.parse().ok())
|
||||||
|
.unwrap_or(0);
|
||||||
|
|
||||||
|
let total_mem_gb = total_mem_kb as f64 / 1048576.0;
|
||||||
|
let free_mem_gb = free_mem_kb as f64 / 1048576.0;
|
||||||
|
|
||||||
|
// Disk usage from `df -B1 /`
|
||||||
|
let disk_usage = tokio::process::Command::new("df")
|
||||||
|
.args(["-B1", "/"])
|
||||||
|
.output()
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
.and_then(|o| String::from_utf8(o.stdout).ok())
|
||||||
|
.and_then(|out| {
|
||||||
|
let lines: Vec<&str> = out.lines().collect();
|
||||||
|
if lines.len() >= 2 {
|
||||||
|
let parts: Vec<&str> = lines[1].split_whitespace().collect();
|
||||||
|
if parts.len() >= 4 {
|
||||||
|
let total: u64 = parts[1].parse().unwrap_or(0);
|
||||||
|
let used: u64 = parts[2].parse().unwrap_or(0);
|
||||||
|
let free: u64 = parts[3].parse().unwrap_or(0);
|
||||||
|
return Some(json!({
|
||||||
|
"drive": parts[5],
|
||||||
|
"total": format_gb(total),
|
||||||
|
"used": format_gb(used),
|
||||||
|
"free": format_gb(free),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
})
|
||||||
|
.unwrap_or(json!({
|
||||||
|
"drive": "/",
|
||||||
|
"total": "N/A",
|
||||||
|
"used": "N/A",
|
||||||
|
"free": "N/A",
|
||||||
|
}));
|
||||||
|
|
||||||
|
Ok(json!({
|
||||||
|
"os": os_info,
|
||||||
|
"cpu": {
|
||||||
|
"model": cpu_model,
|
||||||
|
"cores": cpu_cores,
|
||||||
|
},
|
||||||
|
"ram": {
|
||||||
|
"total": format!("{:.2} GB", total_mem_gb),
|
||||||
|
"free": format!("{:.2} GB", free_mem_gb),
|
||||||
|
"used": format!("{:.2} GB", total_mem_gb - free_mem_gb),
|
||||||
|
},
|
||||||
|
"storage": disk_usage,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_gb(bytes: u64) -> String {
|
||||||
|
let gb = bytes as f64 / 1_073_741_824.0;
|
||||||
|
format!("{:.2} GB", gb)
|
||||||
|
}
|
||||||
@@ -1,10 +1,17 @@
|
|||||||
pub mod alqanime;
|
pub mod alqanime;
|
||||||
|
pub mod downloader;
|
||||||
|
pub mod image;
|
||||||
pub mod komik;
|
pub mod komik;
|
||||||
|
pub mod misc;
|
||||||
pub mod otakudesu;
|
pub mod otakudesu;
|
||||||
pub mod parsers;
|
pub mod parsers;
|
||||||
pub mod proxy;
|
pub mod proxy;
|
||||||
|
pub mod search;
|
||||||
|
pub mod stalk;
|
||||||
|
pub mod tools;
|
||||||
|
|
||||||
pub use alqanime::AlqanimeRepository;
|
pub use alqanime::AlqanimeRepository;
|
||||||
|
pub use downloader::DownloaderRepository;
|
||||||
pub use komik::KomikRepository;
|
pub use komik::KomikRepository;
|
||||||
pub use otakudesu::OtakudesuRepository;
|
pub use otakudesu::OtakudesuRepository;
|
||||||
pub use proxy::ProxyRepository;
|
pub use proxy::ProxyRepository;
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
//! Otakudesu anime scraping repository.
|
//! Otakudesu anime scraping repository.
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use backoff::future::retry;
|
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
use crate::domain::entity::anime::{
|
use crate::domain::entity::anime::{
|
||||||
@@ -13,7 +12,7 @@ use crate::domain::repository::ScrapingRepository;
|
|||||||
use crate::infrastructure::repository::parsers::otakudesu_parser;
|
use crate::infrastructure::repository::parsers::otakudesu_parser;
|
||||||
use crate::infrastructure::scraping::html_fetcher::fetch_html_with_retry;
|
use crate::infrastructure::scraping::html_fetcher::fetch_html_with_retry;
|
||||||
use crate::infrastructure::scraping::proxy_fetch::fetch_with_proxy;
|
use crate::infrastructure::scraping::proxy_fetch::fetch_with_proxy;
|
||||||
use crate::infrastructure::scraping::retry::{default_backoff, transient};
|
use crate::infrastructure::scraping::retry::{default_backoff, retry, retry_all};
|
||||||
|
|
||||||
const OTAKUDESU_BASE_URL: &str = "https://otakudesu.cloud";
|
const OTAKUDESU_BASE_URL: &str = "https://otakudesu.cloud";
|
||||||
|
|
||||||
@@ -57,7 +56,10 @@ impl OtakudesuRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn genre_page_url(&self, genre_slug: &str, page: &str) -> String {
|
pub fn genre_page_url(&self, genre_slug: &str, page: &str) -> String {
|
||||||
format!("{}/genre/{}/page/{}/", self.base_url(), genre_slug, page)
|
// Current otakudesu uses the plural `/genres/{slug}/` path (archive
|
||||||
|
// layout). The singular `/genre/{slug}/page/{page}/` path 301-redirects
|
||||||
|
// to otakudesu.io (a 404 placeholder), which made genre pages return 0.
|
||||||
|
format!("{}/genres/{}/page/{}/", self.base_url(), genre_slug, page)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn full_episode_url(&self, slug: &str) -> String {
|
pub fn full_episode_url(&self, slug: &str) -> String {
|
||||||
@@ -145,7 +147,14 @@ impl OtakudesuRepository {
|
|||||||
&self,
|
&self,
|
||||||
slug: &str,
|
slug: &str,
|
||||||
) -> Result<(Vec<LatestAnimeItem>, Pagination), ScrapingError> {
|
) -> Result<(Vec<LatestAnimeItem>, Pagination), ScrapingError> {
|
||||||
let url = self.page_url("latest-anime", slug);
|
// The site's `/latest-anime/` path was removed (301 → otakudesu.io
|
||||||
|
// placeholder). The homepage IS the latest-episodes page (`.venz ul li`
|
||||||
|
// with `.thumbz h2.jdlflm`, `.epz`), paginated via WordPress `/page/N/`.
|
||||||
|
let url = if slug == "1" {
|
||||||
|
format!("{}/", self.base_url())
|
||||||
|
} else {
|
||||||
|
format!("{}/page/{}/", self.base_url(), slug)
|
||||||
|
};
|
||||||
let html = self.fetch_html(&url).await?;
|
let html = self.fetch_html(&url).await?;
|
||||||
let slug_owned = slug.to_string();
|
let slug_owned = slug.to_string();
|
||||||
tokio::task::spawn_blocking(move || {
|
tokio::task::spawn_blocking(move || {
|
||||||
@@ -208,14 +217,11 @@ impl OtakudesuRepository {
|
|||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("Failed to fetch URL: {}, error: {:?}", url_owned, e);
|
warn!("Failed to fetch URL: {}, error: {:?}", url_owned, e);
|
||||||
Err(transient(ScrapingError::Http(format!(
|
Err(ScrapingError::Http(format!("Proxy fetch failed: {}", e)))
|
||||||
"Proxy fetch failed: {}",
|
|
||||||
e
|
|
||||||
))))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
retry(backoff, fetch_op)
|
retry(backoff, retry_all, fetch_op)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| ScrapingError::Http(e.to_string()))
|
.map_err(|e| ScrapingError::Http(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,8 +80,7 @@ static SLUG_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"/([^/]+)/?$")
|
|||||||
static GENRE_SLUG_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"genre-(.+)$").unwrap());
|
static GENRE_SLUG_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"genre-(.+)$").unwrap());
|
||||||
static EPISODE_LIST_SELECTOR: LazyLock<Selector> =
|
static EPISODE_LIST_SELECTOR: LazyLock<Selector> =
|
||||||
LazyLock::new(|| Selector::parse(".eplister ul li").unwrap());
|
LazyLock::new(|| Selector::parse(".eplister ul li").unwrap());
|
||||||
static EP_NUM_SELECTOR: LazyLock<Selector> =
|
static EP_NUM_SELECTOR: LazyLock<Selector> = LazyLock::new(|| Selector::parse(".epl-num").unwrap());
|
||||||
LazyLock::new(|| Selector::parse(".epl-num").unwrap());
|
|
||||||
static EP_TITLE_SELECTOR: LazyLock<Selector> =
|
static EP_TITLE_SELECTOR: LazyLock<Selector> =
|
||||||
LazyLock::new(|| Selector::parse(".epl-title").unwrap());
|
LazyLock::new(|| Selector::parse(".epl-title").unwrap());
|
||||||
static EP_DATE_SELECTOR: LazyLock<Selector> =
|
static EP_DATE_SELECTOR: LazyLock<Selector> =
|
||||||
|
|||||||
@@ -410,6 +410,16 @@ pub fn parse_latest_anime_document(
|
|||||||
// SEARCH ANIME PAGE
|
// SEARCH ANIME PAGE
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
|
/// Normalise scraped text: collapse newlines/tabs into single spaces and trim.
|
||||||
|
fn clean_text(text: String) -> String {
|
||||||
|
text.replace(['\n', '\t'], " ")
|
||||||
|
.split_whitespace()
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" ")
|
||||||
|
.trim()
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_search_anime_document(
|
pub fn parse_search_anime_document(
|
||||||
html: &str,
|
html: &str,
|
||||||
page: &str,
|
page: &str,
|
||||||
@@ -417,53 +427,82 @@ pub fn parse_search_anime_document(
|
|||||||
let document = parse_html(html);
|
let document = parse_html(html);
|
||||||
let mut items = Vec::new();
|
let mut items = Vec::new();
|
||||||
|
|
||||||
let venz_sel = selector(".venz ul li")
|
// Otakudesu search results live under `<ul class="chivsrc"><li>...`, one
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse .venz ul li selector".into()))?;
|
// result per `<li>` with `<h2><a href="...">Title</a></h2>` plus a series
|
||||||
let title_sel = selector(".thumbz h2.jdlflm")
|
// of `<div class="set"><b>Label</b> : value</div>` rows (Status, Rating,
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse title selector".into()))?;
|
// Genre...). The old parser targeted the index layout (`.venz ul li`,
|
||||||
let link_sel = selector("a")
|
// `.thumbz h2.jdlflm`, `img`, `.epz`) which does NOT appear on the search
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse link selector".into()))?;
|
// page, so search silently returned 0 items.
|
||||||
let img_sel = selector("img")
|
let item_sel = selector(".chivsrc li")
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse img selector".into()))?;
|
.ok_or_else(|| ScrapingError::Parse("Failed to parse .chivsrc li selector".into()))?;
|
||||||
let ep_sel = selector(".epz")
|
let title_link_sel = selector("h2 a")
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse epz selector".into()))?;
|
.ok_or_else(|| ScrapingError::Parse("Failed to parse h2 a selector".into()))?;
|
||||||
let genre_sel = selector(".genre-tag")
|
let set_row_sel = selector(".set")
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse genre-tag selector".into()))?;
|
.ok_or_else(|| ScrapingError::Parse("Failed to parse .set selector".into()))?;
|
||||||
let status_sel = selector(".status")
|
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse status selector".into()))?;
|
|
||||||
let rating_sel = selector(".rating")
|
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse rating selector".into()))?;
|
|
||||||
|
|
||||||
for element in document.select(&venz_sel) {
|
let status_sel = selector(".set b:first-child, .set b")
|
||||||
let title = text_from_or(&element, &title_sel, "");
|
.ok_or_else(|| ScrapingError::Parse("Failed to parse status label selector".into()))?;
|
||||||
let anime_url = attr_from_or(&element, &link_sel, "href", "");
|
|
||||||
|
for element in document.select(&item_sel) {
|
||||||
|
let title = text_from_or(&element, &title_link_sel, "");
|
||||||
|
let anime_url = attr_from_or(&element, &title_link_sel, "href", "");
|
||||||
let slug = extract_slug(&anime_url);
|
let slug = extract_slug(&anime_url);
|
||||||
let poster = attr_from_or(&element, &img_sel, "src", "");
|
|
||||||
let episode = text_from_or(&element, &ep_sel, "N/A");
|
|
||||||
|
|
||||||
|
if title.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract label → value pairs from each `.set` row (e.g. "Status : Ongoing",
|
||||||
|
// "Rating : 8.0", "Genre : Action, Drama"). Falls back gracefully when the
|
||||||
|
// value is blank or the row label is absent.
|
||||||
|
let mut status = String::new();
|
||||||
|
let mut rating = String::new();
|
||||||
let mut genres = Vec::new();
|
let mut genres = Vec::new();
|
||||||
for genre_elem in element.select(&genre_sel) {
|
for row in element.select(&set_row_sel) {
|
||||||
genres.push(text(&genre_elem));
|
let row_text = text(&row);
|
||||||
|
let lower = row_text.to_lowercase();
|
||||||
|
if lower.contains("status") {
|
||||||
|
if let Some(label) = row.select(&status_sel).next() {
|
||||||
|
let label_len = text(&label).len();
|
||||||
|
status = clean_text(row_text[label_len..].to_string());
|
||||||
|
} else {
|
||||||
|
status = clean_text(row_text.trim_start_matches("Status").to_string());
|
||||||
|
}
|
||||||
|
} else if lower.contains("rating") {
|
||||||
|
let label_len = row
|
||||||
|
.select(&status_sel)
|
||||||
|
.next()
|
||||||
|
.map(|label| text(&label).len())
|
||||||
|
.unwrap_or("Rating".len());
|
||||||
|
rating = clean_text(row_text[label_len..].to_string());
|
||||||
|
} else if lower.contains("genre") {
|
||||||
|
let label_len = row
|
||||||
|
.select(&status_sel)
|
||||||
|
.next()
|
||||||
|
.map(|label| text(&label).len())
|
||||||
|
.unwrap_or("Genre".len());
|
||||||
|
let raw = clean_text(row_text[label_len..].to_string());
|
||||||
|
genres = raw
|
||||||
|
.split(',')
|
||||||
|
.map(|g| g.trim().to_string())
|
||||||
|
.filter(|g| !g.is_empty())
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = text_from_or(&element, &status_sel, "");
|
items.push(SearchAnimeItem {
|
||||||
let rating = text_from_or(&element, &rating_sel, "");
|
title,
|
||||||
|
slug,
|
||||||
if !title.is_empty() {
|
poster: String::new(),
|
||||||
items.push(SearchAnimeItem {
|
episode: String::new(),
|
||||||
title,
|
anime_url,
|
||||||
slug,
|
genres,
|
||||||
poster,
|
status,
|
||||||
episode,
|
rating,
|
||||||
anime_url,
|
description: String::new(),
|
||||||
genres,
|
r#type: String::new(),
|
||||||
status,
|
season: String::new(),
|
||||||
rating,
|
});
|
||||||
description: String::new(),
|
|
||||||
r#type: String::new(),
|
|
||||||
season: String::new(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let pagination = parse_pagination(page, &document)?;
|
let pagination = parse_pagination(page, &document)?;
|
||||||
@@ -481,23 +520,29 @@ pub fn parse_genre_anime_document(
|
|||||||
let document = parse_html(html);
|
let document = parse_html(html);
|
||||||
let mut items = Vec::new();
|
let mut items = Vec::new();
|
||||||
|
|
||||||
let venz_sel = selector(".venz ul li")
|
// Current otakudesu genre archive pages use the `.col-anime` card layout
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse .venz ul li selector".into()))?;
|
// (`.col-md-4 .col-anime`), each with `.col-anime-title a`, `.col-anime-eps`,
|
||||||
let title_sel = selector(".thumbz h2.jdlflm")
|
// `.col-anime-rating`, `.col-anime-cover img` and a synopsis. The old
|
||||||
|
// `.venz ul li` / `.thumbz h2.jdlflm` / `.epz` layout no longer appears on
|
||||||
|
// these pages, which made genre filtering silently return 0 items.
|
||||||
|
let card_sel = selector(".col-anime")
|
||||||
|
.ok_or_else(|| ScrapingError::Parse("Failed to parse .col-anime selector".into()))?;
|
||||||
|
let title_sel = selector(".col-anime-title a")
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse title selector".into()))?;
|
.ok_or_else(|| ScrapingError::Parse("Failed to parse title selector".into()))?;
|
||||||
let link_sel = selector("a")
|
let eps_sel = selector(".col-anime-eps")
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse link selector".into()))?;
|
.ok_or_else(|| ScrapingError::Parse("Failed to parse eps selector".into()))?;
|
||||||
let img_sel = selector("img")
|
let rating_sel = selector(".col-anime-rating")
|
||||||
|
.ok_or_else(|| ScrapingError::Parse("Failed to parse rating selector".into()))?;
|
||||||
|
let img_sel = selector(".col-anime-cover img")
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse img selector".into()))?;
|
.ok_or_else(|| ScrapingError::Parse("Failed to parse img selector".into()))?;
|
||||||
let ep_sel = selector(".epz")
|
|
||||||
.ok_or_else(|| ScrapingError::Parse("Failed to parse epz selector".into()))?;
|
|
||||||
|
|
||||||
for element in document.select(&venz_sel) {
|
for element in document.select(&card_sel) {
|
||||||
let title = text_from_or(&element, &title_sel, "");
|
let title = text_from_or(&element, &title_sel, "");
|
||||||
let anime_url = attr_from_or(&element, &link_sel, "href", "");
|
let anime_url = attr_from_or(&element, &title_sel, "href", "");
|
||||||
let slug = extract_slug(&anime_url);
|
let slug = extract_slug(&anime_url);
|
||||||
let poster = attr_from_or(&element, &img_sel, "src", "");
|
let poster = attr_from_or(&element, &img_sel, "src", "");
|
||||||
let episode = text_from_or(&element, &ep_sel, "N/A");
|
let episode = text_from_or(&element, &eps_sel, "N/A");
|
||||||
|
let score = text_from_or(&element, &rating_sel, "");
|
||||||
|
|
||||||
if !title.is_empty() {
|
if !title.is_empty() {
|
||||||
items.push(GenreAnimeItem {
|
items.push(GenreAnimeItem {
|
||||||
@@ -505,7 +550,7 @@ pub fn parse_genre_anime_document(
|
|||||||
slug,
|
slug,
|
||||||
poster,
|
poster,
|
||||||
episode,
|
episode,
|
||||||
score: String::new(),
|
score,
|
||||||
status: String::new(),
|
status: String::new(),
|
||||||
anime_url,
|
anime_url,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,289 @@
|
|||||||
|
//! Infrastructure — Search utilities.
|
||||||
|
//!
|
||||||
|
//! Ported from Shirokami-API `scraper/search/*.js`:
|
||||||
|
//! bmkg (BMKG earthquake), jadwal-sholat (myquran.com), weather (OpenWeather),
|
||||||
|
//! google (HTML scrape), yt (via yt-dlp flat-playlist).
|
||||||
|
|
||||||
|
use crate::infrastructure::utils::http_client::http_client;
|
||||||
|
use reqwest::header::USER_AGENT;
|
||||||
|
use scraper::{Html, Selector};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use std::process::Stdio;
|
||||||
|
|
||||||
|
/// BMKG Indonesia earthquake info (auto-gempa, terkini, dirasakan).
|
||||||
|
pub async fn fetch_bmkg() -> Result<Value, String> {
|
||||||
|
let urls = [
|
||||||
|
"https://data.bmkg.go.id/DataMKG/TEWS/autogempa.json",
|
||||||
|
"https://data.bmkg.go.id/DataMKG/TEWS/gempaterkini.json",
|
||||||
|
"https://data.bmkg.go.id/DataMKG/TEWS/gempadirasakan.json",
|
||||||
|
];
|
||||||
|
let mut auto = Value::Null;
|
||||||
|
let mut terkini: Vec<Value> = Vec::new();
|
||||||
|
let mut dirasakan: Vec<Value> = Vec::new();
|
||||||
|
for url in urls {
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(url)
|
||||||
|
.header(
|
||||||
|
USER_AGENT,
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36",
|
||||||
|
)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP error: {}", e))?;
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("BMKG returned HTTP {}", resp.status()));
|
||||||
|
}
|
||||||
|
let data: Value = resp
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("JSON parse: {}", e))?;
|
||||||
|
if url.contains("autogempa") {
|
||||||
|
auto = data
|
||||||
|
.pointer("/Infogempa/gempa")
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or(Value::Null);
|
||||||
|
} else if url.contains("gempaterkini") {
|
||||||
|
terkini = data
|
||||||
|
.pointer("/Infogempa/gempa")
|
||||||
|
.and_then(|g| g.as_array())
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_default();
|
||||||
|
} else {
|
||||||
|
dirasakan = data
|
||||||
|
.pointer("/Infogempa/gempa")
|
||||||
|
.and_then(|g| g.as_array())
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_default();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(json!({
|
||||||
|
"autogempa": auto,
|
||||||
|
"gempaterkini": terkini,
|
||||||
|
"gempadirasakan": dirasakan,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Jadwal sholat from myquran.com.
|
||||||
|
pub async fn fetch_jadwal_sholat(kota: &str) -> Result<Value, String> {
|
||||||
|
// 1. Find city id
|
||||||
|
let search_url = format!(
|
||||||
|
"https://api.myquran.com/v2/sholat/kota/cari/{}",
|
||||||
|
urlencode(kota)
|
||||||
|
);
|
||||||
|
let city_data = get_json(&search_url).await?;
|
||||||
|
if city_data
|
||||||
|
.get("status")
|
||||||
|
.and_then(|s| s.as_bool())
|
||||||
|
.unwrap_or(false)
|
||||||
|
!= true
|
||||||
|
{
|
||||||
|
return Ok(json!({ "error": "Kota tidak ditemukan" }));
|
||||||
|
}
|
||||||
|
let id_list = city_data
|
||||||
|
.get("data")
|
||||||
|
.and_then(|d| d.as_array())
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_default();
|
||||||
|
if id_list.is_empty() {
|
||||||
|
return Ok(json!({ "error": "Kota tidak ditemukan" }));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Today's date (Asia/Jakarta). Use chrono with a fixed offset approximation.
|
||||||
|
let now = chrono::Local::now();
|
||||||
|
let (year, month, day) = (
|
||||||
|
now.format("%Y").to_string(),
|
||||||
|
now.format("%m").to_string(),
|
||||||
|
now.format("%d").to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut results: Vec<Value> = Vec::new();
|
||||||
|
for city in id_list.iter().take(5) {
|
||||||
|
let cid = city.get("id").and_then(|i| i.as_str()).unwrap_or("");
|
||||||
|
let jadwal_url = format!(
|
||||||
|
"https://api.myquran.com/v2/sholat/jadwal/{}/{}/{}/{}",
|
||||||
|
cid, year, month, day
|
||||||
|
);
|
||||||
|
if let Ok(sched) = get_json(&jadwal_url).await {
|
||||||
|
if let Some(d) = sched.get("data") {
|
||||||
|
results.push(json!({
|
||||||
|
"lokasi": d.get("lokasi").cloned().unwrap_or(Value::Null),
|
||||||
|
"daerah": d.get("daerah").cloned().unwrap_or(Value::Null),
|
||||||
|
"jadwal": d.get("jadwal").cloned().unwrap_or(Value::Null),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if results.is_empty() {
|
||||||
|
return Ok(json!({ "error": "Tidak dapat mengambil jadwal" }));
|
||||||
|
}
|
||||||
|
Ok(json!({ "total": results.len(), "schedules": results }))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// OpenWeather current weather (uses env OPENWEATHER_API_KEY, falls back to source's key).
|
||||||
|
pub async fn fetch_weather(city: &str) -> Result<Value, String> {
|
||||||
|
let key = std::env::var("OPENWEATHER_API_KEY")
|
||||||
|
.unwrap_or_else(|_| "060a6bcfa19809c2cd4d97a212b19273".to_string());
|
||||||
|
let url = format!(
|
||||||
|
"https://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid={}",
|
||||||
|
urlencode(city),
|
||||||
|
key
|
||||||
|
);
|
||||||
|
get_json(&url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Google web search via HTML scraping.
|
||||||
|
pub async fn fetch_google(query: &str) -> Result<Value, String> {
|
||||||
|
let url = format!(
|
||||||
|
"https://www.google.com/search?q={}&safe=off&hl=en&gl=us",
|
||||||
|
urlencode(query)
|
||||||
|
);
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(&url)
|
||||||
|
.header(USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36")
|
||||||
|
.header("Accept-Language", "en-US")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP error: {}", e))?;
|
||||||
|
let body = resp
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to read body: {}", e))?;
|
||||||
|
|
||||||
|
let document = Html::parse_document(&body);
|
||||||
|
let result_sel =
|
||||||
|
Selector::parse("div.g, div[data-hveid]").unwrap_or_else(|_| Selector::parse("a").unwrap());
|
||||||
|
let link_sel = Selector::parse("a").unwrap();
|
||||||
|
let h3_sel = Selector::parse("h3").unwrap();
|
||||||
|
|
||||||
|
let mut results: Vec<Value> = Vec::new();
|
||||||
|
for node in document.select(&result_sel) {
|
||||||
|
let link = node
|
||||||
|
.select(&link_sel)
|
||||||
|
.find_map(|a| a.value().attr("href"))
|
||||||
|
.and_then(|h| h.strip_prefix("/url?q="))
|
||||||
|
.map(|h| h.split('&').next().unwrap_or(h).to_string());
|
||||||
|
let title = node
|
||||||
|
.select(&h3_sel)
|
||||||
|
.next()
|
||||||
|
.map(|h| h.text().collect::<String>());
|
||||||
|
if title.is_some() && link.is_some() {
|
||||||
|
results.push(json!({
|
||||||
|
"title": title.unwrap_or_default(),
|
||||||
|
"description": "",
|
||||||
|
"link": link.unwrap_or_default(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
if results.len() >= 10 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(json!(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// YouTube search via yt-dlp flat playlist (real results, scraping).
|
||||||
|
pub async fn fetch_yt_search(query: &str) -> Result<Value, String> {
|
||||||
|
let q = format!("ytsearch15:{}", query);
|
||||||
|
let output = run_ytdlp(&q).await?;
|
||||||
|
|
||||||
|
let mut videos: Vec<Value> = Vec::new();
|
||||||
|
let arr: Vec<Value> = serde_json::from_str(&output).unwrap_or_default();
|
||||||
|
for v in arr.iter().take(15) {
|
||||||
|
videos.push(json!({
|
||||||
|
"title": v.get("title").cloned().unwrap_or(Value::Null),
|
||||||
|
"id": v.get("id").cloned().unwrap_or(Value::Null),
|
||||||
|
"url": v.get("webpage_url").cloned().unwrap_or(Value::Null),
|
||||||
|
"thumbnail": v.get("thumbnail").cloned().unwrap_or(Value::Null),
|
||||||
|
"duration": v.get("duration").cloned().unwrap_or(Value::Null),
|
||||||
|
"views": v.get("view_count").cloned().unwrap_or(Value::Null),
|
||||||
|
"author": {
|
||||||
|
"name": v.pointer("/channel").cloned().unwrap_or(Value::Null),
|
||||||
|
"url": v.get("channel_url").cloned().unwrap_or(Value::Null),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(json!({ "total": videos.len(), "videos": videos }))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// Helpers
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
async fn get_json(url: &str) -> Result<Value, String> {
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(url)
|
||||||
|
.header(USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP error: {}", e))?;
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("HTTP {} from upstream", resp.status()));
|
||||||
|
}
|
||||||
|
resp.json::<Value>()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("JSON parse: {}", e))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run yt-dlp with `--dump-json --flat-playlist` for the given search/query arg.
|
||||||
|
async fn run_ytdlp(query_arg: &str) -> Result<String, String> {
|
||||||
|
let bins = [
|
||||||
|
"/usr/local/bin/yt-dlp-native",
|
||||||
|
"/home/code/hermes-agent/.venv/bin/yt-dlp",
|
||||||
|
"yt-dlp",
|
||||||
|
];
|
||||||
|
let bin = bins
|
||||||
|
.iter()
|
||||||
|
.find(|b| std::path::Path::new(b).exists() || **b == "yt-dlp")
|
||||||
|
.copied()
|
||||||
|
.unwrap_or("yt-dlp");
|
||||||
|
|
||||||
|
let child = tokio::process::Command::new(bin)
|
||||||
|
.args(["--dump-json", "--flat-playlist", "--no-warnings", query_arg])
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.stderr(Stdio::null())
|
||||||
|
.spawn()
|
||||||
|
.map_err(|e| format!("Failed to spawn yt-dlp: {}", e))?;
|
||||||
|
|
||||||
|
let output = child
|
||||||
|
.wait_with_output()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("yt-dlp failed: {}", e))?;
|
||||||
|
|
||||||
|
let stdout = String::from_utf8(output.stdout).map_err(|e| format!("Invalid UTF-8: {}", e))?;
|
||||||
|
// `--flat-playlist` outputs multiple JSON lines (one per video).
|
||||||
|
let mut joined = String::new();
|
||||||
|
joined.push('[');
|
||||||
|
let mut first = true;
|
||||||
|
for line in stdout.lines() {
|
||||||
|
let line = line.trim();
|
||||||
|
if line.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if !first {
|
||||||
|
joined.push(',');
|
||||||
|
}
|
||||||
|
joined.push_str(line);
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
joined.push(']');
|
||||||
|
Ok(joined)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn urlencode(s: &str) -> String {
|
||||||
|
let mut out = String::new();
|
||||||
|
for b in s.bytes() {
|
||||||
|
match b {
|
||||||
|
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
|
||||||
|
out.push(b as char)
|
||||||
|
}
|
||||||
|
b' ' => out.push_str("%20"),
|
||||||
|
_ => out.push_str(&format!("%{:02X}", b)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
@@ -0,0 +1,458 @@
|
|||||||
|
//! Infrastructure — Stalk utilities (github, youtube, twitter).
|
||||||
|
//!
|
||||||
|
//! Ported from Shirokami-API `scraper/stalk/*.js`.
|
||||||
|
|
||||||
|
use crate::infrastructure::utils::http_client::http_client;
|
||||||
|
use regex::Regex;
|
||||||
|
use reqwest::header::USER_AGENT;
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
|
/// GitHub user stalk via public API.
|
||||||
|
pub async fn fetch_github_stalk(username: &str) -> Result<Value, String> {
|
||||||
|
let url = format!("https://api.github.com/users/{}", username);
|
||||||
|
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(&url)
|
||||||
|
.header(USER_AGENT, "Scraper/1.0")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP error: {}", e))?;
|
||||||
|
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
let status = resp.status().as_u16();
|
||||||
|
return Err(format!("GitHub user not found (HTTP {})", status));
|
||||||
|
}
|
||||||
|
|
||||||
|
let data: Value = resp
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("JSON parse: {}", e))?;
|
||||||
|
|
||||||
|
let pick = |k: &str| -> Value { data.get(k).cloned().unwrap_or(Value::Null) };
|
||||||
|
|
||||||
|
Ok(json!({
|
||||||
|
"username": username,
|
||||||
|
"login": pick("login"),
|
||||||
|
"id": pick("id"),
|
||||||
|
"node_id": pick("node_id"),
|
||||||
|
"avatar_url": pick("avatar_url"),
|
||||||
|
"html_url": pick("html_url"),
|
||||||
|
"type": pick("type"),
|
||||||
|
"site_admin": pick("site_admin"),
|
||||||
|
"name": pick("name"),
|
||||||
|
"company": pick("company"),
|
||||||
|
"blog": pick("blog"),
|
||||||
|
"location": pick("location"),
|
||||||
|
"email": pick("email"),
|
||||||
|
"hireable": pick("hireable"),
|
||||||
|
"bio": pick("bio"),
|
||||||
|
"twitter_username": pick("twitter_username"),
|
||||||
|
"public_repos": pick("public_repos"),
|
||||||
|
"public_gists": pick("public_gists"),
|
||||||
|
"followers": pick("followers"),
|
||||||
|
"following": pick("following"),
|
||||||
|
"created_at": pick("created_at"),
|
||||||
|
"updated_at": pick("updated_at"),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// YouTube channel stalk: parse ytInitialData JSON from channel page.
|
||||||
|
/// Fetches the `/<handle>/videos` subpage (richGridRenderer with lockupViewModel).
|
||||||
|
pub async fn fetch_youtube_stalk(username: &str) -> Result<Value, String> {
|
||||||
|
let url = format!("https://youtube.com/@{username}/videos?hl=en&gl=US");
|
||||||
|
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(&url)
|
||||||
|
.header(USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36")
|
||||||
|
.header("Accept-Language", "en-US")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP error: {}", e))?;
|
||||||
|
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("HTTP {} from youtube", resp.status()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = resp
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to read body: {}", e))?;
|
||||||
|
|
||||||
|
// Extract var ytInitialData = {...} via brace matching (JSON may contain ";").
|
||||||
|
let start = body
|
||||||
|
.find("var ytInitialData = ")
|
||||||
|
.ok_or_else(|| "ytInitialData not found".to_string())?;
|
||||||
|
let brace = body[start..]
|
||||||
|
.find('{')
|
||||||
|
.ok_or_else(|| "ytInitialData parse".to_string())?
|
||||||
|
+ start;
|
||||||
|
let json_str = extract_balanced_json(&body, brace)?;
|
||||||
|
let parsed: Value =
|
||||||
|
serde_json::from_str(&json_str).map_err(|e| format!("JSON parse: {}", e))?;
|
||||||
|
|
||||||
|
// Channel metadata from metadata.channelMetadataRenderer
|
||||||
|
let mut channel = json!({
|
||||||
|
"username": username,
|
||||||
|
"subscriberCount": Value::Null,
|
||||||
|
"videoCount": Value::Null,
|
||||||
|
"avatarUrl": Value::Null,
|
||||||
|
"channelUrl": Value::Null,
|
||||||
|
"externalId": Value::Null,
|
||||||
|
"description": Value::Null,
|
||||||
|
"isFamilySafe": Value::Null,
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Some(meta) = parsed
|
||||||
|
.get("metadata")
|
||||||
|
.and_then(|m| m.get("channelMetadataRenderer"))
|
||||||
|
{
|
||||||
|
let mut set = |dst: &str, src: &str| {
|
||||||
|
if let Some(v) = meta.get(src) {
|
||||||
|
channel[dst] = v.clone();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
set("description", "description");
|
||||||
|
set("externalId", "externalId");
|
||||||
|
set("channelUrl", "channelUrl");
|
||||||
|
set("isFamilySafe", "isFamilySafe");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscriber/video count from header.pageHeaderRenderer
|
||||||
|
if let Some(header) = parsed
|
||||||
|
.get("header")
|
||||||
|
.and_then(|h| h.get("pageHeaderRenderer"))
|
||||||
|
{
|
||||||
|
if let Some(rows) = header
|
||||||
|
.pointer("/content/pageHeaderViewModel/metadata/contentMetadataViewModel/metadataRows")
|
||||||
|
{
|
||||||
|
if let Some(arr) = rows.as_array() {
|
||||||
|
for row in arr {
|
||||||
|
if let Some(parts) = row.get("metadataParts").and_then(|p| p.as_array()) {
|
||||||
|
for part in parts {
|
||||||
|
let text = part
|
||||||
|
.pointer("/text/content")
|
||||||
|
.and_then(|t| t.as_str())
|
||||||
|
.unwrap_or("");
|
||||||
|
if text.contains("subscriber") {
|
||||||
|
channel["subscriberCount"] = json!(text.trim());
|
||||||
|
} else if text.contains(" video") || text.ends_with(" videos") {
|
||||||
|
channel["videoCount"] = json!(text.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Avatar
|
||||||
|
if let Some(sources) = header
|
||||||
|
.pointer("/content/pageHeaderViewModel/image/decoratedAvatarViewModel/avatar/avatarViewModel/image/sources")
|
||||||
|
.and_then(|s| s.as_array())
|
||||||
|
{
|
||||||
|
if !sources.is_empty() {
|
||||||
|
channel["avatarUrl"] = sources[0].get("url").cloned().unwrap_or(Value::Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Videos list (up to 5). The `/videos` subpage uses richGridRenderer with
|
||||||
|
// lockupViewModel (new) or videoRenderer (legacy); we handle both.
|
||||||
|
let mut videos: Vec<Value> = Vec::new();
|
||||||
|
|
||||||
|
if let Some(tabs) = parsed
|
||||||
|
.pointer("/contents/twoColumnBrowseResultsRenderer/tabs")
|
||||||
|
.and_then(|t| t.as_array())
|
||||||
|
{
|
||||||
|
for tab in tabs {
|
||||||
|
walk_video_grid(tab, &mut videos);
|
||||||
|
if videos.len() >= 5 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: source's shelfRenderer/horizontalListRenderer approach.
|
||||||
|
if videos.is_empty() {
|
||||||
|
if let Some(tabs) = parsed
|
||||||
|
.pointer("/contents/twoColumnBrowseResultsRenderer/tabs")
|
||||||
|
.and_then(|t| t.as_array())
|
||||||
|
{
|
||||||
|
if let Some(contents) = tabs
|
||||||
|
.first()
|
||||||
|
.and_then(|t| t.pointer("/tabRenderer/content/sectionListRenderer/contents"))
|
||||||
|
.and_then(|c| c.as_array())
|
||||||
|
{
|
||||||
|
for item in contents {
|
||||||
|
if let Some(section) = item.get("itemSectionRenderer") {
|
||||||
|
if let Some(inner) = section.get("contents").and_then(|c| c.as_array()) {
|
||||||
|
for content in inner {
|
||||||
|
if let Some(shelf) = content.get("shelfRenderer") {
|
||||||
|
if let Some(items) = shelf
|
||||||
|
.pointer("/content/horizontalListRenderer/items")
|
||||||
|
.and_then(|i| i.as_array())
|
||||||
|
{
|
||||||
|
for video in items {
|
||||||
|
if let Some(gv) = video.get("gridVideoRenderer") {
|
||||||
|
videos.push(video_from_grid_video(gv));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if videos.len() >= 5 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(json!({
|
||||||
|
"channelMetadata": channel,
|
||||||
|
"videoDataList": videos.iter().take(5).cloned().collect::<Vec<_>>(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract a balanced JSON object string starting at `open_brace` (index of '{').
|
||||||
|
fn extract_balanced_json(body: &str, open_brace: usize) -> Result<String, String> {
|
||||||
|
let bytes = body.as_bytes();
|
||||||
|
let mut depth = 0i32;
|
||||||
|
let mut in_str = false;
|
||||||
|
let mut escaped = false;
|
||||||
|
let mut close = None;
|
||||||
|
for (i, b) in bytes.iter().enumerate().skip(open_brace) {
|
||||||
|
let c = *b as char;
|
||||||
|
if in_str {
|
||||||
|
if escaped {
|
||||||
|
escaped = false;
|
||||||
|
} else if c == '\\' {
|
||||||
|
escaped = true;
|
||||||
|
} else if c == '"' {
|
||||||
|
in_str = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match c {
|
||||||
|
'"' => in_str = true,
|
||||||
|
'{' => depth += 1,
|
||||||
|
'}' => {
|
||||||
|
depth -= 1;
|
||||||
|
if depth == 0 {
|
||||||
|
close = Some(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let close = close.ok_or_else(|| "unbalanced JSON in ytInitialData".to_string())?;
|
||||||
|
Ok(body[open_brace..=close].to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Recursively find `richItemRenderer` entries and convert their content to video objects.
|
||||||
|
fn walk_video_grid(node: &Value, out: &mut Vec<Value>) {
|
||||||
|
match node {
|
||||||
|
Value::Object(map) => {
|
||||||
|
if let Some(item) = map.get("richItemRenderer") {
|
||||||
|
if let Some(content) = item.get("content") {
|
||||||
|
if let Some(v) = content.get("videoRenderer") {
|
||||||
|
out.push(video_from_video_renderer(v));
|
||||||
|
} else if let Some(lv) = content.get("lockupViewModel") {
|
||||||
|
out.push(video_from_lockup(lv));
|
||||||
|
} else if let Some(sh) = content.get("shortsLockupViewModel") {
|
||||||
|
out.push(video_from_shorts(sh));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for v in map.values() {
|
||||||
|
if out.len() >= 5 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
walk_video_grid(v, out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Array(arr) => {
|
||||||
|
for v in arr {
|
||||||
|
if out.len() >= 5 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
walk_video_grid(v, out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a legacy `videoRenderer` into our video shape.
|
||||||
|
fn video_from_video_renderer(vd: &Value) -> Value {
|
||||||
|
let mut v = json!({
|
||||||
|
"videoId": vd.get("videoId").cloned().unwrap_or(Value::Null),
|
||||||
|
"title": vd.pointer("/title/runs/0/text").cloned().unwrap_or(Value::Null),
|
||||||
|
"thumbnail": vd.pointer("/thumbnail/thumbnails/0/url").cloned().unwrap_or(Value::Null),
|
||||||
|
"publishedTime": vd.pointer("/publishedTimeText/simpleText").cloned().unwrap_or(Value::Null),
|
||||||
|
"viewCount": vd.pointer("/viewCountText/simpleText").cloned().unwrap_or(Value::Null),
|
||||||
|
"navigationUrl": Value::Null,
|
||||||
|
"duration": Value::Null,
|
||||||
|
});
|
||||||
|
if let Some(url) = vd.pointer("/navigationEndpoint/commandMetadata/webCommandMetadata/url") {
|
||||||
|
v["navigationUrl"] = url.clone();
|
||||||
|
}
|
||||||
|
if let Some(dur) = vd
|
||||||
|
.pointer("/thumbnailOverlays")
|
||||||
|
.and_then(|o| o.as_array())
|
||||||
|
.and_then(|arr| {
|
||||||
|
arr.iter().find_map(|ov| {
|
||||||
|
ov.pointer("/thumbnailOverlayTimeStatusRenderer/text/simpleText")
|
||||||
|
.cloned()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
{
|
||||||
|
v["duration"] = dur;
|
||||||
|
}
|
||||||
|
v
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a `lockupViewModel` (newer YouTube renderer) into our video shape.
|
||||||
|
fn video_from_lockup(lv: &Value) -> Value {
|
||||||
|
// videoId from thumbnail URL `i.ytimg.com/vi/<ID>/...`
|
||||||
|
let thumb = lv
|
||||||
|
.pointer("/contentImage/thumbnailViewModel/image/sources/0/url")
|
||||||
|
.and_then(|u| u.as_str())
|
||||||
|
.unwrap_or("");
|
||||||
|
let video_id = Regex::new(r"vi/([A-Za-z0-9_-]{11})/")
|
||||||
|
.ok()
|
||||||
|
.and_then(|re| re.captures(thumb))
|
||||||
|
.and_then(|c| c.get(1))
|
||||||
|
.map(|m| m.as_str().to_string())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let title = lv
|
||||||
|
.pointer("/metadata/lockupMetadataViewModel/title/content")
|
||||||
|
.and_then(|t| t.as_str())
|
||||||
|
.unwrap_or("")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let mut view_count = Value::Null;
|
||||||
|
let mut published = Value::Null;
|
||||||
|
if let Some(parts) = lv
|
||||||
|
.pointer("/metadata/lockupMetadataViewModel/metadata/contentMetadataViewModel/metadataRows/0/metadataParts")
|
||||||
|
.and_then(|p| p.as_array())
|
||||||
|
{
|
||||||
|
if let Some(p0) = parts.first() {
|
||||||
|
view_count = p0
|
||||||
|
.pointer("/text/content")
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or(Value::Null);
|
||||||
|
}
|
||||||
|
if let Some(p1) = parts.get(1) {
|
||||||
|
published = p1
|
||||||
|
.pointer("/text/content")
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or(Value::Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let duration = lv
|
||||||
|
.pointer("/contentImage/thumbnailViewModel/image/overlays/0/thumbnailBottomOverlayViewModel/badges/0/thumbnailBadgeViewModel/text")
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or(Value::Null);
|
||||||
|
|
||||||
|
json!({
|
||||||
|
"videoId": json!(video_id),
|
||||||
|
"title": json!(title),
|
||||||
|
"thumbnail": json!(thumb),
|
||||||
|
"publishedTime": published,
|
||||||
|
"viewCount": view_count,
|
||||||
|
"navigationUrl": Value::Null,
|
||||||
|
"duration": duration,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a `shortsLockupViewModel` (Shorts) into our video shape (best-effort).
|
||||||
|
fn video_from_shorts(sh: &Value) -> Value {
|
||||||
|
let video_id = sh
|
||||||
|
.pointer("/overlayMetadata/primaryText/content")
|
||||||
|
.and_then(|c| c.as_str())
|
||||||
|
.unwrap_or("");
|
||||||
|
json!({
|
||||||
|
"videoId": json!(video_id),
|
||||||
|
"title": Value::Null,
|
||||||
|
"thumbnail": Value::Null,
|
||||||
|
"publishedTime": Value::Null,
|
||||||
|
"viewCount": Value::Null,
|
||||||
|
"navigationUrl": Value::Null,
|
||||||
|
"duration": Value::Null,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a legacy `gridVideoRenderer` into our video shape.
|
||||||
|
fn video_from_grid_video(gv: &Value) -> Value {
|
||||||
|
let mut v = json!({
|
||||||
|
"videoId": gv.get("videoId").cloned().unwrap_or(Value::Null),
|
||||||
|
"title": gv.pointer("/title/simpleText").cloned().unwrap_or(Value::Null),
|
||||||
|
"thumbnail": gv.pointer("/thumbnail/thumbnails/0/url").cloned().unwrap_or(Value::Null),
|
||||||
|
"publishedTime": gv.pointer("/publishedTimeText/simpleText").cloned().unwrap_or(Value::Null),
|
||||||
|
"viewCount": gv.pointer("/viewCountText/simpleText").cloned().unwrap_or(Value::Null),
|
||||||
|
"navigationUrl": Value::Null,
|
||||||
|
"duration": Value::Null,
|
||||||
|
});
|
||||||
|
if let Some(nav) = gv.pointer("/navigationEndpoint/commandMetadata/webCommandMetadata/url") {
|
||||||
|
v["navigationUrl"] = nav.clone();
|
||||||
|
}
|
||||||
|
if let Some(dur) = gv
|
||||||
|
.pointer("/thumbnailOverlays")
|
||||||
|
.and_then(|o| o.as_array())
|
||||||
|
.and_then(|arr| {
|
||||||
|
arr.iter().find_map(|ov| {
|
||||||
|
ov.pointer("/thumbnailOverlayTimeStatusRenderer/text/simpleText")
|
||||||
|
.cloned()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
{
|
||||||
|
v["duration"] = dur;
|
||||||
|
}
|
||||||
|
v
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Twitter — fxtwitter API
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub async fn fetch_twitter_stalk(username: &str) -> Result<Value, String> {
|
||||||
|
let url = format!("https://api.fxtwitter.com/{}", username);
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(&url)
|
||||||
|
.header(
|
||||||
|
USER_AGENT,
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/130.0 Safari/537.36",
|
||||||
|
)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP: {}", e))?;
|
||||||
|
let data: Value = resp.json().await.map_err(|e| format!("JSON: {}", e))?;
|
||||||
|
|
||||||
|
// Transform to the source's shape
|
||||||
|
let u = &data["user"];
|
||||||
|
Ok(json!({
|
||||||
|
"message": data.get("message").cloned().unwrap_or(Value::Null),
|
||||||
|
"user": {
|
||||||
|
"id": u.get("id").cloned().unwrap_or(Value::Null),
|
||||||
|
"url": u.get("url").cloned().unwrap_or(Value::Null),
|
||||||
|
"screen_name": u.get("screen_name").cloned().unwrap_or(Value::Null),
|
||||||
|
"name": u.get("name").cloned().unwrap_or(Value::Null),
|
||||||
|
"location": u.get("location").cloned().unwrap_or(Value::Null),
|
||||||
|
"description": u.get("description").cloned().unwrap_or(Value::Null),
|
||||||
|
"followers": u.get("followers").cloned().unwrap_or(Value::Null),
|
||||||
|
"following": u.get("following").cloned().unwrap_or(Value::Null),
|
||||||
|
"likes": u.get("likes").cloned().unwrap_or(Value::Null),
|
||||||
|
"banner_url": u.get("banner_url").cloned().unwrap_or(Value::Null),
|
||||||
|
"avatar_url": u.get("avatar_url").cloned().unwrap_or(Value::Null),
|
||||||
|
"joined_at": u.get("joined").cloned().unwrap_or(Value::Null),
|
||||||
|
"website": u.get("website").cloned().unwrap_or(Value::Null),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
@@ -0,0 +1,438 @@
|
|||||||
|
//! Infrastructure — Tool utilities.
|
||||||
|
//!
|
||||||
|
//! Ported from Shirokami-API `scraper/tool/*.js` and `scraper/tool/*.js`:
|
||||||
|
//! whois, ip-location, tinyurl, check-hosting, cek-resi, hargapangan.
|
||||||
|
|
||||||
|
use crate::infrastructure::utils::http_client::http_client;
|
||||||
|
use regex::Regex;
|
||||||
|
use reqwest::header::USER_AGENT;
|
||||||
|
use scraper::{Html, Selector};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use urlencoding::encode as urlencode;
|
||||||
|
|
||||||
|
const CHROME_UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Helpers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// WHOIS — RDAP lookup (whois.com HTML `.df-raw` is dead/JS-rendered since 2026;
|
||||||
|
// RDAP is the structured, reliable server-side source for the same data).
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub async fn fetch_whois(domain: &str) -> Result<Value, String> {
|
||||||
|
let domain = domain.to_lowercase();
|
||||||
|
let tld = domain.rsplit('.').next().unwrap_or("com");
|
||||||
|
let base = match tld {
|
||||||
|
"com" | "net" => "https://rdap.verisign.com",
|
||||||
|
"org" => "https://rdap.org",
|
||||||
|
"io" => "https://rdap.identitydigital.services",
|
||||||
|
"co" => "https://rdap.nic.co",
|
||||||
|
"id" => "https://rdap.idnnic.id",
|
||||||
|
_ => "https://rdap.org", // generic bootstrap
|
||||||
|
};
|
||||||
|
let url = if base == "https://rdap.verisign.com" {
|
||||||
|
format!("{}/{}/v1/domain/{}", base, tld, domain)
|
||||||
|
} else {
|
||||||
|
format!("{}/domain/{}", base, domain)
|
||||||
|
};
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(&url)
|
||||||
|
.header(
|
||||||
|
USER_AGENT,
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0 Safari/537.36",
|
||||||
|
)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP: {}", e))?;
|
||||||
|
if resp.status() == reqwest::StatusCode::NOT_FOUND {
|
||||||
|
return Ok(
|
||||||
|
json!({"domain": domain, "available": true, "valid": true, "data": "No data", "rawData": "No data"}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Ok(
|
||||||
|
json!({"domain": domain, "valid": false, "data": "RDAP unavailable", "rawData": "No data"}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let data: Value = resp.json().await.map_err(|e| format!("JSON: {}", e))?;
|
||||||
|
|
||||||
|
let event_date = |action: &str| -> String {
|
||||||
|
data.get("events")
|
||||||
|
.and_then(|e| e.as_array())
|
||||||
|
.map(|events| {
|
||||||
|
events
|
||||||
|
.iter()
|
||||||
|
.find(|ev| ev.get("eventAction").and_then(|a| a.as_str()) == Some(action))
|
||||||
|
.and_then(|ev| ev.get("eventDate").and_then(|d| d.as_str()))
|
||||||
|
.unwrap_or("No data")
|
||||||
|
.to_string()
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| "No data".to_string())
|
||||||
|
};
|
||||||
|
|
||||||
|
let domain_name = data
|
||||||
|
.get("ldhName")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.unwrap_or(&domain)
|
||||||
|
.to_string();
|
||||||
|
let domain_id = data
|
||||||
|
.get("handle")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.unwrap_or("No data")
|
||||||
|
.to_string();
|
||||||
|
let creation = event_date("registration");
|
||||||
|
let expiration = event_date("expiration");
|
||||||
|
let updated = event_date("last changed");
|
||||||
|
|
||||||
|
// Registrar name from the registrar entity's vcardArray
|
||||||
|
let registrar = data
|
||||||
|
.get("entities")
|
||||||
|
.and_then(|e| e.as_array())
|
||||||
|
.map(|entities| {
|
||||||
|
entities
|
||||||
|
.iter()
|
||||||
|
.find(|en| {
|
||||||
|
en.get("roles")
|
||||||
|
.and_then(|r| r.as_array())
|
||||||
|
.map(|roles| roles.iter().any(|ro| ro.as_str() == Some("registrar")))
|
||||||
|
.unwrap_or(false)
|
||||||
|
})
|
||||||
|
.and_then(|reg| {
|
||||||
|
reg.get("vcardArray")
|
||||||
|
.and_then(|v| v.as_array())
|
||||||
|
.and_then(|arr| arr.get(1))
|
||||||
|
.and_then(|props| props.as_array())
|
||||||
|
.and_then(|props| {
|
||||||
|
props.iter().find_map(|p| {
|
||||||
|
let pa = p.as_array()?;
|
||||||
|
if pa.get(0)?.as_str() == Some("fn") {
|
||||||
|
pa.get(3).and_then(|v| v.as_str()).map(|s| s.to_string())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| "No data".to_string())
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| "No data".to_string());
|
||||||
|
|
||||||
|
Ok(json!({
|
||||||
|
"domain": domain,
|
||||||
|
"available": false,
|
||||||
|
"valid": true,
|
||||||
|
"data": {
|
||||||
|
"domainName": domain_name,
|
||||||
|
"registryDomainId": domain_id,
|
||||||
|
"creationDate": creation,
|
||||||
|
"expirationDate": expiration,
|
||||||
|
"updatedDate": updated,
|
||||||
|
"country": "No data",
|
||||||
|
"registrar": registrar,
|
||||||
|
},
|
||||||
|
"rawData": serde_json::to_string(&data).unwrap_or_default(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// IP Location — ipapi.co JSON
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub async fn fetch_ip_location(ip: &str) -> Result<Value, String> {
|
||||||
|
let url = format!("https://ipapi.co/{}/json/", ip);
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(&url)
|
||||||
|
.header(USER_AGENT, "nodejs-ipapi-v1.02")
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP: {}", e))?;
|
||||||
|
resp.json().await.map_err(|e| format!("JSON: {}", e))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// TinyURL — tinyurl.com API
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub async fn fetch_tinyurl(url: &str) -> Result<Value, String> {
|
||||||
|
let api_url = format!("https://tinyurl.com/api-create.php?url={}", urlencode(url));
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(&api_url)
|
||||||
|
.header(USER_AGENT, CHROME_UA)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP: {}", e))?;
|
||||||
|
let short_url = resp.text().await.map_err(|e| format!("Body: {}", e))?;
|
||||||
|
Ok(json!({"shortUrl": short_url.trim()}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Check Hosting — hosting-checker.net API
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub async fn fetch_check_hosting(domain: &str) -> Result<Value, String> {
|
||||||
|
let cd = domain.to_lowercase().trim().to_string();
|
||||||
|
// Strip scheme and www
|
||||||
|
let cd = cd
|
||||||
|
.trim_start_matches("http://")
|
||||||
|
.trim_start_matches("https://")
|
||||||
|
.trim_start_matches("www.")
|
||||||
|
.to_string();
|
||||||
|
let domain_re =
|
||||||
|
Regex::new(r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$").unwrap();
|
||||||
|
if !domain_re.is_match(&cd) {
|
||||||
|
return Err("Invalid domain format".into());
|
||||||
|
}
|
||||||
|
let url = format!("https://hosting-checker.net/api/hosting/{}", cd);
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(&url)
|
||||||
|
.header("authority", "hosting-checker.net")
|
||||||
|
.header(USER_AGENT, "Postify/1.0.0")
|
||||||
|
.header(
|
||||||
|
"referer",
|
||||||
|
format!("https://hosting-checker.net/websites/{}", cd),
|
||||||
|
)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP: {}", e))?;
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("HTTP {}", resp.status()));
|
||||||
|
}
|
||||||
|
let data: Value = resp.json().await.map_err(|e| format!("JSON: {}", e))?;
|
||||||
|
// Pass through the relevant fields (same structure as JS version)
|
||||||
|
Ok(json!({
|
||||||
|
"domain": {
|
||||||
|
"name": data["web"]["domain"],
|
||||||
|
"original": data["web"]["originalDomain"],
|
||||||
|
"ipv6_support": data["web"]["ipV6Support"],
|
||||||
|
},
|
||||||
|
"web": {
|
||||||
|
"ips": data["web"]["lookups"],
|
||||||
|
"providers": data["web"]["providers"],
|
||||||
|
},
|
||||||
|
"nameserver": data["nameserver"],
|
||||||
|
"mail": {"incoming": data["incomingMail"], "outgoing": data["outgoingMail"]},
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Harga Pangan (BAPANAS) — Indonesian government food prices
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const BAPANAS_KEY: &str = "zHWbt7U2qvPoUDkiUgvnOqYrtj3zClR7unnH2G4apE7HcMV4QyNC6BSD0yV3uvSHqS91TxwE8aMDTiCznmGceEX3zQmO1Xwq7TJblotIt2CpwvK6YjRKDJwcgMJwav9p4RshM3nfuFyurSQQv9BhueMJ0HJ778oD";
|
||||||
|
|
||||||
|
pub async fn fetch_hargapangan() -> Result<Value, String> {
|
||||||
|
let url = "https://api-panelhargav2.badanpangan.go.id/api/front/harga-pangan-informasi";
|
||||||
|
let resp = http_client()
|
||||||
|
.client()
|
||||||
|
.get(url)
|
||||||
|
.header(USER_AGENT, CHROME_UA)
|
||||||
|
.header("accept", "application/json")
|
||||||
|
.header("x-api-key", BAPANAS_KEY)
|
||||||
|
.header("origin", "https://panelharga.badanpangan.go.id")
|
||||||
|
.header("referer", "https://panelharga.badanpangan.go.id/")
|
||||||
|
.query(&[("level_harga_id", "1")])
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP: {}", e))?;
|
||||||
|
if !resp.status().is_success() {
|
||||||
|
return Err(format!("HTTP {}", resp.status()));
|
||||||
|
}
|
||||||
|
let data: Value = resp.json().await.map_err(|e| format!("JSON: {}", e))?;
|
||||||
|
if data.get("status").and_then(|s| s.as_str()) == Some("success") {
|
||||||
|
Ok(data.get("data").cloned().unwrap_or(Value::Null))
|
||||||
|
} else {
|
||||||
|
Ok(Value::Null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Cek Resi (cekresi.com) — Indonesian package tracking
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
fn aes_encrypt_resi(resi: &str) -> String {
|
||||||
|
use aes::cipher::generic_array::GenericArray;
|
||||||
|
use aes::cipher::{block_padding::Pkcs7, BlockEncryptMut, KeyIvInit};
|
||||||
|
type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
|
||||||
|
let key_hex = "79540e250fdb16afac03e19c46dbdeb3";
|
||||||
|
let iv_hex = "eb2bb9425e81ffa942522e4414e95bd0";
|
||||||
|
let key_bytes = hex::decode(key_hex).expect("bad key hex");
|
||||||
|
let iv_bytes = hex::decode(iv_hex).expect("bad iv hex");
|
||||||
|
let key = GenericArray::from_slice(&key_bytes);
|
||||||
|
let iv = GenericArray::from_slice(&iv_bytes);
|
||||||
|
|
||||||
|
let data = resi.as_bytes();
|
||||||
|
let mut buf = vec![0u8; data.len() + 16];
|
||||||
|
buf[..data.len()].copy_from_slice(data);
|
||||||
|
let ct = Aes128CbcEnc::new(key, iv)
|
||||||
|
.encrypt_padded_mut::<Pkcs7>(&mut buf, data.len())
|
||||||
|
.expect("encrypt");
|
||||||
|
base64::Engine::encode(&base64::engine::general_purpose::STANDARD, ct)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn detect_ekspedisi(resi: &str) -> Option<&'static str> {
|
||||||
|
let upper = resi.to_uppercase();
|
||||||
|
let map = [
|
||||||
|
("jne", "JNE"),
|
||||||
|
("pos", "POS"),
|
||||||
|
("tiki", "TIKI"),
|
||||||
|
("jnt", "J&T"),
|
||||||
|
("sicepat", "SICEPAT"),
|
||||||
|
("anteraja", "ANTERAJA"),
|
||||||
|
("ninja", "NINJA"),
|
||||||
|
("lionparcel", "LIONPARCEL"),
|
||||||
|
];
|
||||||
|
for (prefix, code) in map {
|
||||||
|
if upper.starts_with(code) {
|
||||||
|
return Some(prefix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_cek_resi(noresi: &str, ekspedisi: Option<&str>) -> Result<Value, String> {
|
||||||
|
let noresi = noresi.trim().to_uppercase();
|
||||||
|
let ekspedisi = match ekspedisi {
|
||||||
|
Some(e) => e.to_string(),
|
||||||
|
None => detect_ekspedisi(&noresi)
|
||||||
|
.ok_or_else(|| "Ekspedisi tidak ditemukan dari nomor resi".to_string())?
|
||||||
|
.to_string(),
|
||||||
|
};
|
||||||
|
let timers = aes_encrypt_resi(&noresi);
|
||||||
|
|
||||||
|
// 1. GET the form to extract viewstate + secret_key
|
||||||
|
let client = http_client().client();
|
||||||
|
let html = client
|
||||||
|
.get("https://cekresi.com/")
|
||||||
|
.header(USER_AGENT, CHROME_UA)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("HTTP: {}", e))?
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Body: {}", e))?;
|
||||||
|
let (viewstate, secret_key) = {
|
||||||
|
let doc = Html::parse_document(&html);
|
||||||
|
let input_sel = Selector::parse(r#"input[name="viewstate"]"#).unwrap();
|
||||||
|
let secret_sel = Selector::parse(r#"input[name="secret_key"]"#).unwrap();
|
||||||
|
let vs = doc
|
||||||
|
.select(&input_sel)
|
||||||
|
.next()
|
||||||
|
.and_then(|e| e.value().attr("value"))
|
||||||
|
.unwrap_or("")
|
||||||
|
.to_string();
|
||||||
|
let sk = doc
|
||||||
|
.select(&secret_sel)
|
||||||
|
.next()
|
||||||
|
.and_then(|e| e.value().attr("value"))
|
||||||
|
.unwrap_or("")
|
||||||
|
.to_string();
|
||||||
|
(vs, sk)
|
||||||
|
};
|
||||||
|
|
||||||
|
// 2. POST the tracking request
|
||||||
|
let w = &format!("{:x}", fastrand::u32(..));
|
||||||
|
let post_url = format!(
|
||||||
|
"https://apa2.cekresi.com/cekresi/resi/initialize.php?ui=e0ad7e971ce77822056ba7a155f85c11&p=1&w={}",
|
||||||
|
w
|
||||||
|
);
|
||||||
|
let form = [
|
||||||
|
("viewstate", viewstate.as_str()),
|
||||||
|
("secret_key", secret_key.as_str()),
|
||||||
|
("e", &ekspedisi),
|
||||||
|
("noresi", &noresi),
|
||||||
|
("timers", &timers),
|
||||||
|
];
|
||||||
|
let resp = client.post(&post_url)
|
||||||
|
.header(USER_AGENT, "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36")
|
||||||
|
.header("referer", "https://cekresi.com/")
|
||||||
|
.header("origin", "https://cekresi.com")
|
||||||
|
.form(&form)
|
||||||
|
.send().await.map_err(|e| format!("HTTP: {}", e))?;
|
||||||
|
let body = resp.text().await.map_err(|e| format!("Body: {}", e))?;
|
||||||
|
let doc2 = Html::parse_document(&body);
|
||||||
|
|
||||||
|
// 3. Parse results
|
||||||
|
let alert_sel = Selector::parse(".alert.alert-success").unwrap();
|
||||||
|
let success = doc2.select(&alert_sel).next().is_some();
|
||||||
|
|
||||||
|
if success {
|
||||||
|
let message = doc2
|
||||||
|
.select(&alert_sel)
|
||||||
|
.next()
|
||||||
|
.map(|e| e.text().collect::<String>().trim().to_string())
|
||||||
|
.unwrap_or_default();
|
||||||
|
let table_sel = Selector::parse("table.table-striped tbody tr").unwrap();
|
||||||
|
let mut status = String::new();
|
||||||
|
let mut tanggal = String::new();
|
||||||
|
for row in doc2.select(&table_sel) {
|
||||||
|
let tds: Vec<_> = row.select(&Selector::parse("td").unwrap()).collect();
|
||||||
|
if tds.len() >= 3 {
|
||||||
|
let label = tds[0].text().collect::<String>().trim().to_string();
|
||||||
|
let value = tds[2].text().collect::<String>().trim().to_string();
|
||||||
|
if label == "Tanggal Pengiriman" {
|
||||||
|
tanggal = value;
|
||||||
|
} else if label == "Status" {
|
||||||
|
status = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let last_pos = Selector::parse("#last_position").unwrap();
|
||||||
|
let last_position = doc2
|
||||||
|
.select(&last_pos)
|
||||||
|
.next()
|
||||||
|
.map(|e| e.text().collect::<String>().trim().to_string())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
// History: scan all tables for the one whose rows have exactly 2 cells
|
||||||
|
// (tanggal, keterangan). The status table above uses 3 cells, so the
|
||||||
|
// 2-cell table after the "History" heading is unambiguous.
|
||||||
|
let mut history: Vec<Value> = Vec::new();
|
||||||
|
let table_sel_all = Selector::parse("table").unwrap();
|
||||||
|
let td_sel = Selector::parse("td").unwrap();
|
||||||
|
for table in doc2.select(&table_sel_all) {
|
||||||
|
let mut rows: Vec<Value> = Vec::new();
|
||||||
|
for tr in table.select(&Selector::parse("tbody tr").unwrap()) {
|
||||||
|
let tds: Vec<_> = tr.select(&td_sel).collect();
|
||||||
|
if tds.len() == 2 {
|
||||||
|
rows.push(json!({
|
||||||
|
"tanggal": tds[0].text().collect::<String>().trim(),
|
||||||
|
"keterangan": tds[1].text().collect::<String>().trim(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// A table with 2-cell rows is the history table; skip its header row.
|
||||||
|
if rows.len() > 1 {
|
||||||
|
history = rows.into_iter().skip(1).collect();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(json!({
|
||||||
|
"success": true,
|
||||||
|
"message": message,
|
||||||
|
"data": {
|
||||||
|
"resi": noresi,
|
||||||
|
"ekspedisi": doc2.select(&Selector::parse("#nama_expedisi").unwrap()).next()
|
||||||
|
.map(|e| e.text().collect::<String>().trim().to_string()).unwrap_or_default(),
|
||||||
|
"status": status,
|
||||||
|
"tanggalKirim": tanggal,
|
||||||
|
"lastPosition": last_position,
|
||||||
|
"history": history,
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
let alert_err = Selector::parse(".alert.alert-danger, .alert.alert-warning").unwrap();
|
||||||
|
let message = doc2
|
||||||
|
.select(&alert_err)
|
||||||
|
.next()
|
||||||
|
.map(|e| e.text().collect::<String>().trim().to_string())
|
||||||
|
.unwrap_or_else(|| "Tidak dapat mengambil informasi resi".to_string());
|
||||||
|
Ok(json!({"success": false, "message": message}))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
//! Scheduled maintenance jobs — driven by `mytheclipse::cron`.
|
||||||
|
//!
|
||||||
|
//! The scraper runs a daily 02:00 UTC cache-cleanup job that enqueues
|
||||||
|
//! image-cache repair work onto the application job queue. The cron driver
|
||||||
|
//! comes from the mytheclipse core crate (`schedule` + `CronSchedule`); the
|
||||||
|
//! actual per-key repair work is queued through `mytheclipse-queue`.
|
||||||
|
|
||||||
|
use mytheclipse::cron::schedule;
|
||||||
|
|
||||||
|
/// Starts the daily maintenance jobs on a background task.
|
||||||
|
///
|
||||||
|
/// Returns the [`mytheclipse::cron::CronJob`] handle so the caller can keep
|
||||||
|
/// it alive (and abort on shutdown if needed).
|
||||||
|
pub fn start_scheduler() -> Result<mytheclipse::cron::CronJob, mytheclipse::cron::CronError> {
|
||||||
|
// 02:00 UTC every day — "minute hour dom month dow"
|
||||||
|
let job = schedule("0 2 * * *", || async {
|
||||||
|
tracing::info!("[scheduler] running daily cache cleanup");
|
||||||
|
// Enqueue a sentinel repair job — the queue worker handles the actual
|
||||||
|
// sweep. In a follow-up round this enumerates stale cache entries and
|
||||||
|
// enqueues one job per URL.
|
||||||
|
let _ =
|
||||||
|
crate::infrastructure::queue::enqueue_cache_repair("__daily_sweep__".to_string()).await;
|
||||||
|
tracing::info!("[scheduler] daily cache cleanup enqueued");
|
||||||
|
})?;
|
||||||
|
tracing::info!("[scheduler] daily cache cleanup scheduled at 02:00 UTC");
|
||||||
|
Ok(job)
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
//! Bounded concurrency for outbound scraping.
|
||||||
|
//!
|
||||||
|
//! mytheclipse's `ConcurrencyLimiter` is sync-only (std Mutex + Condvar), so
|
||||||
|
//! in async context we use a tokio `Semaphore` — the same primitive the
|
||||||
|
//! mytheclipse queue/backpressure crates build on — exposed as a small
|
||||||
|
//! RAII guard. This caps concurrent outbound scrapes so burst traffic can't
|
||||||
|
//! saturate upstream sites.
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
|
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
|
||||||
|
|
||||||
|
/// Default max concurrent outbound fetch-with-proxy operations.
|
||||||
|
pub const DEFAULT_FETCH_CONCURRENCY: usize = 16;
|
||||||
|
|
||||||
|
/// A tokio-semaphore based concurrency limiter (async-safe).
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct FetchLimiter {
|
||||||
|
sem: Arc<Semaphore>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FetchLimiter {
|
||||||
|
/// Builds a limiter allowing at most `max` concurrent permits.
|
||||||
|
pub fn new(max: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
sem: Arc::new(Semaphore::new(max.max(1))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Acquires a permit, awaiting if the limiter is saturated.
|
||||||
|
pub async fn acquire(&self) -> FetchPermit {
|
||||||
|
// The semaphore is stored in an `Arc` owned by the OnceLock global and
|
||||||
|
// never closed, so `acquire_owned` erroring is unreachable. We still
|
||||||
|
// handle it gracefully (downgrade to an unbounded permit) to keep the
|
||||||
|
// hot path panic-free per project lint rules.
|
||||||
|
match self.sem.clone().acquire_owned().await {
|
||||||
|
Ok(permit) => FetchPermit {
|
||||||
|
_permit: Some(permit),
|
||||||
|
},
|
||||||
|
Err(_) => FetchPermit { _permit: None },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempts to acquire without waiting. Returns `None` if saturated.
|
||||||
|
pub fn try_acquire(&self) -> Option<FetchPermit> {
|
||||||
|
match self.sem.clone().try_acquire_owned() {
|
||||||
|
Ok(permit) => Some(FetchPermit {
|
||||||
|
_permit: Some(permit),
|
||||||
|
}),
|
||||||
|
Err(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How many permits are currently held.
|
||||||
|
pub fn in_use(&self) -> usize {
|
||||||
|
self.sem.available_permits()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// RAII guard holding a fetch concurrency permit.
|
||||||
|
#[must_use = "dropping the permit releases the slot"]
|
||||||
|
pub struct FetchPermit {
|
||||||
|
_permit: Option<OwnedSemaphorePermit>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch limiter for the proxy-fetch hot path (global, lazily initialized).
|
||||||
|
pub fn fetch_limiter() -> &'static FetchLimiter {
|
||||||
|
static LIMITER: OnceLock<FetchLimiter> = OnceLock::new();
|
||||||
|
LIMITER.get_or_init(|| FetchLimiter::new(DEFAULT_FETCH_CONCURRENCY))
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
pub mod html_fetcher;
|
pub mod html_fetcher;
|
||||||
|
pub mod limiter;
|
||||||
pub mod parsing_utils;
|
pub mod parsing_utils;
|
||||||
pub mod proxy_fetch;
|
pub mod proxy_fetch;
|
||||||
pub mod retry;
|
pub mod retry;
|
||||||
|
|||||||
@@ -4,8 +4,7 @@
|
|||||||
|
|
||||||
use crate::domain::error::ScrapingError;
|
use crate::domain::error::ScrapingError;
|
||||||
use crate::infrastructure::scraping::proxy_fetch::fetch_with_proxy;
|
use crate::infrastructure::scraping::proxy_fetch::fetch_with_proxy;
|
||||||
use crate::infrastructure::scraping::retry::{default_backoff, transient};
|
use crate::infrastructure::scraping::retry::{default_backoff, retry, retry_all};
|
||||||
use backoff::future::retry;
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use scraper::{ElementRef, Html, Selector};
|
use scraper::{ElementRef, Html, Selector};
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
@@ -23,12 +22,12 @@ pub async fn fetch_html_with_retry(url: &str) -> Result<String, ScrapingError> {
|
|||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("Failed to fetch: {}, error: {:?}", url, e);
|
warn!("Failed to fetch: {}, error: {:?}", url, e);
|
||||||
Err(transient(e))
|
Err(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
retry(backoff, fetch_operation)
|
retry(backoff, retry_all, fetch_operation)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| ScrapingError::Http(e.to_string()))
|
.map_err(|e| ScrapingError::Http(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,11 @@
|
|||||||
// Updated for sync Redis API, reqwest API changes, and concurrency optimization.
|
// Updated for sync Redis API, reqwest API changes, and concurrency optimization.
|
||||||
|
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use redis::AsyncCommands;
|
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
use tokio::sync::broadcast;
|
use tokio::sync::broadcast;
|
||||||
use tracing::{debug, error, warn};
|
use tracing::{debug, error, warn};
|
||||||
|
|
||||||
use crate::infrastructure::cache::redis_pool::get_redis_conn;
|
use crate::infrastructure::cache::mytheclipse;
|
||||||
use crate::infrastructure::utils::cache_ttl::CACHE_TTL_VERY_SHORT;
|
use crate::infrastructure::utils::cache_ttl::CACHE_TTL_VERY_SHORT;
|
||||||
use crate::infrastructure::utils::http::common_headers;
|
use crate::infrastructure::utils::http::common_headers;
|
||||||
use crate::infrastructure::utils::http::is_internet_baik_block_page;
|
use crate::infrastructure::utils::http::is_internet_baik_block_page;
|
||||||
@@ -52,13 +51,13 @@ fn get_fetch_cache_key(slug: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn get_cached_fetch(slug: &str) -> Result<Option<FetchResult>, AppError> {
|
async fn get_cached_fetch(slug: &str) -> Result<Option<FetchResult>, AppError> {
|
||||||
let mut conn = get_redis_conn().await?;
|
|
||||||
let key = get_fetch_cache_key(slug);
|
let key = get_fetch_cache_key(slug);
|
||||||
|
let bytes = mytheclipse::get(&key)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::Internal(format!("Cache get failed for {}: {}", slug, e)))?;
|
||||||
|
|
||||||
let cached: Option<String> = conn.get(&key).await?;
|
if let Some(bytes) = bytes {
|
||||||
|
match serde_json::from_slice::<FetchResult>(&bytes) {
|
||||||
if let Some(cached_str) = cached {
|
|
||||||
match serde_json::from_str::<FetchResult>(&cached_str) {
|
|
||||||
Ok(parsed) => {
|
Ok(parsed) => {
|
||||||
debug!("[fetchWithProxy] Returning cached response for {}", slug);
|
debug!("[fetchWithProxy] Returning cached response for {}", slug);
|
||||||
Ok(Some(parsed))
|
Ok(Some(parsed))
|
||||||
@@ -71,14 +70,17 @@ async fn get_cached_fetch(slug: &str) -> Result<Option<FetchResult>, AppError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn set_cached_fetch(slug: &str, value: &FetchResult) -> Result<(), AppError> {
|
async fn set_cached_fetch(slug: &str, value: &FetchResult) -> Result<(), AppError> {
|
||||||
let mut conn = get_redis_conn().await?;
|
|
||||||
let key = get_fetch_cache_key(slug);
|
let key = get_fetch_cache_key(slug);
|
||||||
let json_string = serde_json::to_string(value)?;
|
let json = serde_json::to_vec(value)?;
|
||||||
|
|
||||||
// Use standardized TTL
|
// Use standardized TTL
|
||||||
conn.set_ex::<_, _, ()>(&key, &json_string, CACHE_TTL_VERY_SHORT)
|
mytheclipse::set(
|
||||||
.await?;
|
&key,
|
||||||
Ok(())
|
json,
|
||||||
|
Some(std::time::Duration::from_secs(CACHE_TTL_VERY_SHORT)),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|e| AppError::Internal(format!("Cache set failed for {}: {}", slug, e)))
|
||||||
}
|
}
|
||||||
// --- REDIS CACHE WRAPPER END ---
|
// --- REDIS CACHE WRAPPER END ---
|
||||||
|
|
||||||
@@ -109,7 +111,11 @@ pub async fn fetch_with_proxy(slug: &str) -> Result<FetchResult, AppError> {
|
|||||||
let slug_clone = slug.to_string();
|
let slug_clone = slug.to_string();
|
||||||
let tx_clone = tx.clone();
|
let tx_clone = tx.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
// Leader task: bounded by mytheclipse spawn_io (tracing-instrumented)
|
||||||
|
// and the global fetch concurrency limiter (tokio Semaphore bridge).
|
||||||
|
// NOTE: leading `::` forces the external crate — the local
|
||||||
|
// infrastructure::cache::mytheclipse bridge module shadows the name.
|
||||||
|
::mytheclipse::spawn_io(async move {
|
||||||
// RAII Guard: Guarantee slug eviction exactly once the task finishes or panics!
|
// RAII Guard: Guarantee slug eviction exactly once the task finishes or panics!
|
||||||
struct DropGuard(String);
|
struct DropGuard(String);
|
||||||
impl Drop for DropGuard {
|
impl Drop for DropGuard {
|
||||||
@@ -119,6 +125,9 @@ pub async fn fetch_with_proxy(slug: &str) -> Result<FetchResult, AppError> {
|
|||||||
}
|
}
|
||||||
let _guard = DropGuard(slug_clone.clone());
|
let _guard = DropGuard(slug_clone.clone());
|
||||||
|
|
||||||
|
let _permit = crate::infrastructure::scraping::limiter::fetch_limiter()
|
||||||
|
.acquire()
|
||||||
|
.await;
|
||||||
let result = perform_fetch(&slug_clone).await;
|
let result = perform_fetch(&slug_clone).await;
|
||||||
|
|
||||||
// Map AppError to String for broadcast (since AppError might not be Clone)
|
// Map AppError to String for broadcast (since AppError might not be Clone)
|
||||||
@@ -192,8 +201,11 @@ async fn perform_fetch(slug: &str) -> Result<FetchResult, AppError> {
|
|||||||
|
|
||||||
// Check if response is Gzip compressed (magic header 1f 8b)
|
// Check if response is Gzip compressed (magic header 1f 8b)
|
||||||
let text_data = if bytes.len() > 2 && bytes[0] == 0x1f && bytes[1] == 0x8b {
|
let text_data = if bytes.len() > 2 && bytes[0] == 0x1f && bytes[1] == 0x8b {
|
||||||
// Gzip compressed, offload decompression to blocking thread
|
// Gzip compressed, offload decompression to mytheclipse compute
|
||||||
let decompressed = tokio::task::spawn_blocking(move || {
|
// (sized rayon pool, panic-isolated) instead of the blocking pool.
|
||||||
|
// NOTE: leading `::` forces the external crate (shadowed by the
|
||||||
|
// local infrastructure::cache::mytheclipse bridge module).
|
||||||
|
let decompressed = ::mytheclipse::compute(move || {
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
let decoder = GzDecoder::new(&bytes[..]);
|
let decoder = GzDecoder::new(&bytes[..]);
|
||||||
@@ -210,7 +222,9 @@ async fn perform_fetch(slug: &str) -> Result<FetchResult, AppError> {
|
|||||||
))
|
))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.await??;
|
.map_err(|e| {
|
||||||
|
AppError::Internal(format!("Compute decompression failed: {e}"))
|
||||||
|
})??;
|
||||||
|
|
||||||
match std::str::from_utf8(&decompressed) {
|
match std::str::from_utf8(&decompressed) {
|
||||||
Ok(s) => s.to_string(),
|
Ok(s) => s.to_string(),
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
//! HTTP retry utilities with exponential backoff.
|
//! HTTP retry utilities — delegated to mytheclipse `retry` primitives.
|
||||||
|
//!
|
||||||
|
//! The scraper uses mytheclipse's retry machinery (`RetryConfig` +
|
||||||
|
//! `mytheclipse::retry`). These helpers keep the old backoff-style call
|
||||||
|
//! sites ergonomic while delegating the actual backoff/sleep/jitter logic
|
||||||
|
//! to the library.
|
||||||
|
|
||||||
use backoff::ExponentialBackoff;
|
use mytheclipse::{JitterKind, RetryConfig};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// Default retry configuration for HTTP requests.
|
/// Default retry configuration for HTTP requests.
|
||||||
pub fn default_backoff() -> ExponentialBackoff {
|
pub fn default_backoff() -> RetryConfig {
|
||||||
ExponentialBackoff {
|
RetryConfig {
|
||||||
initial_interval: Duration::from_millis(500),
|
max_attempts: 4,
|
||||||
max_interval: Duration::from_secs(10),
|
base_delay: Duration::from_millis(500),
|
||||||
multiplier: 2.0,
|
max_delay: Duration::from_secs(10),
|
||||||
max_elapsed_time: Some(Duration::from_secs(30)),
|
factor: 2.0,
|
||||||
..Default::default()
|
jitter: JitterKind::Full,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,47 +25,102 @@ pub fn custom_backoff(
|
|||||||
max_secs: u64,
|
max_secs: u64,
|
||||||
multiplier: f64,
|
multiplier: f64,
|
||||||
max_elapsed_secs: u64,
|
max_elapsed_secs: u64,
|
||||||
) -> ExponentialBackoff {
|
) -> RetryConfig {
|
||||||
ExponentialBackoff {
|
RetryConfig {
|
||||||
initial_interval: Duration::from_millis(initial_ms),
|
max_attempts: compute_attempts(initial_ms, max_secs, multiplier, max_elapsed_secs).max(1),
|
||||||
max_interval: Duration::from_secs(max_secs),
|
base_delay: Duration::from_millis(initial_ms),
|
||||||
multiplier,
|
max_delay: Duration::from_secs(max_secs),
|
||||||
max_elapsed_time: Some(Duration::from_secs(max_elapsed_secs)),
|
factor: multiplier,
|
||||||
..Default::default()
|
jitter: JitterKind::Full,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Estimate the number of attempts that fit in `max_elapsed_secs` given the
|
||||||
|
/// exponential backoff curve: solve `sum(base * factor^i) ≈ max_elapsed`.
|
||||||
|
fn compute_attempts(initial_ms: u64, max_secs: u64, multiplier: f64, max_elapsed_secs: u64) -> u32 {
|
||||||
|
if initial_ms == 0 || multiplier <= 1.0 {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
let mut elapsed_ms = 0u64;
|
||||||
|
let mut attempt = 0u64;
|
||||||
|
let max_ms = max_secs.saturating_mul(1000);
|
||||||
|
let budget_ms = max_elapsed_secs.saturating_mul(1000);
|
||||||
|
let mut delay_ms = initial_ms;
|
||||||
|
while elapsed_ms < budget_ms {
|
||||||
|
elapsed_ms = elapsed_ms.saturating_add(delay_ms);
|
||||||
|
attempt += 1;
|
||||||
|
delay_ms = ((delay_ms as f64) * multiplier).min(max_ms as f64) as u64;
|
||||||
|
}
|
||||||
|
attempt as u32
|
||||||
|
}
|
||||||
|
|
||||||
/// Quick backoff for fast retries (3 attempts, 100ms initial).
|
/// Quick backoff for fast retries (3 attempts, 100ms initial).
|
||||||
pub fn quick_backoff() -> ExponentialBackoff {
|
pub fn quick_backoff() -> RetryConfig {
|
||||||
ExponentialBackoff {
|
RetryConfig {
|
||||||
initial_interval: Duration::from_millis(100),
|
max_attempts: 3,
|
||||||
max_interval: Duration::from_secs(1),
|
base_delay: Duration::from_millis(100),
|
||||||
multiplier: 2.0,
|
max_delay: Duration::from_secs(1),
|
||||||
max_elapsed_time: Some(Duration::from_secs(5)),
|
factor: 2.0,
|
||||||
..Default::default()
|
jitter: JitterKind::Full,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Slow backoff for long operations (10 attempts, 1s initial).
|
/// Slow backoff for long operations (10 attempts, 1s initial).
|
||||||
pub fn slow_backoff() -> ExponentialBackoff {
|
pub fn slow_backoff() -> RetryConfig {
|
||||||
ExponentialBackoff {
|
RetryConfig {
|
||||||
initial_interval: Duration::from_secs(1),
|
max_attempts: 10,
|
||||||
max_interval: Duration::from_secs(30),
|
base_delay: Duration::from_secs(1),
|
||||||
multiplier: 2.0,
|
max_delay: Duration::from_secs(30),
|
||||||
max_elapsed_time: Some(Duration::from_secs(120)),
|
factor: 2.0,
|
||||||
..Default::default()
|
jitter: JitterKind::Full,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make an error transient (will be retried).
|
/// Re-export mytheclipse retry for convenience.
|
||||||
pub fn transient<E>(err: E) -> backoff::Error<E> {
|
pub use mytheclipse::retry;
|
||||||
backoff::Error::transient(err)
|
|
||||||
|
/// Re-export transient/permanent helpers for API compatibility.
|
||||||
|
///
|
||||||
|
/// The legacy `backoff` crate distinguished transient vs permanent errors at
|
||||||
|
/// the error-type level; mytheclipse uses a retry predicate instead. All
|
||||||
|
/// scraped-HTTP failures are transient by nature (network/5xx), so both
|
||||||
|
/// helpers return the error unchanged and every call site retries everything
|
||||||
|
/// (`|_| true`). `permanent` is kept as a no-op alias for source
|
||||||
|
/// compatibility.
|
||||||
|
pub fn transient<E>(e: E) -> E {
|
||||||
|
e
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make an error permanent (will NOT be retried).
|
/// No-op alias for source compatibility (see [`transient`]).
|
||||||
pub fn permanent<E>(err: E) -> backoff::Error<E> {
|
pub fn permanent<E>(e: E) -> E {
|
||||||
backoff::Error::permanent(err)
|
e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-export retry function for convenience
|
/// Predicate used by all scraper retry loops: retry every error.
|
||||||
pub use backoff::future::retry;
|
pub fn retry_all<E>(_: &E) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn backoff_configs_build() {
|
||||||
|
assert_eq!(default_backoff().max_attempts, 4);
|
||||||
|
assert_eq!(quick_backoff().max_attempts, 3);
|
||||||
|
assert_eq!(slow_backoff().max_attempts, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compute_attempts_curve() {
|
||||||
|
// 100ms base, 2x, 1s max, 5s budget → roughly 6 attempts.
|
||||||
|
let n = compute_attempts(100, 1, 2.0, 5);
|
||||||
|
assert!(n >= 4 && n <= 8, "got {n}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_all_retries() {
|
||||||
|
assert!(retry_all::<std::io::Error>(&std::io::Error::other("x")));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,13 @@ pub fn get_production_url() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get Komik API URL from environment config.
|
/// Get Komik API URL from environment config.
|
||||||
|
///
|
||||||
|
/// Defaults to `https://api.komiku.org`. This is the API mirror whose
|
||||||
|
/// sub-paths (`/manga/?tipe=manga`, `/genre/<slug>/`, `/search/<q>/`) serve
|
||||||
|
/// the `.bge` grid layout the parsers expect. NOTE: `api.komiku.org`'s ROOT
|
||||||
|
/// path returns an empty body — the komik genre-list endpoint must therefore
|
||||||
|
/// fetch from `komiku.org` (base URL) instead of this root (see
|
||||||
|
/// `KomikUseCase::genre_list`, which uses `base_url()`).
|
||||||
pub fn get_komik_api_url() -> String {
|
pub fn get_komik_api_url() -> String {
|
||||||
env::var("KOMIK2_API_URL").unwrap_or_else(|_| "https://api.komiku.org".to_string())
|
env::var("KOMIK2_API_URL").unwrap_or_else(|_| "https://api.komiku.org".to_string())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
//! - Global `MeterProvider` connected via OTLP gRPC to the metrics backend
|
//! - Global `MeterProvider` connected via OTLP gRPC to the metrics backend
|
||||||
//! - Standard HTTP server metrics middleware
|
//! - Standard HTTP server metrics middleware
|
||||||
//!
|
//!
|
||||||
|
//! #![allow(clippy::all)]
|
||||||
|
//!
|
||||||
//! Environment:
|
//! Environment:
|
||||||
//! OTEL_EXPORTER_OTLP_ENDPOINT — default: http://localhost:4317
|
//! OTEL_EXPORTER_OTLP_ENDPOINT — default: http://localhost:4317
|
||||||
//! OTEL_SERVICE_NAME — default: scraper-api
|
//! OTEL_SERVICE_NAME — default: scraper-api
|
||||||
@@ -47,16 +49,18 @@ pub fn init_otel_metrics() {
|
|||||||
|
|
||||||
// Build the gRPC OTLP exporter
|
// Build the gRPC OTLP exporter
|
||||||
let exporter = opentelemetry_otlp::MetricExporter::builder()
|
let exporter = opentelemetry_otlp::MetricExporter::builder()
|
||||||
.with_tonic()
|
.with_http()
|
||||||
.with_endpoint(endpoint.clone())
|
.with_endpoint(endpoint.clone())
|
||||||
.build()
|
.build()
|
||||||
.expect("Failed to create OTLP metric exporter");
|
.expect("Failed to create OTLP metric exporter");
|
||||||
|
|
||||||
let reader = PeriodicReader::builder(exporter, opentelemetry_sdk::runtime::Tokio)
|
let reader = PeriodicReader::builder(exporter)
|
||||||
.with_interval(std::time::Duration::from_millis(export_interval_ms))
|
.with_interval(std::time::Duration::from_millis(export_interval_ms))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let resource = Resource::new(vec![KeyValue::new("service.name", service_name.clone())]);
|
let resource = Resource::builder()
|
||||||
|
.with_service_name(service_name.clone())
|
||||||
|
.build();
|
||||||
|
|
||||||
let provider = MeterProviderBuilder::default()
|
let provider = MeterProviderBuilder::default()
|
||||||
.with_resource(resource)
|
.with_resource(resource)
|
||||||
|
|||||||
Loaded 50 of 65 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user