mirror of
https://gitee.com/xiarenalofs/squad-rain-ops-mini.git
synced 2026-08-06 13:26:25 +08:00
160 lines
5.4 KiB
C#
160 lines
5.4 KiB
C#
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
|
||
{
|
||
|
||
/// <summary>
|
||
/// 获取个人首页html代码
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
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;
|
||
|
||
/// <summary>
|
||
/// 异步获取Squad总游戏时长(分钟)
|
||
/// 使用 GetOwnedGames 接口以获取准确的总时长
|
||
/// </summary>
|
||
public static async Task<int> 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;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取Squad运行时长(小时) - Deprecated, kept for compatibility
|
||
/// </summary>
|
||
/// <param name="steamid"></param>
|
||
/// <returns></returns>
|
||
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<Games> Recently = JsonConvert.DeserializeObject<Recently<Games>>(getinfo);
|
||
|
||
if (Recently.response == null)
|
||
{
|
||
return 0;
|
||
}
|
||
if (Recently.response.games == null)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
|
||
List<GamesItem> 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;
|
||
}
|
||
|
||
|
||
}
|
||
/// <summary>
|
||
/// GET请求
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="Timeout"></param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
|
||
|
||
}
|
||
}
|