Add playback logging support
This commit is contained in:
parent
7a9d910d86
commit
840cc49865
|
@ -1,8 +1,11 @@
|
|||
@page "/np"
|
||||
@using zotanpw_web.database
|
||||
@model NowPlayingModel
|
||||
|
||||
@{
|
||||
ViewData["title"] = "now playing";
|
||||
var db = new Database.DbConn();
|
||||
var albums = db.AlbumHistory.OrderByDescending(p => p.EntryId).Take(10);
|
||||
}
|
||||
|
||||
<p>Here you can see what kind of music I've been listening to lately. This table is updated every couple minutes from multiple data sources.</p>
|
||||
|
@ -12,22 +15,26 @@
|
|||
</ul>
|
||||
<h3>>> Albums</h3>
|
||||
<table>
|
||||
<th>Date</th>
|
||||
<th>Artist</th>
|
||||
<th>Album</th>
|
||||
<th>Link</th>
|
||||
@foreach (var album in albums) {
|
||||
<tr>
|
||||
<td>@album.DateTime.ToString("yyyy-MM-dd")</td>
|
||||
<td>@album.Artist</td>
|
||||
<td>@album.Title</td>
|
||||
<td>
|
||||
<a href="@album.Link" target="_blank">@album.Source</a>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
<tr>
|
||||
<td>Röyksopp</td>
|
||||
<td>Profound Mysteries</td>
|
||||
<td><a href="https://music.zotan.services/?/R%C3%B6yksopp/Profound%20Mysteries">music.zotan.services</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2022-11-04</td>
|
||||
<td>t+pazolite</td>
|
||||
<td>Refactoring Travel</td>
|
||||
<td><a href="https://music.apple.com/gb/album/%E3%83%AA%E3%83%95%E3%82%A1%E3%82%AF%E3%82%BF%E3%83%AA%E3%83%B3%E3%82%B0-%E3%83%88%E3%83%A9%E3%83%99%E3%83%AB/1494424249">Apple Music</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>test</td>
|
||||
<td>test</td>
|
||||
<td>test</td>
|
||||
<td>
|
||||
<a href="https://music.apple.com/gb/album/%E3%83%AA%E3%83%95%E3%82%A1%E3%82%AF%E3%82%BF%E3%83%AA%E3%83%B3%E3%82%B0-%E3%83%88%E3%83%A9%E3%83%99%E3%83%AB/1494424249">Apple Music</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
|
40
zotan.pw-web/PlaybackHistory/LogPlayback.cs
Normal file
40
zotan.pw-web/PlaybackHistory/LogPlayback.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using LinqToDB;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using zotanpw_web.database;
|
||||
using zotanpw_web.database.Tables;
|
||||
|
||||
namespace zotanpw_web.PlaybackHistory;
|
||||
|
||||
[ApiController]
|
||||
[Route("/np/log")]
|
||||
public class LogPlayback : Controller {
|
||||
private static readonly string Token = System.IO.File.ReadAllLines(".bearer_token")[0];
|
||||
|
||||
[HttpPost]
|
||||
public AlbumHistoryEntry Log([FromBody] LogPlaybackRequest rq) {
|
||||
var token = Request.Headers.Authorization;
|
||||
if (token == Token) {
|
||||
var db = new Database.DbConn();
|
||||
if (!db.AlbumHistory.Any(p => p.Title == rq.Title && p.Artist == rq.Artist && p.DateTime.Between(DateTime.Now - TimeSpan.FromHours(8), DateTime.Now))) {
|
||||
if (!db.AlbumHistory.Any() || !(db.AlbumHistory.OrderByDescending(p => p.EntryId).First().Artist == rq.Artist && db.AlbumHistory.OrderByDescending(p => p.EntryId).First().Title == rq.Title)) {
|
||||
var entry = new AlbumHistoryEntry {
|
||||
Artist = rq.Artist ?? throw new InvalidOperationException(),
|
||||
Title = rq.Title ?? throw new InvalidOperationException(),
|
||||
Link = rq.Link ?? throw new InvalidOperationException(),
|
||||
Source = rq.Source ?? throw new InvalidOperationException(),
|
||||
DateTime = DateTime.Now
|
||||
};
|
||||
db.InsertWithIdentity(entry);
|
||||
Response.StatusCode = 201;
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
Response.StatusCode = 202;
|
||||
return null!;
|
||||
}
|
||||
|
||||
Response.StatusCode = 403;
|
||||
return null!;
|
||||
}
|
||||
}
|
8
zotan.pw-web/PlaybackHistory/LogPlaybackRequest.cs
Normal file
8
zotan.pw-web/PlaybackHistory/LogPlaybackRequest.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
namespace zotanpw_web.PlaybackHistory;
|
||||
|
||||
public class LogPlaybackRequest {
|
||||
public string? Artist { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? Source { get; set; }
|
||||
public string? Link { get; set; }
|
||||
}
|
Loading…
Reference in a new issue