internal void Download(ModInfo mod, string targetfile = null) { if (mod.Releases == null) { //only got half of information? try to complete with online mods //TODO: complete mod } List <ModInfo.ReleaseInfo> releases = new List <ModInfo.ReleaseInfo>(mod.Releases); if (releases.Count > 0) { ModInfo.ReleaseInfo ri = releases[0]; ModInfo.ReleaseInfo.ReleaseFileInfo rfi = new List <ModInfo.ReleaseInfo.ReleaseFileInfo>(ri.Files)[0]; Task.Factory.StartNew(() => { Uri dl; if (rfi.Mirror != null) { dl = new Uri(rfi.Mirror.ToString()); } else { dl = rfi.Url; } using (WebClientCached wc = new WebClientCached()) { Console.WriteLine("Downloading: " + Path.GetFileName(dl.LocalPath)); wc.DownloadFile(dl, Path.Combine(Settings.Default.CachePath, Path.GetFileName(dl.LocalPath))); Console.WriteLine("Downloaded: " + Path.GetFileName(dl.LocalPath)); } }); } }
private List <ModInfo> _fetchAllMods() { try { using (WebClientCached wc = new WebClientCached()) { //fetching mods List <ModInfo> allMods = new List <ModInfo>(); for (int i = 1; i < int.MaxValue; i++) { OnFetchingOnlineProgress?.Invoke(2, 0, $"Fetching page {i}"); string modsStr = wc.GetString($"http://api.factoriomods.com/mods?page={i}"); List <ModInfo> modsPage = JsonConvert.DeserializeObject <List <ModInfo> >(modsStr); if (modsPage == null || modsPage.Count == 0) { OnFetchingOnlineProgress?.Invoke(2, 100, $"Fetched {i} pages", i > 100 ? i : int.MinValue); //allMods.AddRange(modsPage); ? run or not run... that's the question - btw: do null checking break; } allMods.AddRange(modsPage); } return(allMods); } } catch (Exception) { return(null); } }
private void _fetchThumbnails(List <ModInfo> allMods) { //fetching mods thumbnails int modCounter = 0; if (!Properties.Settings.Default.OnlineModsSkipThumbnails) { OnFetchingOnlineProgress?.Invoke(3, modCounter, $"Worked on {modCounter} mods", allMods.Count); foreach (ModInfo mod in allMods) //TODO: try again as multithreaded... was Parallel but that had a lot of race conditions { modCounter++; Console.WriteLine($"Getting image of {mod.Name}"); string cacheFile = mod.GetImageCacheFile(Settings.Default.CachePath); //in cache? if (File.Exists(cacheFile)) { continue; } if (string.IsNullOrWhiteSpace(mod.Url)) { continue; } // ... if not using (WebClientCached thumbnailClient = new WebClientCached()) { string pageContent = thumbnailClient.GetString(mod.Url); //<div class='mod-image'><a href="http://i.imgur.com/1LrWdOj.jpg"><img src="http://i.imgur.com/1LrWdOjl.jpg" /></a></div> Match match = Regex.Match(pageContent, "<div class=\'mod-image\'>.+?<img src=\"(?<ImgUri>.+?)\" />.+?</div>", RegexOptions.Compiled); if (match.Success) { try { Debug.WriteLine($"Downloading: {mod.Name}"); thumbnailClient.DownloadFile(match.Result("$1"), cacheFile); } catch { // ignored } } } OnFetchingOnlineProgress?.Invoke(3, modCounter, $"Worked on {modCounter} mods"); } } OnFetchingOnlineProgress?.Invoke(3, modCounter, $"Worked on {allMods.Count} mods", allMods.Count); }
private List <CategorieInfo> _getCategories() { try { using (WebClientCached wc = new WebClientCached()) { string categories = wc.GetString("http://api.factoriomods.com/categories/"); List <CategorieInfo> res = JsonConvert.DeserializeObject <List <CategorieInfo> >(categories); OnFetchingOnlineProgress?.Invoke(1, 100, $"Found: {res.Count}"); return(res); //CategorieInfo } } catch (Exception) { return(null); } }