Finally
This commit is contained in:
parent
3f97fdcdcf
commit
3be630baaa
3 changed files with 31 additions and 13 deletions
31
src/json.zig
31
src/json.zig
|
@ -1,37 +1,50 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const dbg_print = std.debug.print;
|
const dbg_print = std.debug.print;
|
||||||
|
const eql = std.mem.eql;
|
||||||
|
|
||||||
|
|
||||||
pub fn decode_song_data(allocator: std.mem.Allocator, json_string: []const u8) !void {
|
pub const SongData = struct {
|
||||||
const parsed = try std.json.parseFromSlice(
|
track_url: []const u8,
|
||||||
std.json.Value,
|
track_title: []const u8,
|
||||||
allocator,
|
};
|
||||||
json_string,
|
|
||||||
.{},
|
pub fn decode_song_data(allocator: std.mem.Allocator, json_string: []const u8) !SongData {
|
||||||
);
|
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, json_string, .{});
|
||||||
defer parsed.deinit();
|
defer parsed.deinit();
|
||||||
|
|
||||||
const json_data = parsed.value;
|
const json_data = parsed.value;
|
||||||
|
var track_url: ?[]const u8 = null;
|
||||||
|
var track_title: ?[]const u8 = null;
|
||||||
|
|
||||||
if (json_data == .array) {
|
if (json_data == .array) {
|
||||||
for (json_data.array.items) |item| {
|
for (json_data.array.items) |item| {
|
||||||
if (item == .object) {
|
if (item == .object) {
|
||||||
if (item.object.get("OriginalTrackUrl")) |url_value| {
|
if (item.object.get("OriginalTrackUrl")) |url_value| {
|
||||||
if (url_value == .string) {
|
if (url_value == .string) {
|
||||||
dbg_print("OriginalTrackUrl: {s}\n", .{url_value.string});
|
track_url = try allocator.dupe(u8, url_value.string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (item.object.get("title")) |title_value| {
|
if (item.object.get("title")) |title_value| {
|
||||||
if (title_value == .string) {
|
if (title_value == .string) {
|
||||||
dbg_print("Title: {s}\n", .{title_value.string});
|
track_title = try allocator.dupe(u8, title_value.string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (track_url) |url| {
|
||||||
|
if (track_title) |title| {
|
||||||
|
return SongData{
|
||||||
|
.track_url = url,
|
||||||
|
.track_title = title,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return error.Unexpected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn decode_album_data() void {
|
pub fn decode_album_data() void {
|
||||||
unreachable;
|
unreachable;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,11 @@ pub fn main() !void {
|
||||||
const data = try url.fetch(allocator, "https://tidal.401658.xyz/track/?id=286266927&quality=LOSSLESS");
|
const data = try url.fetch(allocator, "https://tidal.401658.xyz/track/?id=286266927&quality=LOSSLESS");
|
||||||
defer allocator.free(data);
|
defer allocator.free(data);
|
||||||
|
|
||||||
try json.decode_song_data(allocator, data);
|
const songData = try json.decode_song_data(allocator, data);
|
||||||
|
|
||||||
try url.download(allocator, "https://sp-pr-cf.audio.tidal.com/mediatracks/CAEaKwgDEidkYjY2ZWFkZTEwYmRmYjYyZTRiYjRiZmViNmU1YzNkM182MS5tcDQ/0.flac?Expires=1743711930&Signature=PRw1k4gM7m015MdIRZ81Xqn7mvzVBz1I9hpuYrT0lPu6DRvWQpAt2xxMvPCzEWwoFtH7CacZCjY7aeXbHanq6r68SHVkidkC0c1yZu~3Wk5BUri54Vh9sHr9Zcs6OmrtaaJv9XYRqeY9rDOwYykc47MrO6-cTaeW-Fp3UMaw5P243qPd3FZHYMYiKkaQtR791CgAmJ79ihzs8X0sKQrRKOH2jZ1Yy48qAIcyc5oyoKaN7owEPb-EbvS5h~vKXijK8jyb~dwzRZ0~uN3v3yREyBvrFL4~csnawkVL03mFlmPcRVnru96cVBm195j79OUrbYI64BmoC~4gkgSCR35zpQ__&Key-Pair-Id=K14LZCZ9QUI4JL");
|
defer allocator.free(songData.track_url);
|
||||||
|
defer allocator.free(songData.track_title);
|
||||||
|
|
||||||
|
try url.download(allocator, songData.track_url, songData.track_title);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,14 +33,16 @@ pub fn fetch(allocator: std.mem.Allocator, url: []const u8) ![]u8 {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn download(allocator: std.mem.Allocator, url: []const u8) !void {
|
pub fn download(allocator: std.mem.Allocator, url: []const u8, name: []const u8) !void {
|
||||||
const data = try fetch(allocator, url);
|
const data = try fetch(allocator, url);
|
||||||
defer allocator.free(data);
|
defer allocator.free(data);
|
||||||
|
|
||||||
//dbg_print("Success", .{});
|
//dbg_print("Success", .{});
|
||||||
|
const file_name = try std.fmt.allocPrint(allocator, "{s}.flac", .{name});
|
||||||
|
defer allocator.free(file_name);
|
||||||
|
|
||||||
const file = try std.fs.cwd().createFile(
|
const file = try std.fs.cwd().createFile(
|
||||||
"test.flac",
|
file_name,
|
||||||
.{ .read = true },
|
.{ .read = true },
|
||||||
);
|
);
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue