| ... | @@ -0,0 +1,33 @@ |
| 1 | const std = @import("std"); |
| 2 | |
| 3 | const json = @import("json"); |
| 4 | const zfetch = @import("zfetch"); |
| 5 | |
| 6 | pub fn main() !void { |
| 7 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 8 | const alloc = &gpa.allocator; |
| 9 | |
| 10 | const url = "https://old.reddit.com/r/Zig/.json"; |
| 11 | |
| 12 | const req = try zfetch.Request.init(alloc, url, null); |
| 13 | defer req.deinit(); |
| 14 | |
| 15 | try req.do(.GET, null, null); |
| 16 | const r = req.reader(); |
| 17 | |
| 18 | const body_content = try r.readAllAlloc(alloc, std.math.maxInt(usize)); |
| 19 | const val = try json.parse(alloc, body_content); |
| 20 | |
| 21 | std.log.info("hot posts from r/Zig", .{}); |
| 22 | std.log.info("", .{}); |
| 23 | |
| 24 | var count: usize = 1; |
| 25 | for (val.fetch(.{ "data", "children" }).?.Array) |ch| { |
| 26 | const post = ch.get("data").?; |
| 27 | const title = post.get("title").?.String; |
| 28 | const author = post.get("author").?.String; |
| 29 | std.log.info("{}: {s} -- u/{s} ", .{ count, title, author }); |
| 30 | count += 1; |
| 31 | } |
| 32 | _ = std.math.lossyCast(i32, 2.4); |
| 33 | } |