Exemple #1
0
    public static async Task prompt()
    {
        Console.WriteLine(@"
        Howdy, fatso!!  
        --------
        Enter a food or beverage to get caloric info:

        ");

        string    term         = Console.ReadLine();
        IJSONAPI  mashapi      = new MashapeAPI();
        var       nutrixapiKey = "8H6stHhT25mshXH1okEaCywiRiCUp1DYIsxjsnyespJHYCy7ca";
        Nutrition n            = await mashapi.GetData <Nutrition>(term, nutrixapiKey, "");

        if (term == "" || term == "n")
        {
            string hitGoogleWith = Search.mostCommonSearch(searchTerms);
            Console.WriteLine("most common search term:" + hitGoogleWith);                //mostCommonSearch(searchTerms);
            googler.promptGoogle(hitGoogleWith).Wait();
            Environment.Exit(0);
        }
        else if (n.hits.Count() > 0)
        {
            searchTerms.Add(term);
            string item     = n.hits.ElementAt(0).fields.item_name;
            double?calories = n.hits.ElementAt(0).fields.nf_calories ?? 0;
            double?fatGrams = n.hits.ElementAt(0).fields.nf_total_fat ?? 0;
            Console.WriteLine("Item: " + item + " Calories: " + calories + " Fat Grams: " + fatGrams + " grams");

            string        stickem = (item + calories.ToString() + fatGrams.ToString());
            List <string> list1   = new List <string>();
            list1.Add(item); list1.Add(calories.ToString()); list1.Add(fatGrams.ToString());

            if (!File.Exists(@"csv/nutrix.csv"))
            {
                File.AppendAllLines(@"csv/nutrix.csv", list1);

                // or : File.WriteAllText(@"csv/nutrix.csv", stickem); but writes all in 1 line
            }
            File.AppendAllLines(@"csv/nutrix.csv", list1);
        }

        prompt().Wait();
    }