示例#1
0
        /// <summary>
        /// Get a list of fish from a website
        /// </summary>
        /// <param name="url">The website url</param>
        /// <param name="havnId">Habour id</param>
        /// <returns>A list of fish</returns>
        public IEnumerable <Fisk> GetContentFiskeContent(string url, int havnId)
        {
            var allFish = new List <Fisk>();
            var web     = new HtmlWeb();
            var doc     = web.Load(url);

            var nodes = doc.DocumentNode.SelectNodes("//table/tr");
            var table = new DataTable("MyTable");

            table.Columns.Add("Havn");
            table.Columns.Add("Art");
            table.Columns.Add("Sort");
            table.Columns.Add("Gns.Pris");
            table.Columns.Add("Max.Kr");
            table.Columns.Add("Mængde");


            var rows = nodes.Skip(5).Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToArray());

            foreach (var row in rows.TakeWhile(row => row.Count() >= 3))
            {
                table.Rows.Add(row);
                var art = GetArtFromName(HttpUtility.HtmlDecode(row[1]));
                if (art == null)
                {
                    var artToAdd = new Arter {
                        Name = HttpUtility.HtmlDecode(row[1]).Normalize().Trim()
                    };
                    art = AddArt(artToAdd);
                }
                var havn    = GetHavnFromId(havnId);
                var dato    = DateTime.UtcNow;
                var addFish = new Fisk
                {
                    Arter = art,
                    Havn  = havn,
                    Date  = dato,
                };
                addFish.AvgPrice =
                    decimal.Parse(
                        HttpUtility.HtmlDecode(row[3]).Normalize(NormalizationForm.FormKC).Split(' ')[1].Replace(
                            ',', '.'));
                addFish.MaxPrice =
                    decimal.Parse(
                        HttpUtility.HtmlDecode(row[4]).Normalize(NormalizationForm.FormKC).Split(' ')[1].Replace(
                            ',', '.'));
                addFish.Amount =
                    decimal.Parse(
                        HttpUtility.HtmlDecode(row[5]).Normalize(NormalizationForm.FormKC).Split(' ')[0].Replace(
                            '.', ','));
                addFish.Sort = int.Parse(HttpUtility.HtmlDecode(row[2]).Normalize(NormalizationForm.FormKC));
                allFish.Add(addFish);
            }

            return(allFish);
        }
示例#2
0
        //Fish methods
        #region Fish

        /// <summary>
        /// Add fish (single)
        /// </summary>
        /// <param name="fish">Fish object to add</param>
        public void AddFisk(Fisk fish)
        {
            _repo.Fisks.Add(fish);
            Save();
        }