1- using System ;
1+ using DiscordModNotifiyer . Events ;
2+ using DiscordModNotifiyer . Extensions ;
3+ using DiscordModNotifiyer . Models ;
4+ using Newtonsoft . Json ;
5+ using System ;
26using System . Collections . Generic ;
7+ using System . IO ;
8+ using System . Linq ;
9+ using System . Net ;
310using System . Net . Http ;
411using System . Threading . Tasks ;
512
613namespace 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 }
0 commit comments