public string GetSessionFileName(string query, int pageIndex) { return(PathFormatter.GetSession(GetSessionQuery(query, pageIndex))); }
private SearchResult Search(bool checkSession, int lifetime, string uri, string searchQuery) { SearchResult searchResult; string url = "https://nhentai.net/api/galleries/" + uri; string sessionName = uri.Replace("?", "/"); if (SearchResultCache.Items.TryGetValue(sessionName, out searchResult)) { BrowsingModel.AddSearchHistory(searchQuery); Logger.InfoLineFormat("Loaded SearchResult from memory cache: {0}", searchQuery); return(searchResult); } string cachedSearchResultsFilePath = PathFormatter.GetSession(sessionName); if (File.Exists(cachedSearchResultsFilePath)) { if (checkSession && lifetime > 0) { FileInfo cachedSessionFileInfo = new FileInfo(cachedSearchResultsFilePath); DateTime now = DateTime.Now; if ((now - cachedSessionFileInfo.CreationTime).TotalMilliseconds > lifetime) { File.Delete(cachedSearchResultsFilePath); } } searchResult = JsonUtility.LoadFromFile <SearchResult>(cachedSearchResultsFilePath); if (searchResult != null) { if (lifetime != 0) { SearchResultCache.Items.Add(sessionName, searchResult); } BrowsingModel.AddSearchHistory(searchQuery); Logger.InfoLineFormat("Loaded SearchResult from file cache: {0}", searchQuery); return(searchResult); } } Logger.InfoLineFormat("Downloading SearchResult: {0}", searchQuery); try { using (HttpResponseMessage response = Task.Run(() => HttpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead)).GetAwaiter().GetResult()) { if (!response.IsSuccessStatusCode) { Logger.ErrorLineFormat("{0} ({1})", response.ReasonPhrase, response.StatusCode); response.EnsureSuccessStatusCode(); return(null); } string jsonText = Task.Run(() => response.Content.ReadAsStringAsync()).GetAwaiter().GetResult(); searchResult = JsonConvert.DeserializeObject <SearchResult>(jsonText); if (lifetime != 0) { if (searchResult != null) { if (searchResult.Error) { Logger.WarnLineFormat("The server returned an error while downloading search result."); throw new InvalidHttpResponseException("The server returned an error while downloading search result."); } Logger.LogLineFormat("File caching SearchResult: {0}", searchQuery); SearchResultCache.Items.Add(sessionName, searchResult); Directory.CreateDirectory(Path.GetDirectoryName(cachedSearchResultsFilePath)); File.WriteAllText(cachedSearchResultsFilePath, jsonText); } } } } catch (Exception ex) { Logger.ErrorLine(ex.ToString()); throw; } BrowsingModel.AddSearchHistory(searchQuery); return(searchResult); }
public string GetSessionFileName(int tagId, int pageIndex) { return(PathFormatter.GetSession(GetSessionQuery(tagId, pageIndex))); }