Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created January 6, 2017 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovstanton/b82ecd9912bb3545a37a3651e8a59dc1 to your computer and use it in GitHub Desktop.
Save angelovstanton/b82ecd9912bb3545a37a3651e8a59dc1 to your computer and use it in GitHub Desktop.
class Program
{
private static string filePath = string.Empty;
private static string yearInput = string.Empty;
private static int year = -1;
private static int profileId = 0;
private static string profileIdInput = string.Empty;
private static List<Article> articlesInfos;
static void Main(string[] args)
{
var commandLineParser = new FluentCommandLineParser();
commandLineParser.Setup<string>('p', "path").Callback(s => filePath = s);
commandLineParser.Setup<string>('y', "year").Callback(y => yearInput = y);
commandLineParser.Setup<string>('i', "profileId").Callback(p => profileIdInput = p);
commandLineParser.Parse(args);
bool isProfileIdCorrect = int.TryParse(profileIdInput, out profileId);
if (string.IsNullOrEmpty(profileIdInput) || !isProfileIdCorrect)
{
Console.WriteLine("Please specify a correct profileId.");
return;
}
if (string.IsNullOrEmpty(filePath))
{
Console.WriteLine("Please specify a correct file path.");
return;
}
if (!string.IsNullOrEmpty(yearInput))
{
bool isYearCorrect = int.TryParse(yearInput, out year);
if (!isYearCorrect)
{
Console.WriteLine("Please specify a correct year!");
return;
}
}
articlesInfos = GetAllArticlesInfos();
if (year == -1)
{
CreateReportAllTime();
}
else
{
CreateReportYear();
}
Console.WriteLine("Total VIEWS: {0}", articlesInfos.Sum(x => x.Views));
Console.ReadLine();
}
private static void CreateReportAllTime()
{
TextWriter textWriter = new StreamWriter(filePath);
var csv = new CsvWriter(textWriter);
csv.WriteRecords(articlesInfos.OrderByDescending(x => x.Views));
}
private static void CreateReportYear()
{
TextWriter currentYearTextWriter = new StreamWriter(filePath);
var csv = new CsvWriter(currentYearTextWriter);
csv.WriteRecords(articlesInfos.Where(x => x.PublishDate.Year.Equals(year)).OrderByDescending(x => x.Views));
}
private static List<Article> GetAllArticlesInfos()
{
var articlesInfos = new List<Article>();
using (var driver = new ChromeDriver())
{
var articlePage = new ArticlesPage(driver, profileId);
articlesInfos.AddRange(articlePage.GetArticlesByUrl("#Articles"));
}
using (var driver = new ChromeDriver())
{
var articlePage = new ArticlesPage(driver, profileId);
articlesInfos.AddRange(articlePage.GetArticlesByUrl("#TechnicalBlog"));
}
using (var driver = new ChromeDriver())
{
var articlePage = new ArticlesPage(driver, profileId);
articlesInfos.AddRange(articlePage.GetArticlesByUrl("#Tip"));
}
return articlesInfos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment