FreeTubeSyncer/FreeTubeSyncer/Program.cs
Mario Steele f9af985de4 Adding some noise
Adding some noise to ensure that syncing is progressing as it should.
2025-07-30 12:30:14 -05:00

113 lines
No EOL
4.7 KiB
C#

using Avalonia;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FreeTubeSyncer.Library;
using FreeTubeSyncer.Models.DatabaseModels;
using FreeTubeSyncer.REST;
namespace FreeTubeSyncer;
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args)
{
GlobalJsonOptions.Options.Converters.Add(new StringToLongJsonConverter(false));
var paths = new string[]
{
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config/FreeTube/"),
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".var/app/io.freetubeapp.FreeTube/config/FreeTube")
};
var path = "";
foreach (var tpath in paths)
{
if (!Directory.Exists(tpath)) continue;
path = tpath;
break;
}
var dbWatcher = new DBSyncWatcher(path);
var historySyncer = new Syncer<History>(dbWatcher, Path.Join(path, "history.db"), "history.db", "/history");
var playlistSyncer = new Syncer<Playlist>(dbWatcher, Path.Join(path, "playlists.db"), "playlists.db", "/playlist");
var profileSyncer = new Syncer<Profile>(dbWatcher, Path.Join(path, "profiles.db"), "profiles.db", "/profile");
var searchHistorySyncer = new Syncer<SearchHistory>(dbWatcher, Path.Join(path, "search-history.db"), "search-history.db", "/searchHistory");
var settingsSyncer = new Syncer<Setting>(dbWatcher, Path.Join(path, "settings.db"), "settings.db", "/settings");
Task.Run(() => CheckCanSync(historySyncer, playlistSyncer, profileSyncer, searchHistorySyncer, settingsSyncer));
Console.WriteLine("Watching databases... Press return to exit...");
Console.ReadLine();
}
private static void CheckCanSync(Syncer<History>? historySyncer = null, Syncer<Playlist>? playlistSyncer = null,
Syncer<Profile>? profileSyncer = null, Syncer<SearchHistory>? searchHistorySyncer = null,
Syncer<Setting>? settingsSyncer = null)
{
var syncers = new List<ISyncer>()
{
historySyncer,
playlistSyncer,
profileSyncer,
searchHistorySyncer,
settingsSyncer
};
var lastTime = DateTime.Now;
while (true)
{
if (syncers.Any(x => x != null && x.IsDirty() ))
{
Thread.Sleep(100);
if (lastTime - DateTime.Now > TimeSpan.FromSeconds(30))
{
var start = DateTime.Now;
Console.WriteLine("Checking for updates...");
foreach (var syncer in syncers)
syncer.FetchDatabase().Wait();
lastTime = DateTime.Now;
var end = DateTime.Now - start;
Console.WriteLine($"Check completed. Total Time: {end}");
}
if (Process.GetProcessesByName("FreeTube").Length > 0) continue;
Console.WriteLine("FreeTube has closed and we have updates, we're going to try and update.");
Thread.Sleep(1500);
if (historySyncer != null && historySyncer.IsDirty())
historySyncer.Sync();
if (playlistSyncer != null && playlistSyncer.IsDirty())
playlistSyncer.Sync();
if (profileSyncer != null && profileSyncer.IsDirty())
profileSyncer.Sync();
if (searchHistorySyncer != null && searchHistorySyncer.IsDirty())
searchHistorySyncer.Sync();
if (settingsSyncer != null && settingsSyncer.IsDirty())
settingsSyncer.Sync();
}
else
{
Thread.Sleep(100);
if (lastTime - DateTime.Now <= TimeSpan.FromSeconds(30)) continue;
foreach (var syncer in syncers)
syncer.FetchDatabase().Wait();
lastTime = DateTime.Now;
}
}
}
// public static void Main(string[] args) => BuildAvaloniaApp()
// .StartWithClassicDesktopLifetime(args);
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}