This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamJson.cs
More file actions
93 lines (87 loc) · 3.06 KB
/
SteamJson.cs
File metadata and controls
93 lines (87 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.Globalization;
using System.Text.Json;
namespace SteamCurrency
{
#pragma warning disable IDE1006, CA1707, CS8600, CS8603, CS8604
public class SteamJson
{
public bool success { get; set; }
public string lowest_price { get; set; }
public string volume { get; set; }
public string median_price { get; set; }
public SteamJson()
{
success = false;
lowest_price = string.Empty;
volume = string.Empty;
median_price = string.Empty;
}
// 1 - доллары, 5 - рубли, 37 - тенге (https://partner.steamgames.com/doc/store/pricing/currencies)
public static SteamJson GetData(int CurrencyCode, string item = "M4A1-S | Hyper Beast (Factory New)", int gameId = 730)
{
HttpResponseMessage response;
try
{
response = SCHttpClient.Client.GetAsync($"https://steamcommunity.com/market/priceoverview/?country=RU¤cy={CurrencyCode}&appid={gameId}&market_hash_name={item}").Result;
}
catch
{
return new SteamJson();
}
string text = response.Content.ReadAsStringAsync().Result;
SteamJson json = JsonSerializer.Deserialize<SteamJson>(text);
return json;
}
public static float GetRate(int from = 1, int to = 5)
{
float rate;
SteamJson jsonUSD = GetData(from);
SteamJson jsonRUB = GetData(to);
if (jsonUSD?.success == false || jsonRUB?.success == false)
{
rate = 0;
}
else
{
rate = TrimData(jsonRUB, to) / TrimData(jsonUSD, from);
if (rate == 1)
rate = 0;
}
return rate;
}
private static float TrimData(SteamJson rawText, int id)
{
string text;
float rate;
switch (id)
{
case 1:
text = rawText.lowest_price[1..^3];
rate = Convert.ToSingle(text, new CultureInfo("en-US"));
break;
case 5:
text = rawText.lowest_price[..^5];
rate = Convert.ToSingle(text, new CultureInfo("ru-RU"));
break;
case 37:
text = rawText.lowest_price[..^1];
text = text.Replace(" ", "");
rate = Convert.ToSingle(text, new CultureInfo("ru-RU"));
break;
default:
text = rawText.lowest_price;
try
{
rate = Convert.ToSingle(text, CultureInfo.InvariantCulture);
}
catch
{
rate = 1;
}
break;
}
return rate;
}
}
#pragma warning restore IDE1006, CA1707, CS8600, CS8603, CS8604
}