-2

Okay so I'm still quite new to coding and mostly only knows the basics. I have never worked with API. I'm trying to make a program that gets the number of subs from PewDiePie and Cocomelon and compare them.

namespace PewdiepieVsCoco
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var len = args?.Length;

                if (len == null || len.Value == 0)
                {
                    PrintStart();
                    return;
                }

                var pdpSubCount = args[0];
                var pdpSub = GetPDPSubcount(pdpSubCount).Result;

                PrintPDPResult(pdpSub);

            }

            catch (AggregateException agg)
            {
                foreach (var e in agg.Flatten().InnerExceptions)
                    Console.WriteLine(e.Message);
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }

        private static async Task<dynamic> GetPDPSubcount(string pdpSubCount)
        {
            var parameters = new Dictionary<String, String>
            {
                ["key"] = ConfigurationManager.AppSettings["APIKey"],
                ["channelsId"] = "UC-lHJZR3Gqxm24_Vd_AJ5Yw",
                ["part"] = "statistics",
                ["forUsername"] = "PewDiePie",
                ["fields"] = "items/statistics(subscriberCount)"
            };

            var baseUrl = "https://www.googleapis.com/youtube/v3/channels";
            var fullUrl = MakeUrlWithQuery(baseUrl, parameters);

            var pdpSub =  await new HttpClient().GetStringAsync(fullUrl);
            if (pdpSub != null)
            {
                //Does the thing
                return JsonConvert.DeserializeObject(pdpSubCount);
            }

            return default(dynamic);
        }

        private static string MakeUrlWithQuery(string baseUrl, 
            IEnumerable<KeyValuePair<string, string>> parameters)
        {
            if (string.IsNullOrEmpty(baseUrl))
                throw new ArgumentException(nameof(baseUrl));

            if (parameters == null || parameters.Count() == 0)
                return baseUrl;

            return parameters.Aggregate(baseUrl,
                (accumulated, kvp) => string.Format($"{accumulated}{kvp.Value}&"));

        }

        private static void PrintPDPResult(dynamic pdpSub)
        {
            Console.WriteLine($"PewDiePie currently have: {pdpSub} subscribers");//insert subs
        }


        private static void PrintStart()
        {
            Console.WriteLine("The war is on!");
            Thread.Sleep(500);
            Console.WriteLine("PewDiePie Vs Cocomelon – Nursery Rhymes");
            Thread.Sleep(500);
        }
    }
}

Here is the code, I have followed what an Indian dude did in a YT video so some things I don't know what do but I have an idea on what's going on.

  • Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – EJoshuaS - Stand with Ukraine Jun 06 '20 at 02:48

1 Answers1

0

Your program is likely returning before making any API calls because you aren't executing it with any command line arguments. Remove this block of code, so it continues without returning early.

var len = args?.Length;

if (len == null || len.Value == 0)
{
    PrintStart();
    return;
}

Also, pdpSubCount should not be a function argument, and should not be passed into DeserializeObject. You should be deserializing the response from the api call

return JsonConvert.DeserializeObject(pdpSub)
Chris Danna
  • 1,164
  • 8
  • 17