🎨 refactoring
Gitea Actions Demo / Explore-Gitea-Actions (8.0.x) (push) Successful in 1m33s

This commit is contained in:
2025-06-09 22:42:08 +00:00
parent 45f318d8f2
commit 80951ff41c
+31 -49
View File
@@ -9,20 +9,16 @@ namespace Simpsons_generator;
class Program
{
/* public static void openit(string x) {
System.Diagnostics.Process.Start("cmd", "/C start" + " " + x);
}*/
public static readonly string folder_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Simpsons_generator");
public static readonly string filePath = Path.Combine(folder_path, "episodes-watched.csv");
public static readonly string overrideBrowser = Path.Combine(folder_path, "overrideBrowser.xml");
private static readonly string FolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Simpsons_generator");
private static readonly string OverrideBrowser = Path.Combine(FolderPath, "overrideBrowser.xml");
static SQLiteConnection CreateConnection()
{
SQLiteConnection sqliteconn;
// Create a new database connection:
var sqllitedatasource = $"Data Source={folder_path}/database.db;Version=3;New=True;Compress=True;";
var sqllitedatasource = $"Data Source={FolderPath}/database.db;Version=3;New=True;Compress=True;";
sqliteconn = new SQLiteConnection(sqllitedatasource);
// Open the connection:
try
@@ -47,12 +43,13 @@ class Program
// hack because of this: https://github.com/dotnet/corefx/issues/10361
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Console.WriteLine("Using Windows");
url = url.Replace("&", "^&");
if (File.Exists(overrideBrowser))
if (File.Exists(OverrideBrowser))
{
string browser_path = File.ReadAllText(overrideBrowser);
Console.WriteLine("browser: "+browser_path);
Process.Start(new ProcessStartInfo(browser_path, url) { CreateNoWindow = true });
string browserPath = File.ReadAllText(OverrideBrowser);
Console.WriteLine("browser: "+browserPath);
Process.Start(new ProcessStartInfo(browserPath, url) { CreateNoWindow = true });
}
else {
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
@@ -60,11 +57,22 @@ class Program
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Console.WriteLine("Using Linux");
try
{
Process.Start("xdg-open", url);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Console.WriteLine("Using Mac");
Process.Start("open", url);
}
else
@@ -79,42 +87,16 @@ class Program
}
private static bool CheckEpisodeAlreadyRun(string url)
static object RunSqlQuery(SQLiteConnection conn, string query)
{
if (File.Exists(filePath))
SQLiteDataReader sqliteDatareader;
SQLiteCommand sqliteCmd;
sqliteCmd = conn.CreateCommand();
sqliteCmd.CommandText = query;
sqliteDatareader = sqliteCmd.ExecuteReader();
if (sqliteDatareader.Read())
{
var linesRead = File.ReadLines(filePath);
// iterate through each element within the array and
// print it out
//
foreach (var lineRead in linesRead)
{
if (lineRead == url)
{
Console.WriteLine("already watched");
return true;
}
}
}
return false;
}
static object RunSQLQuery(SQLiteConnection conn, string query)
{
SQLiteDataReader sqlite_datareader;
SQLiteCommand sqlite_cmd;
sqlite_cmd = conn.CreateCommand();
sqlite_cmd.CommandText = query;
sqlite_datareader = sqlite_cmd.ExecuteReader();
if (sqlite_datareader.Read())
{
return sqlite_datareader["result"];
return sqliteDatareader["result"];
}
else
{
@@ -150,7 +132,7 @@ class Program
//get maximum number of seasons
string maximumNumberOfSeasonsQuery = "SELECT COUNT(DISTINCT season) as result FROM episodes;";
int maximumNumberSeasons = Convert.ToInt32(RunSQLQuery(sqliteconn, maximumNumberOfSeasonsQuery));
int maximumNumberSeasons = Convert.ToInt32(RunSqlQuery(sqliteconn, maximumNumberOfSeasonsQuery));
//random for the season
@@ -161,19 +143,19 @@ class Program
//get maximum number of episodes for this season
string maximumNumberOfEpisodesQuery = $"SELECT COUNT() as result FROM episodes WHERE season = {randomSeason};";
int maximumNumberOfEpisodes = Convert.ToInt32(RunSQLQuery(sqliteconn, maximumNumberOfEpisodesQuery));
int maximumNumberOfEpisodes = Convert.ToInt32(RunSqlQuery(sqliteconn, maximumNumberOfEpisodesQuery));
randomEpisode = rnd.Next(1, maximumNumberOfEpisodes);
Console.WriteLine("random episode: "+randomEpisode);
//url generated
string urlGeneratedQuery = $"SELECT url as result FROM episodes WHERE season = {randomSeason} and episode = {randomEpisode};";
urlGenerated = Convert.ToString(RunSQLQuery(sqliteconn, urlGeneratedQuery));
urlGenerated = Convert.ToString(RunSqlQuery(sqliteconn, urlGeneratedQuery));
//CheckEpisodeAlreadyRun
string checkEpisodeAlreadyRunQuery = $"SELECT seen as result FROM episodes WHERE season = {randomSeason} and episode = {randomEpisode};";
bool checkEpisodeAlreadyRun = Convert.ToBoolean(RunSQLQuery(sqliteconn, checkEpisodeAlreadyRunQuery));
bool checkEpisodeAlreadyRun = Convert.ToBoolean(RunSqlQuery(sqliteconn, checkEpisodeAlreadyRunQuery));
if (checkEpisodeAlreadyRun)
{