9 Commits

7 changed files with 117 additions and 77 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@
/TwitchDesktopNotifications/obj
/.vs
/TwitchDesktopNotifications/App.config
/TwitchDesktopNotifications/TwitchDetails.cs

View File

@@ -1,7 +1,5 @@
# Twitch Notify
## not affiliated with Twitch or Amazon
[![Build Status](https://travis-ci.org/joemccann/dillinger.svg?branch=master)](https://travis-ci.org/joemccann/dillinger)
## Not affiliated with Twitch or Amazon
## Installation
Twitch Notify Requires [.NET 6 Desktop Runtime](https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/runtime-desktop-6.0.13-windows-x64-installer).
@@ -11,19 +9,33 @@
Want to contribute? Great!
Project is built using Visual Studios 2022, must have Windows 10.0.17763 SDK installed
Project is built using Visual Studios 2022,
You must create a `App.config` file inside `TwitchDesktopNotifications` project.
```xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="TwitchClientID" value="" />
<add key="TwitchClientSecret" value="" />
</appSettings>
</configuration>
You need to create Application to obtain a ID and Secret on [Twitch Developer Console](https://dev.twitch.tv/console)
Add a new C# Class to the project named `TwitchDetails.cs` add the following code with your ID and Secret
```cs
namespace TwitchDesktopNotifications
{
static public class TwitchDetails
{
public static string TwitchClientID = "";
public static string TwitchClientSecret = "";
}
}
```
### CommunityToolkit 8.0.0 Pre-release
Project Requests `CommunityToolkit-MainLatest` NuGET Package Source
1. Tool > NuGET Package Manager > Package Manager Settings
2. Click on Package Source (just below the select General in the Left hand column
3. Click the + icon top right
5. 4. Enter the Name `CommunityToolkit-MainLatest` and Source `https://pkgs.dev.azure.com/dotnet/CommunityToolkit/_packaging/CommunityToolkit-MainLatest/nuget/v3/index.json`
6. Click Update
7. Click Ok
## License
MIT

View File

@@ -9,6 +9,7 @@ using System.IO;
using Windows.Foundation.Collections;
using System.Diagnostics;
using System.ComponentModel;
using Windows.UI.Notifications;
namespace TwitchDesktopNotifications.Core
{
@@ -22,10 +23,18 @@ namespace TwitchDesktopNotifications.Core
try
{
if (
// action is defined and set to watch
( args.Contains("action") && args["action"] == "watch" )
||
// action is not defined so the user just generally clicked on the notification
!args.Contains("action")
){
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = args["streamerUrl"];
myProcess.Start();
}
}catch(Exception ex) { }
};
}
@@ -49,7 +58,10 @@ namespace TwitchDesktopNotifications.Core
// download there profile picture
string fileNameProfilePic = profilePic.Split("/").Last();
if (!File.Exists(FilePath + "/" + fileNameProfilePic))
{
(new WebClient()).DownloadFile(new Uri(profilePic), FilePath + "/" + fileNameProfilePic);
}
// download there profile picture
string fileNameThumbnailPic = streamThumbnail.Split("/").Last();
@@ -59,6 +71,7 @@ namespace TwitchDesktopNotifications.Core
var builder = new ToastContentBuilder()
.AddArgument("streamerUrl", streamerUrl)
.AddArgument("thumbnail_path", FilePath + "/" + fileNameThumbnailPic)
.AddText(streamerName + " is now live on Twitch")
.AddHeroImage(new Uri("file://" + (FilePath + "/" + fileNameThumbnailPic).Replace("\\", "/")))
.AddAppLogoOverride(new Uri("file://" + (FilePath + "/" + fileNameProfilePic).Replace("\\", "/")), ToastGenericAppLogoCrop.Circle)
@@ -77,9 +90,22 @@ namespace TwitchDesktopNotifications.Core
builder.Show(toast =>
{
toast.ExpirationTime = DateTime.Now.AddSeconds(15);
toast.Dismissed += (ToastNotification sender, ToastDismissedEventArgs args) =>
{
try
{
File.Delete(FilePath + "/" + fileNameThumbnailPic);
}catch(Exception) { }
};
toast.Activated += (ToastNotification sender, object args) =>
{
try
{
File.Delete(FilePath + "/" + fileNameThumbnailPic);
}
catch (Exception) { }
};
});
}
}
}

View File

@@ -1,33 +1,21 @@
using ABI.System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using System.Web;
using TwitchDesktopNotifications.JsonStructure;
using TwitchDesktopNotifications.JsonStructure.Helix;
using Windows.ApplicationModel.Background;
namespace TwitchDesktopNotifications.Core
{
internal class TwitchFetcher
{
private TwitchFetcher() { }
private TwitchFetcher() {
}
public static TwitchFetcher instance { get; private set; }
string TwitchClientID = ConfigurationManager.AppSettings["TwitchClientID"];
string TwitchClientSecret = ConfigurationManager.AppSettings["TwitchClientSecret"];
List <StreamsData> currentlyLive = null;
public string guid { get; private set; }
@@ -54,6 +42,10 @@ namespace TwitchDesktopNotifications.Core
private T MakeRequest<T>(string endpoint)
{
if (DataStore.GetInstance().Store == null)
{
throw new Exception("Not Authenticated");
}
if (DataStore.GetInstance().Store.Authentication.ExpiresAsDate <= DateTime.UtcNow)
{
@@ -63,7 +55,7 @@ namespace TwitchDesktopNotifications.Core
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"] = TwitchClientID;
request.Headers["Client-ID"] = TwitchDetails.TwitchClientID;
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
@@ -83,7 +75,7 @@ namespace TwitchDesktopNotifications.Core
DataStore.GetInstance().Save();
}catch(System.Exception ex)
{
Environment.Exit(1);
MessageBox.Show("Twitch Connection not authenticated you need to Reconnect it.", "Twitch Notify");
}
}
@@ -94,7 +86,7 @@ namespace TwitchDesktopNotifications.Core
return MakeRequest<User>("helix/users?id=" + user_id).Data[0];
}catch(System.Exception ex)
{
Environment.Exit(1);
MessageBox.Show("Twitch Connection not authenticated you need to Reconnect it.", "Twitch Notify");
}
return null;
}
@@ -134,7 +126,7 @@ namespace TwitchDesktopNotifications.Core
currentlyLive = following.Data;
}catch(System.Exception ex)
{
Environment.Exit(1);
MessageBox.Show("Twitch Connection not authenticated you need to Reconnect it.", "Twitch Notify");
}
}
@@ -142,8 +134,8 @@ namespace TwitchDesktopNotifications.Core
{
Dictionary<string, string> postData = new Dictionary<string, string>();
postData["client_id"] = TwitchClientID;
postData["client_secret"] = TwitchClientSecret;
postData["client_id"] = TwitchDetails.TwitchClientID;
postData["client_secret"] = TwitchDetails.TwitchClientSecret;
postData["grant_type"] = "refresh_token";
postData["refresh_token"] = DataStore.GetInstance().Store.Authentication.RefreshToken;
@@ -176,7 +168,7 @@ namespace TwitchDesktopNotifications.Core
WebServer.GetInstance().TwitchState = guid;
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = String.Format("https://id.twitch.tv/oauth2/authorize?&redirect_uri=http://localhost:32584/twitchRedirect&scope=user:read:subscriptions%20user:read:follows%20user:read:email%20openid&response_type=code&state={0}&nonce={1}&client_id={2}", guid, guid, TwitchClientID);
myProcess.StartInfo.FileName = String.Format("https://id.twitch.tv/oauth2/authorize?&redirect_uri=http://localhost:32584/twitchRedirect&scope=user:read:subscriptions%20user:read:follows%20user:read:email%20openid&response_type=code&state={0}&nonce={1}&client_id={2}", guid, guid, TwitchDetails.TwitchClientID);
myProcess.Start();
}
@@ -184,8 +176,8 @@ namespace TwitchDesktopNotifications.Core
{
Dictionary<string, string> postData = new Dictionary<string, string>();
postData["client_id"] = TwitchClientID;
postData["client_secret"] = TwitchClientSecret;
postData["client_id"] = TwitchDetails.TwitchClientID;
postData["client_secret"] = TwitchDetails.TwitchClientSecret;
postData["grant_type"] = "authorization_code";
postData["redirect_uri"] = "http://localhost:32584/twitchRedirect";
postData["code"] = code;

View File

@@ -40,6 +40,10 @@ namespace TwitchDesktopNotifications
String FileName = "store.json";
string fileContent = JsonSerializer.Serialize<JsonStructure.Store>(Store);
Console.WriteLine("I'm trying to save:");
Console.WriteLine(fileContent);
Console.WriteLine("to {0}", FilePath + "/" + FileName);
Directory.CreateDirectory(FilePath);
File.WriteAllText(FilePath + "/" + FileName, fileContent);
}

View File

@@ -1,5 +1,6 @@
// See https://aka.ms/new-console-template for more information
using System.Drawing;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Windows.Controls;
@@ -18,7 +19,6 @@ internal class Program
private static NotifyIcon notifyIcon;
private static ContextMenuStrip cms;
public static void Ws_CodeRecived(object? sender, EventArgs e)
{
ws.CodeRecived -= Ws_CodeRecived;
@@ -60,25 +60,22 @@ internal class Program
TwitchFetcher.GetInstance().BeginConnection();
if (DataStore.GetInstance().Store.Authentication == null)
{
var timerForCrash = new PeriodicTimer(TimeSpan.FromSeconds(10));
await timerForCrash.WaitForNextTickAsync();
if (isConnecting)
{
MessageBox.Show("Twitch Connection not authenticated Exiting for saftey.", "Twitch Notify");
notifyIcon.Visible = false;
notifyIcon.Dispose();
Environment.Exit(1);
MessageBox.Show("Twitch Connection not authenticated you need to Reconnect it.", "Twitch Notify");
}
}
}
private static async Task Main(string[] args)
{
try
{
var timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
notifyIcon = new NotifyIcon();
notifyIcon.Icon = new Icon("Assets/icon.ico");
notifyIcon.Text = "Notify";
notifyIcon.Text = "Twitch Notify";
cms = new ContextMenuStrip();
@@ -99,11 +96,18 @@ internal class Program
while (true)
{
Thread.Sleep(10000);
if (DataStore.GetInstance().Store != null)
{
TwitchFetcher.GetInstance().GetLiveFollowingUsers();
}
}
}).Start();
Application.Run();
}
catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
}

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
@@ -11,6 +11,7 @@
<AssemblyName>Twitch Notify</AssemblyName>
<UseWPF>True</UseWPF>
<UseWindowsForms>True</UseWindowsForms>
<UserSecretsId>2dfb7064-609b-41c3-a80d-a9e4d842a55d</UserSecretsId>
</PropertyGroup>
<ItemGroup>