示例#1
0
        public async Task <List <AppInfo> > GetGoogleData(string searchText)
        {
            googleApps.Clear();

            string str = googleSearchReq + searchText;

            var response = await client.GetByteArrayAsync(str);

            string source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);

            source = WebUtility.HtmlDecode(source);
            HtmlDocument document = new HtmlDocument();

            document.LoadHtml(source);

            List <HtmlNode> nodes = document.DocumentNode.Descendants()
                                    .Where(
                x => (x.Name == "div" &&
                      x.Attributes["class"] != null &&
                      x.Attributes["class"].Value.Contains("WHE7ib mpg5gc")))
                                    .Take(3).ToList();

            foreach (HtmlNode node in nodes)
            {
                AppInfo app = new AppInfo();

                // Записываем по какому поисковому запросу получено приложение
                app.SearchQuery = searchText;

                // Получаем ссылку на страницу приложения
                app.Link = node.Descendants("a").ToList()[0].GetAttributeValue("href", null);

                // Получаем список ссылок на скриншоты
                app.Screenshots = await GetGoogleAppScreenshots(app.Link);

                // Получаем название приложения
                app.Name = node.Descendants("div")
                           .Where(
                    x => (x.Name == "div" &&
                          x.Attributes["class"] != null &&
                          x.Attributes["class"].Value.Contains("WsMG1c nnK0zc")))
                           .ToList()[0].GetAttributeValue("title", null);

                // Получаем ссылку на иконку
                app.IconLink = node.Descendants("img").ToList()[0].GetAttributeValue("data-src", null);

                try
                {
                    // Извлекаем рейтинг приложения из атрибута aria-label
                    app.Rating = node.Descendants("div")
                                 .Where(
                        x => (x.Name == "div" &&
                              x.Attributes["role"] != null &&
                              x.Attributes["role"].Value.Contains("img")))
                                 .ToList()[0].GetAttributeValue("aria-label", null);

                    app.Rating = app.Rating.Split(" ").ToArray()[1];
                }
                catch
                {
                    app.Rating = "Unknown";
                }

                googleApps.Add(app);
            }

            return(googleApps);
        }
示例#2
0
        private async Task <AppInfo> AppStoreWorker(HtmlNode node, string searchStr)
        {
            AppInfo app = new AppInfo();

            // Записываем по какому поисковому запросу получено приложение
            app.SearchQuery = searchStr;

            // Получаем ссылку на приложение в официальном AppStore
            app.Link = node.Descendants("a").ToList()[0].GetAttributeValue("href", null);

            if (app.Link.Contains("https://apps.apple.com/"))
            {
                using (HttpClient auxClient = new HttpClient())
                {
                    string appStoreSource = "";
                    try
                    {
                        var appStoreResponse = await auxClient.GetByteArrayAsync(app.Link);

                        appStoreSource = Encoding.GetEncoding("utf-8").GetString(appStoreResponse, 0,
                                                                                 appStoreResponse.Length - 1);
                    }
                    catch
                    {
                        System.Windows.MessageBox.Show($"плохая ссылка {app.Link} на приложение в AppStore");
                    }

                    appStoreSource = WebUtility.HtmlDecode(appStoreSource);
                    HtmlDocument appStoreDoc = new HtmlDocument();
                    appStoreDoc.LoadHtml(appStoreSource);

                    // Получаем ноду иконки и саму иконку
                    HtmlNode iconNode = appStoreDoc.DocumentNode.Descendants()
                                        .Where(
                        x => (x.Name == "div" &&
                              x.Attributes["class"] != null &&
                              x.Attributes["class"].Value.Contains("product-hero__media l-column small-5 medium-4 " +
                                                                   "large-3 small-valign-top")))
                                        .FirstOrDefault();
                    app.IconLink = node.Descendants("img").ToList()[0].GetAttributeValue("src", null);

                    // Получаем название
                    app.Name = appStoreDoc.DocumentNode.Descendants()
                               .Where(
                        x => (x.Name == "h1" &&
                              x.Attributes["class"] != null &&
                              x.Attributes["class"].Value.Contains("product-header__title app-header__title")))
                               .FirstOrDefault().InnerText;
                    app.Name = app.Name.Split("\n")[1].Trim();

                    try
                    {
                        // Полуачаем рейтинг
                        app.Rating = appStoreDoc.DocumentNode.Descendants()
                                     .Where(
                            x => (x.Name == "span" &&
                                  x.Attributes["class"] != null &&
                                  x.Attributes["class"].Value.Contains("we-customer-ratings__averages__display")))
                                     .FirstOrDefault().InnerText;
                    }
                    catch
                    {
                        app.Rating = "Unknown";
                    }

                    // Получаем ноду со скриншотами
                    HtmlNode screenshotsNode = appStoreDoc.DocumentNode.Descendants()
                                               .Where(
                        x => (x.Name == "div" &&
                              x.Attributes["class"] != null &&
                              x.Attributes["class"].Value.Contains("we-screenshot-viewer ember-view")))
                                               .FirstOrDefault();

                    // Получаем скриншоты
                    app.Screenshots = GetAppStoreScreenshots(screenshotsNode);
                }
            }
            else
            {
                app = new AppInfo
                {
                    Name        = "Не нашлось:(",
                    Link        = "nope",
                    Rating      = "nope",
                    Screenshots = new List <string> {
                        "nope"
                    },
                    IconLink    = "nope",
                    SearchQuery = searchStr
                };
            }

            return(app);
        }