Skip to content

Commit

Permalink
Track CurrentWorld in SharedViewModel, and use tracked value in addin…
Browse files Browse the repository at this point in the history
…g tracks to Spotify playlist.
  • Loading branch information
Soapwood committed Apr 22, 2024
1 parent ebbe146 commit dd09721
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 58 deletions.
57 changes: 1 addition & 56 deletions VXMusic/API/VXMusicAPI.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
using System.Diagnostics;
using IF.Lastfm.Core.Scrobblers;
using SpotifyAPI.Web;
using VXMusic.Audio.Recording;
using VXMusic.Lastfm;
using SpotifyAPI.Web;
using VXMusic.Lastfm.Authentication;
using VXMusic.Lastfm.Scrobbling;
using VXMusic.Recognition.AudD;
using VXMusic.Recognition.Shazam;
using VXMusic.Spotify;
using VXMusic.Spotify.Authentication;

namespace VXMusic.API
{
Expand All @@ -34,52 +26,5 @@ public async static Task<bool> LinkLastfm(string clientId, string clientSecret,
//var last = await LastfmClientBuilder.CreateLastfmClient();
return await LastfmAuthentication.Login(username, password);
}

public async static void ReportTrackToSpotifyPlaylist(IRecognitionApiClientResponse result)
{
var spotify = await SpotifyClientBuilder.Instance;

var me = await spotify.UserProfile.Current();
Trace.WriteLine($"Welcome {me.DisplayName} ({me.Id}), you're authenticated!");

// get track name from output

var searchRequest = new SearchRequest(SearchRequest.Types.Track, $"{result.Result.Artist} {result.Result.Title}");
var searchResult = spotify.Search.Item(searchRequest);
var uri = searchResult.Result.Tracks.Items[0].Uri;

PlaylistAddItemsRequest playlistAddItemsRequest = new PlaylistAddItemsRequest(new List<string>()
{
//result.result.song_link
uri
});

var playlists = await spotify.PaginateAll(await spotify.Playlists.CurrentUsers().ConfigureAwait(false));

Trace.WriteLine($"Total Playlists in your Account: {playlists.Count}");

// Prefix Playlist name with dd/MM date
var currentDate = DateTime.Now.ToString("dd/MM");
var playlistName = $"{currentDate}";

// TODO Only run this if VRChat is running
var lastKnownLocationName = LogParser.VRChat.VRChatLogParser.LastKnownLocationName;

if (lastKnownLocationName != null)
playlistName += " - " + lastKnownLocationName;

var existingPlaylist = SpotifyPlaylistManager.GetPlaylistIdByNameIfExists(playlistName, playlists);

if (existingPlaylist == null)
{
var response = await SpotifyPlaylistManager.CreatePlaylist(me.Id, playlistName, false); // TODO config isPublic above
// TODO check if null
await SpotifyPlaylistManager.AddTrackToPlaylist(response.Id, playlistAddItemsRequest);
}
else
{
await SpotifyPlaylistManager.AddTrackToPlaylist(existingPlaylist, playlistAddItemsRequest);
}
}
}
}
15 changes: 14 additions & 1 deletion VXMusic/LogParser/VRChat/VRChatLogParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class VRChatLogParser : INotifyPropertyChanged
static Timer LogDetectionTimer { get; set; }

public static string LastKnownLocationName { get; set; } // World name
static string LastKnownLocationID { get; set; } // World ID
public static string LastKnownLocationID { get; set; } // World ID
static bool PlayerIsBetweenWorlds { get; set; }
static bool NextJoinIsLocalUser { get; set; }

Expand All @@ -54,6 +54,8 @@ public class VRChatLogParser : INotifyPropertyChanged
private bool IsVRChatShuttingDown;
private bool _isVrChatSessionRunning { get; set; }

private string _currentVrChatWorld { get; set; }

public bool IsVrChatSessionRunning
{
get { return _isVrChatSessionRunning; }
Expand All @@ -64,6 +66,16 @@ public bool IsVrChatSessionRunning
}
}

public string CurrentVrChatWorld
{
get { return _currentVrChatWorld; }
set
{
_currentVrChatWorld = value;
OnPropertyChanged(nameof(CurrentVrChatWorld));
}
}

public VRChatLogParser(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
Expand Down Expand Up @@ -311,6 +323,7 @@ void ParseTick(string content)
NextJoinIsLocalUser = true;

_logger.LogInformation($"World changed to {LastKnownLocationName} -> {LastKnownLocationID}");
CurrentVrChatWorld = LastKnownLocationName;
// TODO Wtf is happening here??
//_logger.LogInformation($"http://vrchat.com/home/launch?worldId={LastKnownLocationID.Replace(":", "&instanceId=")}", true);
}
Expand Down
26 changes: 26 additions & 0 deletions VXMusicDesktop/MVVM/ViewModel/SharedViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SharedViewModel : INotifyPropertyChanged
public SharedViewModel()
{
App.VXMusicSession.VRChatLogParser.PropertyChanged += VRChatLogParserIsVrChatSessionRunningPropertyChanged;
App.VXMusicSession.VRChatLogParser.PropertyChanged += VRChatLogParserCurrentVrChatWorldPropertyChanged;
}

// Recognition Shared Fields
Expand All @@ -35,6 +36,7 @@ public SharedViewModel()

// Game Client Shared Fields
private bool _isVrChatConnected;
private string _currentVrChatWorld;

// Overlay Shared Fields
private bool _isOverlayRunning;
Expand Down Expand Up @@ -109,6 +111,16 @@ public bool IsVrChatConnected
}
}

public string CurrentVrChatWorld
{
get { return _currentVrChatWorld; }
set
{
_currentVrChatWorld = value;
OnPropertyChanged(nameof(CurrentVrChatWorld));
}
}

public bool IsOverlayRunning
{
get { return _isOverlayRunning; }
Expand Down Expand Up @@ -156,9 +168,23 @@ private void VRChatLogParserIsVrChatSessionRunningPropertyChanged(object sender,
}
}

private void VRChatLogParserCurrentVrChatWorldPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(App.VXMusicSession.VRChatLogParser.CurrentVrChatWorld))
{
// The IsVRChatSessionRunning property has changed
OnCurrentVrChatWorldChanged(App.VXMusicSession.VRChatLogParser.CurrentVrChatWorld);
}
}

private void OnVRChatSessionRunningChanged(bool isVrChatConnected)
{
IsVrChatConnected = isVrChatConnected;
}

private void OnCurrentVrChatWorldChanged(string currentVrChatWorld)
{
CurrentVrChatWorld = currentVrChatWorld;
}

}
56 changes: 55 additions & 1 deletion VXMusicDesktop/VXMusicActions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SpotifyAPI.Web;
using VXMusic;
using VXMusic.API;
using VXMusic.Spotify;
using VXMusic.Spotify.Authentication;
using VXMusicDesktop.MVVM.ViewModel;

namespace VXMusicDesktop;

Expand Down Expand Up @@ -62,7 +67,7 @@ await Task.Run(() =>
}

if (result.Result != null && SpotifyAuthentication.CurrentConnectionState == SpotifyConnectionState.Connected)
VXMusicAPI.ReportTrackToSpotifyPlaylist(result);
ReportTrackToSpotifyPlaylist(result);

if (result.Result != null)
{
Expand All @@ -88,4 +93,53 @@ await Task.Run(() =>

return true;
}

public async static void ReportTrackToSpotifyPlaylist(IRecognitionApiClientResponse result)
{
var spotify = await SpotifyClientBuilder.Instance;

var me = await spotify.UserProfile.Current();
Logger.LogInformation($"Welcome {me.DisplayName} ({me.Id}), you're authenticated!");

// get track name from output

var searchRequest = new SearchRequest(SearchRequest.Types.Track, $"{result.Result.Artist} {result.Result.Title}");
var searchResult = spotify.Search.Item(searchRequest);
var uri = searchResult.Result.Tracks.Items[0].Uri;

PlaylistAddItemsRequest playlistAddItemsRequest = new PlaylistAddItemsRequest(new List<string>()
{
//result.result.song_link
uri
});

var playlists = await spotify.PaginateAll(await spotify.Playlists.CurrentUsers().ConfigureAwait(false));

Logger.LogTrace($"Total Playlists in Account: {playlists.Count}");

// Prefix Playlist name with dd/MM date
var currentDate = DateTime.Now.ToString("dd/MM");
var playlistName = $"{currentDate}";

// TODO Only run this if VRChat is running
var lastKnownLocationName = App.VXMusicSession.VRChatLogParser.CurrentVrChatWorld;

if (lastKnownLocationName != null)
playlistName += " - " + lastKnownLocationName;

var existingPlaylist = SpotifyPlaylistManager.GetPlaylistIdByNameIfExists(playlistName, playlists);

if (existingPlaylist == null)
{
var response = await SpotifyPlaylistManager.CreatePlaylist(me.Id, playlistName, false); // TODO config isPublic above

Logger.LogInformation($"Adding Track {result.Result.Title} - {result.Result.Artist} to Playlist {playlistName}");
await SpotifyPlaylistManager.AddTrackToPlaylist(response.Id, playlistAddItemsRequest);
}
else
{
Logger.LogInformation($"Adding Track {result.Result.Title} - {result.Result.Artist} to Playlist {playlistName}");
await SpotifyPlaylistManager.AddTrackToPlaylist(existingPlaylist, playlistAddItemsRequest);
}
}
}

0 comments on commit dd09721

Please sign in to comment.