Added Support for Debug Logging
Updated a couple of variables for better memory use. Added Support for ignoring specific streamers Moved to using a SingletonFactory for Singlton Classes No Longer Using MessageBox for disconnected message Added better detection for disconnected to stop it poping with other problems Added Window and menu option to open window to manage ignored streamers
This commit is contained in:
@@ -9,11 +9,30 @@ using TwitchDesktopNotifications.JsonStructure.Helix;
|
||||
|
||||
namespace TwitchDesktopNotifications.Core
|
||||
{
|
||||
internal class TwitcherRefreshException : Exception
|
||||
{
|
||||
public TwitcherRefreshException(string? message, Exception? innerException) : base(message, innerException) {
|
||||
}
|
||||
}
|
||||
internal class TwitchFetcher
|
||||
{
|
||||
private TwitchFetcher() {
|
||||
}
|
||||
|
||||
ReconnectionNeeded rnFrm;
|
||||
|
||||
public void OpenFailedNotification()
|
||||
{
|
||||
if (rnFrm == null)
|
||||
{
|
||||
rnFrm = new ReconnectionNeeded();
|
||||
}
|
||||
if (rnFrm.IsActive)
|
||||
{
|
||||
rnFrm.Show();
|
||||
}
|
||||
}
|
||||
|
||||
public static TwitchFetcher instance { get; private set; }
|
||||
|
||||
List <StreamsData> currentlyLive = null;
|
||||
@@ -46,36 +65,55 @@ namespace TwitchDesktopNotifications.Core
|
||||
{
|
||||
throw new Exception("Not Authenticated");
|
||||
}
|
||||
|
||||
if (DataStore.GetInstance().Store.Authentication.ExpiresAsDate <= DateTime.UtcNow)
|
||||
{
|
||||
Refresh();
|
||||
}
|
||||
|
||||
WebRequest request = WebRequest.Create("https://api.twitch.tv/" + endpoint);
|
||||
request.Method = "GET";
|
||||
request.Headers[HttpRequestHeader.Authorization] = String.Format("Bearer {0}", DataStore.GetInstance().Store.Authentication.AccessToken);
|
||||
request.Headers["Client-ID"] = TwitchDetails.TwitchClientID;
|
||||
WebResponse response = request.GetResponse();
|
||||
Stream dataStream = response.GetResponseStream();
|
||||
StreamReader reader = new StreamReader(dataStream);
|
||||
string responseFromServer = reader.ReadToEnd();
|
||||
reader.Close();
|
||||
dataStream.Close();
|
||||
response.Close();
|
||||
try
|
||||
{
|
||||
WebRequest request = WebRequest.Create("https://api.twitch.tv/" + endpoint);
|
||||
request.Method = "GET";
|
||||
request.Headers[HttpRequestHeader.Authorization] = String.Format("Bearer {0}", DataStore.GetInstance().Store.Authentication.AccessToken);
|
||||
request.Headers["Client-ID"] = TwitchDetails.TwitchClientID;
|
||||
WebResponse response = request.GetResponse();
|
||||
Stream dataStream = response.GetResponseStream();
|
||||
StreamReader reader = new StreamReader(dataStream);
|
||||
string responseFromServer = reader.ReadToEnd();
|
||||
reader.Close();
|
||||
dataStream.Close();
|
||||
response.Close();
|
||||
|
||||
return JsonSerializer.Deserialize<T>(responseFromServer);
|
||||
return JsonSerializer.Deserialize<T>(responseFromServer);
|
||||
}
|
||||
catch (TwitcherRefreshException ex)
|
||||
{
|
||||
OpenFailedNotification();
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Logger.GetInstance().Writer.WriteLineAsync(ex.ToString());
|
||||
}
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public void FetchCurrentUser()
|
||||
{
|
||||
try
|
||||
{
|
||||
DataStore.GetInstance().Store.UserData = MakeRequest<User>("helix/users").Data[0];
|
||||
DataStore.GetInstance().Save();
|
||||
}catch(System.Exception ex)
|
||||
var UserList = MakeRequest<User>("helix/users");
|
||||
if (UserList.Data.Count > 0) {
|
||||
DataStore.GetInstance().Store.UserData = UserList.Data[0];
|
||||
DataStore.GetInstance().Save();
|
||||
}
|
||||
}
|
||||
catch (TwitcherRefreshException ex)
|
||||
{
|
||||
MessageBox.Show("Twitch Connection not authenticated you need to Reconnect it.", "Twitch Notify");
|
||||
OpenFailedNotification();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.GetInstance().Writer.WriteLineAsync(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,10 +121,19 @@ namespace TwitchDesktopNotifications.Core
|
||||
{
|
||||
try
|
||||
{
|
||||
return MakeRequest<User>("helix/users?id=" + user_id).Data[0];
|
||||
}catch(System.Exception ex)
|
||||
var Response = MakeRequest<User>("helix/users?id=" + user_id);
|
||||
if (Response.Data.Count > 0)
|
||||
{
|
||||
return Response.Data[0];
|
||||
}
|
||||
}
|
||||
catch (TwitcherRefreshException ex)
|
||||
{
|
||||
MessageBox.Show("Twitch Connection not authenticated you need to Reconnect it.", "Twitch Notify");
|
||||
OpenFailedNotification();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.GetInstance().Writer.WriteLineAsync(ex.ToString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -104,7 +151,7 @@ namespace TwitchDesktopNotifications.Core
|
||||
string QueryUrl = "helix/streams/followed?first=100&user_id=" + DataStore.GetInstance().Store.UserData.UserId;
|
||||
Streams following = MakeRequest<Streams>(QueryUrl);
|
||||
|
||||
if (currentlyLive != null)
|
||||
if (following != null && currentlyLive != null)
|
||||
{
|
||||
following.Data.ForEach(x =>
|
||||
{
|
||||
@@ -124,49 +171,53 @@ namespace TwitchDesktopNotifications.Core
|
||||
}
|
||||
|
||||
currentlyLive = following.Data;
|
||||
}catch(System.Exception ex)
|
||||
{
|
||||
if (!ex.Message.Contains("Notification"))
|
||||
{
|
||||
MessageBox.Show("Twitch Connection not authenticated you need to Reconnect it.", "Twitch Notify");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Unable to use Windows Notifications.", "Twitch Notify");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (TwitcherRefreshException ex)
|
||||
{
|
||||
OpenFailedNotification();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.GetInstance().Writer.WriteLineAsync(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
Dictionary<string, string> postData = new Dictionary<string, string>();
|
||||
try
|
||||
{
|
||||
Dictionary<string, string> postData = new Dictionary<string, string>();
|
||||
|
||||
postData["client_id"] = TwitchDetails.TwitchClientID;
|
||||
postData["client_secret"] = TwitchDetails.TwitchClientSecret;
|
||||
postData["grant_type"] = "refresh_token";
|
||||
postData["refresh_token"] = DataStore.GetInstance().Store.Authentication.RefreshToken;
|
||||
postData["client_id"] = TwitchDetails.TwitchClientID;
|
||||
postData["client_secret"] = TwitchDetails.TwitchClientSecret;
|
||||
postData["grant_type"] = "refresh_token";
|
||||
postData["refresh_token"] = DataStore.GetInstance().Store.Authentication.RefreshToken;
|
||||
|
||||
byte[] byteArray = buildPostData(postData);
|
||||
byte[] byteArray = buildPostData(postData);
|
||||
|
||||
WebRequest request = WebRequest.Create("https://id.twitch.tv/oauth2/token");
|
||||
request.Method = "POST";
|
||||
request.ContentType = "application/x-www-form-urlencoded";
|
||||
request.ContentLength = byteArray.Length;
|
||||
Stream dataStream = request.GetRequestStream();
|
||||
dataStream.Write(byteArray, 0, byteArray.Length);
|
||||
dataStream.Close();
|
||||
WebResponse response = request.GetResponse();
|
||||
dataStream = response.GetResponseStream();
|
||||
StreamReader reader = new StreamReader(dataStream);
|
||||
string responseFromServer = reader.ReadToEnd();
|
||||
reader.Close();
|
||||
dataStream.Close();
|
||||
response.Close();
|
||||
WebRequest request = WebRequest.Create("https://id.twitch.tv/oauth2/token");
|
||||
request.Method = "POST";
|
||||
request.ContentType = "application/x-www-form-urlencoded";
|
||||
request.ContentLength = byteArray.Length;
|
||||
Stream dataStream = request.GetRequestStream();
|
||||
dataStream.Write(byteArray, 0, byteArray.Length);
|
||||
dataStream.Close();
|
||||
WebResponse response = request.GetResponse();
|
||||
dataStream = response.GetResponseStream();
|
||||
StreamReader reader = new StreamReader(dataStream);
|
||||
string responseFromServer = reader.ReadToEnd();
|
||||
reader.Close();
|
||||
dataStream.Close();
|
||||
response.Close();
|
||||
|
||||
DataStore.GetInstance().Store.Authentication = JsonSerializer.Deserialize<Authentication>(responseFromServer);
|
||||
DateTime unixStart = DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc);
|
||||
DataStore.GetInstance().Store.Authentication.ExpiresAt = (long)Math.Floor((DateTime.Now.AddSeconds(DataStore.GetInstance().Store.Authentication.ExpiresSeconds) - unixStart).TotalMilliseconds);
|
||||
DataStore.GetInstance().Save();
|
||||
DataStore.GetInstance().Store.Authentication = JsonSerializer.Deserialize<Authentication>(responseFromServer);
|
||||
DateTime unixStart = DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc);
|
||||
DataStore.GetInstance().Store.Authentication.ExpiresAt = (long)Math.Floor((DateTime.Now.AddSeconds(DataStore.GetInstance().Store.Authentication.ExpiresSeconds) - unixStart).TotalMilliseconds);
|
||||
DataStore.GetInstance().Save();
|
||||
}catch(Exception e)
|
||||
{
|
||||
throw new TwitcherRefreshException("Unable to refresh", e);
|
||||
}
|
||||
}
|
||||
|
||||
async public void BeginConnection()
|
||||
|
||||
Reference in New Issue
Block a user