Skip to content

Commit

Permalink
Fixed song requests not working. This was due to Spotify rolling out …
Browse files Browse the repository at this point in the history
…changes to their API which caused major problems.

Credentials are now standard hidden in Settings -> Twitch
Removed SpotifyAPI.Net nuget and implemented the code manually
  • Loading branch information
Inzaniity committed Feb 28, 2024
1 parent b73bb22 commit 485d7e6
Show file tree
Hide file tree
Showing 83 changed files with 7,428 additions and 38 deletions.
17 changes: 9 additions & 8 deletions Songify Slim/App.xaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<Application x:Class="Songify_Slim.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup"
ShutdownMode="OnMainWindowClose"
DispatcherUnhandledException="Application_DispatcherUnhandledException">
<Application
x:Class="Songify_Slim.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DispatcherUnhandledException="Application_DispatcherUnhandledException"
ShutdownMode="OnMainWindowClose"
Startup="Application_Startup">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Strings.xaml" />
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<!-- Theme setting -->
<!-- Theme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Expand Down
4 changes: 2 additions & 2 deletions Songify Slim/Models/SongInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using SpotifyAPI.Web.Models;
using System.Collections.Generic;
using System.Collections.Generic;
using Songify_Slim.Util.Spotify.SpotifyAPI.Web.Models;

namespace Songify_Slim.Models
{
Expand Down
4 changes: 2 additions & 2 deletions Songify Slim/Models/TrackInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using SpotifyAPI.Web.Models;
using System.Collections.Generic;
using System.Collections.Generic;
using Songify_Slim.Util.Spotify.SpotifyAPI.Web.Models;

namespace Songify_Slim.Models
{
Expand Down
7 changes: 4 additions & 3 deletions Songify Slim/Resources/Strings.xaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="LicenseUsing" xml:space="preserve">The MIT License (MIT)
applies to:
- Mahapps Metro, Copyright (c) 2016 MahApps
Expand Down
77 changes: 71 additions & 6 deletions Songify Slim/Songify Slim.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,6 @@
<Reference Include="Octokit, Version=6.1.0.0, Culture=neutral, PublicKeyToken=0be8860aee462442, processorArchitecture=MSIL">
<HintPath>..\packages\Octokit.6.1.0\lib\netstandard2.0\Octokit.dll</HintPath>
</Reference>
<Reference Include="SpotifyAPI.Web, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SpotifyAPI.Web.5.1.1\lib\netstandard2.0\SpotifyAPI.Web.dll</HintPath>
</Reference>
<Reference Include="SpotifyAPI.Web.Auth, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SpotifyAPI.Web.Auth.5.1.1\lib\netstandard2.0\SpotifyAPI.Web.Auth.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.AppContext, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.AppContext.4.3.0\lib\net463\System.AppContext.dll</HintPath>
Expand Down Expand Up @@ -542,6 +536,70 @@
</Compile>
<Compile Include="Util\BooleanToVisibilityConverter.cs" />
<Compile Include="Util\Songify\ApiClient.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web.Auth\AuthorizationCodeAuth.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web.Auth\AuthUtil.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web.Auth\CredentialsAuth.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web.Auth\ImplicitGrantAuth.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web.Auth\SpotifyAuthServer.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web.Auth\TokenSwapAuth.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web.Auth\TokenSwapWebAPIFactory.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Enums\AlbumType.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Enums\FollowType.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Enums\RepeatState.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Enums\Scope.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Enums\SearchType.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Enums\TimeRangeType.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Enums\TrackType.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\IClient.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\AnalysisMeta.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\AnalysisSection.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\AnalysisSegment.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\AnalysisTimeSlice.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\AnalysisTrack.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\ArrayResponse.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\AudioAnalysis.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\AudioFeatures.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\AvailabeDevices.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\BasicModel.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\Category.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\CategoryList.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\CategoryPlaylist.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\CursorPaging.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\Device.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\FeaturedPlaylists.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\FollowedArtists.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\FullAlbum.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\FullArtist.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\FullPlaylist.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\FullTrack.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\GeneralModels.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\NewAlbumReleases.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\Paging.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\PlaybackContext.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\PlayHistory.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\PrivateProfile.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\PublicProfile.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\Recommendations.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\RecommendationSeed .cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\RecommendationSeedGenres.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\ResponseInfo.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\SearchItem.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\SeveralAlbums.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\SeveralArtists.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\SeveralAudioFeatures.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\SeveralTracks.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\SimpleAlbum.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\SimpleArtist.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\SimplePlaylist.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\SimpleTrack.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\Snapshot.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\Token.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Models\TuneableTrack.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\ProxyConfig.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\SpotifyWebAPI.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\SpotifyWebBuilder.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\SpotifyWebClient.cs" />
<Compile Include="Util\Spotify\SpotifyAPI.Web\Util.cs" />
<Compile Include="Views\AboutWindow.xaml.cs">
<DependentUpon>AboutWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -764,6 +822,13 @@
</Resource>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Content Include="Util\Spotify\SpotifyAPI.Web.Auth\Resources\AuthorizationCodeAuth\css\bulma.min.css" />
<Content Include="Util\Spotify\SpotifyAPI.Web.Auth\Resources\ImplicitGrantAuth\index.html" />
<Resource Include="Util\Spotify\SpotifyAPI.Web.Auth\Resources\AuthorizationCodeAuth\images\1.png" />
<Resource Include="Util\Spotify\SpotifyAPI.Web.Auth\Resources\AuthorizationCodeAuth\images\2.png" />
<Content Include="Util\Spotify\SpotifyAPI.Web.Auth\Resources\AuthorizationCodeAuth\start.html" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions Songify Slim/UserControls/UC_PlaylistItem.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using SpotifyAPI.Web.Models;
using System;
using System;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Songify_Slim.Util.Spotify.SpotifyAPI.Web.Models;

namespace Songify_Slim.UserControls
{
Expand Down
12 changes: 6 additions & 6 deletions Songify Slim/Util/Songify/Apihandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
using Songify_Slim.Models;
using Songify_Slim.Util.General;
using Songify_Slim.Views;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using Songify_Slim.Util.Spotify.SpotifyAPI.Web;
using Songify_Slim.Util.Spotify.SpotifyAPI.Web.Auth;
using Songify_Slim.Util.Spotify.SpotifyAPI.Web.Enums;
using Songify_Slim.Util.Spotify.SpotifyAPI.Web.Models;
using Timer = System.Timers.Timer;

namespace Songify_Slim.Util.Songify
Expand Down Expand Up @@ -241,11 +241,11 @@ public static TrackInfo GetSongInfo()
Title = context.Item.Name,
Albums = albums,
SongId = context.Item.Id,
DurationMs = context.Item.DurationMs - context.ProgressMs,
DurationMs = (int)context.Item.DurationMs - context.ProgressMs,
IsPlaying = context.IsPlaying,
Url = "https://open.spotify.com/track/" + context.Item.Id,
DurationPercentage = (int)percentage,
DurationTotal = context.Item.DurationMs,
DurationTotal = (int)context.Item.DurationMs,
Progress = context.ProgressMs,
Playlist = playlistInfo
};
Expand Down
2 changes: 1 addition & 1 deletion Songify Slim/Util/Songify/SongFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Songify_Slim.Models;
using Songify_Slim.Util.General;
using Songify_Slim.Views;
using SpotifyAPI.Web.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -12,6 +11,7 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation;
using Songify_Slim.Util.Spotify.SpotifyAPI.Web.Models;
using Unosquare.Swan;
using Unosquare.Swan.Formatters;

Expand Down
157 changes: 157 additions & 0 deletions Songify Slim/Util/Songify/SpotifyApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using Songify_Slim.Util.Settings;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Songify_Slim.Util.General;

namespace Songify_Slim.Util.Songify
{
internal class SpotifyApiClient
{
private const string BaseUrl = "https://auth.overcode.tv";
private const string ClientId = "6b08bd81685e4115b2c615e18499fe6c";
private const string ClientSecret = "561d947c50324d8783b4e1601b5ceafd";
private static EmbedIOAuthServer? _server;
public static SpotifyClient? Client;
private static readonly System.Timers.Timer AuthTimer = new()
{
Interval = 1000 * 60 * 30,
};

public static async Task Auth()
{
AuthTimer.Elapsed += AuthTimer_Elapsed;
if (!(string.IsNullOrEmpty(Settings.Settings.SpotifyAccessToken) || string.IsNullOrEmpty(Settings.Settings.SpotifyRefreshToken)))
{
Debug.WriteLine("Refreshing Tokens");
RefreshTokens();
return;
}
Debug.WriteLine("Getting new tokens");
_server = new EmbedIOAuthServer(new Uri("http://localhost:4002/auth"), 4002);
await _server.Start();

_server.AuthorizationCodeReceived += OnAuthorizationCodeReceived;

var request = new LoginRequest(_server.BaseUri, ClientId, LoginRequest.ResponseType.Code)
{
Scope = new List<string> {
Scopes.UserReadPlaybackState , Scopes.UserReadPrivate , Scopes.UserModifyPlaybackState , Scopes.PlaylistModifyPublic , Scopes.PlaylistModifyPrivate , Scopes.PlaylistReadPrivate
}
};
var uri = request.ToUri();
try
{
BrowserUtil.Open(uri);
}
catch (Exception)
{
Console.WriteLine("Unable to open URL, manually open: {0}", uri);
}
}

private static void AuthTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
RefreshTokens();
}

private static async void RefreshTokens()
{
var oauth = new OAuthClient();
var refreshRequest = new TokenSwapRefreshRequest(
new Uri($"{BaseUrl}/refresh?id={ClientId}&secret={ClientSecret}"),
Settings.Settings.SpotifyRefreshToken
);
var refreshResponse = await oauth.RequestToken(refreshRequest);

Debug.WriteLine($"We got a new refreshed access token from server: {refreshResponse.AccessToken}");

var config = SpotifyClientConfig.CreateDefault().WithAuthenticator(
new AuthorizationCodeAuthenticator(ClientId, ClientSecret, new AuthorizationCodeTokenResponse
{
AccessToken = Settings.Settings.SpotifyAccessToken,
RefreshToken = Settings.Settings.SpotifyRefreshToken
}));

Settings.Settings.SpotifyAccessToken = refreshResponse.AccessToken;
Settings.Settings.SpotifyRefreshToken = refreshResponse.RefreshToken;

Client = new SpotifyClient(config);
if (!AuthTimer.Enabled)
{
AuthTimer.Start();
}
}

private static async Task OnAuthorizationCodeReceived(object? sender, AuthorizationCodeResponse response)
{
Debug.WriteLine("Got here");
var oauth = new OAuthClient();
var tokenRequest = new TokenSwapTokenRequest(new Uri($"{BaseUrl}/swap?id={ClientId}&secret={ClientSecret}"), response.Code);
var tokenResponse = await oauth.RequestToken(tokenRequest);
Debug.WriteLine($"We got an access token from server: {tokenResponse.AccessToken}");

var refreshRequest = new TokenSwapRefreshRequest(
new Uri($"{BaseUrl}/refresh?id={ClientId}&secret={ClientSecret}"),
tokenResponse.RefreshToken
);
var refreshResponse = await oauth.RequestToken(refreshRequest);

Debug.WriteLine($"We got a new refreshed access token from server: {refreshResponse.AccessToken}");

var config = SpotifyClientConfig.CreateDefault().WithAuthenticator(new AuthorizationCodeAuthenticator(ClientId, ClientSecret, tokenResponse));

Client = new SpotifyClient(config);
if (!AuthTimer.Enabled)
{
AuthTimer.Start();
}
}

public static async Task<CurrentlyPlayingContext?> GetCurrentSongAsync()
{
if (Client == null) { return null; }
try
{
return await Client.Player.GetCurrentPlayback();
}
catch (Exception)
{
return null;
}
}

public static async Task<QueueResponse?> GetPlaybackQueue()
{
if (Client == null) { return null; }
try
{
return await Client.Player.GetQueue();
}
catch (Exception)
{
return null;
}
}

public static async Task<bool> AddToQueue(string v)
{
return await Client.Player.AddToQueue(new PlayerAddToQueueRequest(v));
}

public static async Task<bool> SkipSong()
{
return await Client.Player.SkipNext();
}

public static async Task<PrivateUser> GetProfileAsync()
{
return await Client.UserProfile.Current();
}
}
}
4 changes: 2 additions & 2 deletions Songify Slim/Util/Songify/TwitchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Songify_Slim.Util.Settings;
using Songify_Slim.Util.Songify.TwitchOAuth;
using Songify_Slim.Views;
using SpotifyAPI.Web.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -20,6 +19,7 @@
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using Songify_Slim.Util.Spotify.SpotifyAPI.Web.Models;
using TwitchLib.Api;
using TwitchLib.Api.Auth;
using TwitchLib.Api.Core.Enums;
Expand Down Expand Up @@ -2358,7 +2358,7 @@ private static async Task UploadToQueue(FullTrack track, string displayName)
// remove the last ", "
artists = artists.Remove(artists.Length - 2, 2);

string length = FormattedTime(track.DurationMs);
string length = FormattedTime((int)track.DurationMs);

// upload to the queue
//WebHelper.UpdateWebQueue(track.Id, artists, track.Name, length, displayName, "0", "i");
Expand Down
Loading

0 comments on commit 485d7e6

Please sign in to comment.