private static async Task PopulateTargetSpecies(ExpiredProduct expiredProduct) { var tdoc = ""; // ReSharper disable once RedundantAssignment var tdocx = ""; // ReSharper disable once RedundantAssignment var tf = ""; var doc = expiredProduct.SPC_Link.EndsWith(".doc", StringComparison.InvariantCultureIgnoreCase); if (doc) { tf = Path.GetTempFileName(); tdoc = $"{tf}.doc"; File.Move(tf, tdoc); using (var fs = File.OpenWrite(tdoc)) { (await GetHttpStream(expiredProduct.SPC_Link)).CopyTo(fs); fs.Flush(); } tdocx = WordConverter.ConvertDocToDocx(tdoc); } else { tf = Path.GetTempFileName(); tdocx = $"{tf}.docx"; File.Move(tf, tdocx); using (var fs = File.OpenWrite(tdocx)) { (await GetHttpStream(expiredProduct.SPC_Link)).CopyTo(fs); } } using (var spcStream = File.Open(tdocx, FileMode.Open)) { var ts = SPCParser.GetTargetSpecies(spcStream); expiredProduct.TargetSpecies = ts; } if (!string.IsNullOrEmpty(tdoc) && File.Exists(tdoc)) { File.Delete(tdoc); } if (!string.IsNullOrEmpty(tdocx) && File.Exists(tdocx)) { File.Delete(tdocx); } PopulateStaticTypedTargetSpecies(expiredProduct); }
private static async Task PopulateTargetSpeciesFromEma(ReferenceProduct expiredProduct) { var possibleTargetSpecies = new Dictionary <string, string[]>(); var searchResults = await EPARTools.GetSearchResults(expiredProduct.Name); foreach (var result in searchResults) { var tf = Path.GetTempFileName(); using (var tfs = File.OpenWrite(tf)) { (await GetHttpStream(result)).CopyTo(tfs); } var targetSpecies = SPCParser.GetTargetSpeciesFromMultiProductPdf(tf); foreach (var ts in targetSpecies) { possibleTargetSpecies[ts.Key.ToLowerInvariant()] = ts.Value; } File.Delete(tf); } var productKey = expiredProduct.Name.ToLowerInvariant(); //exact name match if (possibleTargetSpecies.ContainsKey(productKey)) { expiredProduct.TargetSpecies = possibleTargetSpecies[productKey]; } //todo:smarter nonexact matching //name starts with else if (possibleTargetSpecies.Keys.Any(k => k.StartsWith(productKey))) { productKey = possibleTargetSpecies.Keys.Single(k => k.StartsWith(productKey)); expiredProduct.TargetSpecies = possibleTargetSpecies[productKey]; } //resolve inconsistent spacing in name between VMD and EMA else if (possibleTargetSpecies.Keys.Any(k => k.Replace(" ", "").Equals(productKey.Replace(" ", "")))) { productKey = possibleTargetSpecies.Keys.Single(k => k.Replace(" ", "").Equals(productKey.Replace(" ", ""))); expiredProduct.TargetSpecies = possibleTargetSpecies[productKey]; } //get the bit after "for" in the name. Risky - e.g. "solution for injection" //could maybe do with species lookup for validation else if (productKey.Contains(" for ")) { var forSplit = productKey.Split(new[] { "for" }, StringSplitOptions.None); var postFor = forSplit[forSplit.Length - 1].Replace("and", ",").Split(',') .Select(t => t.Trim().ToLowerInvariant()) .Where(t => !string.IsNullOrWhiteSpace(t)); expiredProduct.TargetSpecies = postFor.ToArray(); } else { Debug.WriteLine($"{expiredProduct.Name} ReferenceProduct not found"); } PopulateStaticTypedTargetSpecies(expiredProduct); }