Esempio n. 1
0
        public void Save(MyFitnessPalDay dayObject)
        {
            //db = dayObject.db;
            //MyFitnessPalDay dayDb = db.MyFitnessPalDays.FirstOrDefault(x => x.Id == dayObject.Id);
            //if (dayDb == null)
            //{
            //    //db.FoodItems.Add(foodItem);
            //    //db.SaveChanges();
            //    throw new NullReferenceException();
            //}
            //dayDb.DateOfPage = dayObject.DateOfPage;
            //dayDb.TimeStamp = dayObject.TimeStamp;
            //dayDb.Html = dayObject.Html;

            //db.Entry(dayDb).State = EntityState.Modified;
            //db.SaveChanges();
        }
 public void Update(MyFitnessPalDay day)
 {
     TimeStamp  = day.TimeStamp;
     DateOfPage = day.DateOfPage;
     Html       = day.Html;
 }
Esempio n. 3
0
        public static async Task <MfpDay> Parse(string website, MfpDay day)
        {
            HttpClient   http     = new HttpClient();
            HtmlDocument resultat = new HtmlDocument();
            String       source;

            try
            {
                var response = await http.GetByteArrayAsync(website);

                source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
                source = WebUtility.HtmlDecode(source);
                resultat.LoadHtml(source);
            }
            catch
            {
                string          date = day.SelectedDate.ToString("yyyy-MM-dd");
                MyFitnessPalDay mfpd = db.MyFitnessPalDays.FirstOrDefault(x => x.DateOfPage.ToString("yyyy-MM-dd") == date);
                if (mfpd == null)
                {
                    throw new HttpException();
                }
                source = mfpd.Html;
            }

            day.Htlm = source;


            List <HtmlNode> toftitle = resultat.DocumentNode.Descendants().Where
                                           (x => (x.Name == "td" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("first alt"))).ToList();

            Meal meal = new Meal();


            List <HtmlNode> listOfRows = resultat.DocumentNode.Descendants().Where(x => (x.Name == "tr")).ToList();

            foreach (HtmlNode node in listOfRows)
            {
                string line         = node.InnerText;
                bool   isHeaderLine = false;
                if (line.Trim() == string.Empty)
                {
                    break;
                }
                if (line.Contains("AM - 12"))
                {
                    meal         = Meal.Meal1;
                    isHeaderLine = true;
                }
                if (line.Contains("12 - 13"))
                {
                    meal         = Meal.Meal2;
                    isHeaderLine = true;
                }
                if (line.Contains("13 - 14"))
                {
                    meal         = Meal.Meal3;
                    isHeaderLine = true;
                }
                if (line.Contains("14 - 17"))
                {
                    meal         = Meal.Meal4;
                    isHeaderLine = true;
                }
                if (line.Contains("17 - 20"))
                {
                    meal         = Meal.Meal5;
                    isHeaderLine = true;
                }
                if (line.Contains("20 - PM"))
                {
                    meal         = Meal.Meal6;
                    isHeaderLine = true;
                }

                if (!isHeaderLine)
                {
                    FoodItemViewModel fivm = ParseLine(line);
                    fivm.MealId = (int)meal;
                    day.All.Add(fivm);
                    switch (meal)
                    {
                    case Meal.Meal1:
                        day.meal1.Add(fivm);
                        break;

                    case Meal.Meal2:
                        day.meal2.Add(fivm);
                        break;

                    case Meal.Meal3:
                        day.meal3.Add(fivm);
                        break;

                    case Meal.Meal4:
                        day.meal4.Add(fivm);
                        break;

                    case Meal.Meal5:
                        day.meal5.Add(fivm);
                        break;

                    case Meal.Meal6:
                        day.meal6.Add(fivm);
                        break;

                    default:
                        break;
                    }
                }
            }
            return(day);
        }