mirror of
https://github.com/n00mkrad/flowframes.git
synced 2025-12-15 16:07:45 +01:00
Fix patron list text encoding & refactor a little
This commit is contained in:
@@ -2,83 +2,87 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Win32Interop.Structs;
|
||||
|
||||
namespace Flowframes.Ui
|
||||
{
|
||||
class GetWebInfo
|
||||
{
|
||||
public static async Task LoadNews (Label newsLabel)
|
||||
private static async Task<string> GetWebText(string url, System.Text.Encoding encoding = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
string url = $"https://raw.githubusercontent.com/n00mkrad/flowframes/main/changelog.txt";
|
||||
var client = new WebClient();
|
||||
var str = await client.DownloadStringTaskAsync(new Uri(url));
|
||||
newsLabel.Invoke(() => newsLabel.Text = str);
|
||||
using (var client = new WebClient { Encoding = encoding ?? System.Text.Encoding.UTF8 })
|
||||
{
|
||||
string text = await client.DownloadStringTaskAsync(new Uri(url));
|
||||
return text;
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Log($"Failed to load news: {e.Message}", true);
|
||||
Logger.Log($"Failed to load text from URL: {e.Message}", true);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task LoadNews (Label newsLabel)
|
||||
{
|
||||
string text = await GetWebText("https://raw.githubusercontent.com/n00mkrad/flowframes/main/changelog.txt");
|
||||
newsLabel.Invoke(() => newsLabel.Text = text);
|
||||
}
|
||||
|
||||
public static async Task LoadPatronListCsv(Label patronsLabel)
|
||||
{
|
||||
try
|
||||
{
|
||||
string url = $"https://raw.githubusercontent.com/n00mkrad/flowframes/main/patrons.csv";
|
||||
var client = new WebClient();
|
||||
var csvData = await client.DownloadStringTaskAsync(new Uri(url));
|
||||
patronsLabel.Invoke(() => patronsLabel.Text = ParsePatreonCsv(csvData));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Log($"Failed to load patreon CSV: {e.Message}", true);
|
||||
}
|
||||
string csvData = await GetWebText("https://raw.githubusercontent.com/n00mkrad/flowframes/main/patrons.csv");
|
||||
var patronsText = ParsePatreonCsv(csvData);
|
||||
patronsLabel.Invoke(() => patronsLabel.Text = patronsText);
|
||||
}
|
||||
|
||||
public static string ParsePatreonCsv(string csvData)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Logger.Log("Parsing Patrons from CSV...", true);
|
||||
List<string> goldPatrons = new List<string>();
|
||||
List<string> silverPatrons = new List<string>();
|
||||
string str = "Gold:\n";
|
||||
string[] lines = csvData.SplitIntoLines().Select(x => x.Replace(";", ",")).ToArray();
|
||||
string patronsStr = "";
|
||||
string[] lines = csvData.SplitIntoLines().Select(l => Regex.Replace(l, @";{2,}", ";").Replace(";", ",").Trim(',')).ToArray();
|
||||
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
string line = lines[i];
|
||||
string[] values = line.Split(',');
|
||||
if (i == 0 || line.Length < 10 || values.Length < 5) continue;
|
||||
string name = values[0].Trim();
|
||||
string status = values[4].Trim();
|
||||
string tier = values[9].Trim();
|
||||
string[] parts = line.Split(',');
|
||||
if (parts.Length < 3)
|
||||
continue;
|
||||
string name = parts[0].Trim().Trunc(45);
|
||||
string status = parts[1].Trim();
|
||||
string tier = parts[2].Trim();
|
||||
|
||||
if (status.Contains("Active"))
|
||||
{
|
||||
if (tier.Contains("Gold"))
|
||||
goldPatrons.Add(name.Trunc(30));
|
||||
if (!status.StartsWith("Active"))
|
||||
continue;
|
||||
|
||||
if (tier.Contains("Silver"))
|
||||
silverPatrons.Add(name.Trunc(30));
|
||||
}
|
||||
if (tier.Contains("Gold"))
|
||||
goldPatrons.Add(name);
|
||||
|
||||
if (tier.Contains("Silver"))
|
||||
silverPatrons.Add(name);
|
||||
}
|
||||
|
||||
// Logger.Log($"Found {goldPatrons.Count} Gold Patrons, {silverPatrons.Count} Silver Patrons", true);
|
||||
|
||||
foreach (string pat in goldPatrons)
|
||||
str += pat + "\n";
|
||||
if(goldPatrons.Count > 0)
|
||||
{
|
||||
patronsStr += $"Gold:\n{string.Join("\n", goldPatrons)}\n\n";
|
||||
}
|
||||
|
||||
str += "\nSilver:\n";
|
||||
if(silverPatrons.Count > 0)
|
||||
{
|
||||
patronsStr += $"Silver:\n{string.Join("\n", silverPatrons)}\n\n";
|
||||
}
|
||||
|
||||
foreach (string pat in silverPatrons)
|
||||
str += pat + "\n";
|
||||
|
||||
return str;
|
||||
return patronsStr;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user