🎨 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
+32 -50
View File
@@ -9,20 +9,16 @@ namespace Simpsons_generator;
class Program 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"); private static readonly string FolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Simpsons_generator");
public static readonly string filePath = Path.Combine(folder_path, "episodes-watched.csv"); private static readonly string OverrideBrowser = Path.Combine(FolderPath, "overrideBrowser.xml");
public static readonly string overrideBrowser = Path.Combine(folder_path, "overrideBrowser.xml");
static SQLiteConnection CreateConnection() static SQLiteConnection CreateConnection()
{ {
SQLiteConnection sqliteconn; SQLiteConnection sqliteconn;
// Create a new database connection: // 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); sqliteconn = new SQLiteConnection(sqllitedatasource);
// Open the connection: // Open the connection:
try try
@@ -47,12 +43,13 @@ class Program
// hack because of this: https://github.com/dotnet/corefx/issues/10361 // hack because of this: https://github.com/dotnet/corefx/issues/10361
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
Console.WriteLine("Using Windows");
url = url.Replace("&", "^&"); url = url.Replace("&", "^&");
if (File.Exists(overrideBrowser)) if (File.Exists(OverrideBrowser))
{ {
string browser_path = File.ReadAllText(overrideBrowser); string browserPath = File.ReadAllText(OverrideBrowser);
Console.WriteLine("browser: "+browser_path); Console.WriteLine("browser: "+browserPath);
Process.Start(new ProcessStartInfo(browser_path, url) { CreateNoWindow = true }); Process.Start(new ProcessStartInfo(browserPath, url) { CreateNoWindow = true });
} }
else { else {
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
@@ -61,10 +58,21 @@ class Program
} }
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{ {
Process.Start("xdg-open", url); Console.WriteLine("Using Linux");
try
{
Process.Start("xdg-open", url);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
} }
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{ {
Console.WriteLine("Using Mac");
Process.Start("open", url); Process.Start("open", url);
} }
else else
@@ -79,42 +87,16 @@ class Program
} }
static object RunSqlQuery(SQLiteConnection conn, string query)
private static bool CheckEpisodeAlreadyRun(string url)
{ {
if (File.Exists(filePath)) SQLiteDataReader sqliteDatareader;
SQLiteCommand sqliteCmd;
sqliteCmd = conn.CreateCommand();
sqliteCmd.CommandText = query;
sqliteDatareader = sqliteCmd.ExecuteReader();
if (sqliteDatareader.Read())
{ {
return sqliteDatareader["result"];
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"];
} }
else else
{ {
@@ -150,7 +132,7 @@ class Program
//get maximum number of seasons //get maximum number of seasons
string maximumNumberOfSeasonsQuery = "SELECT COUNT(DISTINCT season) as result FROM episodes;"; 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 //random for the season
@@ -161,19 +143,19 @@ class Program
//get maximum number of episodes for this season //get maximum number of episodes for this season
string maximumNumberOfEpisodesQuery = $"SELECT COUNT() as result FROM episodes WHERE season = {randomSeason};"; 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); randomEpisode = rnd.Next(1, maximumNumberOfEpisodes);
Console.WriteLine("random episode: "+randomEpisode); Console.WriteLine("random episode: "+randomEpisode);
//url generated //url generated
string urlGeneratedQuery = $"SELECT url as result FROM episodes WHERE season = {randomSeason} and episode = {randomEpisode};"; 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 //CheckEpisodeAlreadyRun
string checkEpisodeAlreadyRunQuery = $"SELECT seen as result FROM episodes WHERE season = {randomSeason} and episode = {randomEpisode};"; 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) if (checkEpisodeAlreadyRun)
{ {