harness: normalize twitter/x URLs before reporting download failure
Media-index variants ("/video/1?a=...") make the extractor mis-resolve
media ("Media #1 is not a video") while the bare status URL finds the
real video. On first failure the job now rebuilds https://host/status/<id>
and retries once before sending the error to the downloads room.
👾 Generated with [Letta Code](https://letta.com)
Co-Authored-By: Letta Code <noreply@letta.com>
This commit is contained in:
1 parent
51046627b1
commit
cc0a566978
1 file changed
+79
-19
+79
-19
@@ -447,6 +447,37 @@ fn resolveDownloadRoom(alloc: std.mem.Allocator, io: std.Io, token: []const u8,
|
||||
return alloc.dupe(u8, alias) catch null;
|
||||
}
|
||||
|
||||
/// Normalized bare status URL for twitter/x links: rebuilds
|
||||
/// "https://<host>/status/<id>" from "/status/..." paths, dropping /video/N
|
||||
/// suffixes and query params that confuse the extractor. Returns null for
|
||||
/// non-status URLs or when the URL is already bare.
|
||||
fn normalizeXUrl(alloc: std.mem.Allocator, url: []const u8) ?[]u8 {
|
||||
const scheme_end = std.mem.indexOf(u8, url, "://") orelse return null;
|
||||
const host_rel = url[scheme_end + 3 ..];
|
||||
const slash = std.mem.indexOfScalar(u8, host_rel, '/') orelse return null;
|
||||
const host = host_rel[0..slash];
|
||||
const path = host_rel[slash..];
|
||||
const known = std.mem.eql(u8, host, "twitter.com") or
|
||||
std.mem.eql(u8, host, "x.com") or
|
||||
std.mem.eql(u8, host, "www.twitter.com") or
|
||||
std.mem.eql(u8, host, "www.x.com") or
|
||||
std.mem.eql(u8, host, "mobile.twitter.com") or
|
||||
std.mem.eql(u8, host, "mobile.x.com");
|
||||
if (!known) return null;
|
||||
const marker = "/status/";
|
||||
const status_idx = std.mem.indexOf(u8, path, marker) orelse return null;
|
||||
const rest = path[status_idx + marker.len ..];
|
||||
var id_len: usize = 0;
|
||||
while (id_len < rest.len and std.ascii.isDigit(rest[id_len])) id_len += 1;
|
||||
if (id_len == 0) return null;
|
||||
const bare = std.fmt.allocPrint(alloc, "https://{s}/status/{s}", .{ host, rest[0..id_len] }) catch return null;
|
||||
if (std.mem.eql(u8, bare, url)) {
|
||||
alloc.free(bare);
|
||||
return null;
|
||||
}
|
||||
return bare;
|
||||
}
|
||||
|
||||
/// One download request, run on its own thread. Owns everything it touches.
|
||||
const DownloadJob = struct {
|
||||
arena_state: std.heap.ArenaAllocator,
|
||||
@@ -480,32 +511,61 @@ const DownloadJob = struct {
|
||||
const io = self.io;
|
||||
const url = firstUrl(alloc, self.body) orelse return error.NoUrl;
|
||||
defer alloc.free(url);
|
||||
std.debug.print("matrix_harness: downloading {s}\n", .{url});
|
||||
|
||||
std.Io.Dir.cwd().createDirPath(io, DOWNLOAD_DIR) catch {};
|
||||
|
||||
const out_tmpl = try std.fmt.allocPrint(alloc, "{s}/%(id)s.%(ext)s", .{DOWNLOAD_DIR});
|
||||
defer alloc.free(out_tmpl);
|
||||
const result = std.process.run(alloc, io, .{
|
||||
.argv = &.{ "yt-dlp", "--no-playlist", "--max-filesize", "2G", "-o", out_tmpl, url },
|
||||
.stderr_limit = .limited(8 * 1024),
|
||||
}) catch |e| {
|
||||
std.debug.print("matrix_harness: yt-dlp spawn failed: {s}\n", .{@errorName(e)});
|
||||
const msg = try std.fmt.allocPrint(alloc, "yt-dlp failed to start: {s}", .{@errorName(e)});
|
||||
defer alloc.free(msg);
|
||||
if (matrix.sendText(alloc, io, self.token, self.room, msg)) |ev| alloc.free(ev) else |_| {}
|
||||
return;
|
||||
};
|
||||
defer alloc.free(result.stdout);
|
||||
defer alloc.free(result.stderr);
|
||||
const ok = switch (result.term) {
|
||||
.exited => |code| code == 0,
|
||||
else => false,
|
||||
};
|
||||
if (!ok) {
|
||||
|
||||
// Twitter/x media-index variants ("/video/1?a=...") make the extractor
|
||||
// mis-resolve media ("Media #1 is not a video") while the bare status
|
||||
// URL finds the real video — retry normalized on failure.
|
||||
// Candidate loop state.
|
||||
var retried = false;
|
||||
var attempt_url = url;
|
||||
var last_stderr: ?[]u8 = null;
|
||||
defer if (last_stderr) |s| alloc.free(s);
|
||||
var download_ok = false;
|
||||
while (true) {
|
||||
std.debug.print("matrix_harness: downloading {s}\n", .{attempt_url});
|
||||
const result = std.process.run(alloc, io, .{
|
||||
.argv = &.{ "yt-dlp", "--no-playlist", "--max-filesize", "2G", "-o", out_tmpl, attempt_url },
|
||||
.stderr_limit = .limited(8 * 1024),
|
||||
}) catch |e| {
|
||||
std.debug.print("matrix_harness: yt-dlp spawn failed: {s}\n", .{@errorName(e)});
|
||||
const msg = try std.fmt.allocPrint(alloc, "yt-dlp failed to start: {s}", .{@errorName(e)});
|
||||
defer alloc.free(msg);
|
||||
if (matrix.sendText(alloc, io, self.token, self.room, msg)) |ev| alloc.free(ev) else |_| {}
|
||||
return;
|
||||
};
|
||||
defer alloc.free(result.stdout);
|
||||
defer alloc.free(result.stderr);
|
||||
const ok = switch (result.term) {
|
||||
.exited => |code| code == 0,
|
||||
else => false,
|
||||
};
|
||||
if (ok) {
|
||||
download_ok = true;
|
||||
break;
|
||||
}
|
||||
// Remember the failure text in case the retry also fails.
|
||||
if (last_stderr) |s| alloc.free(s);
|
||||
last_stderr = alloc.dupe(u8, result.stderr) catch null;
|
||||
if (!retried) {
|
||||
if (normalizeXUrl(alloc, attempt_url)) |bare| {
|
||||
std.debug.print("matrix_harness: retrying normalized {s}\n", .{bare});
|
||||
attempt_url = bare;
|
||||
retried = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!download_ok) {
|
||||
const stderr = last_stderr orelse "";
|
||||
// Pull the last meaningful line from yt-dlp's stderr for the reply.
|
||||
var msg_src: []const u8 = "yt-dlp failed";
|
||||
var lines = std.mem.splitBackwardsScalar(u8, result.stderr, '\n');
|
||||
var lines = std.mem.splitBackwardsScalar(u8, stderr, '\n');
|
||||
while (lines.next()) |line| {
|
||||
const t = std.mem.trim(u8, line, " \r");
|
||||
if (t.len > 0 and !std.mem.startsWith(u8, t, "[download]")) {
|
||||
|
||||
Reference in new issue
Block a user