/// <summary> /// Gets a default list of feeds for the current user's region, with a fall back to English. /// </summary> /// <returns></returns> public static List <FeedBookmark> GetDefaultRegionalFeeds() { if (_defaultFeeds == null) { try { // if the default feeds haven't been loaded yet, load xml from sever using (var client = new CompressionWebClient()) { // use our special client that has a lower timeout and uses compression by default string defaultFeedsData = client.DownloadString(DEFAULT_FEEDS_URL); // deserialize feeds from xml file var serializer = new XmlSerializer(typeof(RegionalFeedBookmarksCollection)); using (var reader = new StringReader(defaultFeedsData)) { var loadedFeeds = (RegionalFeedBookmarksCollection)serializer.Deserialize(reader); _defaultFeeds = new Dictionary <string, List <FeedBookmark> >(); foreach (var region in loadedFeeds) { _defaultFeeds[region.RegionCode] = region.FeedBookmarks; } } } } catch (Exception ex) { ServiceRegistration.Get <ILogger>().Warn("Unable to load default news feeds xml from server: {0}", ex.Message); // return an empty list, so next time this method is called it will try to download the default feeds again return(new List <FeedBookmark>()); } } // find the best matching list of feeds for the user's culture List <FeedBookmark> result; var culture = ServiceRegistration.Get <ILocalization>().CurrentCulture; // first try to get feeds for this language and region if (_defaultFeeds.TryGetValue(culture.Name, out result)) { return(result.ToList()); } // then try to get feeds for this language if (_defaultFeeds.TryGetValue(culture.TwoLetterISOLanguageName, out result)) { return(result.ToList()); } // fallback is always the generic english feeds return(_defaultFeeds["en"].ToList()); }
/// <summary> /// Gets a default list of feeds for the current user's region, with a fall back to English. /// </summary> /// <returns></returns> public static List<FeedBookmark> GetDefaultRegionalFeeds() { if (_defaultFeeds == null) { try { // if the default feeds haven't been loaded yet, load xml from sever using (var client = new CompressionWebClient()) { // use our special client that has a lower timeout and uses compression by default string defaultFeedsData = client.DownloadString(DEFAULT_FEEDS_URL); // deserialize feeds from xml file var serializer = new XmlSerializer(typeof(RegionalFeedBookmarksCollection)); using (var reader = new StringReader(defaultFeedsData)) { var loadedFeeds = (RegionalFeedBookmarksCollection) serializer.Deserialize(reader); _defaultFeeds = new Dictionary<string, List<FeedBookmark>>(); foreach (var region in loadedFeeds) _defaultFeeds[region.RegionCode] = region.FeedBookmarks; } } } catch (Exception ex) { ServiceRegistration.Get<ILogger>().Warn("Unable to load default news feeds xml from server: {0}", ex.Message); // return an empty list, so next time this method is called it will try to download the default feeds again return new List<FeedBookmark>(); } } // find the best matching list of feeds for the user's culture List<FeedBookmark> result; var culture = ServiceRegistration.Get<ILocalization>().CurrentCulture; // first try to get feeds for this language and region if (_defaultFeeds.TryGetValue(culture.Name, out result)) return result.ToList(); // then try to get feeds for this language if (_defaultFeeds.TryGetValue(culture.TwoLetterISOLanguageName, out result)) return result.ToList(); // fallback is always the generic english feeds return _defaultFeeds["en"].ToList(); }
protected override string DownloadJSON(string url) { var webClient = new CompressionWebClient(EnableCompression) { Encoding = Encoding.UTF8 }; foreach (var headerEntry in Headers) { webClient.Headers[headerEntry.Key] = headerEntry.Value; } LIMITER.RateLimit().Wait(); try { return(webClient.DownloadString(url)); } finally { LIMITER.RequestDone(); } }
protected override string DownloadJSON(string url) { bool retry = false; lock (_requestSync) { if (_denyRequestsUntilTime.HasValue) { if (RequestsDisabled) { return(null); } _failedRequests = 0; _denyRequestsUntilTime = null; } } var webClient = new CompressionWebClient(EnableCompression) { Encoding = Encoding.UTF8 }; foreach (var headerEntry in Headers) { webClient.Headers[headerEntry.Key] = headerEntry.Value; } LIMITER.RateLimit().Wait(); try { string fullUrl = Mirrors[_currentMirror] + url; string json = webClient.DownloadString(fullUrl); if (_failedRequests > 0) { _failedRequests--; } return(json); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.Timeout || (ex.Response != null && ( (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.ServiceUnavailable || ((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.RequestTimeout) ))) { //Rate limiting _failedRequests++; if (_failedRequests >= MAX_FAILED_REQUESTS) { DisableRequestsTemporarily(); return(null); } _currentMirror = (_currentMirror + 1) % Mirrors.Count; retry = true; } else { throw; } } finally { LIMITER.RequestDone(); } if (retry) { return(DownloadJSON(url)); } return(null); }