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); } }
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(); } }