using System using System Collections Generic using System Linq using

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HtmlAgilityPack;
namespace ObedServer.Models
{
public class DishProcessor
{
private const string Url = "http://www.obedtula.ru";
private readonly HtmlDocument html;
public IList<Dish> Dishes { get; private set; }
public DishProcessor(HtmlDocument html)
{
this.html = html;
Dishes = new List<Dish>();
}
public void GenerateList()
{
int pos = 0;
var nodeWithMenu = html.DocumentNode.SelectSingleNode("//div[@class='simplemenu']");
var tablesWithMenu = nodeWithMenu.SelectNodes("table[@class='menu-table']");
List<string> dishTypeList = GenereteDishTypeList(nodeWithMenu);
foreach (var table in tablesWithMenu)
{
var rowsOfTalbe = table.ChildNodes.Where(x => x.Name == "tr").Skip(1).ToArray();
foreach (var row in rowsOfTalbe)
{
Dish dish = GenerateDish(row, dishTypeList[pos]);
Dishes.Add(dish);
}
pos++;
}
}
private List<Garnish> HasGarnishes(HtmlNode node)
{
List<Garnish> garnishes = new List<Garnish>();
var menuWithGarnish = node.SelectSingleNode("div[@class='list-garnir']");
if (menuWithGarnish != null)
{
var rowWithGarnish = menuWithGarnish.ChildNodes["table"].ChildNodes.Where(x => x.Name == "tr").ToArray();
foreach (var rowWithFood in rowWithGarnish)
{
foreach (var columnsInRow in rowWithGarnish)
{
var columnsWithData = columnsInRow.ChildNodes.Where(x => x.Name == "td").ToArray();
garnishes.Add(AddGarnish(columnsWithData));
}
}
return garnishes;
}
return null;
}
private Garnish AddGarnish(HtmlNode[] garnish)
{
return new Garnish()
{
Name = garnish[0].ChildNodes["a"].InnerText,
Weight = int.Parse(RemoveSpecialSymbols(garnish[1].InnerText.Split(' ')[0])),
Price = int.Parse(RemoveSpecialSymbols(garnish[2].InnerText.Split(' ')[0])),
};
}
private List<string> GenereteDishTypeList(HtmlNode node)
{
return node.ChildNodes.Where(x => x.Name == "p").Select(x => x.InnerText).ToList();
}
private string GetUrlOfPic(HtmlNode column)
{
string urlOfPic;
try
{
urlOfPic = Url + column.ChildNodes["img"].Attributes["src"].Value;
}
catch
{
urlOfPic = "";
}
return urlOfPic;
}
private string GetTextFromColumn(HtmlNode column)
{
return RemoveSpecialSymbols(column.FirstChild.InnerText);
}
private Dish GenerateDish(HtmlNode rowWithFood, string dishType)
{
Dish dish = new Dish();
List<int> weightAndPrice = new List<int>();
var columnsOfRow = rowWithFood.ChildNodes.Where(x => x.Name == "td").ToArray();
foreach (var column in columnsOfRow)
{
if (column.HasAttributes)
{
switch (column.Attributes["class"].Value)
{
case "img":
dish.Pic = GetUrlOfPic(column);
break;
case "name":
dish.Name = GetTextFromColumn(column);
dish.Garnishes = HasGarnishes(column);
break;
case "desc":
dish.Description = GetTextFromColumn(column);
break;
}
}
else
{
var value = column.ChildNodes[0].InnerText;
var element = int.Parse(value.Split(' ')[0]);
weightAndPrice.Add(element);
}
}
dish.DishType = dishType;
dish.Weight = weightAndPrice[0];
dish.Price = weightAndPrice[1];
return dish;
}
private string RemoveSpecialSymbols(string str)
{
return str.Replace("&nbsp", " ").Replace(";", "").Replace("\n", "").Replace("\t", "");
}
}
}