@@ -4,6 +4,7 @@ namespace BD.SteamClient.Services.Implementation;
44using AngleSharp . Dom ;
55using AngleSharp . Html . Dom ;
66using AngleSharp . Html . Parser ;
7+ using BD . SteamClient . Constants ;
78using BD . SteamClient . Models . Idle ;
89using Nito . Comparers . Linq ;
910using System . Linq ;
@@ -210,6 +211,52 @@ public async Task<IEnumerable<CardsMarketPrice>> GetCardsMarketPrice(uint appId,
210211 }
211212 return Enumerable . Empty < CardsMarketPrice > ( ) ;
212213 }
214+
215+ public async Task < ( IReadOnlyCollection < uint > appIds , HttpStatusCode status ) > GetPrivateGameAppIdsAsync ( string steam_id )
216+ {
217+ var steamSession = _sessionService . RentSession ( steam_id ) ;
218+ if ( steamSession == null )
219+ throw new Exception ( $ "Unable to find session for { steam_id } , pelese login first") ;
220+
221+ try
222+ {
223+ var url = SteamApiUrls . STEAM_PROFILE_GAMES_ALL_URL . Format ( steamSession . SteamId ) ;
224+ var response = await steamSession . HttpClient ! . GetAsync ( url ) ;
225+ if ( ! response . IsSuccessStatusCode )
226+ return ( Array . Empty < uint > ( ) , response . StatusCode ) ;
227+
228+ var html = await response . Content . ReadAsStringAsync ( ) ;
229+ var match = Regex . Match ( html , @"var\s+rgGames\s*=\s*(\[[\s\S]*?\]);" ) ;
230+ if ( ! match . Success )
231+ return ( Array . Empty < uint > ( ) , HttpStatusCode . OK ) ;
232+
233+ var privateAppIds = new HashSet < uint > ( ) ;
234+ using var document = JsonDocument . Parse ( match . Groups [ 1 ] . Value ) ;
235+ if ( document . RootElement . ValueKind == JsonValueKind . Array )
236+ {
237+ foreach ( var item in document . RootElement . EnumerateArray ( ) )
238+ {
239+ if ( ! item . TryGetProperty ( "appid" , out var appidValue ) )
240+ continue ;
241+ if ( ! TryGetUInt32 ( appidValue , out var appId ) )
242+ continue ;
243+
244+ if ( item . TryGetProperty ( "is_private" , out var isPrivateValue )
245+ && IsTrueJsonValue ( isPrivateValue ) )
246+ {
247+ privateAppIds . Add ( appId ) ;
248+ }
249+ }
250+ }
251+
252+ return ( privateAppIds . ToArray ( ) , HttpStatusCode . OK ) ;
253+ }
254+ catch ( Exception ex )
255+ {
256+ Log . Warn ( nameof ( GetPrivateGameAppIdsAsync ) , ex , "获取私密游戏列表失败" ) ;
257+ return ( Array . Empty < uint > ( ) , HttpStatusCode . InternalServerError ) ;
258+ }
259+ }
213260 #endregion
214261
215262 #region Private
@@ -325,5 +372,28 @@ SteamCard GetCard(IElement element)
325372 return card ;
326373 }
327374 }
375+
376+ private static bool IsTrueJsonValue ( JsonElement value )
377+ {
378+ return value . ValueKind switch
379+ {
380+ JsonValueKind . True => true ,
381+ JsonValueKind . False => false ,
382+ JsonValueKind . Number => value . TryGetInt32 ( out var v ) && v != 0 ,
383+ JsonValueKind . String => bool . TryParse ( value . GetString ( ) , out var v ) && v ,
384+ _ => false ,
385+ } ;
386+ }
387+
388+ private static bool TryGetUInt32 ( JsonElement value , out uint appId )
389+ {
390+ if ( value . ValueKind == JsonValueKind . Number && value . TryGetUInt32 ( out appId ) )
391+ return true ;
392+ if ( value . ValueKind == JsonValueKind . String && uint . TryParse ( value . GetString ( ) , out appId ) )
393+ return true ;
394+
395+ appId = default ;
396+ return false ;
397+ }
328398 #endregion
329399}
0 commit comments