public static List <WebIndex> GetIndexes(string url) { using (WebClient client = new IndexWebClient()) { string html = client.DownloadString(url); List <WebIndex> Indexes = new List <WebIndex>(); foreach (Match match in REGEX_INDEX.Matches(html)) { if (match.Groups.Count == 5) { string iurl = Path.Combine(url, match.Groups[1].Value); string name = match.Groups[2].Value; string size = match.Groups[4].Value.Trim(); string date = match.Groups[3].Value; WebIndex index = new WebIndex() { Url = iurl, Name = name, Size = (size == "-") ? 0 : int.Parse(size), Date = date }; Indexes.Add(index); } } return(Indexes); } }
/// <summary> /// Retrieve available versions of Blender (from cache if available/recent) /// </summary> public static List <BlenderVersion> GetBlenderVersions(string cacheFile = null, string customFile = null) { if (cacheFile != null) { string fullCachePath = Path.GetFullPath(cacheFile); //Intercept designer if (fullCachePath.StartsWith("C:\\Windows")) { return(new List <BlenderVersion>()); } } List <BlenderVersion> custom = GetCustomBlenderVersions(customFile); //IMPORTANT: always use cache if able, or get a chance to get your IP blacklisted. Cache cache = Cache.GetCache(cacheFile); if (cache != null) { //Refresh every >= CACHE_DAYS days if (Math.Abs(cache.Date.DayOfYear - DateTime.Now.DayOfYear) < CACHE_DAYS) { return(custom.Concat(cache.Versions).ToList()); } } try { List <BlenderVersion> versions = new List <BlenderVersion>(); List <WebIndex> coreVersions = WebIndex.GetIndexes(VERSIONS_URL); //Parse Blenders Previous Version pages.. foreach (WebIndex v in coreVersions.Where(x => REGEX_BLENDERVERSION.IsMatch(x.Name) && !x.IsFile)) { List <WebIndex> subVersions = v.GetIndexes(); List <(WebIndex, Match)> matches = subVersions.Select(x => (x, REGEX_BLENDERSUBVERSION.Match(x.Name))) .Where(x => x.Item2.Groups.Count == 4) .ToList(); Dictionary <string, BlenderVersion> submapping = new Dictionary <string, BlenderVersion>(); foreach (var match in matches) { string url = match.Item1.Url; DateTime date = match.Item1.DateTime; string name = match.Item2.Groups[1].Value; string os = match.Item2.Groups[2].Value; string ext = match.Item2.Groups[3].Value.ToLower(); if (IsValidFile(os, ext)) { if (!submapping.ContainsKey(name)) { submapping.Add(name, new BlenderVersion() { Name = name, Date = date }); } submapping[name].AssignUrl(os, url); } } versions.AddRange(submapping.Values.Where(x => x.HasAll).ToList()); } List <BlenderVersion> vs = versions.OrderByDescending(x => x.Date).ToList(); //Prevent server spam Thread.Sleep(500); Cache.UpdateCache(vs, cacheFile); return(custom.Concat(vs).ToList()); } catch (Exception ex) { Console.WriteLine("Failed to load versions due to " + ex.Message); if (cache != null) { return(custom.Concat(cache.Versions).ToList()); } else { throw; } } }