Example #1
0
        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));
                    }
                });
            }
        }
Example #2
0
        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);
        }