コード例 #1
0
        public IActionResult CreateSocialData(AppDataCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueSocialFileName = ProcessUploadAppSocialImage(model);

                AppSocialAddress newAppSocialAddress = new AppSocialAddress
                {
                    UrlAddress   = model.UrlAddress,
                    AppSocialImg = uniqueSocialFileName
                };

                _appRepository.Add(newAppSocialAddress);

                TempData["message"] = $"Address {model.UrlAddress} was created.";
                return(RedirectToAction("SocialList"));
            }
            return(View());
        }
コード例 #2
0
        public async Task ScrapeAsync(string appId)
        {
            var url  = string.Format(StoreUrlTemplate, appId);
            var url2 = string.Format(StoreUrlTemplate2, appId);
            var msg  = new HttpRequestMessage(HttpMethod.Get, url);

            msg.Headers.Add("Accept", "text/json");
            var response = await Client.SendAsync(msg);

            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();

            var json = JsonConvert.DeserializeObject <dynamic>(content);

            if (json == null || json[appId]["success"] != true)
            {
                _loger?.Log(LogLevel.Warning, $"Steam url [{url}] returned unsuccessfull status (app not found)");
                return;
            }

            dynamic result = json[appId].data;
            var     data   = new AppData();

            data.Name = result.name;
            var headerUrl = result.header_image;

            var wc       = new System.Net.WebClient();
            var fileName = $"{path}/{result.steam_appid}.jpg";
            var fullPath = Path.Combine(Environment.CurrentDirectory, fileName);

            wc.DownloadFile(headerUrl.ToString(), fullPath);

            var doc           = new HtmlDocument();
            var hasWindowsReq = result.platforms?.windows;

            if (hasWindowsReq != null && (bool)hasWindowsReq)
            {
                try
                {
                    var hasMinimum = result.pc_requirements?.minimum;
                    if (hasMinimum != null)
                    {
                        doc.LoadHtml(result.pc_requirements?.minimum.ToString());
                        var minHtml = doc.DocumentNode.Descendants("li")
                                      .Select(x => x)
                                      .ToList();
                        if (minHtml.Any())
                        {
                            var req = SystemRequirementses(minHtml, RequirementsType.Minimum, SystemType.windows);
                            data.Requirements.Add(req);
                        }
                    }

                    var hasRecomedeted = result.pc_requirements?.recommended;
                    if (hasRecomedeted != null)
                    {
                        doc.LoadHtml(result.pc_requirements?.recommended?.ToString());

                        var maxHtml = doc.DocumentNode.Descendants("li")
                                      .Select(x => x)
                                      .ToList();
                        if (maxHtml.Any())
                        {
                            var req = SystemRequirementses(maxHtml, RequirementsType.Recommended, SystemType.windows);
                            data.Requirements.Add(req);
                        }
                    }
                }
                catch (Exception e)
                {
                    _loger.LogWarning(e.Message);
                }
            }

            var hasMacOsReq = result.platforms?.mac;

            if (hasMacOsReq != null && (bool)hasMacOsReq)
            {
                try
                {
                    doc.LoadHtml(result.mac_requirements?.minimum?.ToString());
                    var minHtml = doc.DocumentNode.Descendants("li")
                                  .Select(x => x)
                                  .ToList();
                    if (minHtml.Any())
                    {
                        var req = SystemRequirementses(minHtml, RequirementsType.Minimum, SystemType.macOs);
                        data.Requirements.Add(req);
                    }

                    doc.LoadHtml(result.mac_requirements?.recommended?.ToString());
                    var reqHtml = doc.DocumentNode.Descendants("li")
                                  .Select(x => x)
                                  .ToList();
                    if (reqHtml.Any())
                    {
                        var req = SystemRequirementses(reqHtml, RequirementsType.Recommended, SystemType.macOs);
                        data.Requirements.Add(req);
                    }
                }
                catch (Exception e)
                {
                    _loger.LogWarning(e.Message);
                }
            }

            var hasLinuxOsReq = result.platforms?.linux;

            if (hasLinuxOsReq != null && (bool)hasLinuxOsReq)
            {
                try
                {
                    doc.LoadHtml(result.linux_requirements?.minimum?.ToString());
                    var minHtml = doc.DocumentNode.Descendants("li")
                                  .Select(x => x)
                                  .ToList();
                    if (minHtml.Any())
                    {
                        var req = SystemRequirementses(minHtml, RequirementsType.Minimum, SystemType.linux);
                        data.Requirements.Add(req);
                    }

                    doc.LoadHtml(result.linux_requirements?.recommended?.ToString());
                    var recHtml = doc.DocumentNode.Descendants("li")
                                  .Select(x => x)
                                  .ToList();
                    if (recHtml.Any())
                    {
                        var req = SystemRequirementses(recHtml, RequirementsType.Recommended, SystemType.linux);
                        data.Requirements.Add(req);
                    }
                }

                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            foreach (var item in result.genres)
            {
                var genre = _genresRepository.GetByName(item.description?.ToString());
                if (genre == null)
                {
                    genre = new Genre()
                    {
                        Name = item.description
                    };
                    _genresRepository.Add(genre);
                    _genresRepository.SaveChanges();
                }

                data.Genres.Add(genre);
            }

            data.Name        = result.name;
            data.Published   = result.publishers[0]?.ToString();
            data.Developer   = result.developers[0]?.ToString();
            data.Description = Utils.StripHtml(result.about_the_game);
            data.ReleaseDate = result.release_date.date;
            data.ImagePath   = fileName;
            _appDatRepository.Add(data);
            var searchApp = _searchDataRepository.GetByAppId(appId);

            _uow.Commit();
        }