Пример #1
0
        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);
            }
        }
Пример #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);
        }
Пример #3
0
 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);
     }
 }