-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #374 from Fooxboy/68-last-fm
#68 last fm scrobbling
- Loading branch information
Showing
18 changed files
with
1,424 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "lastfm"] | ||
path = lastfm | ||
url = https://github.com/tolbxela/lastfm.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using IF.Lastfm.Core.Api; | ||
using IF.Lastfm.Core.Objects; | ||
using IF.Lastfm.Core.Scrobblers; | ||
using MusicX.Models.Enums; | ||
using MusicX.Shared.Player; | ||
|
||
namespace MusicX.Services.Player.TrackStats; | ||
|
||
public class LastFmStats : ITrackStatsListener | ||
{ | ||
private readonly IScrobbler _scrobbler; | ||
private readonly ITrackApi _trackApi; | ||
private readonly ConfigService _configService; | ||
|
||
public LastFmStats(IScrobbler scrobbler, ITrackApi trackApi, ConfigService configService) | ||
{ | ||
_scrobbler = scrobbler; | ||
_trackApi = trackApi; | ||
_configService = configService; | ||
} | ||
|
||
public async Task TrackChangedAsync(PlaylistTrack? previousTrack, PlaylistTrack newTrack, ChangeReason reason, TimeSpan? position = null) | ||
{ | ||
if (_configService.Config.LastFmSession is null || _configService.Config.SendLastFmScrobbles is not true) | ||
return; | ||
|
||
if (previousTrack is not null && previousTrack.Data.Duration > TimeSpan.FromSeconds(30) && | ||
position.HasValue && (position.Value > TimeSpan.FromMinutes(4) || position.Value > previousTrack.Data.Duration / 2)) | ||
await _scrobbler.ScrobbleAsync(new Scrobble(previousTrack.MainArtists.First().Name, | ||
previousTrack.AlbumId?.Name, previousTrack.Title, DateTimeOffset.Now - position.Value) | ||
{ | ||
Duration = previousTrack.Data.Duration | ||
}); | ||
|
||
await _trackApi.UpdateNowPlayingAsync(new Scrobble(newTrack.MainArtists.First().Name, newTrack.AlbumId?.Name, | ||
newTrack.Title, DateTimeOffset.Now) | ||
{ | ||
Duration = newTrack.Data.Duration | ||
}); | ||
} | ||
|
||
public Task TrackPlayStateChangedAsync(PlaylistTrack track, TimeSpan position, bool paused) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using System.Windows.Input; | ||
using AsyncAwaitBestPractices.MVVM; | ||
using IF.Lastfm.Core.Api; | ||
using IF.Lastfm.Core.Api.Helpers; | ||
using MusicX.Services; | ||
using Wpf.Ui; | ||
using Wpf.Ui.Controls; | ||
using Wpf.Ui.Extensions; | ||
using NavigationService = MusicX.Services.NavigationService; | ||
|
||
namespace MusicX.ViewModels.Modals; | ||
|
||
public class LastFmAuthModalViewModel : BaseViewModel | ||
{ | ||
private readonly ILastAuth _auth; | ||
private readonly NavigationService _navigationService; | ||
private readonly ISnackbarService _snackbarService; | ||
private readonly ConfigService _configService; | ||
private string? _token; | ||
|
||
public ICommand ConfirmCommand { get; } | ||
|
||
public LastFmAuthModalViewModel(ILastAuth auth, NavigationService navigationService, ISnackbarService snackbarService, ConfigService configService) | ||
{ | ||
_auth = auth; | ||
_navigationService = navigationService; | ||
_snackbarService = snackbarService; | ||
_configService = configService; | ||
|
||
ConfirmCommand = new AsyncCommand(ConfirmAsync); | ||
} | ||
|
||
private async Task ConfirmAsync() | ||
{ | ||
if (_token is null) | ||
return; | ||
|
||
var response = await _auth.GetSessionTokenAsync(_token); | ||
|
||
if (!response.Success) | ||
{ | ||
_snackbarService.Show("Ошибка авторизации", $"Сервис вернул: {response.Status}", ControlAppearance.Danger); | ||
return; | ||
} | ||
|
||
_navigationService.CloseModal(); | ||
|
||
_configService.Config.LastFmSession = _auth.UserSession; | ||
await _configService.SetConfig(_configService.Config); | ||
} | ||
|
||
public async Task OpenAuthPageAsync() | ||
{ | ||
_token = ((LastResponse<string>)await _auth.GetAuthTokenAsync()).Content; | ||
|
||
Process.Start(new ProcessStartInfo | ||
{ | ||
FileName = $"https://last.fm/api/auth/?api_key={_auth.ApiKey}&token={_token}", | ||
UseShellExecute = true | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Page x:Class="MusicX.Views.Modals.LastFmAuthModal" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="clr-namespace:MusicX.Views.Modals" | ||
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" | ||
xmlns:modals="clr-namespace:MusicX.ViewModels.Modals" | ||
mc:Ignorable="d" | ||
Title="Last.Fm" Height="300" Width="200" | ||
d:DataContext="{d:DesignInstance modals:LastFmAuthModalViewModel}" | ||
Loaded="LastFmAuthModal_OnLoaded"> | ||
<Grid VerticalAlignment="Center"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="*" /> | ||
<RowDefinition Height="Auto" /> | ||
</Grid.RowDefinitions> | ||
<ui:Card> | ||
<ui:SymbolIcon Symbol="Globe24" | ||
FontSize="48"/> | ||
<ui:Card.Footer> | ||
<TextBlock TextWrapping="Wrap" TextAlignment="Center" | ||
FontWeight="SemiBold">Подтвердите вход в аккаунт в браузере</TextBlock> | ||
</ui:Card.Footer> | ||
</ui:Card> | ||
|
||
<ui:Button Grid.Row="1" HorizontalAlignment="Center" Margin="0,15" | ||
Command="{Binding ConfirmCommand}"> | ||
<ui:Button.Icon> | ||
<ui:SymbolIcon Symbol="Checkmark24" /> | ||
</ui:Button.Icon> | ||
Готово | ||
</ui:Button> | ||
</Grid> | ||
</Page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using MusicX.ViewModels.Modals; | ||
|
||
namespace MusicX.Views.Modals; | ||
|
||
public partial class LastFmAuthModal : Page | ||
{ | ||
public LastFmAuthModal() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
private async void LastFmAuthModal_OnLoaded(object sender, RoutedEventArgs e) | ||
{ | ||
if (DataContext is LastFmAuthModalViewModel viewModel) | ||
await viewModel.OpenAuthPageAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.