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