-2

researched on this topic and tried to implement different solutions, but I can't solved it. I have a function plotmyChartAsync() that is getting values from google trends. Using this function in Console Application works fine and I get my desiriable results. But when I try to perform this function with a button from a windows form, it always failing. With this " HttpResponseMessage secondResponse = await clientUsed.GetAsync(secondRequestStr);" I always get 429 failure from the API. It means I am sending to many request. How could I avoid this?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Globalization;
using System.Threading;

namespace GoogleTrendsAnalysis
{
    public partial class Form1 : Form
    {
        private SemaphoreSlim _mutex = new SemaphoreSlim(1);

        public Form1()
        {
            InitializeComponent();
        }

        public async Task plotmyChartAsync()
        {
            await _mutex.WaitAsync();
            try
            {
                const string searchKeyWord = "bmw";
                const string firstTimePeriodString = "2021-01-10T00";
                const string secondTimePeriodString = "+2021-01-15T00";

                const string httprequestStr = "https://trends.google.de/trends/api/explore?hl=de&tz=-60&req=%7B%22comparisonItem%22:%5B%7B%22keyword%22:%22" + searchKeyWord + "%22,%22geo%22:%22DE%22,%22time%22:%22" + firstTimePeriodString + secondTimePeriodString + "%22%7D%5D,%22category%22:0,%22property%22:%22%22%7D&tz=-60";



                CookieContainer cookies = new CookieContainer();
                HttpClientHandler handler = new HttpClientHandler();
                handler.CookieContainer = cookies;

                HttpClient client = new HttpClient(handler);
                HttpResponseMessage response = await client.GetAsync(httprequestStr, HttpCompletionOption.ResponseHeadersRead);


                Uri uri = new Uri(httprequestStr);
                Console.WriteLine(cookies.GetCookies(uri));
                IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri).Cast<Cookie>();

                Console.WriteLine(responseCookies.First());
                string cookieused = responseCookies.First().Name + "=" + responseCookies.First().Value;

                client.CancelPendingRequests();
                client.Dispose();
                handler.Dispose();


                using (var clientUsed = new HttpClient())
                {


                    clientUsed.BaseAddress = new Uri("https://trends.google.de/trends/api/");

                    clientUsed.DefaultRequestHeaders.Add("cookie", cookieused);

                    Console.WriteLine(clientUsed.DefaultRequestHeaders);

                    HttpResponseMessage responseUsed = await clientUsed.GetAsync(httprequestStr);
                    responseUsed.EnsureSuccessStatusCode();

           
                    Console.WriteLine(responseUsed);

                    if (responseUsed.IsSuccessStatusCode)
                    {

                        string result = responseUsed.Content.ReadAsStringAsync().Result;
                        result = result.Substring(4, result.Length - 4);
                        Console.WriteLine("Result: " + result);
                        dynamic data = JObject.Parse(result);

                        Console.WriteLine("TOKEN: " + data.widgets[0]["token"]);
                        string myToken = data.widgets[0]["token"];
          

                        string secondRequestStr = "https://trends.google.de/trends/api/widgetdata/multiline?hl=de&tz=-60&req=%7B%22time%22:%22" + firstTimePeriodString + "%5C%5C:00%5C%5C:00" + secondTimePeriodString + "%5C%5C:00%5C%5C:00%22,%22resolution%22:%22HOUR%22,%22locale%22:%22de%22,%22comparisonItem%22:%5B%7B%22geo%22:%7B%22country%22:%22DE%22%7D,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22" + searchKeyWord + "%22%7D%5D%7D%7D%5D,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22CM%22,%22category%22:0%7D%7D&token=" + myToken + "&tz=-60";
          
                        HttpResponseMessage secondResponse = await clientUsed.GetAsync(secondRequestStr);
                        secondResponse.EnsureSuccessStatusCode();
                        string secondResult = secondResponse.Content.ReadAsStringAsync().Result;

     
                        List<DateTime> dateTimes = new List<DateTime>();
                        List<double> values = new List<double>();

                        secondResult = secondResult.Substring(5, secondResult.Length - 5);
                        dynamic secondData = JObject.Parse(secondResult);
                        foreach (var t in secondData)
                        {
                      
                            foreach (var x in t)
                            {


                                foreach (var s in x.timelineData)
                                {
                                    bool ifhasDataStr = s.hasData[0];
                                    if (ifhasDataStr == true)
                                    {

                                        string formattedValueStr = s.formattedValue[0];
                                        string formattedTimeStr = s.formattedTime;
                                        double myValue = Convert.ToDouble(formattedValueStr);
                                        formattedTimeStr = formattedTimeStr.Replace(" um ", " ");

                                        DateTime dt = DateTime.ParseExact(formattedTimeStr, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture);
                                        dateTimes.Add(dt);
                                        values.Add(myValue);

                                        Console.WriteLine("Die ZEIT: " + formattedTimeStr + " / DER WERT: " + myValue);
                                    }
                                }
                            }
                        }
                        double numberOfPeriods = dateTimes.Count;
                        double[] valuesArr = values.ToArray();
                        double[] xs = new double[valuesArr.Length];

                        for (int i = 0; i < valuesArr.Length; i++)
                        {
                            xs[i] = dateTimes[i].ToOADate();
                        }





                        formsPlot1.Reset();
                        formsPlot1.plt.PlotScatter(xs, valuesArr);
                        formsPlot1.plt.Ticks(dateTimeX: true);
                        formsPlot1.Render();
                    }
                }
            }
            finally
            {
                _mutex.Release();
            }

        }

        protected async void button1_Click(object sender, EventArgs e)
        {
     
               await plotmyChartAsync();
            
          
        }
    }
}


  • 2
    "*How could I avoid this*" - don't send so many.... – TheGeneral Feb 06 '21 at 00:00
  • I dont realy get it how it can be done. I also cant understand at this point why is executes as expected with a console application, but fails here.. i will reasech more.. – tolyadzn Feb 06 '21 at 07:31

1 Answers1

0

After researching it I found that the problem was the cookie... Here are the Adjustment that need to be done to the code above...

var baseAddress = new Uri("https://trends.google.de/trends/api/");
            using (var firstHandler = new HttpClientHandler() { UseCookies = false })
            using (var clientUsed = new HttpClient(firstHandler) { BaseAddress = baseAddress })
            {


                clientUsed.BaseAddress = baseAddress;
                

                clientUsed.DefaultRequestHeaders.Accept.Clear();
                //clientUsed.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //clientUsed.DefaultRequestHeaders.Add("cookie", cookieused);
                

                var firstRequest = new HttpRequestMessage(HttpMethod.Get, httprequestStr);
                firstRequest.Headers.Add("cookie", cookieused);
                Console.WriteLine(httprequestStr);

                HttpResponseMessage responseUsed = await clientUsed.SendAsync(firstRequest);
                Console.WriteLine(responseUsed);
                string result = responseUsed.Content.ReadAsStringAsync().Result;
                Console.WriteLine(result);