using AngleSharp.Html.Dom; using AngleSharp.Html.Parser; using fivaldiHtml.model; using Microsoft.Azure.CosmosDB.Table; using Microsoft.Azure.Storage; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using taloyhtio.idp.parser.common.model; namespace fivaldiHtml { class Program { private static IHtmlDocument document; private static CloudTableClient tableClient; private static List flats; private static Dictionary flatsAzure; private static UserData user; private static CondoMapping condo; private static readonly Regex flatTitle = new Regex("^[A-Z]\\s\\d{1,3}$", RegexOptions.Compiled); private static readonly Regex feeTitle = new Regex("^\\d{3}$", RegexOptions.Compiled); private static readonly Regex date = new Regex("^\\d{2}\\.\\d{2}\\.\\d{4}$", RegexOptions.Compiled); private static readonly Regex containsNumber = new Regex("\\d+", RegexOptions.Compiled); private const string CONDONAME = "Asunto Oy Hiihtomäentie 34"; private static void InitClient() { var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings[Constants.KEY_AZURE_STORAGE].ConnectionString); tableClient = storageAccount.CreateCloudTableClient( new TableConnectionPolicy() { UseDirectMode = false }); } private static List GetCondo(string condoName) { var table = tableClient.GetTableReference("mdbCondoMappings"); table.CreateIfNotExists(); var allCondos = table.CreateQuery().ToList(); if (condoName != null) { return allCondos.Where(x => condoName.Contains(x.PMSCondoName)).ToList(); } return allCondos; } private static List GetFlats(Guid taloyhtioPMCId, string pmsCondoName) { if (Guid.Empty.Equals(taloyhtioPMCId) || string.IsNullOrEmpty(pmsCondoName)) { return null; } var table = tableClient.GetTableReference("mdbFlats"); table.CreateIfNotExists(); var query = table.CreateQuery() .Where(d => d.PMCTaloyhtioId == taloyhtioPMCId && d.CondoPMS == pmsCondoName) .ToArray() .OrderBy(o => o.FlatTitle); return query.ToList(); } static void Main(string[] args) { CollectData(args[0]); InitClient(); condo = GetCondo(CONDONAME)[0]; if (condo == null) { return; } flatsAzure = GetFlats(condo.TaloyhtioPMCId, CONDONAME).ToDictionary(x => x.FlatTitle, x => x.Id); SaveFees(); } private static void SaveFees() { var table = tableClient.GetTableReference("mdbMaintenanceFees"); table.CreateIfNotExists(); flats.Where(flat => flatsAzure.ContainsKey(flat.Title)).ToList().ForEach(flat => { flat.Users.ForEach(user => { user.Fees.ForEach(feeRec => { var fee = new MaintenanceFee(Guid.NewGuid()) { Payer = user.Name.Replace(",", ""), FlatTitle = flat.Title, FlatId = flatsAzure[flat.Title], PaymentType = feeRec.Name, PMSCondoName = condo.PMSCondoName, PMCTaloyhtioId = condo.TaloyhtioPMCId, PeriodOfTime = "202008", Fee = Convert.ToDouble(feeRec.Amount), PartitionKey = user.Name.Replace("/", ""), }; var result = table.Execute(TableOperation.InsertOrReplace(fee)); Console.WriteLine(result.HttpStatusCode); }); }); }); } private static void CollectData(string source) { string dataUrl = Directory.GetCurrentDirectory() + "\\" + source; string sourceDoc = File.ReadAllText(dataUrl, Encoding.GetEncoding(1252)); document = new HtmlParser().ParseDocument(sourceDoc); flats = new List(); var trs = document.QuerySelectorAll("tr").ToList(); for (int i = 0; i < trs.Count; i++) { var tr = trs[i]; var tds = tr.QuerySelectorAll("td").ToList(); if (tds.Count < 2) continue; var tdFlat = tds[1].TextContent; if (tdFlat == null || tdFlat.Trim() == "" || !flatTitle.IsMatch(tdFlat)) continue; // got flat row var flat = flats.FirstOrDefault(f => f.Title.Equals(tdFlat)); if (flat == null) { flats.Add(flat = new FlatData { Title = tdFlat }); } var userName = tds[7].TextContent; flat.Users.Add(user = new UserData { Name = userName, Row = i }); } foreach (var flat in flats) { foreach (var user in flat.Users) { var row = user.Row; while (true) { var tr = trs[row]; var tds = tr.QuerySelectorAll("td").ToList(); if (tds.Count < 2) { row++; continue; } var tdTitle = tds[1].TextContent.Trim(); if (feeTitle.IsMatch(tdTitle)) { var feeName = tds[3].TextContent.Trim(); var amount = tds[5].TextContent.Trim(); user.Fees.Add(new FeeData { Name = feeName, Amount = amount }); } if (tdTitle.Equals("Yhteensä")) { break; } row++; } } } } } }