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 / HzzoHtmlScraper.cs
Created October 19, 2018 19:32
meds-processor, p2, s8
ISet<HzzoMedsDownloadDto> ParseMedsLiElements(IEnumerable<IElement> elems) =>
elems.Aggregate(new HashSet<HzzoMedsDownloadDto>(), (medsList, li) =>
{
var href = li.QuerySelector("a").GetAttribute("href");
// NOTE: this domain is not available, links don't work :-(
if (!href.Contains("cdn.hzzo.hr"))
{
var dtParts = li.TextContent.TrimEnd().Split(' ').LastOrDefault().Split('.');
var downloadDto = new HzzoMedsDownloadDto(
@vmandic
vmandic / HzzoMedsDownloadDto.cs
Last active October 19, 2018 19:59
meds-processor, p2, s9
public class HzzoMedsDownloadDto
{
private readonly string _rootLocation;
public HzzoMedsDownloadDto(string href, string validFrom, string rootLocation)
{
this.Href = href;
this.ValidFrom = DateTime.Parse(validFrom);
this._rootLocation = rootLocation;
}
@vmandic
vmandic / AppController.cs
Created October 19, 2018 19:41
meds-processor, p2, s10
public async Task<ActionResult> Index(
[FromServices] HzzoHtmlScraper scraper,
[FromServices] HzzoExcelDownloader downloader)
{
var startTime = DateTime.Now;
var meds = await downloader.Run(await scraper.Run());
var totalTime = startTime - DateTime.Now;
return Ok(
$"Done! Handler duration: {totalTime.Duration()}" +
@vmandic
vmandic / HzzoExcelParser.cs
Last active October 24, 2018 20:38
meds-processor, p3, s1
using System.Collections.Generic;
using System.Threading.Tasks;
using MedsProcessor.Common.Models;
namespace MedsProcessor.Parser
{
public class HzzoExcelParser
{
public Task<ISet<HzzoMedsDownloadDto>> Run(ISet<HzzoMedsDownloadDto> meds)
{
@vmandic
vmandic / HzzoExcelParser.cs
Created October 25, 2018 16:51
meds-processor, p3, s2
public async Task<ISet<HzzoMedsDownloadDto>> Run(ISet<HzzoMedsDownloadDto> meds)
{
await Task.WhenAll(
// NOTE: due to excel docs designed in different ways, we do this separation of work
StartLongRunning(() => ParsePrimaryListsStartingWith2014_02(meds)),
StartLongRunning(() => ParseSupplementaryListsStartingWith2014_02(meds)),
StartLongRunning(() => ParsePrimaryListsUpTo2014_01(meds)),
StartLongRunning(() => ParseSupplementaryListsUpTo2014_01(meds))
);
@vmandic
vmandic / HzzoExcelParser.cs
Created October 25, 2018 18:17
meds-processor, p3, s3
static readonly DateTime filterDtStartWith2014 = new DateTime(2014, 1, 3);
void ParseSupplementaryListsUpTo2014_01(ISet<HzzoMedsDownloadDto> meds) =>
ParseHzzoExcelDocuments(meds.Where(x =>
x.ValidFrom <= filterDtStartWith2014 &&
(
x.FileName.ToLowerInvariant().Contains("dopunska") ||
x.FileName.ToLowerInvariant().Contains("dll")
)), DrugListType.Supplementary, false);
@vmandic
vmandic / HzzoMedsImportDto.cs
Created October 25, 2018 18:33
meds-processor, p3, s4
public class DrugImportDto
{
public int RowId { get; set; }
public DrugListType ListType { get; set; }
public DateTime ValidFrom { get; set; }
public string AtkCode { get; set; }
public DrugApplicationTypeLimitation ApplicationTypeLimitation { get; set; }
public string GenericName { get; set; }
@vmandic
vmandic / Enums.cs
Created October 25, 2018 18:46
meds-processor, p3, s5
using System;
namespace MedsProcessor.Parser
{
public enum DrugListType
{
Undefined = 0,
Primary = 1,
Supplementary = 2
}
@vmandic
vmandic / HzzoExcelParser.cs
Created October 25, 2018 20:38
meds-processor, p3, s6
static ISheet OpenWorkbookSheetWithNpoi(FileStream stream, HzzoMedsDownloadDto med, HzzoMedsDownloadDto latestMed)
{
ISheet drugListSheet = null;
try
{
if (med.FileName.ToLowerInvariant().EndsWith(".xls"))
{
var hssfWorkbook = new HSSFWorkbook(stream);
drugListSheet = hssfWorkbook.GetSheetAt(0);
@vmandic
vmandic / HzzoExcelParser.cs
Created October 25, 2018 22:27
meds-processor, p3, s7
void ParseHzzoExcelDocuments(IEnumerable<HzzoMedsDownloadDto> filteredMeds, DrugListType listType, bool isListStartingWith2014)
{
HzzoMedsDownloadDto latestMed = null;
int latestRow = 0;
int latestCol = 0;
try
{
Parallel.ForEach(filteredMeds, med =>
{