コード例 #1
0
ファイル: VkontakteBrw.cs プロジェクト: a-postx/Pyhh
        public void AddUser(VkBrwUser user)
        {
            Users.Add(user);

            if (user.Communitites.Count > 0)
            {
                foreach (VkBrwCommunity community in user.Communitites)
                {
                    VkBrwCommunity existingCommunity = Communities.FirstOrDefault(c => c.CommunityId == community.CommunityId);

                    if (existingCommunity == null)
                    {
                        Communities.Add(community);
                    }
                    else
                    {
                        VkBrwUser existingUser = existingCommunity.Users.FirstOrDefault(u => u.ProfileLink == user.ProfileLink);

                        if (existingUser == null)
                        {
                            existingCommunity.Users.Add(user);
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: VkBrwUserFactory.cs プロジェクト: a-postx/Pyhh
        private async Task <VkBrwUser> InitializeAsync(VkBrwUser user)
        {
            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

            LaunchOptions browserOptions = new LaunchOptions {
                Headless = false, Args = new string[] { "--lang=ru" }
            };

            Browser brw = await Puppeteer.LaunchAsync(browserOptions);

            BrowserContext userContext = await brw.CreateIncognitoBrowserContextAsync();

            Page userPage = await userContext.NewPageAsync();

            await userPage.SetViewportAsync(new ViewPortOptions
            {
                Width  = 1024,
                Height = 768
            });

            await userPage.SetUserAgentAsync("Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 74.0.3723.0 Safari / 537.36");

            user.Communitites = await GetCommunities(userContext, userPage, user);

            bool successParse = long.TryParse(user.ProfileLink.Split('/').LastOrDefault()?.Substring(2), out long parseResult);

            user.UserId = successParse ? parseResult : default;

            await userPage.CloseAsync();

            await userContext.CloseAsync();

            return(user);
        }
コード例 #3
0
ファイル: VkBrwUserFactory.cs プロジェクト: a-postx/Pyhh
        protected async Task <VkBrwUser> GetNewAsync(string userPageUrl)
        {
            VkBrwUser user = new VkBrwUser(userPageUrl, Vkontakte);

            await InitializeAsync(user);

            return(user);
        }
コード例 #4
0
ファイル: VkBrwUserFactory.cs プロジェクト: a-postx/Pyhh
        public async Task <List <VkBrwCommunity> > GetCommunities(BrowserContext userContext, Page userPage, VkBrwUser user)
        {
            List <VkBrwCommunity> nwPages = new List <VkBrwCommunity>();

            await userPage.GoToAsync(user.ProfileLink);

            ElementHandle userIsLoaded = await userPage.WaitForSelectorAsync("#page_info_wrap.page_info_wrap");

            ElementHandle[] hiddenProfileBlock = await userPage.QuerySelectorAllAsync("h5.profile_deleted_text");

            if (hiddenProfileBlock.Length == 0)
            {
                ElementHandle idolsLoaded = await userPage.WaitForSelectorAsync("#profile_idols");

                // Интересные страницы
                ElementHandle[] noteworthyPagesBlock = await userPage.QuerySelectorAllAsync("#profile_idols.module.clear.page_list_module._module");

                if (noteworthyPagesBlock.Length == 1)
                {
                    ElementHandle   noteworthyPages            = noteworthyPagesBlock.First();
                    ElementHandle[] noteworthyPagesHeaderBlock = await noteworthyPages.QuerySelectorAllAsync("a.module_header");

                    if (noteworthyPagesHeaderBlock.Length == 1)
                    {
                        ClickOptions clickOptions = new ClickOptions {
                            Delay = new Random().Next(30, 100)
                        };

                        ElementHandle noteworthyPagesLinkElement = noteworthyPagesHeaderBlock.First();
                        await noteworthyPagesLinkElement.ClickAsync(clickOptions);

                        ElementHandle noteworthyPagesIsOpened = await userPage.WaitForSelectorAsync("#fans_rowsidols.fans_rows.fans_idols");

                        ElementHandle[] closeBlock = await userPage.QuerySelectorAllAsync("div.box_x_button");

                        if (closeBlock.Length == 1)
                        {
                            ElementHandle[] pagesCountBlock = await userPage.QuerySelectorAllAsync("span.ui_box_header_cnt");

                            if (pagesCountBlock.Length == 1)
                            {
                                ElementHandle pagesTotalCountElement = pagesCountBlock.First();
                                string        pagesTotalCountValue   = await pagesTotalCountElement.EvaluateFunctionAsync <string>("('span', span => span.innerText)");

                                if (!string.IsNullOrEmpty(pagesTotalCountValue))
                                {
                                    bool pagesTotalCountReceived = int.TryParse(pagesTotalCountValue, out int pagesTotalCount);

                                    if (pagesTotalCountReceived && pagesTotalCount > 0)
                                    {
                                        ElementHandle[] pagesVisibleElements = await userPage.QuerySelectorAllAsync("div.fans_idol_row.inl_bl");

                                        if (pagesVisibleElements.Length < pagesTotalCount)
                                        {
                                            PressOptions pressOptions = new PressOptions {
                                                Delay = new Random().Next(20, 40)
                                            };

                                            await userPage.FocusAsync("input");

                                            await userPage.Keyboard.PressAsync("Tab", pressOptions);

                                            int visiblePagesCounter = pagesVisibleElements.Length;

                                            while (visiblePagesCounter < pagesTotalCount)
                                            {
                                                await userPage.Keyboard.PressAsync("PageDown", pressOptions);

                                                await Task.Delay(new Random().Next(250, 350));

                                                await userPage.Keyboard.PressAsync("PageDown", pressOptions);

                                                await Task.Delay(new Random().Next(250, 350));

                                                await userPage.Keyboard.PressAsync("PageDown", pressOptions);

                                                await Task.Delay(new Random().Next(250, 350));

                                                await userPage.Keyboard.PressAsync("PageDown", pressOptions);

                                                await Task.Delay(new Random().Next(250, 350));

                                                ElementHandle[] newPagesVisibleElements = await userPage.QuerySelectorAllAsync("div.fans_idol_row.inl_bl");

                                                if (newPagesVisibleElements.Length == visiblePagesCounter)
                                                {
                                                    break;
                                                }

                                                visiblePagesCounter = newPagesVisibleElements.Length;
                                            }
                                        }

                                        ElementHandle[] nwPagesElements = await userPage.QuerySelectorAllAsync("div.fans_idol_info");

                                        foreach (var element in nwPagesElements)
                                        {
                                            VkBrwCommunity community = await GetCommunityAsync(element, userContext);

                                            if (community != null)
                                            {
                                                nwPages.Add(community);
                                                community.Users.Add(user);
                                            }
                                        }
                                    }
                                }
                            }

                            ElementHandle closeButtonElement = closeBlock.First();
                            await closeButtonElement.ClickAsync(clickOptions);
                        }
                    }
                }
            }
            else
            {
                user.HiddenProfile = true;

                ElementHandle[] pageNameElements = await userPage.QuerySelectorAllAsync("h2.page_name");

                if (pageNameElements.Length == 1)
                {
                    var pageElement = pageNameElements.First();
                    user.PageName = await pageElement.EvaluateFunctionAsync <string>("('h2', h2 => h2.innerText)");
                }
            }

            return(nwPages);
        }