Files
YABA/API/YABA.Service/MiscService.cs
Carl Tibule 9bbd12e4c3
All checks were successful
ci/woodpecker/tag/api_build Pipeline was successful
ci/woodpecker/tag/api_uploadimage Pipeline was successful
ci/woodpecker/tag/web_uploadimage Pipeline was successful
Created Woodpecker CI/CD deployment
- Created Dockerfile for packing up API and Web projects as Docker image
2024-04-05 23:36:49 -05:00

54 lines
1.8 KiB
C#

using HtmlAgilityPack;
using System;
using System.Net;
using System.Text.RegularExpressions;
using YABA.Common.DTOs;
using YABA.Service.Interfaces;
namespace YABA.Service
{
public class MiscService : IMiscService
{
public MiscService() { }
public WebsiteMetaDataDTO GetWebsiteMetaData(string url)
{
try
{
var webClient = new WebClient();
webClient.Headers.Add("user-agent", "API-Application");
var sourceData = webClient.DownloadString(url);
var title = Regex.Match(sourceData, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
var description = string.Empty;
var getHtmlDoc = new HtmlWeb();
var document = getHtmlDoc.Load(url);
var metaTags = document.DocumentNode.SelectNodes("//meta");
if (metaTags != null)
{
foreach (var sitetag in metaTags)
{
if (sitetag.Attributes["name"] != null && sitetag.Attributes["content"] != null && sitetag.Attributes["name"].Value == "description")
{
description = sitetag.Attributes["content"].Value;
}
}
}
return new WebsiteMetaDataDTO
{
Title = title,
Description = description
};
} catch(Exception)
{
return new WebsiteMetaDataDTO
{
Title = url,
Description = string.Empty
};
}
}
}
}