Files
ExportAssistantLogCS/Program.cs
T
2023-09-05 01:00:39 +08:00

91 lines
2.6 KiB
C#

using System;
using System.Data.SQLite;
using System.Globalization;
using System.IO;
using System.Text;
using CsvHelper;
namespace ExportAssiatantLog
{
class Program
{
static void Main(string[] args)
{
var localAppDataPath = Environment.GetEnvironmentVariable("LOCALAPPDATA");
var dbPath = Path.Combine(localAppDataPath, "ShadowBot", "users", "Assistant", "assistant.db3");
var downloadPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
var dateString = DateTime.UtcNow.ToString("yyyy-MM-dd");
var filename = $"{dateString}-assistant-job-logs.csv";
var exportPath = Path.Combine(downloadPath, filename);
using var conn = new SQLiteConnection($"Data Source={dbPath}");
conn.Open();
using var cmd = new SQLiteCommand(conn);
using var writer = new StreamWriter(exportPath, false, Encoding.UTF8);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.WriteRecords(GetData(cmd));
}
static IEnumerable<DataRecord> GetData(SQLiteCommand cmd)
{
int i = 1;
while (true)
{
var tableName = $"assistant_job_logs_{i}";
var query = $@"SELECT `sceneInstJobUuid`, `level`, `time`, `content` FROM `{tableName}`";
cmd.CommandText = query;
SQLiteDataReader reader = null;
try
{
reader = cmd.ExecuteReader();
}
catch (SQLiteException e)
{
break;
}
if (!reader.HasRows)
{
break;
}
while (reader.Read())
{
yield return new DataRecord
{
SceneInstJobUuid = reader.GetString(0),
Level = reader.GetString(1),
Time = reader.GetString(2),
Content = reader.GetString(3)
};
}
reader.Close();
i++;
}
}
class DataRecord
{
public string SceneInstJobUuid { get; set; }
public string Level { get; set; }
public string Time { get; set; }
public string Content { get; set; }
}
}
}