Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projeto de teste para avaliação #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs
/Oportunidade/.vs/Oportunidade
87 changes: 87 additions & 0 deletions Oportunidade/Oportunidade.Blog/Feed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using HtmlAgilityPack;
using Oportunidade.Interface.Blog;
using Oportunidade.Models.Blog;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Oportunidade.Blog
{
public class Feed : IFeed
{
public RSS ConsultarTopicos()
{
// Recupera os tópicos do blog
RSS rss = (RSS)CallAPI(ConfiguracaoAPI.urlBlog, HttpMethodEnum.GET, typeof(RSS));

TratarDadosRetorno(rss);

return rss;
}

#region TratarDadosRetorno
private void TratarDadosRetorno(RSS rss)
{
// Converte a string para DateTime
rss.Channel.dtLastBuildDate = ConverterData(rss.Channel.LastBuildDate);

// Converte a string em DateTime e remove tags HTML dos textos
Parallel.ForEach(rss.Channel.Items, item =>
{
item.dtPubDate = ConverterData(item.PubDate);
item.Description = RemoveTagsHTML(item.Description);
item.ContendEncoded = RemoveTagsHTML(item.ContendEncoded);
});
}
#endregion

#region RemoveTagsHTML
private DateTime ConverterData(string valor)
{
return DateTime.Parse(valor, new System.Globalization.CultureInfo("en-US"));
}
#endregion

#region RemoveTagsHTML
private string RemoveTagsHTML(string valor)
{
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(valor);
return htmlDoc.DocumentNode.InnerText;
}
#endregion

#region CallAPI
private object CallAPI(string url, HttpMethodEnum httpMethod, Type responseType)
{
var baseAddress = url;

var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
http.Accept = "application/xml";
http.ContentType = "application/xml";
http.Method = httpMethod.ToString();

WebResponse response = http.GetResponse();

var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();

if (responseType != null)
{
XmlSerializer serializer = new XmlSerializer(typeof(RSS));
using (TextReader reader = new StringReader(content))
{
return (RSS)serializer.Deserialize(reader);
}
}
else
{
return content;
}
}
#endregion
}
}
16 changes: 16 additions & 0 deletions Oportunidade/Oportunidade.Blog/Oportunidade.Blog.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack.NetCore" Version="1.5.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Oportunidade.Interface\Oportunidade.Interface.csproj" />
<ProjectReference Include="..\Oportunidade\Oportunidade.csproj" />
</ItemGroup>

</Project>
Loading