Advertisement
Uziel

MFPDiaryScrape

Mar 6th, 2015
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1.  public class Scraper
  2.     {
  3.         public static string Scrape(string userName, string date)
  4.         {
  5.             //example: http://www.myfitnesspal.com/food/diary/chuckgross?date=2015-03-05
  6.             //var url = "http://www.myfitnesspal.com/food/diary/" + userName + "?date=" + date;
  7.             var url = "http://www.myfitnesspal.com/food/diary/chuckgross?date=2015-03-05";
  8.             string results;
  9.  
  10.             using (var client = new WebClient())
  11.             {
  12.                 results = client.DownloadString(url);
  13.             }
  14.  
  15.             var nutritionTable = GetNutritionTable(results);
  16.            
  17.             return results;
  18.         }
  19.  
  20.         private static string GetNutritionTable(string html)
  21.         {
  22.             var document = new HtmlDocument();
  23.             document.LoadHtml(html);
  24.  
  25.             //get the table we care about
  26.             HtmlNode primaryTable = document.DocumentNode.Descendants("table").First(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "table0");
  27.  
  28.             //get the totalsRow
  29.             //<tr class="total">
  30.             //  <td class="first">Totals</td>
  31.             //  <td>0</td>
  32.             //  <td>0</td>
  33.             //  <td>0</td>
  34.             //  <td>0</td>
  35.             //  <td>0</td>
  36.             //<td class="empty"></td>
  37.             //</tr>
  38.             HtmlNode totalsRow = primaryTable.Descendants("tr")
  39.                 .First(d => d.Attributes.Contains("class") && d.Attributes["class"].Value == "total");
  40.             HtmlNodeCollection totalCells = totalsRow.SelectNodes(".//td[not(@class='first') and not(@class='empty')]");
  41.             var totalCellsHtml = totalCells.Aggregate(string.Empty, (current, label) => current + label.OuterHtml);
  42.  
  43.             //get the HeaderRow
  44.             //<tfoot>
  45.             //  <tr>
  46.             //    <td class="first"></td>
  47.             //      <td class="alt">Calories</td>
  48.             //      <td class="alt">Protein</td>
  49.             //      <td class="alt">Carbs</td>
  50.             //      <td class="alt">Fat</td>
  51.             //      <td class="alt">Fiber</td>
  52.             //    <td class="empty"></td>
  53.             //  </tr>
  54.             //</tfoot>
  55.             HtmlNode headerRow = primaryTable.Descendants("tfoot")
  56.                 .First()
  57.                 .Descendants("tr")
  58.                 .First();
  59.  
  60.             HtmlNodeCollection headerCells = headerRow.SelectNodes(".//td[@class='alt']");
  61.             var headerCellsHtml = headerCells.Aggregate(string.Empty, (current, label) => current + label.OuterHtml);
  62.  
  63.            return BuildNutritionTable(totalCellsHtml, headerCellsHtml);
  64.         }
  65.         private static string BuildNutritionTable(string totalsHtml, string labelsHtml)
  66.         {
  67.             labelsHtml = Regex.Replace(labelsHtml, "<td class=\\\"alt\\\">", "<th>").Replace("</td>", "</th>");
  68.             StringBuilder table = new StringBuilder("<table class='resultsTable'><tr class='labels'>");
  69.             table.Append(labelsHtml);
  70.             table.Append("</tr><tr class='resultsTotals'>");
  71.             table.Append(totalsHtml);
  72.             table.Append("</tr></table>");
  73.             var results = table.ToString();
  74.             return results;
  75.         }
  76.  
  77.         public class NutritionRecord
  78.         {
  79.             public string Calories { get; set; }
  80.             public string Protein { get; set; }
  81.             public string Fat { get; set; }
  82.             public string Carbs { get; set; }
  83.         }
  84.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement