Skip to content

Commit 3d43a77

Browse files
author
Lukas Gundermann
committed
Added Steam and Discord Api with parsing json web requests
1 parent ca5694b commit 3d43a77

11 files changed

+262
-21
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using DiscordModNotifiyer.Events;
2+
using DiscordModNotifiyer.Extensions;
3+
using JNogueira.Discord.Webhook.Client;
4+
using System;
5+
using System.Threading.Tasks;
6+
7+
namespace DiscordModNotifiyer.Apis
8+
{
9+
public class DiscordApi
10+
{
11+
public async Task SendHook(object sender, UpdatedModsEventArgs e)
12+
{
13+
ConsoleExtensions.WriteColor(@$"[// ]Send {e.Mods.Count} Steam mods to discord...", ConsoleColor.DarkGreen);
14+
15+
foreach (var mod in e.Mods)
16+
{
17+
ConsoleExtensions.WriteColor(@$"[// ]Send {mod.title} ({mod.publishedfileid}) to discord...", ConsoleColor.DarkGreen);
18+
19+
var player = await SteamApi.GetSteamPlayer(mod.creator);
20+
var client = new DiscordWebhookClient(Program.Settings.DiscordWebHook);
21+
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,
25+
tts: false,
26+
embeds: new[]
27+
{
28+
new DiscordMessageEmbed(
29+
mod.title,
30+
color: 0,
31+
url: $"https://steamcommunity.com/sharedfiles/filedetails/?id={mod.publishedfileid}",
32+
fields: new[]
33+
{
34+
new DiscordMessageEmbedField("Last Update", TimeExtensions.UnixTimeStampToDateTime(mod.time_updated).ToString()),
35+
new DiscordMessageEmbedField("Created", TimeExtensions.UnixTimeStampToDateTime(mod.time_created).ToString()),
36+
new DiscordMessageEmbedField("Views", mod.views.ToString()),
37+
new DiscordMessageEmbedField("Subscriptions", mod.subscriptions.ToString())
38+
},
39+
thumbnail: new DiscordMessageEmbedThumbnail(mod.preview_url),
40+
footer: new DiscordMessageEmbedFooter("(c) by L. Gmann")
41+
)
42+
}
43+
);
44+
await client.SendToDiscord(message);
45+
}
46+
47+
ConsoleExtensions.WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkGreen);
48+
}
49+
}
50+
}

DiscordModNotifiyer/Apis/SteamApi.cs

+65-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
using System;
1+
using DiscordModNotifiyer.Events;
2+
using DiscordModNotifiyer.Extensions;
3+
using DiscordModNotifiyer.Models;
4+
using Newtonsoft.Json;
5+
using System;
26
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Net;
310
using System.Net.Http;
411
using System.Threading.Tasks;
512

613
namespace DiscordModNotifiyer.Apis
714
{
815
class SteamApi
916
{
17+
public event EventHandler<UpdatedModsEventArgs> OnUpdatedModsFound;
18+
1019
private System.Timers.Timer timer;
1120

1221
#region Load / Refresh Timer
@@ -19,17 +28,34 @@ public void RefreshSettings()
1928
}
2029
#endregion
2130

31+
public static async Task<SteamPlayerPlayerModel> GetSteamPlayer(string steamid)
32+
{
33+
var request = WebRequest.Create(Program.STEAM_API_PLAYER_URL + Program.Settings.SteamApiKey + "&steamids=" + steamid.ToString());
34+
request.Method = "GET";
35+
36+
using var webResponse = request.GetResponse();
37+
using var webStream = webResponse.GetResponseStream();
38+
39+
using var reader = new StreamReader(webStream);
40+
var data = await reader.ReadToEndAsync();
41+
var model = JsonConvert.DeserializeObject<SteamPlayerModel>(data);
42+
43+
return model.response.players.FirstOrDefault();
44+
}
45+
2246
#region Executen Steam Web Api
2347
private async void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e) => await UpdateSteamMods();
2448
public async Task UpdateSteamMods()
2549
{
2650
if (Program.Settings.SteamCollection)
2751
{
52+
ConsoleExtensions.WriteColor(@$"[// ]Checking Steam collection with id {Program.Settings.SteamCollectionId}", ConsoleColor.DarkGreen);
53+
2854
var parameters = new List<KeyValuePair<string, string>>();
55+
//parameters.Add(new KeyValuePair<string, string>("itemcount", "1"));
2956
parameters.Add(new KeyValuePair<string, string>("collectioncount", "1"));
3057
parameters.Add(new KeyValuePair<string, string>("publishedfileids[0]", Program.Settings.SteamCollectionId.ToString()));
31-
Console.WriteLine(Program.STEAM_API_COLLECTION_URL);
32-
Console.WriteLine(Program.Settings.SteamCollectionId.ToString());
58+
3359
var httpClient = new HttpClient();
3460

3561
using (var content = new FormUrlEncodedContent(parameters))
@@ -39,30 +65,51 @@ public async Task UpdateSteamMods()
3965

4066
HttpResponseMessage response = await httpClient.PostAsync(Program.STEAM_API_COLLECTION_URL, content);
4167

42-
Console.WriteLine(await response.Content.ReadAsStringAsync());
68+
//Console.WriteLine(await response.Content.ReadAsStringAsync());
69+
//return;
70+
71+
var model = JsonConvert.DeserializeObject<SteamCollectionModel>(await response.Content.ReadAsStringAsync());
72+
var modIds = model.response.collectiondetails.FirstOrDefault()?.children.Select(x => x.publishedfileid);
73+
74+
await CheckSteamMods(modIds.ToList());
4375
}
4476
}
4577
else
4678
{
47-
var parameters = new List<KeyValuePair<string, string>>();
48-
parameters.Add(new KeyValuePair<string, string>("itemcount", "1"));
79+
await CheckSteamMods(Program.Settings.SteamModIds);
80+
}
81+
}
82+
#endregion
4983

50-
int i = 0;
51-
foreach (var id in Program.Settings.SteamModIds)
52-
{
53-
parameters.Add(new KeyValuePair<string, string>($"publishedfileids[{i++}]", id.ToString()));
54-
}
84+
#region Check Mods for an Update
85+
private async Task CheckSteamMods(List<double> modIds)
86+
{
87+
ConsoleExtensions.WriteColor(@$"[// ]Checking {modIds.Count} Steam mods...", ConsoleColor.DarkGreen);
5588

56-
var httpClient = new HttpClient();
89+
var parameters = new List<KeyValuePair<string, string>>();
90+
int i = 0;
91+
foreach (var id in modIds)
92+
{
93+
parameters.Add(new KeyValuePair<string, string>($"publishedfileids[{i++}]", id.ToString()));
94+
}
95+
parameters.Add(new KeyValuePair<string, string>("itemcount", i.ToString()));
5796

58-
using (var content = new FormUrlEncodedContent(parameters))
59-
{
60-
content.Headers.Clear();
61-
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
97+
var httpClient = new HttpClient();
6298

63-
HttpResponseMessage response = await httpClient.PostAsync(Program.STEAM_API_MOD_URL, content);
99+
using (var content = new FormUrlEncodedContent(parameters))
100+
{
101+
content.Headers.Clear();
102+
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
103+
104+
HttpResponseMessage response = await httpClient.PostAsync(Program.STEAM_API_MOD_URL, content);
64105

65-
Console.WriteLine(await response.Content.ReadAsStringAsync());
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+
});
66113
}
67114
}
68115
}

DiscordModNotifiyer/DiscordModNotifiyer.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16+
<PackageReference Include="discord-webhook-client" Version="3.1.0" />
1617
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
1718
</ItemGroup>
1819

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using DiscordModNotifiyer.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace DiscordModNotifiyer.Events
6+
{
7+
public class UpdatedModsEventArgs : EventArgs
8+
{
9+
public List<SteamModJsonDetailModel> Mods { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace DiscordModNotifiyer.Extensions
4+
{
5+
public class TimeExtensions
6+
{
7+
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
8+
{
9+
// Unix timestamp is seconds past epoch
10+
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
11+
dateTime = dateTime.AddSeconds(unixTimeStamp).ToLocalTime();
12+
return dateTime;
13+
}
14+
}
15+
}

DiscordModNotifiyer/Models/Settings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace DiscordModNotifiyer.Models
44
{
55
public class Settings
66
{
7+
public string SteamApiKey { get; set; }
78
public bool AutomaticRefresh { get; set; }
89
public int AutomaticRefreshMin { get; set; }
910

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
3+
namespace DiscordModNotifiyer.Models
4+
{
5+
public class SteamCollectionModel
6+
{
7+
public SteamCollectionResponseModel response { get; set; }
8+
}
9+
10+
public class SteamCollectionResponseModel
11+
{
12+
public int result { get; set; }
13+
public int resultcount { get; set; }
14+
public List<SteamCollectionDetailModel> collectiondetails { get; set; }
15+
}
16+
17+
public class SteamCollectionDetailModel
18+
{
19+
public double publishedfileid { get; set; }
20+
public int result { get; set; }
21+
public List<SteamCollectionChildren> children { get; set; }
22+
}
23+
24+
public class SteamCollectionChildren
25+
{
26+
public double publishedfileid { get; set; }
27+
public int sortorder { get; set; }
28+
public int filetype { get; set; }
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
3+
namespace DiscordModNotifiyer.Models
4+
{
5+
public class SteamModJsonModel
6+
{
7+
public SteamModJsonResponseModel response { get; set; }
8+
}
9+
10+
public class SteamModJsonResponseModel
11+
{
12+
public int result { get; set; }
13+
public int resultcount { get; set; }
14+
public List<SteamModJsonDetailModel> publishedfiledetails { get; set; }
15+
}
16+
17+
public class SteamModJsonDetailModel
18+
{
19+
public double publishedfileid { get; set; }
20+
public int result { get; set; }
21+
public string creator { get; set; }
22+
public double creator_app_id { get; set; }
23+
public double consumer_app_id { get; set; }
24+
public string filename { get; set; }
25+
public double file_size { get; set; }
26+
public string file_url { get; set; }
27+
public string hcontent_file { get; set; }
28+
public string preview_url { get; set; }
29+
public string hcontent_url { get; set; }
30+
public string title { get; set; }
31+
public string description { get; set; }
32+
public double time_created { get; set; }
33+
public double time_updated { get; set; }
34+
public bool visibility { get; set; }
35+
public bool banned { get; set; }
36+
public string ban_reason { get; set; }
37+
public double subscriptions { get; set; }
38+
public double favorited { get; set; }
39+
public double lifetime_subscriptions { get; set; }
40+
public double lifetime_favorited { get; set; }
41+
public double views { get; set; }
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Generic;
2+
3+
namespace DiscordModNotifiyer.Models
4+
{
5+
public class SteamPlayerModel
6+
{
7+
public SteamPlayerResponseModel response { get; set; }
8+
}
9+
10+
public class SteamPlayerResponseModel
11+
{
12+
public List<SteamPlayerPlayerModel> players { get; set; }
13+
}
14+
15+
public class SteamPlayerPlayerModel
16+
{
17+
public string steamid { get; set; }
18+
public int communityvisibilitystate { get; set; }
19+
public int profilstate { get; set; }
20+
public string personaname { get; set; }
21+
public string profileurl { get; set; }
22+
public string avatar { get; set; }
23+
public string avatarmedium { get; set; }
24+
public string avatarfull { get; set; }
25+
public string avatarhash { get; set; }
26+
public int personastate { get; set; }
27+
public string primaryclanid { get; set; }
28+
public double timecreated { get; set; }
29+
public int personastateflags { get; set; }
30+
}
31+
}

DiscordModNotifiyer/Program.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DiscordModNotifiyer.Apis;
22
using DiscordModNotifiyer.Extensions;
33
using DiscordModNotifiyer.Models;
4+
using JNogueira.Discord.Webhook.Client;
45
using Newtonsoft.Json;
56
using System;
67
using System.Collections.Generic;
@@ -28,6 +29,11 @@ class Program
2829
/// </summary>
2930
public const string STEAM_API_MOD_URL = "https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1/?format=json";
3031

32+
/// <summary>
33+
/// Steam Api Link for a player information
34+
/// </summary>
35+
public const string STEAM_API_PLAYER_URL = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002?key=";
36+
3137
/// <summary>
3238
/// Settings json file
3339
/// </summary>
@@ -40,7 +46,11 @@ class Program
4046
static void Main(string[] args)
4147
{
4248
Settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(SETTINGS_FILENAME));
49+
4350
var steamApi = new SteamApi();
51+
var discordApi = new DiscordApi();
52+
53+
steamApi.OnUpdatedModsFound += (sender, e) => _ = discordApi.SendHook(sender, e);
4454

4555
ConsoleExtensions.ClearConsole();
4656

@@ -52,10 +62,11 @@ static void Main(string[] args)
5262
switch (cki.KeyChar)
5363
{
5464
case '1':
65+
ConsoleExtensions.WriteColor(@"[//--Execute Refresh----------------------------------------------]", ConsoleColor.DarkGreen);
5566
_ = steamApi.UpdateSteamMods();
5667
break;
5768
case '2':
58-
69+
5970
break;
6071
case '3':
6172
ReloadSettings();

DiscordModNotifiyer/settings.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"__General": "General settings",
3+
"SteamApiKey": "B23067EC4C7E972A901F43C617CB8DE4",
34
"AutomaticRefresh": false,
45
"AutomaticRefreshMin": 10,
56

67
"__Discord": "This Area is for discord settings",
78
"DiscordWebHook": "https://discord.com/api/webhooks/885800670299574282/d4axeqS_qErnk-EKyMEMZcZCrlWH2QFI1PnmcV96aJsDeuisz8EU1t0dYwE4a8p5ZClr",
89

910
"__Steam": "This Area is for steam settings",
10-
"SteamCollection": true,
11+
"SteamCollection": false,
1112
"SteamCollectionId": 2512034643,
12-
"SteamModIds": [ 123, 456 ]
13+
"SteamModIds": [ 1551907938 ]
1314
}

0 commit comments

Comments
 (0)