Skip to content

Instantly share code, notes, and snippets.

View vmandic's full-sized avatar
🤠
chillin'

Vedran Mandić vmandic

🤠
chillin'
View GitHub Profile
@vmandic
vmandic / Startup.cs
Last active October 19, 2018 18:46
meds-processor, p2, s2
public void ConfigureServices(IServiceCollection services)
{
services.AddAngleSharp();
// adding the AppPathsInfo singleton instance carrying the ContentRootPath
// provided by the IHostingEnvironment injected instance
services.AddSingleton(
s => new AppPathsInfo(s.GetService<IHostingEnvironment>().ContentRootPath));
services.AddSingleton<HzzoHtmlScraper>();
@vmandic
vmandic / AppPathsInfo.cs
Created October 16, 2018 18:59
meds-processor, p2, s1
using System;
namespace MedsProcessor.Common.Models
{
public sealed class AppPathsInfo
{
public AppPathsInfo(string appRootPath)
{
if (appRootPath == null)
throw new ArgumentNullException(nameof(appRootPath));
@vmandic
vmandic / AppController.cs
Created October 13, 2018 18:51
meds-processor, part 1 of 4, snippet 10
public async Task<ActionResult> Index([FromServices] HzzoHtmlScraper scraper)
{
var startTime = DateTime.Now;
// TODO: implement scraper and parser logic
var meds = await scraper.Run();
var totalTime = startTime - DateTime.Now;
return Ok(
$"Done! Handler duration: {totalTime.Duration()}" +
@vmandic
vmandic / HzzoHtmlScraper.cs
Last active October 16, 2018 17:24
meds-processor, part 1 of 4, snippet 9
ISet<HzzoMedsDownloadDto> ParseHtmlDocuments(IDocument[] docs) =>
docs.Aggregate(
new HashSet<HzzoMedsDownloadDto>(),
(docList, doc) => new HashSet<HzzoMedsDownloadDto>(docList.Concat(ParseHtmlDocument(doc)))
);
static ISet<HzzoMedsDownloadDto> ParseMedsLiElements(IEnumerable<IElement> elems) =>
elems.Aggregate(new HashSet<HzzoMedsDownloadDto>(), (medsList, li) =>
{
var href = li.QuerySelector("a").GetAttribute("href");
@vmandic
vmandic / HzzoMedsDownloadDto.cs
Created October 13, 2018 18:38
meds-processor, part 1 of 4, snippet 8
using System;
using System.Linq;
namespace MedsProcessor.Common.Models
{
public class HzzoMedsDownloadDto
{
private readonly string _rootLocation;
public HzzoMedsDownloadDto(string href, string validFrom, string rootLocation)
@vmandic
vmandic / Constants.cs
Created October 13, 2018 18:18
meds-processor, part 1 of 6, snippet 7
namespace MedsProcessor.Common
{
public static class Constants
{
public const string CURRENT_LISTS_URL = "http://www.hzzo.hr/zdravstveni-sustav-rh/trazilica-za-lijekove-s-vazecih-lista/";
public const string ARCHIVE_LISTS_URL = "http://www.hzzo.hr/zdravstveni-sustav-rh/trazilica-za-lijekove-s-vazecih-lista/arhiva-liste-lijekova/";
public const string DOWNLOAD_DIR = "";
}
}
@vmandic
vmandic / HzzoHtmlScraper.cs
Created October 13, 2018 18:15
meds-processor, part 1 of 6, snippet 6
Task<IDocument[]> DownloadHtmlDocuments() =>
Task.WhenAll(
_browsingContext.OpenAsync(CURRENT_LISTS_URL),
_browsingContext.OpenAsync(ARCHIVE_LISTS_URL)
);
@vmandic
vmandic / HzzoHtmlScraper.cs
Created October 13, 2018 18:12
meds-processor, part 1 of 6, snippet 5
public async Task<ISet<HzzoMedsDownloadDto>> Run()
{
var htmlDocs = await DownloadHtmlDocuments();
var parsedDocs = ParseHtmlDocuments(htmlDocs);
return parsedDocs;
}
@vmandic
vmandic / Startup.cs
Created October 13, 2018 17:53
meds-processor, part 1 of 6, snippet 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MedsProcessor.Scraper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
@vmandic
vmandic / ServiceCollectionExtensions.cs
Created October 13, 2018 17:49
meds-processor, part 1 of 6, snippet 3
using AngleSharp;
using Microsoft.Extensions.DependencyInjection;
namespace MedsProcessor.Scraper
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddAngleSharp(this IServiceCollection services) =>
services.AddSingleton(BrowsingContext.New(
AngleSharp.Configuration.Default.WithDefaultLoader()));