Skip to content

Commit

Permalink
Reduced number of requests to temp mail api
Browse files Browse the repository at this point in the history
  • Loading branch information
Harabe-x committed Feb 26, 2024
1 parent 522ff2d commit c5014a5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
1 change: 1 addition & 0 deletions PixaiBot/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ protected override void OnStartup(StartupEventArgs e)
{
InitialConfiguration.CreateConfigFile();
InitialConfiguration.CreateStatisticsFile();
InitialConfiguration.CreateApiKeysFile();
_logger.Log("=====Application Started=====", _logger.ApplicationLogFilePath);
base.OnStartup(e);
var mainWindow = _serviceProvider.GetRequiredService<NavigationPanelView>();
Expand Down
16 changes: 12 additions & 4 deletions PixaiBot/Business Logic/Data Handling/JsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class JsonReader
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static IList<UserAccount> ReadAccountFile(string filePath)
public static IList<UserAccount>? ReadAccountFile(string filePath)
{
var jsonString = File.ReadAllText(filePath);
var accounts = JsonSerializer.Deserialize<IList<UserAccount>>(jsonString);
Expand All @@ -25,11 +25,10 @@ public static IList<UserAccount> ReadAccountFile(string filePath)
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static UserConfig ReadConfigFile(string filePath)
public static UserConfig? ReadConfigFile(string filePath)
{
var jsonString = File.ReadAllText(filePath);
var userConfig = JsonSerializer.Deserialize<UserConfig>(jsonString);

return userConfig;
}

Expand All @@ -39,10 +38,19 @@ public static UserConfig ReadConfigFile(string filePath)
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static BotStatistics ReadStatisticsFile(string filePath)
public static BotStatistics? ReadStatisticsFile(string filePath)
{
var jsonString = File.ReadAllText(filePath);
var statistics = JsonSerializer.Deserialize<BotStatistics>(jsonString);
return statistics;
}

public static Dictionary<string, IEnumerable<string>>? GetDomainsAssociatedWithApiKeys (string filePath)
{
var jsonString = File.ReadAllText(filePath);
var domains = JsonSerializer.Deserialize<Dictionary<string, IEnumerable<string>>>(jsonString);
return domains;

}

}
19 changes: 18 additions & 1 deletion PixaiBot/Business Logic/Data Management/InitialConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using PixaiBot.Business_Logic.Data_Handling;
Expand All @@ -23,6 +24,7 @@ static InitialConfiguration()
AccountsFilePath =
$"{ApplicationDataPath}\\accounts.json";
BotVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
ApiKeysFilePath = $"{ApplicationDataPath}\\apiKeys.json";
if (!Directory.Exists(ApplicationDataPath)) CreateDirectories();
}

Expand All @@ -33,7 +35,7 @@ static InitialConfiguration()
/// <summary>
/// Creates the directories needed for the bot to work
/// </summary>
public static void CreateDirectories()
private static void CreateDirectories()
{
Directory.CreateDirectory(
$"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\PixaiAutoClaimer\\Logs");
Expand Down Expand Up @@ -84,6 +86,15 @@ public static void CreateStatisticsFile()
JsonWriter.WriteJson(statistics, StatisticsFilePath);
}

public static void CreateApiKeysFile()
{
if (File.Exists(ApiKeysFilePath)) return;

var apiKeys = new Dictionary<string, IEnumerable<string>>();
JsonWriter.WriteJson(apiKeys,ApiKeysFilePath);
}


#endregion

#region Fields
Expand Down Expand Up @@ -117,6 +128,12 @@ public static void CreateStatisticsFile()
public static string AccountsFilePath { get; }


/// <summary>
/// Temp-mail domain list
/// </summary>
public static string ApiKeysFilePath { get; }


/// <summary>
/// Path to the application data folder
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public string GenerateEmail()
public string GenerateEmail(string tempMailApiKey)
{
_logger.Log("Creating email with temp mail domain ", _logger.CreditClaimerLogFilePath);
var domains = _tempMailApiManager.GetDomains(tempMailApiKey);
return GenerateRandomString(7, Letters) + domains.ToList()[_random.Next(domains.Count())];
var domain = _tempMailApiManager.GetDomain(tempMailApiKey);
return GenerateRandomString(7, Letters) + domain;
}

public string GeneratePassword()
Expand Down
28 changes: 28 additions & 0 deletions PixaiBot/Business Logic/Data Management/TempMailApiManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using PixaiBot.Business_Logic.Data_Handling;
using PixaiBot.Data.Interfaces;

namespace PixaiBot.Business_Logic.Data_Management;
Expand All @@ -16,6 +18,7 @@ public class TempMailApiManager : ITempMailApiManager
public TempMailApiManager(ILogger logger)
{
_httpClient = new HttpClient();
_random = new Random();
_logger = logger;
}

Expand Down Expand Up @@ -51,6 +54,29 @@ public IEnumerable<string> GetDomains(string tempMailApiKey)
return _domainList = JsonSerializer.Deserialize<List<string>>(responseText);
}

public string GetDomain(string tempMailApiKey)
{
var domainsAssociatedWithApiKeys = JsonReader.GetDomainsAssociatedWithApiKeys(InitialConfiguration.ApiKeysFilePath);

if (domainsAssociatedWithApiKeys == null)
{
domainsAssociatedWithApiKeys = new Dictionary<string, IEnumerable<string>>();
JsonWriter.WriteJson(domainsAssociatedWithApiKeys, InitialConfiguration.ApiKeysFilePath);
}

if (domainsAssociatedWithApiKeys.ContainsKey(tempMailApiKey))
{
return domainsAssociatedWithApiKeys[tempMailApiKey]
.ElementAt(_random.Next(domainsAssociatedWithApiKeys[tempMailApiKey].Count()));
}
var domains = GetDomains(tempMailApiKey);
domainsAssociatedWithApiKeys.Add(tempMailApiKey, domains);
JsonWriter.WriteJson(domainsAssociatedWithApiKeys, InitialConfiguration.ApiKeysFilePath);

return domainsAssociatedWithApiKeys[tempMailApiKey]
.ElementAt(_random.Next(domainsAssociatedWithApiKeys[tempMailApiKey].Count()));
}

public string GetVerificationLink(string email, string apiKey)
{
_logger.Log("Getting verification link", _logger.CreditClaimerLogFilePath);
Expand Down Expand Up @@ -117,5 +143,7 @@ private string GetUrlFromString(string url)

private readonly ILogger _logger;

private readonly Random _random;

#endregion
}
2 changes: 1 addition & 1 deletion PixaiBot/Data/Interfaces/ITempMailApiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface ITempMailApiManager
/// </summary>
/// <param name="tempMailApiKey">Temp-mail api key</param>
/// <returns>List of available domains </returns>
IEnumerable<string> GetDomains(string tempMailApiKey);
string GetDomain(string tempMailApiKey);

/// <summary>
/// Gets Verification link from email.
Expand Down
2 changes: 1 addition & 1 deletion PixaiBot/UI/ViewModel/AccountCreatorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private async void StartAccountCreation()

IDriverCreationStrategy driverCreationStrategy = ShouldUseProxy
? new ProxyDriverCreationStrategy(_proxyManager)
: new HiddenDriverCreationStrategy();
: new DebugDriverCreationStrategy();

if(_configManager.GetConfig().ToastNotifications) _toastNotificationSender.SendNotification("PixaiBot", "Account creation process started", NotificationType.Information);

Expand Down

0 comments on commit c5014a5

Please sign in to comment.