using Crawler.
Utilites;
using [Link];
using [Link];
using [Link];
namespace JobsTrackerCrawler
{
class Program
{
static async Task Main(string[] args)
{
bool IsProgramCrash = false;
int retryCount = 0;
int maxRetries = 10;
int maxAttempts = 10;
int attempts = 0;
while (retryCount < maxRetries)
{
try
{
string documentsPath =
[Link]([Link]);
string folderPath = [Link](documentsPath, "[Link]");
string downloadsFolder =
[Link]([Link]([Link]),
"Downloads");
string successLoggerPath = [Link](folderPath,
"[Link]");
string countryAndLanuagePath = [Link](folderPath,
"[Link]");
var (country, language) =
GetCountryAndLanguage(countryAndLanuagePath);
var initialFiles =
[Link](downloadsFolder).ToHashSet();
string documentsFolder =
[Link]([Link]);
string baseFolder = [Link](documentsFolder, "[Link]",
"GeneratedContent");
[Link] = "[Link]
[Link]("ProfileOne");
[Link](2);
if ([Link]() != null &&
[Link]().Contains("login"))
{
[Link](5);
Login();
[Link](5);
}
string[] topics = GetTopicList();
foreach (var topic in topics)
{
if (!CheckInternetConnection())
{
[Link]("Internet connection lost.
Retrying...");
[Link](5000);
continue;
}
else
{
[Link]([Link],
true);
}
CreateNewDocumentPopUp();
ChangeCountryAndLanguage(country, language);
[Link](5);
[Link]([Link],
"input[placeholder=\"Enter search query\"]", topic, 250);
[Link]([Link],
"//button[span[text()='Start']]", 250);
while (attempts < maxAttempts && !
[Link]([Link], "//button[@role='tab'
and contains(@aria-controls, 'radix') and contains(text(), 'Outline')]"))
{
[Link](3); // Wait for
3 seconds
attempts++;
if (attempts >= maxAttempts)
{
[Link]("Max attempts reached: Element
not found.");
break; // Stop the loop after 10 tries
}
}
[Link](5);
[Link]();
[Link](2);
[Link]([Link],
"button[aria-controls*=\"-content-outline\"]", 250);
[Link]([Link],
"button[class*=\"backdrop-blur-lg bg-emerald-200/30 dark:bg-emerald-800/30\"]",
250);
[Link]([Link],
"button[id=\"optimize-tab\"]", 250);
[Link]([Link],
"div[data-orientation=\"horizontal\"][data-state=\"active\"] button[aria-
haspopup=\"menu\"][aria-expanded=\"false\"][data-state=\"closed\"]", 250);
[Link]([Link],
"a[download=\"[Link]\"]", 250);
WaitForNewFiles(downloadsFolder, initialFiles, 600, topic);
[Link](5);
string topicFolder = [Link](baseFolder, topic);
string latestCsvFile = GetLatestFile(downloadsFolder,
"*.csv");
if ()
{
[Link](topicFolder);
}
if ()
{
string newCsvFilePath = [Link](topicFolder, topic
+ ".csv");
if ([Link](newCsvFilePath))
{
[Link](newCsvFilePath);
[Link]($"Existing CSV file deleted:
{newCsvFilePath}");
}
[Link](4);
[Link](latestCsvFile, newCsvFilePath);
}
else
{
[Link]("No CSV file found in Downloads.");
}
[Link](successLoggerPath, $"'{topic}' has been
successfully generated by AI.{[Link]}");
[Link]($"The topic '{topic}' has been
successfully generated by AI.");
RemoveTopicFromFile(topic);
}
// If execution reaches here, reset retryCount
retryCount = 0;
break;
}
catch (Exception ex)
{
retryCount++;
[Link]($"Attempt {retryCount} failed:
{[Link]}");
LogError("An unknown error occurred in the Scraper: " +
[Link] + "\nStackTrace: " + [Link]);
if (retryCount >= maxRetries)
{
[Link]("Maximum retry attempts reached. Exiting
program.");
break;
}
[Link]("Retrying after 10 seconds...");
[Link](10000); // Wait before retrying
}
finally
{
[Link]();
}
}
}
public static void RemoveTopicFromFile(string completedTopic)
{
try
{
string userDocumentsPath =
[Link]([Link]);
string topicListPath = [Link](userDocumentsPath, "[Link]",
"[Link]");
if ([Link](topicListPath))
{
var topics = [Link](topicListPath)
.Where(line => !
[Link](line) && [Link]() != completedTopic)
.ToList();
[Link](topicListPath, topics);
[Link]($"Removed completed topic:
{completedTopic}");
}
}
catch (Exception ex)
{
[Link]($"Error removing topic: {[Link]}");
}
}
public static bool CheckInternetConnection()
{
try
{
using (var client = new [Link]())
using ([Link]("[Link]
{
return true;
}
}
catch
{
return false;
}
}
static (string Email, string Password) Credentials()
{
// Get the Documents folder path dynamically
string documentsPath =
[Link]([Link]);
string folderPath = [Link](documentsPath, "[Link]");
string filePath = [Link](folderPath, "[Link]");
try
{
// Check if the file exists
if ([Link](filePath))
{
// Read all lines from the file
string[] lines = [Link](filePath);
// Ensure the file has at least two lines
if ([Link] >= 2 && 
&& )
{
return (lines[0].Trim(), lines[1].Trim()); // Return email
and password
}
else
{
[Link]("Error: The file does not contain a valid
email and password.");
}
}
else
{
[Link]("Error: Credentials file not found.");
}
}
catch (Exception ex)
{
[Link]($"An error occurred: {[Link]}");
}
return ([Link], [Link]); // Return empty values if an error
occurs
}
static void Login()
{
[Link](3);
var (email, password) = Credentials();
[Link]([Link],
"input[type='email']", email, 250);
[Link]([Link],
"input[type='password'", password, 250);
[Link]([Link],
"button[type=\"submit\"]", 250);
[Link](1);
}
static void CreateNewDocumentPopUp()
{
[Link](5);
//click on new document button
[Link]([Link],
"(//button[.//span[text()='New document']])[1]", 250);
[Link]([Link], "//span[text()='Blank
Document']/ancestor::div[contains(@class, 'transition-all')]", 250);
[Link]([Link],
"//button[span[text()='Continue']]", 250);
}
static void ChangeCountryAndLanguage(string country, string language)
{
[Link](3);
if ( && )
{
[Link]([Link],
"//input[@placeholder='Enter search query']/following-sibling::div/button[2]", 60);
[Link]([Link],
"div[role=\"dialog\"] form > div[class^=\"space-y-2\"]:nth-child(2) button[aria-
haspopup=\"dialog\"]:nth-child(2)");
var countriesNames =
[Link]([Link], "div[role='group'] >
div");
foreach (var countriesName in countriesNames)
{
if
([Link](countriesName).ToLower().Contains(country.
ToLower()))
{
[Link](countriesName);
Actions actions = new Actions([Link]);
[Link]([Link]).Perform();
break;
}
}
[Link](2);
[Link]([Link],
"div[role=\"dialog\"] form > div[class^=\"space-y-2\"]:nth-child(3) button[aria-
haspopup=\"dialog\"]:nth-child(2)", 60);
var languageNames =
[Link]([Link], "div[role='group'] >
div");
foreach (var name in languageNames)
{
if
([Link](name).ToLower().Contains([Link](
)))
{
[Link](name);
Actions actions = new Actions([Link]);
[Link]([Link]).Perform();
break;
}
}
[Link]([Link],
"div[role=\"dialog\"] form > div:nth-child(4) > button:nth-child(2)");
}
static void LogError(string message)
{
// Get the Documents folder path dynamically
string documentsPath =
[Link]([Link]);
// Define the log file path dynamically
string logFilePath = [Link](documentsPath, "[Link]",
"[Link]");
// Ensure the directory exists
[Link]([Link](logFilePath));
// Write the error message to the log file
[Link](logFilePath, $"{[Link]}: {message}
{[Link]}");
[Link]("Error logged successfully.");
}
public static string[] GetTopicList()
{
try
{
// Get the user's documents folder
string userDocumentsPath =
[Link]([Link]);
// Construct the path to the [Link] file
string topicListPath = [Link](userDocumentsPath, "[Link]",
"[Link]");
// Check if the file exists
if ([Link](topicListPath))
{
// Read the topics from the file, remove empty and whitespace-
only lines
string[] topics = [Link](topicListPath)
.Where(line => !
[Link](line)) // Filters out empty lines
.ToArray();
return topics;
}
else
{
[Link]("[Link] file does not exist.");
return [Link]<string>(); // Return an empty array if file
does not exist
}
}
catch (Exception ex)
{
[Link]($"An error occurred: {[Link]}");
return [Link]<string>(); // Return an empty array in case of
an error
}
}
static string GetLatestFile(string folderPath, string searchPattern)
{
try
{
var directory = new DirectoryInfo(folderPath);
var file = [Link](searchPattern)
.OrderByDescending(f => [Link])
.FirstOrDefault();
return file?.FullName;
}
catch (Exception ex)
{
[Link]($"Error getting latest file: {[Link]}");
return null;
}
}
static void WaitForNewFiles(string folderPath, HashSet<string>
initialFiles, int timeoutInSeconds, string topicName)
{
DateTime endTime = [Link](timeoutInSeconds);
while ([Link] < endTime)
{
var allFiles = [Link](folderPath);
var newFiles = [Link](f => !
[Link](f)).ToList();
var downloadingFiles = [Link](f =>
[Link](".crdownload") || [Link](".part")).ToList();
var csvFiles = [Link](f => [Link](".csv")).ToList();
if ([Link]() && ![Link]())
{
[Link]("File downloaded successfully! Waiting for
file to be fully written...");
string downloadedFile = [Link]();
// Wait until the file is no longer locked
int retries = 5;
while (retries > 0)
{
try
{
using (FileStream fs = new FileStream(downloadedFile,
[Link], [Link], [Link]))
{
[Link]("File is fully available.");
return;
}
}
catch (IOException)
{
[Link]("File is still being written,
retrying...");
[Link](2000);
retries--;
}
}
[Link]("Warning: File may still be incomplete.");
return;
}
[Link](1000);
}
LogError($"Files did not download for topic = '{topicName}'");
}
public static (string Country, string Language)
GetCountryAndLanguage(string filePath)
{
if ([Link](filePath))
{
string[] lines = [Link](filePath);
if ([Link] >= 2)
return (lines[0].Trim(), lines[1].Trim());
}
return ([Link], [Link]); // Return empty values if file is
missing or invalid
}
}
}