Added user info table and update it for each user

This commit is contained in:
Casper Sparre 2025-10-11 15:59:47 +02:00
parent 7bf6271102
commit f8d7e2277f
4 changed files with 74 additions and 29 deletions

View File

@ -273,6 +273,10 @@ public class APIHelper : IAPIHelper
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync(); var body = await response.Content.ReadAsStringAsync();
user = JsonConvert.DeserializeObject<Entities.User>(body, m_JsonSerializerSettings); user = JsonConvert.DeserializeObject<Entities.User>(body, m_JsonSerializerSettings);
if (user is not null && !endpoint.EndsWith("/me"))
await m_DBHelper.UpdateUserInfo(user);
return user; return user;
} }
catch (Exception ex) catch (Exception ex)

View File

@ -136,34 +136,36 @@ namespace OF_DL.Helpers
SqliteConnection connection = await GetAndOpenConnectionAsync($"Data Source={Directory.GetCurrentDirectory()}/users.db"); SqliteConnection connection = await GetAndOpenConnectionAsync($"Data Source={Directory.GetCurrentDirectory()}/users.db");
Log.Debug("Database data source: " + connection.DataSource); Log.Debug("Database data source: " + connection.DataSource);
using (SqliteCommand cmd = new("CREATE TABLE IF NOT EXISTS users (id INTEGER NOT NULL, user_id INTEGER NOT NULL, username VARCHAR NOT NULL, PRIMARY KEY(id), UNIQUE(username));", connection)) using (SqliteCommand cmdUsers = new("CREATE TABLE IF NOT EXISTS users (id INTEGER NOT NULL, user_id INTEGER NOT NULL, username VARCHAR NOT NULL, PRIMARY KEY(id), UNIQUE(username));", connection))
{ {
await cmd.ExecuteNonQueryAsync(); await cmdUsers.ExecuteNonQueryAsync();
}
using (SqliteCommand cmdInfo = new("CREATE TABLE IF NOT EXISTS user_info (user_id INTEGER NOT NULL, name VARCHAR NOT NULL, about VARCHAR NULL, expires_on TIMESTAMP NULL, photo_count INT NOT NULL, video_count INT NOT NULL, PRIMARY KEY(user_id));", connection))
{
await cmdInfo.ExecuteNonQueryAsync();
} }
Log.Debug("Adding missing creators"); Log.Debug("Adding missing creators");
foreach (KeyValuePair<string, int> user in users) foreach (KeyValuePair<string, int> user in users)
{ {
using (SqliteCommand checkCmd = new($"SELECT user_id, username FROM users WHERE user_id = @userId;", connection)) using SqliteCommand checkCmd = new($"SELECT user_id, username FROM users WHERE user_id = @userId;", connection);
checkCmd.Parameters.AddWithValue("@userId", user.Value);
using var reader = await checkCmd.ExecuteReaderAsync();
if (!reader.Read())
{ {
checkCmd.Parameters.AddWithValue("@userId", user.Value); using SqliteCommand insertCmd = new($"INSERT INTO users (user_id, username) VALUES (@userId, @username);", connection);
using (var reader = await checkCmd.ExecuteReaderAsync()) insertCmd.Parameters.AddWithValue("@userId", user.Value);
{ insertCmd.Parameters.AddWithValue("@username", user.Key);
if (!reader.Read())
{ await insertCmd.ExecuteNonQueryAsync();
using (SqliteCommand insertCmd = new($"INSERT INTO users (user_id, username) VALUES (@userId, @username);", connection)) Log.Debug("Inserted new creator: " + user.Key);
{ }
insertCmd.Parameters.AddWithValue("@userId", user.Value); else
insertCmd.Parameters.AddWithValue("@username", user.Key); {
await insertCmd.ExecuteNonQueryAsync(); Log.Debug("Creator " + user.Key + " already exists");
Log.Debug("Inserted new creator: " + user.Key);
}
}
else
{
Log.Debug("Creator " + user.Key + " already exists");
}
}
} }
} }
@ -233,6 +235,39 @@ namespace OF_DL.Helpers
} }
} }
public async Task UpdateUserInfo(User? user)
{
if (user is null)
return;
SqliteConnection connection = await GetAndOpenConnectionAsync($"Data Source={Directory.GetCurrentDirectory()}/users.db");
Log.Debug("Database data source: " + connection.DataSource);
//"SELECT name, about, expires_on, photo_count, video_count FROM user_info WHERE user_id = @userId"
using SqliteCommand cmdInfo = new(
"INSERT OR REPLACE INTO user_info (user_id, name, about, expires_on, photo_count, video_count) " +
"VALUES (@userId, @name, @about, @expiresOn, @photoCount, @videoCount);",
connection
);
cmdInfo.Parameters.AddWithValue("@userId", user.id);
cmdInfo.Parameters.AddWithValue("@name", user.name);
cmdInfo.Parameters.AddWithValue("@about", user.about);
cmdInfo.Parameters.AddWithValue("@expiresOn", user.subscribedByExpireDate);
cmdInfo.Parameters.AddWithValue("@photoCount", user.photosCount ?? 0);
cmdInfo.Parameters.AddWithValue("@videoCount", user.videosCount ?? 0);
try
{
int rowCount = await cmdInfo.ExecuteNonQueryAsync();
Log.Debug("Inserted or updated creator info: {Username:l}", user.username);
}
catch (Exception ex)
{
Log.Warning(ex, "Failed to update User Info for: {Username:l}", user.username);
}
}
public async Task AddMessage(string folder, long post_id, string message_text, string price, bool is_paid, bool is_archived, DateTime created_at, int user_id) public async Task AddMessage(string folder, long post_id, string message_text, string price, bool is_paid, bool is_archived, DateTime created_at, int user_id)
{ {
try try

View File

@ -1,3 +1,5 @@
using OF_DL.Entities;
namespace OF_DL.Helpers namespace OF_DL.Helpers
{ {
public interface IDBHelper public interface IDBHelper
@ -13,5 +15,6 @@ namespace OF_DL.Helpers
Task<long> GetStoredFileSize(string folder, long media_id, string api_type); Task<long> GetStoredFileSize(string folder, long media_id, string api_type);
Task<bool> CheckDownloaded(string folder, long media_id, string api_type); Task<bool> CheckDownloaded(string folder, long media_id, string api_type);
Task<DateTime?> GetMostRecentPostDate(string folder); Task<DateTime?> GetMostRecentPostDate(string folder);
Task UpdateUserInfo(User? user);
} }
} }

View File

@ -894,9 +894,14 @@ public class Program
if (!cliNonInteractive) if (!cliNonInteractive)
{ {
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(5); Log.CloseAndFlush();
Environment.Exit(5);
} }
finally
{
Log.CloseAndFlush();
}
} }
private static async Task DownloadBlockedOrExpiredUsers(APIHelper m_ApiHelper, Entities.Config Config) private static async Task DownloadBlockedOrExpiredUsers(APIHelper m_ApiHelper, Entities.Config Config)
@ -1336,13 +1341,11 @@ public class Program
var downloadContext = new DownloadContext(Auth, Config, GetCreatorFileNameFormatConfig(Config, user.Key), m_ApiHelper, dBHelper); var downloadContext = new DownloadContext(Auth, Config, GetCreatorFileNameFormatConfig(Config, user.Key), m_ApiHelper, dBHelper);
if (Config.DownloadAvatarHeaderPhoto) User? user_info = await m_ApiHelper.GetUserInfo($"/users/{user.Key}");
if (Config.DownloadAvatarHeaderPhoto && user_info != null)
{ {
Entities.User? user_info = await m_ApiHelper.GetUserInfo($"/users/{user.Key}"); await downloadContext.DownloadHelper.DownloadAvatarHeader(user_info.avatar, user_info.header, path, user.Key);
if (user_info != null)
{
await downloadContext.DownloadHelper.DownloadAvatarHeader(user_info.avatar, user_info.header, path, user.Key);
}
} }
if (Config.DownloadPaidPosts) if (Config.DownloadPaidPosts)