From 7994e189cf968becd81c0e657cde65b9725e3ab7 Mon Sep 17 00:00:00 2001 From: Jose Ouellet Date: Wed, 27 Jan 2021 22:16:46 -0500 Subject: [PATCH 1/2] One popup spotify api --- EspionSpotify/API/IExternalAPI.cs | 1 + EspionSpotify/API/LastFMAPI.cs | 5 ++ EspionSpotify/API/SpotifyAPI.cs | 16 +++-- .../AudioSessions/MainAudioSession.cs | 2 +- EspionSpotify/Native/ProcessManager.cs | 65 +++++++++++++++++-- EspionSpotify/Spotify/SpotifyProcess.cs | 2 +- EspionSpotify/Watcher.cs | 33 +++++----- EspionSpotify/frmEspionSpotify.cs | 1 + 8 files changed, 92 insertions(+), 33 deletions(-) diff --git a/EspionSpotify/API/IExternalAPI.cs b/EspionSpotify/API/IExternalAPI.cs index e29a68d4..ccc3a17a 100644 --- a/EspionSpotify/API/IExternalAPI.cs +++ b/EspionSpotify/API/IExternalAPI.cs @@ -9,6 +9,7 @@ public interface IExternalAPI ExternalAPIType GetTypeAPI { get; } bool IsAuthenticated { get; } Task Authenticate(); + void Reset(); Task UpdateTrack(Track track); Task<(string, bool)> GetCurrentPlayback(); diff --git a/EspionSpotify/API/LastFMAPI.cs b/EspionSpotify/API/LastFMAPI.cs index 47f30bd9..8cc9750a 100644 --- a/EspionSpotify/API/LastFMAPI.cs +++ b/EspionSpotify/API/LastFMAPI.cs @@ -143,6 +143,11 @@ public void MapLastFMTrackToTrack(Track track, LastFMTrack trackExtra) track.ArtSmallUrl = trackExtra.Album?.SmallCoverUrl; } + public void Reset() + { + _loggedSilentExceptionOnce = false; + } + #region NotImplementedExternalAPI public bool IsAuthenticated { get => true; } public async Task Authenticate() => await Task.CompletedTask; diff --git a/EspionSpotify/API/SpotifyAPI.cs b/EspionSpotify/API/SpotifyAPI.cs index 60100598..bb107352 100644 --- a/EspionSpotify/API/SpotifyAPI.cs +++ b/EspionSpotify/API/SpotifyAPI.cs @@ -50,8 +50,6 @@ public SpotifyAPI(string clientId, string secretId, string redirectUrl = SPOTIFY } } - public async Task Authenticate() => await GetSpotifyWebAPI(); - public async Task<(string, bool)> GetCurrentPlayback() { var playing = false; @@ -147,10 +145,7 @@ private async Task UpdateTrack(Track track, bool retry = false) // open spotify authentication page if user is disconnected // user might be connected with a different account that the one that granted rights - if (!_connectionDialogOpened) - { - OpenAuthenticationDialog(); - } + OpenAuthenticationDialog(); // fallback in case getting the playback did not work ExternalAPI.Instance = _lastFmApi; @@ -200,6 +195,7 @@ private async void AuthOnAuthReceived(object sender, AuthorizationCode payload) private void OpenAuthenticationDialog() { + if (_connectionDialogOpened) return; _auth.ShowDialog = true; _auth.OpenBrowser(); _connectionDialogOpened = true; @@ -244,6 +240,14 @@ private async Task GetSpotifyWebAPI() return _api; } + public async Task Authenticate() => await GetSpotifyWebAPI(); + + public void Reset() + { + _connectionDialogOpened = false; + } + + public void Dispose() { Dispose(true); diff --git a/EspionSpotify/AudioSessions/MainAudioSession.cs b/EspionSpotify/AudioSessions/MainAudioSession.cs index 9e741958..27895ac9 100644 --- a/EspionSpotify/AudioSessions/MainAudioSession.cs +++ b/EspionSpotify/AudioSessions/MainAudioSession.cs @@ -40,7 +40,7 @@ public MainAudioSession(string audioEndPointDeviceID, IProcessManager processMan { _processManager = processManager; - _spytifyProcessId = _processManager.GetCurrentProcess().Id; + _spytifyProcessId = (int)_processManager.GetCurrentProcess()?.Id; AudioMMDevices = new MMDeviceEnumerator(); AudioMMDevicesManager = new AudioMMDevicesManager(AudioMMDevices, audioEndPointDeviceID); diff --git a/EspionSpotify/Native/ProcessManager.cs b/EspionSpotify/Native/ProcessManager.cs index e48b8f1f..26a753fb 100644 --- a/EspionSpotify/Native/ProcessManager.cs +++ b/EspionSpotify/Native/ProcessManager.cs @@ -1,5 +1,6 @@ -using EspionSpotify.Native.Models; -using System.Linq; +using System.Linq; +using EspionSpotify.Native.Models; +using NativeProcess = System.Diagnostics.Process; namespace EspionSpotify.Native { @@ -7,7 +8,17 @@ public class ProcessManager : IProcessManager { public IProcess GetCurrentProcess() { - var process = System.Diagnostics.Process.GetCurrentProcess(); + NativeProcess process; + + try + { + process = NativeProcess.GetCurrentProcess(); + } + catch + { + return null; + } + return new Process { Id = process.Id, @@ -18,7 +29,17 @@ public IProcess GetCurrentProcess() public IProcess[] GetProcesses() { - var processes = System.Diagnostics.Process.GetProcesses(); + NativeProcess[] processes; + + try + { + processes = NativeProcess.GetProcesses(); + } + catch + { + return new Process[] { }; + } + return processes.Select(x => new Process { Id = x.Id, @@ -29,7 +50,17 @@ public IProcess[] GetProcesses() public IProcess[] GetProcessesByName(string processName) { - var processes = System.Diagnostics.Process.GetProcessesByName(processName); + NativeProcess[] processes; + + try + { + processes = NativeProcess.GetProcessesByName(processName); + } + catch + { + return new Process[] { }; + } + return processes.Select(x => new Process { Id = x.Id, @@ -40,7 +71,17 @@ public IProcess[] GetProcessesByName(string processName) public IProcess GetProcessById(int processId) { - var process = System.Diagnostics.Process.GetProcessById(processId); + NativeProcess process; + + try + { + process = NativeProcess.GetProcessById(processId); + } + catch + { + return null; + } + return new Process { Id = process.Id, @@ -51,7 +92,17 @@ public IProcess GetProcessById(int processId) public IProcess Start(string fileName) { - var process = System.Diagnostics.Process.Start(fileName); + NativeProcess process; + + try + { + process = NativeProcess.Start(fileName); + } + catch + { + return null; + } + return new Process { Id = process.Id, diff --git a/EspionSpotify/Spotify/SpotifyProcess.cs b/EspionSpotify/Spotify/SpotifyProcess.cs index 51b4deff..2c24427d 100644 --- a/EspionSpotify/Spotify/SpotifyProcess.cs +++ b/EspionSpotify/Spotify/SpotifyProcess.cs @@ -67,7 +67,7 @@ public async Task GetSpotifyStatus() { isSpotifyAudioPlaying = await _audioSession.IsSpotifyCurrentlyPlaying(); var process = _processManager.GetProcessById(_spotifyProcessId.Value); - mainWindowTitle = process.MainWindowTitle; + mainWindowTitle = process?.MainWindowTitle ?? ""; } } catch (Exception ex) diff --git a/EspionSpotify/Watcher.cs b/EspionSpotify/Watcher.cs index 581e4a38..2991fc9f 100644 --- a/EspionSpotify/Watcher.cs +++ b/EspionSpotify/Watcher.cs @@ -21,7 +21,6 @@ public class Watcher : IWatcher, IDisposable private bool _disposed = false; private const int NEXT_SONG_EVENT_MAX_ESTIMATED_DELAY_SECS = 5; private const int WATCHER_DELAY_MS = 500; - private const int PREVENT_SLEEP_EVENT_DELAY_MS = (WATCHER_DELAY_MS * 2) * 5 * 60; public static bool Running { get; internal set; } public static bool Ready { get; private set; } = true; @@ -302,17 +301,18 @@ private async Task EnableRecordingTimer() _recordingTimer.Enabled = true; } - private void EndRecordingSession() + private void ResetAudioSession() { - Ready = true; - if (_audioSession != null) { MutesSpotifyAds(false); _audioSession.SetSpotifyVolumeToHighAndOthersToMute(false); _audioSession.ClearSpotifyAudioSessionControls(); } + } + private void ResetSpotifyHandler() + { if (Spotify != null) { Spotify.ListenForEvents = false; @@ -321,7 +321,15 @@ private void EndRecordingSession() Spotify.OnTrackTimeChange -= OnTrackTimeChanged; Spotify.Dispose(); } - + } + + private void EndRecordingSession() + { + Ready = true; + + ResetAudioSession(); + ResetSpotifyHandler(); + _form.UpdateStartButton(); _form.UpdatePlayingTitle(Constants.SPOTIFY); _form.UpdateIconSpotify(false); @@ -385,19 +393,8 @@ protected virtual void Dispose(bool disposing) DoIKeepLastSong(); NativeMethods.AllowSleep(); - if (_audioSession != null) - { - MutesSpotifyAds(false); - _audioSession.SetSpotifyVolumeToHighAndOthersToMute(false); - _audioSession.ClearSpotifyAudioSessionControls(); - - Spotify.ListenForEvents = false; - Spotify.OnPlayStateChange -= OnPlayStateChanged; - Spotify.OnTrackChange -= OnTrackChanged; - Spotify.OnTrackTimeChange -= OnTrackTimeChanged; - Spotify.Dispose(); - Spotify = null; - } + ResetAudioSession(); + ResetSpotifyHandler(); _recorderTasks.ForEach(x => { diff --git a/EspionSpotify/frmEspionSpotify.cs b/EspionSpotify/frmEspionSpotify.cs index aa240f20..d8dcc9c0 100644 --- a/EspionSpotify/frmEspionSpotify.cs +++ b/EspionSpotify/frmEspionSpotify.cs @@ -494,6 +494,7 @@ public void StopRecording() } Watcher.Running = false; + ExternalAPI.Instance.Reset(); _toggleStopRecordingDelayed = false; timer1.Stop(); From 145f3da56197e55d307a361260664f4bb1da1ee8 Mon Sep 17 00:00:00 2001 From: Jose Ouellet Date: Wed, 27 Jan 2021 22:37:21 -0500 Subject: [PATCH 2/2] same instance for spotify api --- EspionSpotify/API/SpotifyAPI.cs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/EspionSpotify/API/SpotifyAPI.cs b/EspionSpotify/API/SpotifyAPI.cs index bb107352..99b65fbc 100644 --- a/EspionSpotify/API/SpotifyAPI.cs +++ b/EspionSpotify/API/SpotifyAPI.cs @@ -55,12 +55,11 @@ public SpotifyAPI(string clientId, string secretId, string redirectUrl = SPOTIFY var playing = false; string title = null; - var api = await GetSpotifyWebAPI(); + await GetSpotifyWebAPI(); - if (api != null) + if (_api != null) { - var playback = await api.GetPlaybackAsync(); - + var playback = await _api.GetPlaybackAsync(); if (playback != null && !playback.HasError()) { playing = playback.IsPlaying; @@ -126,10 +125,11 @@ public void MapSpotifyAlbumToTrack(Track track, FullAlbum spotifyAlbum) #region Spotify Track updater private async Task UpdateTrack(Track track, bool retry = false) { - var api = await GetSpotifyWebAPI(); - if (api == null) return; + await GetSpotifyWebAPI(); + + if (_api == null) return; - var playback = await api.GetPlaybackAsync(); + var playback = await _api.GetPlaybackAsync(); var hasNoPlayback = playback == null || playback.Item == null; if (!retry && hasNoPlayback) @@ -141,7 +141,7 @@ private async Task UpdateTrack(Track track, bool retry = false) if (hasNoPlayback || playback.HasError()) { - api.Dispose(); + _api.Dispose(); // open spotify authentication page if user is disconnected // user might be connected with a different account that the one that granted rights @@ -167,7 +167,7 @@ private async Task UpdateTrack(Track track, bool retry = false) if (playback.Item.Album?.Id == null) return; - var album = await api.GetAlbumAsync(playback.Item.Album.Id); + var album = await _api.GetAlbumAsync(playback.Item.Album.Id); if (album.HasError()) return; @@ -201,12 +201,12 @@ private void OpenAuthenticationDialog() _connectionDialogOpened = true; } - private async Task GetSpotifyWebAPI() + private async Task GetSpotifyWebAPI() { if (_token == null) { OpenAuthenticationDialog(); - return null; + return; } if (_token.IsExpired()) @@ -236,8 +236,6 @@ private async Task GetSpotifyWebAPI() _authorizationCodeAuth.Stop(); } } - - return _api; } public async Task Authenticate() => await GetSpotifyWebAPI();