I wrote code for my needs (i.e. collecting YouTube links from a page) and it is simple, but now I just want to know what can I do just to make it a better and properly written one. For example, I think I should have try
/catch
blocks/exceptions in my methods, or get/set properties. What tests can I write to test the code? I don't know to be honest, but how would the code look if you had written it?
public class Parser
{
public static void Main()
{
Console.WriteLine("Enter the url of the page:");
var url = Console.ReadLine();
var numOfPages = GetNumberOfPages(url);
var numOfLinks = 1;
for (int page = 0; page <= numOfPages; page++)
{
if (page != 1)
{
if (page == 0)
{
GetYouTubeLink(url).ForEach(x => Console.WriteLine($"{numOfLinks++}." +
$"{x.Replace("embed/", "watch?v=").Replace("?feature=oembed", "")}"));
}
else
{
var newUrl = url + string.Format($"{page}/");
GetYouTubeLink(newUrl).ForEach(x => Console.WriteLine($"{numOfLinks++}." +
$"{x.Replace("embed/", "watch?v=").Replace("?feature=oembed", "")}"));
newUrl = string.Empty;
}
}
}
}
private static HtmlDocument GetWebPageHtmlFromUrl(string url)
{
var webGet = new HtmlWeb();
return webGet.Load(url);
}
private static int GetNumberOfPages(string url)
{
var pages = GetWebPageHtmlFromUrl(url)
.DocumentNode.SelectNodes("//div[@class='page-link']")
.Descendants("a")
.Last().InnerText;
return int.Parse(pages);
}
private static List<string> GetYouTubeLink(string url)
{
var links = new List<string>();
var iframeLinks = GetWebPageHtmlFromUrl(url).DocumentNode.SelectNodes("//iframe");
if (iframeLinks != null)
{
foreach (var link in iframeLinks)
{
links.Add(link.Attributes["src"].Value);
}
}
return links;
}
}