Skip to content

Commit

Permalink
Autosave and autoload directory settings, calc start message ID
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianMorozov committed Jan 5, 2023
1 parent 838bebc commit 4973641
Show file tree
Hide file tree
Showing 34 changed files with 747 additions and 403 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Auto calculation of the start message identifier
- Manual set start message identifier
- Auto renaming downloaded files if the option to add an identifier to the file name is enabled
- Autosave and autoload the catalog to download the channel/group
- Autosave and autoload the directory to download the channel/group
### Changed
- Switch method for choice boolean answer
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022-2023 Damian Viktorovich Morozov
Copyright (c) 2022 - 2023 Damian Viktorovich Morozov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
37 changes: 31 additions & 6 deletions TgDownloaderConsole/Helpers/MenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using TgDownloaderCore.Models;
using TgLocaleCore.Interfaces;
using TgStorageCore.Helpers;
using TgStorageCore.Models;
using TgStorageCore.Models.Messages;

namespace TgDownloaderConsole.Helpers;

Expand Down Expand Up @@ -68,6 +68,8 @@ internal void ShowTableCore(string title, Action<Table> fillTableColumns, Action

internal void ShowTableDownload() => ShowTableCore(TgLocale.MenuMainDownload, FillTableColumns, FillTableRowsDownload);

internal void ShowTableScanRange() => ShowTableCore(TgLocale.MenuMainDownload, FillTableColumns, FillTableRowsScanRange);

internal void FillTableColumns(Table table)
{
if (table.Columns.Count > 0) return;
Expand Down Expand Up @@ -128,7 +130,7 @@ internal void FillTableRowsClient(Table table)
}
else
{
User user = TgClient.MySelfUser;
User user = TgClient.Me;
table.AddRow(new Markup(TgLocale.InfoMessage(TgLocale.TgClientUserName)),
new Markup(user.username));
table.AddRow(new Markup(TgLocale.InfoMessage(TgLocale.TgClientUserId)),
Expand All @@ -151,15 +153,15 @@ internal void FillTableRowsDownload(Table table)
new Markup(TgLocale.SettingsIsNeedSetup));
else
table.AddRow(new Markup(TgLocale.InfoMessage(TgLocale.TgSettingsSourceId)),
new Markup(TgClient.TgDownload.SourceId.ToString()));
new Markup(TgClient.TgDownload.SourceId is { } sid ? sid.ToString() : TgLocale.Empty));

// Source user name.
if (!TgClient.TgDownload.IsReadySourceUserName)
table.AddRow(new Markup(TgLocale.WarningMessage(TgLocale.TgSettingsSourceUserName)),
new Markup(TgLocale.SettingsIsNeedSetup));
else
table.AddRow(new Markup(TgLocale.InfoMessage(TgLocale.TgSettingsSourceUserName)),
new Markup(TgClient.TgDownload.SourceUserName ?? TgLocale.Empty));
new Markup(TgClient.TgDownload.SourceUserName));

// Dest dir.
if (string.IsNullOrEmpty(TgClient.TgDownload.DestDirectory))
Expand All @@ -169,6 +171,10 @@ internal void FillTableRowsDownload(Table table)
table.AddRow(new Markup(TgLocale.InfoMessage(TgLocale.TgSettingsDestDirectory)),
new Markup(TgClient.TgDownload.DestDirectory));

// Source start ID / last ID.
table.AddRow(new Markup(TgLocale.InfoMessage(TgLocale.TgSettingsSourceStartLastId)),
new Markup($"{TgClient.TgDownload.SourceStartId} / {TgClient.TgDownload.SourceLastId}"));

// Is rewrite files.
table.AddRow(new Markup(TgLocale.InfoMessage(TgLocale.TgSettingsIsRewriteFiles)),
new Markup(TgClient.TgDownload.IsRewriteFiles.ToString()));
Expand All @@ -182,6 +188,25 @@ internal void FillTableRowsDownload(Table table)
new Markup(TgClient.TgDownload.IsJoinFileNameWithMessageId.ToString()));
}

internal void FillTableRowsScanRange(Table table)
{
// Source ID.
if (!TgClient.TgDownload.IsReadySourceId)
table.AddRow(new Markup(TgLocale.WarningMessage(TgLocale.TgSettingsSourceId)),
new Markup(TgLocale.SettingsIsNeedSetup));
else
table.AddRow(new Markup(TgLocale.InfoMessage(TgLocale.TgSettingsSourceId)),
new Markup(TgClient.TgDownload.SourceId is { } sid ? sid.ToString() : TgLocale.Empty));

// Source user name.
if (!TgClient.TgDownload.IsReadySourceUserName)
table.AddRow(new Markup(TgLocale.WarningMessage(TgLocale.TgSettingsSourceUserName)),
new Markup(TgLocale.SettingsIsNeedSetup));
else
table.AddRow(new Markup(TgLocale.InfoMessage(TgLocale.TgSettingsSourceUserName)),
new Markup(TgClient.TgDownload.SourceUserName));
}

internal double CalcSourceProgress(long count, long current) =>
count == 0 ? 0 : (double)(current * 100) / count;

Expand All @@ -208,7 +233,7 @@ public void RefreshStatusForDownload(string message)
{
if (StatusContext is null) return;
StatusContext.Status(TgLog.GetMarkupString(
$"{GetStatus(TgClient.TgDownload.MessageCount, TgClient.TgDownload.MessageCurrentId)} | {message}"));
$"{GetStatus(TgClient.TgDownload.SourceLastId, TgClient.TgDownload.SourceStartId)} | {message}"));
StatusContext.Refresh();
}

Expand All @@ -218,7 +243,7 @@ public void StoreMessage(long? id, long? sourceId, string message) =>
public void StoreDocument(long? id, long? sourceId, long? messageId, string fileName, long fileSize, long accessHash) =>
TgStorage.AddOrUpdateRecordDocument(id, sourceId, messageId, fileName, fileSize, accessHash, true);

public bool FindExistsMessage(long? id, long? sourceId, string? messageString)
public bool FindExistsMessage(long? id, long? sourceId)
{
TableMessageModel message = TgStorage.GetRecord<TableMessageModel>(id, sourceId);
return TgStorage.IsValid(message);
Expand Down
7 changes: 3 additions & 4 deletions TgDownloaderConsole/Helpers/MenuHelperAction.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
// See https://aka.ms/new-console-template for more information

namespace TgDownloaderConsole.Helpers;

internal partial class MenuHelper
{
#region Public and private methods

public void RunAction(Action action)
public void RunAction(Action action, bool isSkipCheckTgSettings)
{
if (!CheckTgSettings())
if (!CheckTgSettings() && !isSkipCheckTgSettings)
{
TgLog.Warning(TgLocale.TgMustSetSettings);
Console.ReadKey();
Expand All @@ -32,7 +31,7 @@ public void RunAction(Action action)
action();
sw.Stop();
statusContext.Status($"{GetStatus(sw,
TgClient.TgDownload.MessageCurrentId, TgClient.TgDownload.MessageCount)}");
TgClient.TgDownload.SourceStartId, TgClient.TgDownload.SourceLastId)}");
statusContext.Refresh();
});
StatusContext = null;
Expand Down
47 changes: 36 additions & 11 deletions TgDownloaderConsole/Helpers/MenuHelperClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
// See https://aka.ms/new-console-template for more information

using TgStorageCore.Models;
using TgLocaleCore.Utils;
using TgStorageCore.Models.Apps;

namespace TgDownloaderConsole.Helpers;

Expand Down Expand Up @@ -51,27 +51,52 @@ public void SetupClient()
} while (menu is not MenuClient.Return);
}

private string? GetConfigExists(string what) =>
what switch
{
"api_id" => TgClient.ApiId = AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupAppId)),
"api_hash" => TgClient.ApiHash,
"phone_number" => TgClient.PhoneNumber,
"verification_code" => AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupCode)),
"notifications" => AnsiConsole.Ask<bool>(TgLog.GetLineStampInfo(TgLocale.TgSetupNotifications)).ToString(),
"first_name" => AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupFirstName)),
"last_name" => AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupLastName)),
"session_pathname" => FileNameUtils.Session,
"password" => AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupPassword)),
_ => null
};

private string? GetConfigUser(string what) =>
what switch
{
"api_id" => TgClient.ApiId = AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupAppId)),
"api_hash" => TgClient.ApiHash = AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupApiHash)),
"phone_number" => TgClient.PhoneNumber = AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupPhone)),
"verification_code" => AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupCode)),
"notifications" => AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupNotifications)).ToString(),
"first_name" => AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupFirstName)),
"last_name" => AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupLastName)),
"session_pathname" => FileNameUtils.Session,
"password" => AnsiConsole.Ask<string>(TgLog.GetLineStampInfo(TgLocale.TgSetupPassword)),
_ => null
};

public void ClientConnectExists()
{
TableAppModel app = TgStorage.GetRecord<TableAppModel>();
if (TgStorage.IsValid(app))
{
TgClient.Connect(app.ApiHash, app.PhoneNumber);
if (TgClient.IsReady)
{
TgClient.CollectAllChats().GetAwaiter().GetResult();
}
TgClient.Connect(app.ApiHash, app.PhoneNumber, GetConfigExists, null);
TgClient.CollectAllChats().GetAwaiter().GetResult();
}
}

public void ClientConnectNew()
{
TgClient.Connect(string.Empty, string.Empty);
TgClient.Connect(string.Empty, string.Empty, null, GetConfigUser);
if (TgClient.IsReady)
{
TgStorage.AddOrUpdateRecordApp(TgClient.ApiHash, TgClient.PhoneNumber, false);
TgClient.CollectAllChats().GetAwaiter().GetResult();
}
TgClient.CollectAllChats().GetAwaiter().GetResult();
}

public void ClientConnect()
Expand Down
Loading

0 comments on commit 4973641

Please sign in to comment.