196 lines
6.1 KiB
C#
196 lines
6.1 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using System.Data.SQLite;
|
|
using System.Runtime.InteropServices;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
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");
|
|
|
|
static SQLiteConnection CreateConnection()
|
|
{
|
|
|
|
SQLiteConnection sqliteconn;
|
|
// Create a new database connection:
|
|
var sqllitedatasource = $"Data Source={folder_path}/database.db;Version=3;New=True;Compress=True;";
|
|
sqliteconn = new SQLiteConnection(sqllitedatasource);
|
|
// Open the connection:
|
|
try
|
|
{
|
|
sqliteconn.Open();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
return sqliteconn;
|
|
}
|
|
|
|
private static void Openit(string url)
|
|
{
|
|
try
|
|
{
|
|
Process.Start(url);
|
|
}
|
|
catch
|
|
{
|
|
// hack because of this: https://github.com/dotnet/corefx/issues/10361
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
{
|
|
url = url.Replace("&", "^&");
|
|
if (File.Exists(overrideBrowser))
|
|
{
|
|
string browser_path = File.ReadAllText(overrideBrowser);
|
|
Console.WriteLine("browser: "+browser_path);
|
|
Process.Start(new ProcessStartInfo(browser_path, url) { CreateNoWindow = true });
|
|
}
|
|
else {
|
|
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
|
|
}
|
|
|
|
}
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
{
|
|
Process.Start("xdg-open", url);
|
|
}
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
{
|
|
Process.Start("open", url);
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
//Process.Start(new ProcessStartInfo("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", url_generated) { CreateNoWindow = true });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private static bool CheckEpisodeAlreadyRun(string url)
|
|
{
|
|
if (File.Exists(filePath))
|
|
{
|
|
|
|
|
|
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
|
|
{
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private static void SaveIntoDatabase(SQLiteConnection conn,int season, int episode)
|
|
{
|
|
SQLiteCommand sqliteCmd;
|
|
sqliteCmd = conn.CreateCommand();
|
|
string updateQuery = $"update episodes set seen=1 WHERE season = {season} and episode = {episode}";
|
|
sqliteCmd.CommandText = updateQuery;
|
|
sqliteCmd.ExecuteNonQuery();
|
|
}
|
|
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
|
|
string urlGenerated;
|
|
int randomSeason;
|
|
int randomEpisode;
|
|
|
|
SQLiteConnection sqliteconn;
|
|
sqliteconn = CreateConnection();
|
|
bool restart = false;
|
|
do
|
|
{
|
|
restart = false;
|
|
|
|
//get maximum number of seasons
|
|
string maximumNumberOfSeasonsQuery = "SELECT COUNT(DISTINCT season) as result FROM episodes;";
|
|
int maximumNumberSeasons = Convert.ToInt32(RunSQLQuery(sqliteconn, maximumNumberOfSeasonsQuery));
|
|
|
|
|
|
//random for the season
|
|
Random rnd = new Random();
|
|
randomSeason = rnd.Next(1, maximumNumberSeasons);
|
|
|
|
Console.WriteLine("random season: "+randomSeason);
|
|
|
|
//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));
|
|
|
|
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));
|
|
|
|
|
|
//CheckEpisodeAlreadyRun
|
|
string checkEpisodeAlreadyRunQuery = $"SELECT seen as result FROM episodes WHERE season = {randomSeason} and episode = {randomEpisode};";
|
|
bool checkEpisodeAlreadyRun = Convert.ToBoolean(RunSQLQuery(sqliteconn, checkEpisodeAlreadyRunQuery));
|
|
|
|
if (checkEpisodeAlreadyRun)
|
|
{
|
|
//return;
|
|
restart = true;
|
|
Console.WriteLine("already watched");
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (restart);
|
|
Console.WriteLine("urlGenerated "+urlGenerated);
|
|
Openit(urlGenerated);
|
|
|
|
SaveIntoDatabase(sqliteconn,randomSeason,randomEpisode);
|
|
|
|
}
|
|
}
|