using Newtonsoft.Json;
using SqlSugar;
using System.Net;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Threading.Tasks;
using RainOpsMini.Models;
namespace RainOpsMini.Helpers
{
public class SteamHelper
{
///
/// 获取个人首页html代码
///
///
public static string GetSteamHtml(string steamid)
{
try
{
string url = "https://steamcommunity.com/profiles/" + steamid + "/";
string strHTML = "";
// Use HttpClient instead of WebClient for modern async support if needed, but keeping sync for compatibility
using (WebClient myWebClient = new WebClient())
{
myWebClient.Encoding = Encoding.UTF8;
strHTML = myWebClient.DownloadString(url);
}
return strHTML;
}
catch (Exception)
{
return "";
}
}
private static readonly HttpClient _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
private static string SteamApiKey => Environment.GetEnvironmentVariable("STEAM_API_KEY") ?? System.Configuration.ConfigurationManager.AppSettings["SteamApiKey"] ?? string.Empty;
///
/// 异步获取Squad总游戏时长(分钟)
/// 使用 GetOwnedGames 接口以获取准确的总时长
///
public static async Task GetSquadPlaytimeAsync(string steamid)
{
if (string.IsNullOrEmpty(steamid)) return 0;
try
{
if (string.IsNullOrWhiteSpace(SteamApiKey)) return 0;
string url = $"http://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key={SteamApiKey}&steamid={steamid}&format=json&appids_filter[0]=393380";
var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode) return 0;
var json = await response.Content.ReadAsStringAsync();
// Simple parsing to avoid dependency on complex models if not needed
// Or use dynamic/JsonDocument
using (var doc = System.Text.Json.JsonDocument.Parse(json))
{
if (doc.RootElement.TryGetProperty("response", out var resp) &&
resp.TryGetProperty("games", out var games) &&
games.GetArrayLength() > 0)
{
// Get play_time_forever (minutes)
return games[0].GetProperty("playtime_forever").GetInt32();
}
}
return 0;
}
catch (Exception ex)
{
Console.WriteLine($"[SteamHelper] Error fetching playtime for {steamid}: {ex.Message}");
return 0;
}
}
///
/// 获取Squad运行时长(小时) - Deprecated, kept for compatibility
///
///
///
public static int GetSquadRunTime(string steamid)
{
try
{
string token = SteamApiKey;
if (string.IsNullOrWhiteSpace(token)) return 0;
string getinfo = GetHttpResponse("http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v1/?key=" + token + "&steamid=" + steamid, 8000);
Recently Recently = JsonConvert.DeserializeObject>(getinfo);
if (Recently.response == null)
{
return 0;
}
if (Recently.response.games == null)
{
return 0;
}
List SquadGameItem = Recently.response.games.Where(c => c.appid == 393380).ToList();
if (SquadGameItem != null && SquadGameItem.Count > 0)
{
return SquadGameItem[0].playtime_forever / 60;
}
else
{
return 0;
}
}
catch (Exception e)
{
Console.WriteLine("Squad时长查询失败:" + steamid + e.Message);
return 0;
}
}
///
/// GET请求
///
///
///
///
public static string GetHttpResponse(string url, int Timeout)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = Timeout;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
}
}