This commit is contained in:
+195
-97
@@ -1,97 +1,195 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SQLite;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Simpsons_generator;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static readonly string root_path = "C:\\Users\\pgeeraert\\OneDrive - Prometheus Group\\Documents\\Code\\SQLLITE\\";
|
||||
public static readonly string folder_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Simpsons_generator");
|
||||
public static readonly string overrideBrowser = Path.Combine(folder_path, "overrideBrowser.xml");
|
||||
static SQLiteConnection CreateConnection()
|
||||
{
|
||||
|
||||
SQLiteConnection sqliteconn;
|
||||
// Create a new database connection:
|
||||
var sqllite_datasource = $"Data Source={folder_path}/database.db;Version=3;New=True;Compress=True;";
|
||||
sqliteconn = new SQLiteConnection(sqllite_datasource);
|
||||
// Open the connection:
|
||||
try
|
||||
{
|
||||
sqliteconn.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
return sqliteconn;
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
SQLiteConnection sqliteconn;
|
||||
sqliteconn = CreateConnection();
|
||||
//CreateTableSimpsons(sqliteconn);
|
||||
//InsertDataSimpsons(sqliteconn);
|
||||
//readexcel(Path.Combine(root_path, "simpsons-episodes-list.xlsx"));
|
||||
ReadData(sqliteconn);
|
||||
}
|
||||
|
||||
static void readexcel(string filePath)
|
||||
{
|
||||
var linesRead = File.ReadLines(filePath);
|
||||
|
||||
// iterate through each element within the array and
|
||||
// print it out
|
||||
//
|
||||
foreach (var lineRead in linesRead)
|
||||
{
|
||||
Console.WriteLine(lineRead);
|
||||
}
|
||||
}
|
||||
|
||||
static void CreateTableSimpsons(SQLiteConnection conn)
|
||||
{
|
||||
|
||||
SQLiteCommand sqlite_cmd;
|
||||
string Createsql = "CREATE TABLE episodes(season int(3), episode INT(3),url VARCHAR(100),seen INT(1))";
|
||||
sqlite_cmd = conn.CreateCommand();
|
||||
sqlite_cmd.CommandText = Createsql;
|
||||
sqlite_cmd.ExecuteNonQuery();
|
||||
|
||||
}
|
||||
static void InsertDataSimpsons(SQLiteConnection conn)
|
||||
{
|
||||
SQLiteCommand sqlite_cmd;
|
||||
sqlite_cmd = conn.CreateCommand();
|
||||
sqlite_cmd.CommandText = "INSERT INTO episodes(season, episode,url,seen) VALUES (1,1,'https://kisscartoon.sh/Cartoon/The-Simpsons-Season-01.47729/Episode-001?id=1072', 0);";
|
||||
sqlite_cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
|
||||
static void ReadData(SQLiteConnection conn)
|
||||
{
|
||||
SQLiteDataReader sqlite_datareader;
|
||||
SQLiteCommand sqlite_cmd;
|
||||
sqlite_cmd = conn.CreateCommand();
|
||||
sqlite_cmd.CommandText = "SELECT * FROM episodes";
|
||||
|
||||
sqlite_datareader = sqlite_cmd.ExecuteReader();
|
||||
while (sqlite_datareader.Read())
|
||||
{
|
||||
int season = Convert.ToInt32(sqlite_datareader["season"]);
|
||||
int episode = Convert.ToInt32(sqlite_datareader["episode"]);
|
||||
string url = sqlite_datareader["url"].ToString();
|
||||
string seen = sqlite_datareader["seen"].ToString();
|
||||
|
||||
Console.WriteLine($"{season}, {episode}, {url}, {seen}");
|
||||
}
|
||||
conn.Close();
|
||||
}
|
||||
|
||||
}
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user