Skip to content

Commit e75f768

Browse files
committed
Updated to Version 1.0.0
1 parent 3d43a77 commit e75f768

11 files changed

+177
-72
lines changed

DiscordModNotifiyer/Apis/DiscordApi.cs

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using DiscordModNotifiyer.Events;
22
using DiscordModNotifiyer.Extensions;
3+
using DiscordModNotifiyer.Models;
34
using JNogueira.Discord.Webhook.Client;
45
using System;
6+
using System.Collections.Generic;
57
using System.Threading.Tasks;
68

79
namespace DiscordModNotifiyer.Apis
@@ -12,16 +14,32 @@ public async Task SendHook(object sender, UpdatedModsEventArgs e)
1214
{
1315
ConsoleExtensions.WriteColor(@$"[// ]Send {e.Mods.Count} Steam mods to discord...", ConsoleColor.DarkGreen);
1416

17+
// Get all Steam developer
18+
var developerSteamIds = new List<string>();
19+
foreach(var mod in e.Mods)
20+
{
21+
developerSteamIds.Add(mod.creator);
22+
}
23+
1524
foreach (var mod in e.Mods)
1625
{
1726
ConsoleExtensions.WriteColor(@$"[// ]Send {mod.title} ({mod.publishedfileid}) to discord...", ConsoleColor.DarkGreen);
1827

19-
var player = await SteamApi.GetSteamPlayer(mod.creator);
28+
SteamFileDetailJsonDetailModel collection;
29+
string collectionString = "";
30+
if (Program.Settings.SteamCollection)
31+
{
32+
collection = await SteamApi.GetCollectionInfo(Program.Settings.SteamCollectionId);
33+
collectionString = $" | Collection: {collection.title} (Id: {Program.Settings.SteamCollectionId})";
34+
}
35+
36+
var players = await SteamApi.GetSteamPlayers(developerSteamIds);
37+
var gamename = await SteamApi.GetGameInfo(mod.creator_app_id);
2038
var client = new DiscordWebhookClient(Program.Settings.DiscordWebHook);
2139
var message = new DiscordMessage(
22-
$"We got a new Mod Update for the Mod: {mod.title} ({mod.publishedfileid}) \n",
23-
username: $"{player.personaname} ({mod.creator})",
24-
avatarUrl: player.avatar,
40+
$"{gamename}{collectionString}\nMod: {mod.title} (Id: {mod.publishedfileid})",
41+
username: $"{players.Find(x => x.steamid.Equals(mod.creator))?.personaname ?? mod.creator}",
42+
avatarUrl: players.Find(x => x.steamid.Equals(mod.creator))?.avatar,
2543
tts: false,
2644
embeds: new[]
2745
{

DiscordModNotifiyer/Apis/SteamApi.cs

+85-20
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ class SteamApi
2222
public SteamApi() => RefreshSettings();
2323
public void RefreshSettings()
2424
{
25-
timer = new System.Timers.Timer(Program.Settings.AutomaticRefreshMin * 1000);
25+
timer = new System.Timers.Timer(Program.Settings.AutomaticRefreshMin * 60 * 1000);
2626
timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
2727
timer.Enabled = Program.Settings.AutomaticRefresh;
2828
}
2929
#endregion
3030

31-
public static async Task<SteamPlayerPlayerModel> GetSteamPlayer(string steamid)
31+
#region Static methods
32+
public static async Task<List<SteamPlayerPlayerModel>> GetSteamPlayers(List<string> steamids)
3233
{
33-
var request = WebRequest.Create(Program.STEAM_API_PLAYER_URL + Program.Settings.SteamApiKey + "&steamids=" + steamid.ToString());
34+
var request = WebRequest.Create(Program.STEAM_API_PLAYER_URL + Program.Settings.SteamApiKey + "&steamids=" + String.Join(",", steamids));
3435
request.Method = "GET";
3536

3637
using var webResponse = request.GetResponse();
@@ -40,19 +41,54 @@ public static async Task<SteamPlayerPlayerModel> GetSteamPlayer(string steamid)
4041
var data = await reader.ReadToEndAsync();
4142
var model = JsonConvert.DeserializeObject<SteamPlayerModel>(data);
4243

43-
return model.response.players.FirstOrDefault();
44+
return model.response.players;
4445
}
4546

46-
#region Executen Steam Web Api
47+
public static async Task<string> GetGameInfo(double appId)
48+
{
49+
var filename = "./gamelist.json";
50+
var reloadList = !File.Exists(filename) || File.GetLastWriteTime(filename).AddDays(1) < DateTime.Now;
51+
52+
if (reloadList)
53+
{
54+
var request = WebRequest.Create(Program.STEAM_API_GAME_LIST_URL);
55+
request.Method = "GET";
56+
57+
using var webResponse = request.GetResponse();
58+
using var webStream = webResponse.GetResponseStream();
59+
60+
using var reader = new StreamReader(webStream);
61+
var data = await reader.ReadToEndAsync();
62+
63+
using(StreamWriter sw = File.CreateText(filename))
64+
{
65+
sw.WriteLine(data);
66+
}
67+
}
68+
69+
var content = File.ReadAllText(filename);
70+
var apps = JsonConvert.DeserializeObject<GameInfo>(content).applist.apps;
71+
return apps.FirstOrDefault(x => x.appid.Equals(appId)).name ?? "Unknown Gamename";
72+
}
73+
74+
public static async Task<SteamFileDetailJsonDetailModel> GetCollectionInfo(double steamCollectionId)
75+
{
76+
var model = await GetPublishedFileDetails<SteamFileDetailJsonModel>(new List<double> { steamCollectionId });
77+
return model.response.publishedfiledetails.FirstOrDefault();
78+
}
79+
#endregion
80+
81+
#region Execute Steam Web Api
4782
private async void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e) => await UpdateSteamMods();
4883
public async Task UpdateSteamMods()
4984
{
85+
ConsoleExtensions.WriteColor(@"[//--Execute Refresh----------------------------------------------]", ConsoleColor.DarkGreen);
86+
5087
if (Program.Settings.SteamCollection)
5188
{
5289
ConsoleExtensions.WriteColor(@$"[// ]Checking Steam collection with id {Program.Settings.SteamCollectionId}", ConsoleColor.DarkGreen);
5390

5491
var parameters = new List<KeyValuePair<string, string>>();
55-
//parameters.Add(new KeyValuePair<string, string>("itemcount", "1"));
5692
parameters.Add(new KeyValuePair<string, string>("collectioncount", "1"));
5793
parameters.Add(new KeyValuePair<string, string>("publishedfileids[0]", Program.Settings.SteamCollectionId.ToString()));
5894

@@ -65,9 +101,6 @@ public async Task UpdateSteamMods()
65101

66102
HttpResponseMessage response = await httpClient.PostAsync(Program.STEAM_API_COLLECTION_URL, content);
67103

68-
//Console.WriteLine(await response.Content.ReadAsStringAsync());
69-
//return;
70-
71104
var model = JsonConvert.DeserializeObject<SteamCollectionModel>(await response.Content.ReadAsStringAsync());
72105
var modIds = model.response.collectiondetails.FirstOrDefault()?.children.Select(x => x.publishedfileid);
73106

@@ -86,9 +119,49 @@ private async Task CheckSteamMods(List<double> modIds)
86119
{
87120
ConsoleExtensions.WriteColor(@$"[// ]Checking {modIds.Count} Steam mods...", ConsoleColor.DarkGreen);
88121

122+
var filename = "./savedMods.json";
123+
var model = await GetPublishedFileDetails<SteamFileDetailJsonModel>(modIds);
124+
var needUpdateModels = new List<SteamFileDetailJsonDetailModel>();
125+
var savedMods = JsonConvert.DeserializeObject<List<LastEditModModel>>(File.ReadAllText(filename));
126+
127+
foreach(var mod in model.response.publishedfiledetails)
128+
{
129+
var sMod = savedMods.FirstOrDefault(x => x.ModId.Equals(mod.publishedfileid.ToString()));
130+
if (sMod == null || !sMod.LastUpdate.ToString().Equals(mod.time_updated.ToString()))
131+
{
132+
needUpdateModels.Add(mod);
133+
if (sMod == null)
134+
{
135+
savedMods.Add(new LastEditModModel
136+
{
137+
ModId = mod.publishedfileid.ToString(),
138+
LastUpdate = mod.time_updated.ToString()
139+
});
140+
}
141+
else
142+
{
143+
savedMods.Find(x => x.ModId.Equals(mod.publishedfileid.ToString())).LastUpdate = mod.time_updated.ToString();
144+
}
145+
}
146+
}
147+
148+
File.WriteAllText(filename, JsonConvert.SerializeObject(savedMods));
149+
150+
if(OnUpdatedModsFound != null)
151+
{
152+
OnUpdatedModsFound(this, new UpdatedModsEventArgs
153+
{
154+
Mods = needUpdateModels
155+
});
156+
}
157+
}
158+
#endregion
159+
160+
private static async Task<T> GetPublishedFileDetails<T>(List<double> files)
161+
{
89162
var parameters = new List<KeyValuePair<string, string>>();
90163
int i = 0;
91-
foreach (var id in modIds)
164+
foreach (var id in files)
92165
{
93166
parameters.Add(new KeyValuePair<string, string>($"publishedfileids[{i++}]", id.ToString()));
94167
}
@@ -101,18 +174,10 @@ private async Task CheckSteamMods(List<double> modIds)
101174
content.Headers.Clear();
102175
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
103176

104-
HttpResponseMessage response = await httpClient.PostAsync(Program.STEAM_API_MOD_URL, content);
177+
HttpResponseMessage response = await httpClient.PostAsync(Program.STEAM_API_FILE_DETAILS_URL, content);
105178

106-
var model = JsonConvert.DeserializeObject<SteamModJsonModel>(await response.Content.ReadAsStringAsync());
107-
if(OnUpdatedModsFound != null)
108-
{
109-
OnUpdatedModsFound(this, new UpdatedModsEventArgs
110-
{
111-
Mods = model.response.publishedfiledetails
112-
});
113-
}
179+
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
114180
}
115181
}
116-
#endregion
117182
}
118183
}

DiscordModNotifiyer/DiscordModNotifiyer.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21+
<None Update="savedMods.json">
22+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23+
</None>
2124
<None Update="settings.json">
2225
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2326
</None>

DiscordModNotifiyer/Events/UpdatedModsEventArgs.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace DiscordModNotifiyer.Events
66
{
77
public class UpdatedModsEventArgs : EventArgs
88
{
9-
public List<SteamModJsonDetailModel> Mods { get; set; }
9+
public List<SteamFileDetailJsonDetailModel> Mods { get; set; }
1010
}
1111
}

DiscordModNotifiyer/Extensions/ConsoleExtensions.cs

+21-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Linq;
32
using System.Reflection;
43
using System.Text.RegularExpressions;
54

@@ -14,13 +13,6 @@ public static void ClearConsole()
1413
{
1514
Console.Clear();
1615

17-
//if (string.IsNullOrEmpty(Settings.ProxyIp))
18-
//{
19-
// WriteColor(@"[//--Warning------------------------------------------------------]", ConsoleColor.Yellow);
20-
// WriteColor($"[// ProxyIp:] Value is not set", ConsoleColor.Yellow);
21-
// WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.Yellow);
22-
//}
23-
2416
Console.WriteLine(Environment.NewLine);
2517
WriteColor(@"[$$$$$$$$\ $$\ $$\ $$$$$$\ $$\]", ConsoleColor.DarkGreen);
2618
WriteColor(@"[$$ _____|\__| $$ | $$ __$$\ $$ |]", ConsoleColor.DarkGreen);
@@ -36,38 +28,38 @@ public static void ClearConsole()
3628
WriteColor($"[// Title:] {Assembly.GetEntryAssembly().GetName().Name}", ConsoleColor.DarkGreen);
3729
WriteColor($"[// Version:] {Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version}", ConsoleColor.DarkGreen);
3830
WriteColor($"[// Autor:] {Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyCopyrightAttribute>().Copyright}", ConsoleColor.DarkGreen);
39-
//WriteColor(@"[//--Exit Codes---------------------------------------------------]", ConsoleColor.DarkGreen);
40-
//WriteColor($"[// 0:] Application successful exited", ConsoleColor.DarkGreen);
41-
//WriteColor($"[// 1:] Supported OS is not given", ConsoleColor.DarkGreen);
31+
WriteColor(@"[//--Exit Codes---------------------------------------------------]", ConsoleColor.DarkGreen);
32+
WriteColor($"[// 0:] Application successful exited", ConsoleColor.DarkGreen);
33+
WriteColor($"[// 1:] Missing Steam API Key in the Settings.json", ConsoleColor.DarkGreen);
34+
WriteColor($"[// 2:] Missing Discord Web Hook in the Settings.json", ConsoleColor.DarkGreen);
4235
WriteColor(@"[//--Settings-----------------------------------------------------]", ConsoleColor.DarkGreen);
4336
WriteColor($"[// Automatic Refresh / Check:] {Program.Settings.AutomaticRefresh}", ConsoleColor.DarkGreen);
4437
WriteColor($"[// Automatic Refresh every (min):] {Program.Settings.AutomaticRefreshMin}", ConsoleColor.DarkGreen);
4538
WriteColor($"[// Check the Collection Id:] {Program.Settings.SteamCollection}", ConsoleColor.DarkGreen);
46-
var steamModIds = Program.Settings.SteamModIds.ToString();
47-
var ids = Program.Settings.SteamCollection ? Program.Settings.SteamCollectionId.ToString() : String.Join(", ", steamModIds);
39+
var ids = Program.Settings.SteamCollection ? Program.Settings.SteamCollectionId.ToString() : String.Join(", ", Program.Settings.SteamModIds.ToArray());
4840
WriteColor($"[// Collection Id or Mod Ids:] {ids}", ConsoleColor.DarkGreen);
4941
WriteColor(@"[//--Options------------------------------------------------------]", ConsoleColor.DarkGreen);
5042
WriteColor($"[// 1:] Execute Refresh", ConsoleColor.DarkGreen);
51-
WriteColor($"[// 2:] Enable / Disable automatic Refresh", ConsoleColor.DarkGreen);
52-
WriteColor($"[// 3:] Reload settings.json", ConsoleColor.DarkGreen);
43+
WriteColor($"[// 2:] Reload settings.json", ConsoleColor.DarkGreen);
5344
WriteColor($"[// ESC:] Close application", ConsoleColor.DarkGreen);
5445
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkGreen);
5546
Console.WriteLine(Environment.NewLine);
5647

57-
//if (string.IsNullOrEmpty(Settings.NetworkChangeAdapters))
58-
//{
59-
// WriteColor(@"[//--No Networkadapters-------------------------------------------]", ConsoleColor.DarkRed);
60-
// WriteColor($"[//:] Please insert Networkadapters (\"NetworkChangeAdapters\") in the Settings.json", ConsoleColor.DarkRed);
61-
// WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed);
62-
// if (!Debugger.IsAttached)
63-
// {
64-
// Environment.Exit(3);
65-
// }
66-
// else
67-
// {
68-
// Console.WriteLine(Environment.NewLine);
69-
// }
70-
//}
48+
if (string.IsNullOrEmpty(Program.Settings.SteamApiKey))
49+
{
50+
WriteColor(@"[//--No Steam API Key---------------------------------------------]", ConsoleColor.DarkRed);
51+
WriteColor($"[//:] Please insert a Steam API Key into the Settings.json", ConsoleColor.DarkRed);
52+
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed);
53+
Environment.Exit(1);
54+
}
55+
56+
if (string.IsNullOrEmpty(Program.Settings.SteamApiKey))
57+
{
58+
WriteColor(@"[//--No Discord Web Hook------------------------------------------]", ConsoleColor.DarkRed);
59+
WriteColor($"[//:] Please insert a Discord Web Hook into the Settings.json", ConsoleColor.DarkRed);
60+
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed);
61+
Environment.Exit(2);
62+
}
7163
}
7264

7365

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
3+
namespace DiscordModNotifiyer.Models
4+
{
5+
public class GameInfo
6+
{
7+
public GameInfoApps applist { get; set; }
8+
}
9+
10+
public class GameInfoApps
11+
{
12+
public List<GameInfoDetail> apps { get; set; }
13+
}
14+
15+
public class GameInfoDetail
16+
{
17+
public double appid { get; set; }
18+
public string name { get; set; }
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace DiscordModNotifiyer.Models
4+
{
5+
public class LastEditModModel
6+
{
7+
public string ModId { get; set; }
8+
public string LastUpdate { get; set; }
9+
}
10+
}

DiscordModNotifiyer/Models/SteamModJsonModel.cs DiscordModNotifiyer/Models/SteamFileDetailJsonModel.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
namespace DiscordModNotifiyer.Models
44
{
5-
public class SteamModJsonModel
5+
public class SteamFileDetailJsonModel
66
{
7-
public SteamModJsonResponseModel response { get; set; }
7+
public SteamFileDetailJsonResponseModel response { get; set; }
88
}
99

10-
public class SteamModJsonResponseModel
10+
public class SteamFileDetailJsonResponseModel
1111
{
1212
public int result { get; set; }
1313
public int resultcount { get; set; }
14-
public List<SteamModJsonDetailModel> publishedfiledetails { get; set; }
14+
public List<SteamFileDetailJsonDetailModel> publishedfiledetails { get; set; }
1515
}
1616

17-
public class SteamModJsonDetailModel
17+
public class SteamFileDetailJsonDetailModel
1818
{
1919
public double publishedfileid { get; set; }
2020
public int result { get; set; }

0 commit comments

Comments
 (0)