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

Exercício - Capturar Principais Palavras (Feed) - Everson M de Campos #63

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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/Sources/Exercicio.PrincipaisPalavras.Application/obj
/Sources/Exercicio.PrincipaisPalavras.Infra.HttpService/bin/Debug/netcoreapp2.2
/Sources/Exercicio.PrincipaisPalavras.Infra.HttpService/obj
/Sources/Exercicio.PrincipaisPalavras.Service/bin/Debug/netcoreapp2.2
/Sources/Exercicio.PrincipaisPalavras.Application/bin/Debug/netcoreapp2.2
/Sources/Exercicio.PrincipaisPalavras.Service/obj
/Sources/Exercicio.PrincipaisPalavras.Test/bin/Debug/netcoreapp2.2
/Sources/Exercicio.PrincipaisPalavras.Test/obj
/Sources/Exercicio.PrincipaisPalavras.WebApi/bin/Debug/netcoreapp2.2
/Sources/Exercicio.PrincipaisPalavras.WebApi/obj
/Sources/.vs/Exercicio.PrincipaisPalavras/v15/Server/sqlite3
/Sources/.vs/Exercicio.PrincipaisPalavras/v15
/.vs
996 changes: 996 additions & 0 deletions Sources/.vs/config/applicationhost.config

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Exercicio.PrincipaisPalavras.Application.BlogMinutoSeguro.Interfaces;
using Exercicio.PrincipaisPalavras.Service.BlogMinutoSeguro;
using Exercicio.PrincipaisPalavras.Service.BlogMinutoSeguro.Interfaces;
using Exercicio.PrincipaisPalavras.Service.Core;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Exercicio.PrincipaisPalavras.Application.BlogMinutoSeguro
{
public class BlogMinutoSeguroApp : IBlogMinutoSeguroApp
{
private readonly IBlogMinutoSeguroService _blogMinutoSeguroService = null;

public BlogMinutoSeguroApp(IBlogMinutoSeguroService blogMinutoSeguroService)
{
_blogMinutoSeguroService = blogMinutoSeguroService;
}

public async Task<Response<FeedNoticia>> ObterPrincipaisPalavrasAsync()
{
var response = new Response<FeedNoticia>();

var conteudo = await _blogMinutoSeguroService.ObterConteudoFeedAsync();

if (string.IsNullOrEmpty(conteudo))
{
response.RasonPhrase = "Conteudo da página não encontrado";
return response;
}

response.AddContent(new FeedNoticia(conteudo));

return response;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Exercicio.PrincipaisPalavras.Service.BlogMinutoSeguro;
using Exercicio.PrincipaisPalavras.Service.Core;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Exercicio.PrincipaisPalavras.Application.BlogMinutoSeguro.Interfaces
{
public interface IBlogMinutoSeguroApp
{
Task<Response<FeedNoticia>> ObterPrincipaisPalavrasAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Exercicio.PrincipaisPalavras.Service\Exercicio.PrincipaisPalavras.Service.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Exercicio.PrincipaisPalavras.Service.BlogMinutoSeguro.Interfaces;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Exercicio.PrincipaisPalavras.Infra.HttpService
{
public class BlogMinutoSeguroHttpService : HttpClientBase, IBlogMinutoSeguroService
{
private const string uriBlogFeed = "blog/feed";

public BlogMinutoSeguroHttpService(): base("http://www.minutoseguros.com.br", "*/*")
{
}

public Task<string> ObterConteudoFeedAsync()
{
return GetStringAsync(uriBlogFeed);
}

public HttpResponseMessage ObterConteudoFeedSync()
{
return GetSync(uriBlogFeed);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Exercicio.PrincipaisPalavras.Service\Exercicio.PrincipaisPalavras.Service.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Exercicio.PrincipaisPalavras.Infra.HttpService
{
public abstract class HttpClientBase
{
protected readonly HttpClient _httpClient;

private readonly string _mediaTypeAccept;

public HttpClientBase(string url) : this(url, "text/html")
{
}

public HttpClientBase(string url, string mediaTypeAccept, Int64 timeOut = 3000, bool useDefaultCredentials = false)
{
_mediaTypeAccept = mediaTypeAccept;

_httpClient = CreateHttpClienWithHandler(new Uri(url), timeOut, useDefaultCredentials);
}

public virtual HttpResponseMessage GetSync(string uri)
{
return GetAsync(uri).GetAwaiter().GetResult();
}

public virtual Task<HttpResponseMessage> GetAsync(string uri)
{
return _httpClient.GetAsync(uri);
}

public virtual Task<HttpResponseMessage> GetAsync(string uri, CancellationToken cancellationToken)
{
return _httpClient.GetAsync(uri, cancellationToken);
}

public virtual async Task<string> GetStringAsync(string uri)
{
var response = await _httpClient.GetAsync(uri);

return await response.Content.ReadAsStringAsync();
}

public virtual HttpClient CreateHttpClienWithHandler(Uri baseAddress, Int64 timeOut, bool useDefaultCredentials)
{
var http = new HttpClient(CreateHttpClientHandler(useDefaultCredentials))
{
BaseAddress = baseAddress,
Timeout = TimeSpan.FromMilliseconds(timeOut)
};

http.DefaultRequestHeaders.Add("Accept", _mediaTypeAccept);
http.DefaultRequestHeaders.Add("Accept-Language", "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7");
http.DefaultRequestHeaders.Add("Postman-Token", "92defd03-997e-4a76-8bf6-d4c27689e476");
http.DefaultRequestHeaders.Add("User-Agent", "PostmanRuntime/7.15.0");

return http;
}

protected virtual HttpClientHandler CreateHttpClientHandler(bool useDefaultCredentials)
{
var coockies = new CookieCollection();

//coockies.Add(new Cookie("incap_ses_1239_1762536", "RY6beEc+lihW77u1ItAxEWXWJV0AAAAA/4NFHU9GK80fr01ssMIhiA==", "/", ".minutoseguros.com.br"));
coockies.Add(new Cookie("incap_ses_789_1762536", "LWoDSNgCST0CoTOiTxjzCkgoJl0AAAAAzKQZySUBf72Km8CnA2FPdQ==", "/", ".minutoseguros.com.br"));
coockies.Add(new Cookie("nlbi_1762536", "BvZVWkGzJQARObmQFqUGpAAAAADbKfc7jgu9TyKpDoCmRn9N", "/", ".minutoseguros.com.br"));
coockies.Add(new Cookie("visid_incap_1762536", "0zVL0xC1TCyNtyOxkUAeqvvqJV0AAAAAQUIPAAAAAADgSdmxvVk57tSOLKmrglhv", "/", ".minutoseguros.com.br"));

var coockieContaniner = new CookieContainer();
coockieContaniner.Add(coockies);

return new HttpClientHandler()
{
AllowAutoRedirect = true,
UseDefaultCredentials = useDefaultCredentials,
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate,
UseCookies = true,
CookieContainer = coockieContaniner
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Exercicio.PrincipaisPalavras.Service.BlogMinutoSeguro.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Exercicio.PrincipaisPalavras.Service.BlogMinutoSeguro
{
public class FeedNoticia
{
public List<Topico> Topicos { get; private set; }
public string HtmlContent { get; private set; }

public FeedNoticia(string htmlContent)
{
CarregarInformacoesTopico(htmlContent);
}

public void CarregarInformacoesTopico(string html)
{
var htmlTopicos = html.Split("<item>");

if (htmlTopicos.Any())
{
Topicos = new List<Topico>(htmlTopicos.Count() - 1);

for (int i = 0; i < htmlTopicos.Length; i++)
{
if (i > 0)
{
var obj = new Topico(htmlTopicos[i]);
obj.CarregarInformacoesTopico();

Topicos.Add(obj);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercicio.PrincipaisPalavras.Service.BlogMinutoSeguro.Helper
{
public class RegexPatternHelper
{
public static string TagsWithContent(string [] content)
{
string strPattern = string.Empty;

for (int i = 0; i < content.Length; i++)
{
if (i == 0)
strPattern = $"<{content[i]}>\\s*(.+?)\\s*</{content[i]}>";
else
strPattern += $"|<{content[i]}>\\s*(.+?)\\s*</{content[i]}>";
}

return strPattern;
}

public static string BeetweenSpace(string[] content)
{
string strPattern = string.Empty;

for (int i = 0; i < content.Length; i++)
{
if (i == 0)
strPattern = $"\\s{content[i]}\\s";
else
strPattern += $"|\\s{content[i]}\\s";
}

return strPattern;
}

public static string Words(string initial, string length)
{
return "\\w{" + initial + "," + length + "}";
}

public static string Tags()
{
return @"<[^>]*>";
}

public static string Links()
{
return @"http[^\s]+";
}

public static string Number()
{
return @"\d+";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Exercicio.PrincipaisPalavras.Service.BlogMinutoSeguro.Interfaces
{
public interface IBlogMinutoSeguroService
{
Task<string> ObterConteudoFeedAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Exercicio.PrincipaisPalavras.Service.BlogMinutoSeguro
{
public class PalavraPrincipal
{
public PalavraPrincipal(string nome, int qtd)
{
Nome = nome;
Qtd = qtd;
}

public string Nome { get; private set; }

public int Qtd { get; private set; }

public void AdicionarQtd()
{
Qtd++;
}
}
}
Loading