Compare commits

..
3 Commits
Author SHA1 Message Date
asepharyana 7d68ff5620 fix(downloader): terabox proxy as primary link + accept threads.net URLs
Deploy Scraper / build-and-deploy (push) Canceled after 0s
- Terabox: return the working cookie-authenticated proxy URL as the
  FIRST media entry; keep the raw dlink second with an honest note
  (it 403s without cookies).
- Threads: accept threads.net URLs in the snapsave/IG validator so
  /download/threads no longer rejects them.
2026-09-07 13:40:32 +07:00
asepharyana 8b9bdeb56a fix(downloader): real FB media for all URL shapes + public terabox proxy
Facebook:
- Broaden URL validation to accept ALL real FB shapes: /watch/?v=,
  /reel/:id, /videos/:id (user vids), /video.php, /story.php, fb.watch,
  and no-www facebook.com. Old regex rejected /reel, /videos, fb.watch.
- Add yt-dlp as the PRIMARY provider (authoritative, returns direct
  fbcdn progressive URLs with real title/format metadata, no junk).
  Only falls back to downr.org then Playwright. This kills the
  'link exists but 404s' bug where Playwright harvested broken
  bytestart byte-range chunks alongside the one good URL.
- Filter Playwright-harvested bytestart= chunk URLs + dedupe.

TeraBox:
- Add /proxy/terabox streaming endpoint that forwards the resolver's
  cookie-authenticated stream through the public API.
- Rewrite the returned download URL to the public proxy (raw dlink
  403s without cookies; TERABOX_PUBLIC_BASE_URL env override).
- Fix: server_port env is APP_SERVER_PORT (single underscore); the
  OLD stale 4091 bind in tests came from APP__SERVER_PORT not mapping.

flake: bump scraper-0.1.0 -> 0.1.1
2026-09-07 13:20:11 +07:00
asepharyana 029837d0f1 feat(downloader): youtube progressive-first + server-side merge
- /download/youtube now PREFERS a progressive MP4 (single file with
  video+audio) using the android player client — full-speed from VPS,
  no datacenter rate-limit (verified: itag 18 downloads 28MB in ~3s).
- Falls back to adaptive bestvideo+bestaudio (2 URLs) when the requested
  quality has no progressive format.
- New param merge=true triggers server-side ffmpeg merge into one MP4,
  served from GET /file/yt_merge/{name} (temp, 2h TTL cleanup).
- /download/youtube/mp3: report the REAL container ext (webm/opus)
  instead of hardcoded 'mp3'.
2026-09-05 20:11:16 +07:00
6 changed files with 794 additions and 25 deletions

No files matched your search

+1 -1
View File
@@ -15,7 +15,7 @@
};
scraper = pkgs.stdenv.mkDerivation {
name = "scraper-0.1.0";
name = "scraper-0.1.1";
src = ./.;
nativeBuildInputs = with pkgs; [
+7
View File
@@ -42,6 +42,13 @@ pub async fn download_youtube(url: &str, quality: &str) -> Result<DownloadResult
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
}
+590 -20
View File
@@ -5,6 +5,8 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::Duration;
use aes::cipher::{BlockDecrypt, KeyInit};
@@ -511,6 +513,14 @@ impl DownloaderRepository {
fetch_youtube_mp4(url, quality).await
}
/// YouTube video + audio merged server-side into a single MP4.
pub async fn fetch_youtube_merge(
url: &str,
quality: &str,
) -> Result<DownloadResult, ScrapingError> {
fetch_youtube_merge_free(url, quality).await
}
/// YouTube to MP3 via ydlp.yard.id.
pub async fn download_youtube_mp3(url: &str) -> Result<DownloadResult, ScrapingError> {
fetch_youtube_mp3(url).await
@@ -844,12 +854,26 @@ async fn detect_media_type(url: &str) -> MediaType {
/// SnapSave parser — extracts Instagram/Facebook media via snapsave.app.
/// Uses downr.org as fallback for robustness, then Playwright browser scraping.
pub(crate) async fn fetch_snapsave(url: &str) -> Result<DownloadResult, ScrapingError> {
// Validate Instagram/Facebook URL
let valid_fb = regex::Regex::new(r"https?://(web\.|www\.|m\.)(facebook|fb)\.(com|watch)\S+")
// Validate Instagram/Facebook URL.
//
// Facebook link shapes (all must pass):
// https://www.facebook.com/watch/?v=123
// https://fb.watch/abc123/
// https://m.facebook.com/video.php?v=123
// https://www.facebook.com/100012345678901/videos/1234567890
// https://www.facebook.com/reel/123
// https://www.facebook.com/story.php?story_fbid=123
// https://web.facebook.com/...
// NOTE: the old regex required a `www.|m.|web.` subdomain AND ended the
// host capture with `(facebook|fb)\.(com|watch)` — that rejected
// `facebook.com/watch...` (no subdomain) and `fb.watch/...` entirely.
let fb_host = r"(?:[a-z0-9-]*\.)?(?:facebook|fb)\.(?:com|watch)";
let valid_fb = regex::Regex::new(&format!(r"https?://{}(?:/|$)", fb_host))
.unwrap()
.is_match(url);
let valid_ig =
regex::Regex::new(r"https?://(www\.)?instagram\.com/(p|reel|reels|tv|stories)/\S+")
let valid_ig = url.contains("instagram.com")
|| url.contains("threads.net")
|| regex::Regex::new(r"https?://(www\.)?instagram\.com/[^\s]+")
.unwrap()
.is_match(url);
@@ -859,7 +883,24 @@ pub(crate) async fn fetch_snapsave(url: &str) -> Result<DownloadResult, Scraping
));
}
// Try downr.org first
// Try well-known scrapers in order — Playwright is a LAST resort (heavy,
// slow, and its naive response-harvesting returns many broken/404 links
// mixed with the one good URL, which is exactly the "link exists but can't
// download" bug we've seen).
//
// 1) yt-dlp facebook extractor — authoritative, returns direct fbcdn
// progressive URLs with REAL title/format metadata and only the working
// scales (sd + hd, no junk). Also handles /reel, /watch, /videos,
// story.php, video.php, fb.watch URL shapes.
if let Ok(yt) = run_ytdlp_json(url, &["-f", "bestvideo+bestaudio/best"]).await {
if let Some(res) = ytdlp_to_download_result(&yt) {
if !res.media.is_empty() {
return Ok(res);
}
}
}
// 2) downr.org all-in-one (same provider as Shirokami's /fbdl)
match fetch_all_in_one(url).await {
Ok(result) if !result.media.is_empty() => return Ok(result),
Ok(_) => {}
@@ -871,7 +912,7 @@ pub(crate) async fn fetch_snapsave(url: &str) -> Result<DownloadResult, Scraping
}
}
// Fallback: use Playwright browser scraping to extract video URLs
// 3) Playwright browser scraping to extract video URLs
let platform = if valid_ig { "instagram" } else { "facebook" };
let data = run_playwright_scraper(url, platform).await?;
@@ -889,17 +930,25 @@ pub(crate) async fn fetch_snapsave(url: &str) -> Result<DownloadResult, Scraping
let media_arr = data.get("media").and_then(|v| v.as_array());
if let Some(medias) = media_arr {
for m in medias {
let u = m
.get("url")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
// Drop Playwright-harvested links that are actually broken
// byte-range chunks (fbcdn URLs with bytestart=…byteend=… that
// return tiny 200 responses — they 404 or return an HTML error
// page when fetched as a whole), and the empty-string url.
if u.is_empty() || u.contains("bytestart=") {
continue;
}
let item = MediaItem {
url: m
.get("url")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string(),
url: u,
quality: None,
file_type: m.get("ext").and_then(|v| v.as_str()).and_then(|e| match e {
"mp4" | "m3u8" => Some(MediaType::Video),
"mp3" | "m4a" => Some(MediaType::Audio),
_ => Some(MediaType::Video),
file_type: m.get("ext").and_then(|v| v.as_str()).map(|e| match e {
"mp4" | "m3u8" => MediaType::Video,
"mp3" | "m4a" => MediaType::Audio,
_ => MediaType::Video,
}),
extension: m.get("ext").and_then(|v| v.as_str()).map(|s| s.to_string()),
thumbnail: None,
@@ -913,6 +962,13 @@ pub(crate) async fn fetch_snapsave(url: &str) -> Result<DownloadResult, Scraping
}
}
// De-duplicate by URL (Playwright harvests the same CDN file at many
// byte-ranges, and the primary URL appears multiple times).
{
let mut seen = std::collections::HashSet::new();
result.media.retain(|m| seen.insert(m.url.clone()));
}
if result.media.is_empty() {
return Ok(DownloadResult::error(format!(
"Failed to extract media from {} — server IP may be blocked by anti-bot protection",
@@ -922,6 +978,122 @@ pub(crate) async fn fetch_snapsave(url: &str) -> Result<DownloadResult, Scraping
Ok(result)
}
/// Convert a yt-dlp --dump-single-json result into a DownloadResult with the
/// direct progressive/adaptive media URLs (only entries that carry a URL).
/// Used by the Facebook downloader as the primary provider.
fn ytdlp_to_download_result(data: &serde_json::Value) -> Option<DownloadResult> {
let title = data
.get("title")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let mut result = DownloadResult::success(title);
result.provider = Some("yt-dlp".to_string());
result.thumbnail = data
.get("thumbnail")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
result.duration = data
.get("duration")
.and_then(|v| v.as_u64())
.map(|d| format!("{}s", d));
let formats = data.get("formats").and_then(|v| v.as_array());
let requested_formats = data.get("requested_formats").and_then(|v| v.as_array());
let mut seen = std::collections::HashSet::new();
// `-f bestvideo+bestaudio/best` puts the merged URLs in requested_formats.
// `best` single format lands in top-level url + formats[0].
for list in [requested_formats, formats].into_iter().flatten() {
for f in list {
let Some(furl) = f.get("url").and_then(|v| v.as_str()) else {
continue;
};
if !furl.starts_with("http") || !seen.insert(furl.to_string()) {
continue;
}
let proto = f.get("protocol").and_then(|v| v.as_str()).unwrap_or("");
if proto.contains("m3u8")
|| f.get("vcodec").and_then(|v| v.as_str()) == Some("none")
&& f.get("acodec").and_then(|v| v.as_str()) != Some("none")
{
// Skip HLS + audio-only; Facebook's progressive mp4s are direct.
continue;
}
let ext: String = f
.get("ext")
.and_then(|v| v.as_str())
.unwrap_or("mp4")
.to_string();
result.media.push(MediaItem {
url: furl.to_string(),
quality: f
.get("height")
.and_then(|v| v.as_u64())
.map(|h| format!("{}p", h))
.or_else(|| {
f.get("format_note")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
}),
file_type: Some(if ext == "mp4" || ext == "webm" || ext == "mkv" {
MediaType::Video
} else {
MediaType::File
}),
extension: Some(ext),
thumbnail: result.thumbnail.clone(),
file_size: f
.get("filesize")
.and_then(|v| v.as_u64())
.map(format_filesize),
size_bytes: f.get("filesize").and_then(|v| v.as_u64()),
frame_width: f
.get("width")
.and_then(|v| v.as_u64())
.map(|w| w.to_string()),
frame_height: f
.get("height")
.and_then(|v| v.as_u64())
.map(|h| h.to_string()),
note: None,
});
}
}
// Fall back to the merged top-level URL (bestvideo+bestaudio sets it).
if result.media.is_empty() {
if let Some(dl_url) = data.get("url").and_then(|v| v.as_str()) {
if dl_url.starts_with("http") {
result.media.push(MediaItem {
url: dl_url.to_string(),
quality: None,
file_type: Some(MediaType::Video),
extension: data
.get("ext")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.or_else(|| Some("mp4".to_string())),
thumbnail: None,
file_size: data
.get("filesize")
.and_then(|v| v.as_u64())
.map(format_filesize),
size_bytes: data.get("filesize").and_then(|v| v.as_u64()),
frame_width: None,
frame_height: None,
note: None,
});
}
}
}
if result.media.is_empty() {
None
} else {
Some(result)
}
}
/// TikTok via embed-page scraping (primary method).
/// Scrapes `https://www.tiktok.com/embed/v2/{video_id}` HTML and extracts the
/// direct `v16m.tiktokcdn.com` MP4 URL from the `<video data-testid="play-video">` tag.
@@ -1402,7 +1574,11 @@ pub async fn fetch_youtube_mp3(url: &str) -> Result<DownloadResult, ScrapingErro
data.get("abr").and_then(|v| v.as_u64()).unwrap_or(128)
)),
file_type: Some(MediaType::Audio),
extension: Some("mp3".to_string()),
extension: data
.get("ext")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.or_else(|| Some("webm".to_string())),
thumbnail: data
.get("thumbnail")
.and_then(|v| v.as_str())
@@ -1426,6 +1602,110 @@ pub async fn fetch_youtube_mp4(url: &str, quality: &str) -> Result<DownloadResul
.ok_or_else(|| ScrapingError::Http("Invalid YouTube URL".to_string()))?;
let q = quality.trim_end_matches('p');
// ---------------------------------------------------------------
// 1) PREFER a PROGRESSIVE format (video + audio in ONE file).
// The `android` player client exposes progressive itags (18/22) that
// are NOT rate-limited from datacenter IPs (verified 2026-09-05:
// itag 18 downloads at full speed, while audio-only streams crawl).
// Fall back to adaptive (bestvideo+bestaudio) below when no
// progressive format exists at the requested height.
// ---------------------------------------------------------------
let prog_fmt = format!("best[height<={}][ext=mp4]/best[height<={}]", q, q);
if let Ok(prog_data) = run_ytdlp_json(
url,
&[
"--extractor-args",
"youtube:player_client=android",
"-f",
&prog_fmt,
],
)
.await
{
let prog_url = prog_data
.get("url")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let prog_vcodec = prog_data
.get("vcodec")
.and_then(|v| v.as_str())
.unwrap_or("none");
let prog_acodec = prog_data
.get("acodec")
.and_then(|v| v.as_str())
.unwrap_or("none");
let has_audio = prog_acodec != "none" && !prog_acodec.is_empty();
let has_video = prog_vcodec != "none" && !prog_vcodec.is_empty();
if let Some(purl) = prog_url {
if has_audio && has_video && purl.starts_with("http") {
let height = prog_data
.get("height")
.and_then(|v| v.as_u64())
.unwrap_or(360);
// Only use the progressive format if it actually meets the
// requested quality. `best[height<=Q][ext=mp4]` can return a
// lower-height progressive (e.g. 360p when 720p was requested)
// because no 720p progressive exists; in that case fall through
// to the adaptive (bestvideo+bestaudio) path so the caller
// still gets the requested resolution.
let q_num: u64 = q.parse().unwrap_or(0);
if q_num != 0 && height < q_num {
// fall through to adaptive below
} else {
let ext = prog_data
.get("ext")
.and_then(|v| v.as_str())
.unwrap_or("mp4");
let mut result = DownloadResult::success(
prog_data
.get("title")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
);
result.author = prog_data
.get("uploader")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
result.thumbnail = prog_data
.get("thumbnail")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
result.duration = prog_data
.get("duration")
.and_then(|v| v.as_u64())
.map(|d| format!("{}s", d));
result.provider = Some("yt-dlp".to_string());
result.media.push(MediaItem {
url: purl,
quality: Some(format!("{}p", height)),
file_type: Some(MediaType::Video),
extension: Some(ext.to_string()),
thumbnail: prog_data
.get("thumbnail")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
file_size: prog_data
.get("filesize")
.and_then(|v| v.as_u64())
.map(format_filesize),
size_bytes: prog_data.get("filesize").and_then(|v| v.as_u64()),
frame_width: prog_data
.get("width")
.and_then(|v| v.as_u64())
.map(|w| w.to_string()),
frame_height: prog_data
.get("height")
.and_then(|v| v.as_u64())
.map(|h| h.to_string()),
note: Some("Progressive MP4 — video and audio in one file".to_string()),
});
return Ok(result);
}
}
}
}
let fmt = format!(
"bestvideo[height<={}]+bestaudio/best[height<={}]/best",
q, q
@@ -1479,8 +1759,16 @@ pub async fn fetch_youtube_mp4(url: &str, quality: &str) -> Result<DownloadResul
continue;
}
if seen.insert(url_string.clone()) {
let is_video = f.get("vcodec").and_then(|v| v.as_str()).map(|c| c != "none").unwrap_or(false);
let is_audio_only = f.get("acodec").and_then(|v| v.as_str()).map(|c| c != "none").unwrap_or(false);
let is_video = f
.get("vcodec")
.and_then(|v| v.as_str())
.map(|c| c != "none")
.unwrap_or(false);
let is_audio_only = f
.get("acodec")
.and_then(|v| v.as_str())
.map(|c| c != "none")
.unwrap_or(false);
let fmt_type = if is_video && is_audio_only {
MediaType::Video
} else if is_audio_only {
@@ -1607,6 +1895,252 @@ pub async fn fetch_youtube_mp4(url: &str, quality: &str) -> Result<DownloadResul
Ok(result)
}
// ============================================================================
// YouTube video + audio SERVER-SIDE MERGE (produces a single MP4 with audio).
//
// Modern YouTube videos (AV1/VP9, 720p+) only expose separate video-only and
// audio-only streams. `fetch_youtube_mp4` returns those two URLs — the caller
// would have to merge them manually. This endpoint downloads both streams and
// remuxes them into ONE MP4 with `ffmpeg`, stored under the uploads dir and
// served via GET /file/yt_merge/{name}. Files older than 2h are cleaned up on
// each new merge request to bound disk usage.
// ============================================================================
pub async fn fetch_youtube_merge_free(
url: &str,
quality: &str,
) -> Result<DownloadResult, ScrapingError> {
let video_id = extract_youtube_id(url)
.ok_or_else(|| ScrapingError::Http("Invalid YouTube URL".to_string()))?;
let q = quality.trim_end_matches('p');
let fmt = format!(
"bestvideo[height<={}]+bestaudio/best[height<={}]/best",
q, q
);
let data = run_ytdlp_json(url, &["-f", &fmt, "--merge-output-format", "mp4"]).await?;
let title = data
.get("title")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let author = data
.get("uploader")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let thumbnail = data
.get("thumbnail")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let duration = data
.get("duration")
.and_then(|v| v.as_u64())
.map(|d| format!("{}s", d));
// Locate the video-only and audio-only URLs from requested_formats.
let requested_formats = data
.get("requested_formats")
.and_then(|v| v.as_array())
.ok_or_else(|| ScrapingError::Http("No requested_formats from yt-dlp".to_string()))?;
let mut video_url: Option<String> = None;
let mut audio_url: Option<String> = None;
let mut video_ext = "mp4";
let mut audio_ext = "webm";
for f in requested_formats {
let vcodec = f.get("vcodec").and_then(|v| v.as_str()).unwrap_or("none");
let acodec = f.get("acodec").and_then(|v| v.as_str()).unwrap_or("none");
let f_url = f.get("url").and_then(|v| v.as_str());
let proto = f.get("protocol").and_then(|v| v.as_str()).unwrap_or("");
if proto.contains("m3u8") {
continue; // HLS streams are not directly downloadable via reqwest
}
if vcodec != "none" && acodec == "none" {
if let Some(u) = f_url {
video_url = Some(u.to_string());
video_ext = f.get("ext").and_then(|v| v.as_str()).unwrap_or("mp4");
}
} else if vcodec == "none" && acodec != "none" {
if let Some(u) = f_url {
audio_url = Some(u.to_string());
audio_ext = f.get("ext").and_then(|v| v.as_str()).unwrap_or("webm");
}
}
}
let video_url = video_url
.ok_or_else(|| ScrapingError::Http("No video stream URL from yt-dlp".to_string()))?;
let audio_url = audio_url
.ok_or_else(|| ScrapingError::Http("No audio stream URL from yt-dlp".to_string()))?;
// --- download both streams to the merge temp dir ---
let merge_dir = PathBuf::from("/var/lib/scraper/uploads/yt_merge");
tokio::fs::create_dir_all(&merge_dir)
.await
.map_err(|e| ScrapingError::Http(format!("create merge dir failed: {e}")))?;
// Cleanup stale merged files (> 2h) before writing anything new.
cleanup_stale_merges(&merge_dir).await;
let job_id = uuid::Uuid::new_v4().simple().to_string();
let vpath = merge_dir.join(format!("{video_id}_{job_id}_v.{video_ext}"));
let apath = merge_dir.join(format!("{video_id}_{job_id}_a.{audio_ext}"));
let outpath = merge_dir.join(format!("{video_id}_{job_id}_merged.mp4"));
download_to_file(&video_url, &vpath).await?;
download_to_file(&audio_url, &apath).await?;
// --- merge with ffmpeg ---
let merge_result = tokio::task::spawn_blocking({
let vpath = vpath.clone();
let apath = apath.clone();
let outpath = outpath.clone();
move || merge_with_ffmpeg(&vpath, &apath, &outpath)
})
.await
.map_err(|e| ScrapingError::Http(format!("ffmpeg join failed: {e}")))?;
match merge_result {
Ok(()) => {}
Err(e) => {
let _ = tokio::fs::remove_file(&vpath).await;
let _ = tokio::fs::remove_file(&apath).await;
return Err(ScrapingError::Http(format!("ffmpeg merge failed: {e}")));
}
}
// Clean up the transient inputs now that the merged file exists.
let _ = tokio::fs::remove_file(&vpath).await;
let _ = tokio::fs::remove_file(&apath).await;
let file_size = tokio::fs::metadata(&outpath).await.ok().map(|m| m.len());
let file_name = outpath
.file_name()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_default();
let public_url = format!("/file/yt_merge/{file_name}");
let mut result = DownloadResult::success(title);
result.author = author;
result.thumbnail = thumbnail.clone();
result.duration = duration;
result.provider = Some("yt-dlp+ffmpeg".to_string());
result.media.push(MediaItem {
url: public_url,
quality: Some(format!("{}p merged", q)),
file_type: Some(MediaType::Video),
extension: Some("mp4".to_string()),
thumbnail: thumbnail.clone(),
file_size: file_size.map(format_filesize),
size_bytes: file_size,
frame_width: None,
frame_height: None,
note: Some(
"Merged server-side: single MP4 file containing both video and audio".to_string(),
),
});
Ok(result)
}
/// Download `url` to `path` with a generous timeout. The yt CDN stream URLs are
/// signed and short-lived, so we must download promptly and cannot retry after
/// the URL expires.
async fn download_to_file(url: &str, path: &Path) -> Result<(), ScrapingError> {
// Dedicated client with a longer timeout than the shared 30s one: merged
// files are often tens of MB and the yt CDN can be slow from a datacenter.
// .no_gzip()/.no_brotli()/.no_deflate() disable reqwest's auto-decompress
// so we can write raw bytes — some CDN responses are mis-labelled and fail
// the auto-decode step.
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(300))
.connect_timeout(Duration::from_secs(20))
.no_gzip()
.no_brotli()
.no_deflate()
.build()
.map_err(|e| ScrapingError::Http(format!("stream client build failed: {e}")))?;
let resp = 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("Accept-Encoding", "identity")
.send()
.await
.map_err(|e| ScrapingError::Http(format!("stream download failed: {e}")))?;
let status = resp.status();
if !status.is_success() {
return Err(ScrapingError::Http(format!(
"stream download HTTP {status}"
)));
}
let bytes = resp
.bytes()
.await
.map_err(|e| ScrapingError::Http(format!("stream body read failed: {e}")))?;
tokio::fs::write(path, bytes)
.await
.map_err(|e| ScrapingError::Http(format!("stream write failed: {e}")))?;
Ok(())
}
/// Run ffmpeg to remux the video and audio streams into a single MP4.
/// `-c:v copy` avoids a full re-encode (fast); audio is transcoded to AAC so
/// the output plays everywhere. `+faststart` moves the moov atom to the front
/// so the file streams immediately.
fn merge_with_ffmpeg(video: &Path, audio: &Path, out: &Path) -> Result<(), String> {
let status = Command::new("/bin/ffmpeg")
.args([
"-y",
"-v",
"error",
"-i",
video.to_str().unwrap_or_default(),
"-i",
audio.to_str().unwrap_or_default(),
"-c:v",
"copy",
"-c:a",
"aac",
"-movflags",
"+faststart",
"-shortest",
out.to_str().unwrap_or_default(),
])
.stdout(Stdio::null())
.stderr(Stdio::piped())
.status()
.map_err(|e| format!("spawn ffmpeg failed: {e}"))?;
if status.success() {
Ok(())
} else {
Err(format!("ffmpeg exited with {status:?}"))
}
}
/// Remove merged output + transient files older than 2 hours. Bounded, best
/// effort — never fails the request.
async fn cleanup_stale_merges(dir: &Path) {
let deadline = std::time::SystemTime::now() - std::time::Duration::from_secs(2 * 60 * 60);
let Ok(mut entries) = tokio::fs::read_dir(dir).await else {
return;
};
while let Ok(Some(entry)) = entries.next_entry().await {
let Ok(meta) = entry.metadata().await else {
continue;
};
let Ok(modified) = meta.modified() else {
continue;
};
if modified < deadline {
let _ = tokio::fs::remove_file(entry.path()).await;
}
}
}
// Spotify downloaders
// ============================================================================
@@ -2920,10 +3454,46 @@ pub async fn fetch_terabox(url: &str) -> Result<DownloadResult, ScrapingError> {
.and_then(|v| v.as_str())
.map(|s| s.to_string());
// Primary download link
// Primary download link.
//
// IMPORTANT: the resolver's download_link is a raw terabox dlink that
// 403s with {"error_code":31045,"user not exists"} when fetched WITHOUT
// cookies — even from residential IPs. So we rewrite the public URL to
// OUR OWN /proxy/terabox streaming endpoint (which forwards the resolver's
// cookie-authenticated stream through the public API). This is the only
// way users get a link that actually downloads.
// The public API base that users can reach. `CONFIG.urls.site_url` is the
// main site (hub → 4003) — NOT the scraper API domain. Use an explicit
// env override (TERABOX_PUBLIC_BASE_URL), falling back to the scraper's
// own public domain via site_url only when it's clearly the API host.
let public_base = std::env::var("TERABOX_PUBLIC_BASE_URL").unwrap_or_else(|_| {
let site = crate::config::CONFIG.urls.site_url.clone();
if site.contains("scraper") || site.contains("api") {
site
} else {
// default to the scraper API subdomain (this API's public host)
"https://api.asepharyana.my.id".to_string()
}
});
let proxy_dl = format!("{}/proxy/terabox?surl={}", public_base, surl);
if let Some(download_url) = data.get("download_link").and_then(|v| v.as_str()) {
let size_bytes = data.get("file_size").and_then(|v| v.as_u64()).unwrap_or(0);
// PRIMARY = the cookie-authenticated public proxy. The raw terabox
// dlink is only a secondary entry — it 403s without cookies, so it
// must NOT be the first link users see.
result.media.push(MediaItem {
url: proxy_dl.clone(),
quality: None,
file_type: Some(MediaType::File),
extension: None,
thumbnail: None,
file_size: Some(format_filesize(size_bytes)),
size_bytes: Some(size_bytes),
frame_width: None,
frame_height: None,
note: Some("Direct download via cookie-authenticated proxy (works).".into()),
});
result.media.push(MediaItem {
url: download_url.to_string(),
quality: None,
@@ -2934,7 +3504,7 @@ pub async fn fetch_terabox(url: &str) -> Result<DownloadResult, ScrapingError> {
size_bytes: Some(size_bytes),
frame_width: None,
frame_height: None,
note: None,
note: Some("Raw CDN link (may require a TeraBox session cookie).".into()),
});
// Directory: add child file links
+1
View File
@@ -66,6 +66,7 @@ use utoipa::OpenApi;
crate::presentation::handler::downloader::download_streamable,
crate::presentation::handler::downloader::download_videy,
crate::presentation::handler::downloader::download_bilibili,
crate::presentation::handler::downloader::serve_merged_file,
// ── Misc module ────────────────────────────────────────────
crate::presentation::handler::misc::currency_converter_handler,
crate::presentation::handler::misc::harga_emas_handler,
+187 -4
View File
@@ -21,6 +21,9 @@ pub struct DownloadParams {
pub cookies: Option<String>,
pub api_key: Option<String>,
pub quality: Option<String>,
/// When `true`, YouTube downloads are merged server-side (video + audio
/// into a single MP4) instead of returning separate video/audio URLs.
pub merge: Option<String>,
}
// ========================================================================
@@ -44,7 +47,25 @@ pub struct DownloadParams {
pub async fn download(
Query(params): Query<DownloadParams>,
) -> Result<Json<DownloadResponse>, AppError> {
let result = use_cases::download_all_in_one(&params.url, params.cookies.as_deref()).await?;
let merge = params
.merge
.as_deref()
.map(|v| v == "true" || v == "1")
.unwrap_or(false);
let result = if merge {
// All-in-one with merge=true → force merge for YouTube URLs.
if crate::application::downloader::detect_platform(&params.url) == "youtube" {
use_cases::download_youtube_merge(
&params.url,
params.quality.as_deref().unwrap_or("720"),
)
.await?
} else {
use_cases::download_all_in_one(&params.url, params.cookies.as_deref()).await?
}
} else {
use_cases::download_all_in_one(&params.url, params.cookies.as_deref()).await?
};
Ok(Json(DownloadResponse::ok(result)))
}
@@ -152,9 +173,17 @@ pub async fn download_tiktok(
pub async fn download_youtube(
Query(params): Query<DownloadParams>,
) -> Result<Json<DownloadResponse>, AppError> {
let result =
use_cases::download_youtube(&params.url, params.quality.as_deref().unwrap_or("720"))
.await?;
let quality = params.quality.as_deref().unwrap_or("720");
let merge = params
.merge
.as_deref()
.map(|v| v == "true" || v == "1")
.unwrap_or(false);
let result = if merge {
use_cases::download_youtube_merge(&params.url, quality).await?
} else {
use_cases::download_youtube(&params.url, quality).await?
};
Ok(Json(DownloadResponse::ok(result)))
}
@@ -284,6 +313,101 @@ pub async fn download_terabox(
Ok(Json(DownloadResponse::ok(result)))
}
/// Stream a TeraBox file through this API.
/// `/proxy/terabox?surl=...`
///
/// The resolver's `/download` endpoint replays the full 302 chain WITH
/// cookies and streams real bytes — this handler forwards those bytes to the
/// caller so the returned URL is a public, downloadable link (not a 403
/// dlink, not a private 127.0.0.1 URL).
#[utoipa::path(
get,
path = "/proxy/terabox",
tag = "download",
operation_id = "dl_proxy_terabox",
params(
("surl" = String, Query, description = "TeraBox share short URL ID"),
("filename" = Option<String>, Query, description = "Download file name hint"),
),
responses(
(status = 200, description = "File stream"),
(status = 400, description = "Missing surl"),
(status = 502, description = "Upstream error"),
)
)]
pub async fn proxy_terabox(
Query(params): Query<TeraboxProxyParams>,
) -> Result<axum::response::Response, AppError> {
let surl = params.surl;
if surl.is_empty() {
return Err(AppError::BadRequest("missing surl parameter".into()));
}
let resolver_url = std::env::var("TERABOX_RESOLVER_URL")
.unwrap_or_else(|_| "http://127.0.0.1:4092".to_string());
let fetch_url = format!("{}/download?surl={}", resolver_url, surl);
// Ask the resolver for the redirect chain (it follows 302s internally with
// cookies) — but we only want the final CDN stream URL. The resolver
// already streams; simplest reliable path is to just proxy the resolver's
// response body as-is.
let client = crate::infrastructure::utils::http_client::http_client();
let resp = client
.client()
.get(&fetch_url)
.header("User-Agent", "Mozilla/5.0")
.timeout(std::time::Duration::from_secs(120))
.send()
.await
.map_err(|e| AppError::ScraperError(format!("terabox proxy fetch failed: {e}")))?;
if !resp.status().is_success() {
let status = resp.status();
let body = resp.text().await.unwrap_or_default();
return Err(AppError::ScraperError(format!(
"terabox proxy returned HTTP {status}: {body}"
)));
}
let content_type = resp
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream")
.to_string();
let content_disposition = resp
.headers()
.get(reqwest::header::CONTENT_DISPOSITION)
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string())
.unwrap_or_else(|| {
let fname = params
.filename
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "terabox-download.bin".to_string());
format!("attachment; filename=\"{fname}\"")
});
let stream = resp.bytes_stream();
Ok(axum::response::Response::builder()
.header("Content-Type", content_type)
.header("Content-Disposition", content_disposition)
.header("X-Accel-Buffering", "no")
.header("Cache-Control", "no-store")
.body(axum::body::Body::from_stream(stream))
.map_err(|e| AppError::Internal(format!("build stream response: {e}")))?)
}
/// Params for the TeraBox streaming proxy.
#[derive(Debug, Deserialize, IntoParams)]
pub struct TeraboxProxyParams {
pub surl: String,
pub filename: Option<String>,
}
/// Download from Google Drive.
/// `/download/gdrive?url=...`
#[utoipa::path(
@@ -556,3 +680,62 @@ pub async fn download_bilibili(
let result = use_cases::download_bilibili(&params.url).await?;
Ok(Json(DownloadResponse::ok(result)))
}
// ========================================================================
// Merged-file static serving
// ========================================================================
/// Serve a merged YouTube MP4 produced by `/download/youtube?merge=true`.
/// Path is sanitised: only a plain filename (no `/`, `..`) inside the
/// `yt_merge` uploads dir is allowed.
///
/// `/file/yt_merge/{filename}`
#[utoipa::path(
get,
path = "/file/yt_merge/{filename}",
tag = "download",
operation_id = "dl_serve_merged_file",
params(
("filename" = String, Path, description = "Merged MP4 file name")
),
responses(
(status = 200, description = "Merged MP4 file", content_type = "video/mp4"),
(status = 404, description = "File not found or invalid name")
)
)]
pub async fn serve_merged_file(
axum::extract::Path(filename): axum::extract::Path<String>,
) -> Result<axum::response::Response, AppError> {
// Reject any path traversal or nested segments.
if filename.contains('/')
|| filename.contains('\\')
|| filename.contains("..")
|| filename.is_empty()
{
return Err(AppError::NotFound(format!("invalid file name: {filename}")));
}
let dir = std::path::PathBuf::from("/var/lib/scraper/uploads/yt_merge");
let path = dir.join(&filename);
let bytes = tokio::fs::read(&path)
.await
.map_err(|_| AppError::NotFound(format!("file not found: {filename}")))?;
let content_type = if filename.ends_with(".mp4") {
"video/mp4"
} else if filename.ends_with(".webm") {
"video/webm"
} else {
"application/octet-stream"
};
Ok(axum::response::Response::builder()
.header("Content-Type", content_type)
.header("Content-Length", bytes.len().to_string())
.header(
"Content-Disposition",
format!("attachment; filename=\"{filename}\""),
)
.body(axum::body::Body::from(bytes))
.map_err(|e| AppError::Internal(format!("build response: {e}")))?)
}
+8
View File
@@ -184,6 +184,10 @@ pub fn build_router(app_state: Arc<AppState>) -> anyhow::Result<Router> {
"/download/youtube/mp3",
axum::routing::get(crate::presentation::handler::downloader::download_youtube_mp3),
)
.route(
"/file/yt_merge/{filename}",
axum::routing::get(crate::presentation::handler::downloader::serve_merged_file),
)
.route(
"/download/spotify",
axum::routing::get(crate::presentation::handler::downloader::download_spotify),
@@ -204,6 +208,10 @@ pub fn build_router(app_state: Arc<AppState>) -> anyhow::Result<Router> {
"/download/terabox",
axum::routing::get(crate::presentation::handler::downloader::download_terabox),
)
.route(
"/proxy/terabox",
axum::routing::get(crate::presentation::handler::downloader::proxy_terabox),
)
.route(
"/download/gdrive",
axum::routing::get(crate::presentation::handler::downloader::download_gdrive),