public static async Task <WebDirectory> ParseIndex(IHtmlDocument htmlDocument, HttpClient httpClient, WebDirectory webDirectory) { try { OpenDirectoryIndexer.Session.GDIndex = true; if (!OpenDirectoryIndexer.Session.Parameters.ContainsKey(Constants.Parameters_Password)) { Console.WriteLine($"{Parser} will always be indexed at a maximum rate of 1 per second, else you will run into problems and errors."); Logger.Info($"{Parser} will always be indexed at a maximum rate of 1 per second, else you will run into problems and errors."); Console.WriteLine("Check if password is needed (unsupported currently)..."); Logger.Info("Check if password is needed (unsupported currently)..."); OpenDirectoryIndexer.Session.Parameters[Constants.Parameters_Password] = string.Empty; Dictionary <string, string> postValues = new Dictionary <string, string> { { "password", OpenDirectoryIndexer.Session.Parameters[Constants.Parameters_Password] }, { "page_token", string.Empty }, { "page_index", "0" }, { "q", "" } }; HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, webDirectory.Uri) { Content = new FormUrlEncodedContent(postValues) }; HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage); if (httpResponseMessage.IsSuccessStatusCode) { string responseJson = await DecodeResponse(htmlDocument, httpClient, httpResponseMessage); BhadooIndexResponse response = BhadooIndexResponse.FromJson(responseJson); webDirectory = await ScanAsync(htmlDocument, httpClient, webDirectory); } } else { webDirectory = await ScanAsync(htmlDocument, httpClient, webDirectory); } } catch (Exception ex) { Logger.Error(ex, $"Error parsing {Parser} for URL: {webDirectory.Url}"); webDirectory.Error = true; OpenDirectoryIndexer.Session.Errors++; if (!OpenDirectoryIndexer.Session.UrlsWithErrors.Contains(webDirectory.Url)) { OpenDirectoryIndexer.Session.UrlsWithErrors.Add(webDirectory.Url); } throw; } return(webDirectory); }
private static async Task <WebDirectory> ScanAsync(IHtmlDocument htmlDocument, HttpClient httpClient, WebDirectory webDirectory) { webDirectory.Parser = Parser; try { Polly.Retry.AsyncRetryPolicy asyncRetryPolicy = Library.GetAsyncRetryPolicy((ex, waitTimeSpan, retry, pollyContext) => { Logger.Warn($"Error retrieving directory listing for {webDirectory.Uri}, waiting {waitTimeSpan.TotalSeconds} seconds.. Error: {ex.Message}"); RateLimiter.AddDelay(waitTimeSpan); }, 8); if (!webDirectory.Url.EndsWith("/")) { webDirectory.Url += "/"; } long pageIndex = 0; string nextPageToken = string.Empty; do { await asyncRetryPolicy.ExecuteAndCaptureAsync(async() => { await RateLimiter.RateLimit(); Logger.Warn($"Retrieving listings for {webDirectory.Uri.PathAndQuery}, page {pageIndex + 1}{(!string.IsNullOrWhiteSpace(OpenDirectoryIndexer.Session.Parameters[Constants.Parameters_Password]) ? $" with password: {OpenDirectoryIndexer.Session.Parameters[Constants.Parameters_Password]}" : string.Empty)}"); Dictionary <string, string> postValues = new Dictionary <string, string> { { "password", OpenDirectoryIndexer.Session.Parameters[Constants.Parameters_Password] }, { "page_token", nextPageToken }, { "page_index", pageIndex.ToString() } }; HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, webDirectory.Uri) { Content = new FormUrlEncodedContent(postValues) }; HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage); webDirectory.ParsedSuccessfully = httpResponseMessage.IsSuccessStatusCode; httpResponseMessage.EnsureSuccessStatusCode(); try { string responseJson = await DecodeResponse(htmlDocument, httpClient, httpResponseMessage); BhadooIndexResponse indexResponse = BhadooIndexResponse.FromJson(responseJson); webDirectory.ParsedSuccessfully = indexResponse.Data.Error == null; if (indexResponse.Data.Error?.Message == "Rate Limit Exceeded") { throw new Exception("Rate limit exceeded"); } else { if (indexResponse.Data.Files == null) { throw new Exception("Directory listing retrieval error (Files null)"); } else { nextPageToken = indexResponse.NextPageToken; pageIndex = indexResponse.CurPageIndex + 1; foreach (File file in indexResponse.Data.Files) { if (file.MimeType == FolderMimeType) { webDirectory.Subdirectories.Add(new WebDirectory(webDirectory) { Parser = Parser, // Yes, string concatenation, do not use new Uri(webDirectory.Uri, file.Name), because things could end with a space... Url = $"{webDirectory.Uri}{GetSafeName(file.Name)}/", Name = file.Name }); } else { webDirectory.Files.Add(new WebFile { Url = new Uri(webDirectory.Uri, GetSafeName(file.Name)).ToString(), FileName = file.Name, FileSize = file.Size }); } } } } } catch (Exception ex) { throw new Exception($"Retrieving listings for {webDirectory.Uri.PathAndQuery}, page {pageIndex + 1}. Error: {ex.Message}"); } }); } while (!string.IsNullOrWhiteSpace(nextPageToken)); } catch (Exception ex) { Logger.Error(ex, $"Error retrieving directory listing for {webDirectory.Url}"); webDirectory.Error = true; OpenDirectoryIndexer.Session.Errors++; if (!OpenDirectoryIndexer.Session.UrlsWithErrors.Contains(webDirectory.Url)) { OpenDirectoryIndexer.Session.UrlsWithErrors.Add(webDirectory.Url); } //throw; } return(webDirectory); }