private async Task <string> GetAPITokenAsync(CancellationToken cancellationToken)
        {
            WebClientRequest request = new WebClientRequest(InitialPageUrl);

            request.Accept    = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.UserAgent = "animerecs.com stream update tool";

            Console.WriteLine("Getting Viewster API token.");
            using (IWebClientResult result = await _webClient.GetAsync(request, cancellationToken).ConfigureAwait(continueOnCapturedContext: false))
            {
                CookieCollection viewsterCookies = _webClient.Cookies.GetCookies(new Uri("https://www.viewster.com/"));
                if (viewsterCookies == null)
                {
                    throw new Exception("No cookie collection for Viewster somehow.");
                }
                Cookie apiTokenCookie = viewsterCookies["api_token"];

                if (apiTokenCookie == null)
                {
                    throw new Exception("Viewster api_token cookie not set on initial page request. Maybe Viewster changed their site.");
                }

                return(WebUtility.UrlDecode(apiTokenCookie.Value));
            }
        }
Ejemplo n.º 2
0
 public static async Task <string> GetStringAsync(this IWebClient webClient, WebClientRequest request, CancellationToken cancellationToken)
 {
     using (IWebClientResult result = await webClient.GetAsync(request, cancellationToken).ConfigureAwait(continueOnCapturedContext: false))
     {
         return(await result.ReadResponseAsStringAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false));
     }
 }