Fix LastFM uploadig error

I figured it out; if you have a song currently being played, then it doesn't have a date
This commit is contained in:
mitteneer 2025-05-16 01:05:32 -04:00
parent 5697f95355
commit 614607ae71
2 changed files with 25 additions and 23 deletions

View file

@ -147,7 +147,7 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
2 => { 2 => {
if (params.getT(.string, "username")) |username| { if (params.getT(.string, "username")) |username| {
_ = username; _ = username;
const query: []const u8 = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=VAOTM&api_key=b0c410a48a6078a651e0832699e3cd41&limit=200&format=json"; const query: []const u8 = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=VAOTM&api_key=b0c410a48a6078a651e0832699e3cd41&limit=1000&format=json";
const user_agent: []const u8 = "Zuletzt/0.0.1"; const user_agent: []const u8 = "Zuletzt/0.0.1";
var client = Client{ .allocator = request.allocator }; var client = Client{ .allocator = request.allocator };
var ar = std.ArrayList(u8).init(request.allocator); var ar = std.ArrayList(u8).init(request.allocator);
@ -155,12 +155,13 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
const first_response = try ar.toOwnedSlice(); const first_response = try ar.toOwnedSlice();
const json = try std.json.parseFromSliceLeaky(Data.LastFMWeb, request.allocator, first_response, .{ .ignore_unknown_fields = true }); const json = try std.json.parseFromSliceLeaky(Data.LastFMWeb, request.allocator, first_response, .{ .ignore_unknown_fields = true });
for (json.recenttracks.track) |scrobble| { appends: for (json.recenttracks.track) |scrobble| {
if (scrobble.date == null) continue :appends;
const pre_formatted_scrobble = Data.ImportedScrobble{ const pre_formatted_scrobble = Data.ImportedScrobble{
.track = scrobble.name, .track = scrobble.name,
.album = if (scrobble.album) |album| album.@"#text".? else "", .album = if (scrobble.album) |album| album.@"#text" else "Not Provided",
.artist = scrobble.artist.@"#text".?, .artist = scrobble.artist.@"#text",
.date = try std.fmt.parseInt(i64, scrobble.date.uts, 10), .date = try std.fmt.parseInt(i64, scrobble.date.?.uts, 10),
}; };
const formatted_scrobble = if (rule_list) |rl| const formatted_scrobble = if (rule_list) |rl|
@ -195,18 +196,19 @@ pub fn post(request: *jetzig.Request) !jetzig.View {
} }
const max_pages = (try std.fmt.parseInt(usize, json.recenttracks.@"@attr".totalPages, 10)) + 1; const max_pages = (try std.fmt.parseInt(usize, json.recenttracks.@"@attr".totalPages, 10)) + 1;
for (2..max_pages) |page| { for (2..max_pages) |page| {
const rest_query: []const u8 = try std.fmt.allocPrint(request.allocator, "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=VAOTM&api_key=b0c410a48a6078a651e0832699e3cd41&limit=200&page={}&format=json", .{page}); const rest_query: []const u8 = try std.fmt.allocPrint(request.allocator, "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=VAOTM&api_key=b0c410a48a6078a651e0832699e3cd41&limit=1000&page={}&format=json", .{page});
std.log.debug("{s}", .{rest_query}); std.log.debug("{s}", .{rest_query});
_ = try client.fetch(.{ .response_storage = .{ .dynamic = &ar }, .location = .{ .url = rest_query }, .method = .GET, .headers = .{ .user_agent = .{ .override = user_agent } } }); _ = try client.fetch(.{ .response_storage = .{ .dynamic = &ar }, .location = .{ .url = rest_query }, .method = .GET, .headers = .{ .user_agent = .{ .override = user_agent } } });
const response = try ar.toOwnedSlice(); const response = try ar.toOwnedSlice();
const json2 = try std.json.parseFromSliceLeaky(Data.LastFMWeb, request.allocator, response, .{ .ignore_unknown_fields = true }); const json2 = try std.json.parseFromSliceLeaky(Data.LastFMWeb, request.allocator, response, .{ .ignore_unknown_fields = true });
for (json2.recenttracks.track) |scrobble| { appends: for (json2.recenttracks.track) |scrobble| {
if (scrobble.date == null) continue :appends;
const pre_formatted_scrobble = Data.ImportedScrobble{ const pre_formatted_scrobble = Data.ImportedScrobble{
.track = scrobble.name, .track = scrobble.name,
.album = if (scrobble.album) |album| album.@"#text".? else "", .album = if (scrobble.album) |album| album.@"#text" else "Not Provided",
.artist = scrobble.artist.@"#text".?, .artist = scrobble.artist.@"#text",
.date = try std.fmt.parseInt(i64, scrobble.date.uts, 10), .date = try std.fmt.parseInt(i64, scrobble.date.?.uts, 10),
}; };
const formatted_scrobble = if (rule_list) |rl| const formatted_scrobble = if (rule_list) |rl|

View file

@ -50,34 +50,34 @@ pub const LastFMWeb = struct {
album: ?LastFMWebHyperlinkData = null, album: ?LastFMWebHyperlinkData = null,
name: []const u8, name: []const u8,
mbid: ?[]const u8 = null, mbid: ?[]const u8 = null,
image: ?[]struct { image: []struct {
size: []const u8, size: []const u8,
@"#text": []const u8, @"#text": []const u8,
} = null, },
date: struct { date: ?struct {
uts: []const u8, uts: []const u8,
@"#text": []const u8, @"#text": []const u8,
},
@"@attr": ?struct {
nowplaying: ?[]const u8 = null,
} = null, } = null,
url: ?[]const u8 = null, @"@attr": ?struct {
nowplaying: []const u8,
} = null,
url: []const u8,
}, },
@"@attr": LastFMWebAttr, @"@attr": LastFMWebAttr,
}, },
}; };
pub const LastFMWebAttr = struct { pub const LastFMWebAttr = struct {
perPage: ?[]const u8 = null, perPage: []const u8,
totalPages: []const u8, totalPages: []const u8,
page: ?[]const u8 = null, page: []const u8,
user: ?[]const u8 = null, user: []const u8,
total: ?[]const u8 = null, total: []const u8,
}; };
pub const LastFMWebHyperlinkData = struct { pub const LastFMWebHyperlinkData = struct {
mbid: ?[]const u8 = null, mbid: []const u8,
@"#text": ?[]const u8 = null, @"#text": []const u8,
}; };
pub const Rule = struct { pub const Rule = struct {