Added files

This commit is contained in:
Medvidek77 2025-03-31 22:55:41 +02:00
parent 00518f628b
commit c20536084b
6 changed files with 196 additions and 2 deletions

32
src/json.zig Normal file
View file

@ -0,0 +1,32 @@
const std = @import("std");
pub fn extract(allocator: std.mem.Allocator, json_string: []const u8) !void {
const parsed = try std.json.parseFromSlice(
std.json.Value,
allocator,
json_string,
.{},
);
defer parsed.deinit();
const json_data = parsed.value;
if (json_data == .array) {
for (json_data.array.items) |item| {
if (item == .object) {
if (item.object.get("OriginalTrackUrl")) |url_value| {
if (url_value == .string) {
std.debug.print("OriginalTrackUrl: {s}\n", .{url_value.string});
}
}
if (item.object.get("title")) |title_value| {
if (title_value == .string) {
std.debug.print("Title: {s}\n", .{title_value.string});
}
}
}
}
}
}

24
src/main.zig Normal file
View file

@ -0,0 +1,24 @@
const std = @import("std");
const url = @import("url.zig");
const json = @import("json.zig");
const dbg_print = std.debug.print;
pub fn main() !void {
// gpa allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
while (args.next()) |arg| {
dbg_print("{s}", .{arg});
}
const data = try url.fetch(allocator, "https://tidal.401658.xyz/track/?id=286266927&quality=LOSSLESS");
defer allocator.free(data);
try json.extract(allocator, data);
}

29
src/url.zig Normal file
View file

@ -0,0 +1,29 @@
const std = @import("std");
const dbg_print = std.debug.print;
pub fn fetch(allocator: std.mem.Allocator, url: []const u8) ![]u8 {
const uri = try std.Uri.parse(url);
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();
const server_header_buffer: []u8 = try allocator.alloc(u8, 1024 * 8);
defer allocator.free(server_header_buffer);
var req = try client.open(.GET, uri, .{
.server_header_buffer = server_header_buffer,
});
defer req.deinit();
try req.send();
try req.finish();
try req.wait();
//dbg_print("Response status: {d}\n", .{req.response.status});
const body = try req.reader().readAllAlloc(allocator, 1024 * 64);
return body;
}