Exemplo n.º 1
0
        public static async Task twitterAsync(LoginInfo loginInfo, Article article)
        {
            var browser = await PuppeteerInstance.getInstance();

            using (var page = await browser.NewPageAsync())
            {
                await twitterLoginAsync(loginInfo, page);
                await twitterPostAsync(article, page);
            }
            await browser.CloseAsync();

            browser.Dispose();
        }
Exemplo n.º 2
0
        public async Task <string> getImgPath()
        {
            var browser = await PuppeteerInstance.getInstance();

            using (var page = await browser.NewPageAsync())
            {
                await page.GoToAsync(_imgSrc);

                var imgElement = await page.WaitForSelectorAsync("body > img");

                await imgElement.ScreenshotAsync("TEMP.jpg");

                await browser.CloseAsync();

                browser.Dispose();
                return(System.Windows.Forms.Application.StartupPath + "\\TEMP.jpg");
            }
        }
Exemplo n.º 3
0
        getArticleInfoAsync(int MAX_SIZE, string URL, string TITLE, string IMG, string numSelector)
        {
            var browser = await PuppeteerInstance.getInstance();

            using (var page = await browser.NewPageAsync())
            {
                /*go to url*/
                await page.GoToAsync(URL);

                /*get random num(1~MAX_SIZE)*/
                var random    = new Random();
                var randomNum = random.Next(1, MAX_SIZE);

                /*get element from selector(title, img)*/
                var titleHandle = await selectArticleAsync(
                    articleNumber(numSelector, randomNum), TITLE, page);

                /*get article information from element(text, imgSrc, url)*/
                var title = titleHandle.GetPropertyAsync("text").Result.ToString().Replace("JSHandle:", "");
                var href  = titleHandle.GetPropertyAsync("href").Result.ToString().Replace("JSHandle:", "");

                ElementHandle imgHandle;
                await Task.Delay(3000);

                await titleHandle.ClickAsync();

                await Task.Delay(3000);

                try
                {
                    imgHandle = await page.WaitForSelectorAsync(IMG, new WaitForSelectorOptions { Timeout = 3000 });
                }
                catch
                {
                    imgHandle = await page.WaitForSelectorAsync("#newsEndContents > span:nth-child(1) > img", new WaitForSelectorOptions { Timeout = 3000 });
                }
                var imgSrc = imgHandle.GetPropertyAsync("src").Result.ToString().Replace("JSHandle:", "");

                await browser.CloseAsync();

                browser.Dispose();
                return(await ArticleFactory.makeArticleAsync(title, href, imgSrc));
            }
        }
Exemplo n.º 4
0
        getUserProfile(LoginInfo loginInfo)
        {
            var browser = await PuppeteerInstance.getInstance();

            using (var page = await browser.NewPageAsync())
            {
                await TwitterPoster.twitterLoginAsync(loginInfo, page);

                var profileButtonHandle = await page.WaitForSelectorAsync(ProfileSelector);

                await profileButtonHandle.ClickAsync();

                await Task.Delay(3000);

                ElementHandle profilePictureHandle     = null;
                ElementHandle profileTitleHandle       = null;
                ElementHandle profileDescriptionHandle = null;
                ElementHandle profileSiteHandle        = null;
                string        imgSrc      = "";
                string        title       = "";
                string        description = "";
                string        site        = "";
                try
                {
                    profilePictureHandle = await page.WaitForSelectorAsync(PictureSelector);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                    profilePictureHandle = null;
                }
                if (profilePictureHandle != null)
                {
                    imgSrc = profilePictureHandle.GetPropertyAsync("src").Result.ToString().Replace("JSHandle:", "");
                }
                await Task.Delay(3000);

                try
                {
                    profileTitleHandle = await page.WaitForSelectorAsync(TitleSelector, new WaitForSelectorOptions { Timeout = 3000 });
                }
                catch (Exception)
                {
                    profileTitleHandle = null;
                }
                try
                {
                    profileDescriptionHandle = await page.WaitForSelectorAsync(DescriptionSelector, new WaitForSelectorOptions { Timeout = 3000 });
                }
                catch (Exception)
                {
                    profileDescriptionHandle = null;
                }
                try
                {
                    profileSiteHandle = await page.WaitForSelectorAsync(SiteSelector, new WaitForSelectorOptions { Timeout = 3000 });
                }
                catch (Exception)
                {
                    profileSiteHandle = null;
                }

                if (profileTitleHandle != null)
                {
                    title = profileTitleHandle.GetPropertyAsync("text").Result.ToString().Replace("JSHandle:", "");
                }
                if (profileDescriptionHandle != null)
                {
                    description = profileDescriptionHandle.GetPropertyAsync("textContent").Result.ToString().Replace("JSHandle:", "");
                }
                if (profileSiteHandle != null)
                {
                    site = profileSiteHandle.GetPropertyAsync("text").Result.ToString().Replace("JSHandle:", "");
                }

                var profilePicture = SaveImage(imgSrc);

                var profile = new Profile
                {
                    picture     = profilePicture,
                    title       = title,
                    description = description,
                    site        = site
                };
                FormControl(profile);
                await page.GoBackAsync();

                await Task.Delay(3000);

                await browser.CloseAsync();

                browser.Dispose();
                return(profile);
            }
        }
Exemplo n.º 5
0
        ModifyProfile(Profile profile, LoginInfo loginInfo)
        {
            var browser = await PuppeteerInstance.getInstance();

            using (var page = await browser.NewPageAsync())
            {
                try
                {
                    await TwitterPoster.twitterLoginAsync(loginInfo, page);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                    await page.GoToAsync(URL);
                }

                await Task.Delay(3000);

                var profileButtonHandle = await page.WaitForSelectorAsync(ProfileSelector);

                await profileButtonHandle.ClickAsync();

                await Task.Delay(3000);

                var modifyButtonHandle = await page.WaitForSelectorAsync(ModifyButtonSelector);

                await modifyButtonHandle.ClickAsync();

                await Task.Delay(3000);

                ElementHandle modifyPictureHandle = null;
                try
                {
                    modifyPictureHandle = await page.WaitForSelectorAsync(ModifyPictureSelector);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                    modifyPictureHandle = await page.WaitForSelectorAsync("#page-container > div.ProfileCanopy.ProfileCanopy--withNav.js-variableHeightTopBar.ProfileCanopy--large > div > div.ProfileCanopy-header.u-bgUserColor > div.AppContainer > div.ProfileCanopy-avatar > div.ProfileAvatarEditing > div.ProfileAvatarEditing-buttonContainer > button > div.ProfileAvatarEditing-addAvatarHelp");

                    await Task.Delay(3000);
                }
                var modifyTitleHandle = await page.WaitForSelectorAsync(ModifyTitleSelector);

                var modifyDescriptionHandle = await page.WaitForSelectorAsync(ModifyDescriptionSelector);

                var modifySiteHandle = await page.WaitForSelectorAsync(ModifySiteSelector);

                var saveButtonHandle = await page.WaitForSelectorAsync(SaveButtonSelector);

                await modifyPictureHandle.ClickAsync();

                byte[]       byteArrayIn = profile.picture;
                MemoryStream ms          = new MemoryStream(byteArrayIn, 0, byteArrayIn.Length);
                ms.Position = 0;
                var returnImage = Image.FromStream(ms, true);
                returnImage.Save("./ModifyTEMP.png");
                var uploadPictureHandle = await page.WaitForSelectorAsync(UploadPictureSelector);

                await uploadPictureHandle.UploadFileAsync("./ModifyTEMP.png");

                var button = await page.WaitForSelectorAsync
                                 ("#profile_image_upload_dialog-dialog > div.modal-content > div.modal-footer > button.EdgeButton.EdgeButton--primary.profile-image-save");

                await button.ClickAsync();

                await Task.Delay(3000);

                await modifyTitleHandle.FocusAsync();

                await page.Keyboard.DownAsync("Control");

                await page.Keyboard.PressAsync("A");

                await page.Keyboard.UpAsync("Control");

                await page.Keyboard.PressAsync("Backspace");

                await page.Keyboard.TypeAsync(profile.title);

                await modifyDescriptionHandle.FocusAsync();

                await page.Keyboard.DownAsync("Control");

                await page.Keyboard.PressAsync("A");

                await page.Keyboard.UpAsync("Control");

                await page.Keyboard.PressAsync("Backspace");

                await page.Keyboard.TypeAsync(profile.description);

                await modifySiteHandle.FocusAsync();

                await page.Keyboard.DownAsync("Control");

                await page.Keyboard.PressAsync("A");

                await page.Keyboard.UpAsync("Control");

                await page.Keyboard.PressAsync("Backspace");

                await page.Keyboard.TypeAsync(profile.site);

                await saveButtonHandle.ClickAsync();

                await browser.CloseAsync();

                browser.Dispose();
            }
        }