-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
129 lines (121 loc) · 5.56 KB
/
Program.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using static System.ConsoleColor;
using System.Net;
using System.Text.RegularExpressions;
namespace Program
{
public static class Extentions
{
public static void _sout(this object _text, ConsoleColor col = White, bool newStr = true)
{
Console.ForegroundColor = col;
Console.Write($"{_text}{(newStr ? "\n" : "")}");
Console.ForegroundColor = White;
}
public static ConsoleColor rndColor
{
get
{
return (ConsoleColor)Program.rnd.Next(9, 16);
}
}
}
public class Achievement
{
public string name;
public string description;
public string currentProgress;
public string totalProgress;
}
public static class Program
{
public static Random rnd = new Random();
public static string GetTextBetween(string strSource, string strStart, string strEnd)
{
int Start, End;
if (strSource.Contains(strStart) && strSource.Contains(strEnd))
{
Start = strSource.IndexOf(strStart, 0) + strStart.Length;
End = strSource.IndexOf(strEnd, Start);
return strSource.Substring(Start, End - Start);
}
return string.Empty;
}
static void Main(string[] args)
{
$"Enter you steam url name (steam api-key not required so set public profile privacy): "._sout(Green, false);
var username = Console.ReadLine();
$"Enter app id you want to check achievements progress: "._sout(Blue, false);
var appid = Console.ReadLine();
string uri = $"https://steamcommunity.com/id/{username}/stats/{appid}/?tab=achievements";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
try
{
using var webResponse = request.GetResponse();
using var responseStreamReader = new StreamReader((webResponse as HttpWebResponse).GetResponseStream());
var htmlResult = responseStreamReader.ReadToEnd();
List<Achievement> achievements = new List<Achievement>();
string achievementName = string.Empty;
string achievementDescription = string.Empty;
foreach (var segment in htmlResult.Split(" / ").ToList().Select(x => Regex.Replace(x, @"\s*(<[^>]+>)\s*", "$1", RegexOptions.Singleline)))
{
var numStart = string.Empty;
var numEnd = string.Empty;
if (char.IsNumber(segment[0]))
for (int i = 0; i < segment.Length; i++)
{
char c = segment[i];
if (char.IsDigit(c) || c == ',' || c == ',')
numEnd += c;
else
break;
}
if (char.IsNumber(segment[segment.Length - 1]))
for (int i = segment.Length; i > 0; i--)
{
char c = segment[i - 1];
if (char.IsDigit(c) || c == ',' || c == ',')
numStart += c;
else
break;
}
numStart = new string(numStart.Reverse().ToArray());
if (segment.Contains("<div class=\"achieveTxt withProgress\"><h3 class=\"ellipsis\">"))
achievementName = GetTextBetween(segment, "<div class=\"achieveTxt withProgress\"><h3 class=\"ellipsis\">", "</h3>");
if (segment.Contains($"{achievementName}</h3><h5 class=\"ellipsis\">"))
achievementDescription = GetTextBetween(segment, $"{achievementName}</h3><h5 class=\"ellipsis\">", "</h5>");
bool numStartPresents = !string.IsNullOrWhiteSpace(numStart);
if (numStartPresents)
{
var achievement = new Achievement();
achievement.name = achievementName.Replace(""", "\"");
achievement.description = achievementDescription.Replace(""", "\"");
achievement.currentProgress = numStart;
achievements.Add(achievement);
}
if (!string.IsNullOrWhiteSpace(numEnd) && achievements.Count > 0)
achievements[achievements.Count - (numStartPresents ? 2 : 1)].totalProgress = numEnd;
}
$"Total achievements in progress: {achievements.Count}"._sout(Yellow);
for (int i = 0; i < achievements.Count; i++)
{
var achievement = achievements[i];
$"{i + 1}) [{achievement.currentProgress}/{achievement.totalProgress}] {achievement.name} ({achievement.description})"._sout(Cyan);
}
}
catch (WebException webExc)
{
try
{
using var exceptionStreamReader = new StreamReader(webExc.Response.GetResponseStream());
$"Request error: {exceptionStreamReader.ReadToEnd()}"._sout(Red);
}
catch (Exception exc)
{
$"Request exception: {exc.Message}"._sout(Red);
}
}
Console.ReadKey();
}
}
}