Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using OpenQA.Selenium.Chrome;
- //using OpenQA.Selenium.Firefox;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Support.UI;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Net;
- namespace Facebook_Monitoring_System
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.OutputEncoding = Encoding.UTF8;
- //Get User Input
- //Console.WriteLine("------------- Enter Your Facebook User Name or Email or Phone -------------");
- //String FbUsername = Console.ReadLine();
- //Console.WriteLine("------------- Enter Your Facebook Password -------------");
- //String FbPassword = Console.ReadLine();
- //Console.SetCursorPosition(0, Console.CursorTop - 1);
- //ClearCurrentConsoleLine();
- Console.WriteLine("------------- Enter Desired Username to get Information -------------");
- String DesiredFbUser = Console.ReadLine();
- //Temp user
- String FbUsername = "dev.dnj";
- String FbPassword = "xxxxxxxxxx";
- //Desired user to get Information
- String DesiredUserURL = String.Format("https://mbasic.facebook.com/{0}?v=timeline", DesiredFbUser);
- //var chromeOptions = new ChromeOptions();
- //chromeOptions.AddArguments(new List<string>() { "headless" });
- //var driver = new ChromeDriver(chromeOptions);
- var chrome_options = new ChromeOptions();
- chrome_options.AddArgument("--disable-notifications");
- // chrome_options.addargument("headless");
- var driver = new ChromeDriver(chrome_options);
- // var driver = new FirefoxDriver();
- // Go to google
- driver.Navigate().GoToUrl("https://mbasic.facebook.com/");
- //IWebElement query = driver.FindElement(By.Name("q"));
- // query.SendKeys("Rafusoft");
- /*******************
- * Login Page
- ********************/
- //Selectors
- //Login
- if (IsElementPresent(driver, By.CssSelector("input[name=\"email\"]")) && IsElementPresent(driver, By.CssSelector("input[name=\"pass\"]")) && IsElementPresent(driver, By.CssSelector("input[value=\"Log In\"]")))
- {
- var EmailInput = driver.FindElement(By.CssSelector("input[name=\"email\"]"));
- var PassInput = driver.FindElement(By.CssSelector("input[name=\"pass\"]"));
- var LoginButton = driver.FindElement(By.CssSelector("input[value=\"Log In\"]"));
- //Perform Input action and submit login
- EmailInput.SendKeys(FbUsername);
- // driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
- PassInput.SendKeys(FbPassword);
- //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
- LoginButton.Click();
- // driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
- if (IsElementPresent(driver, By.CssSelector("a[href^=\"/login/save-device\"]")))
- {
- var NotNowLink = driver.FindElement(By.CssSelector("a[href^=\"/login/save-device\"]"));
- NotNowLink.Click();
- }
- /*************************
- * Desired User Profile
- ***************************/
- // Go to desired user profile
- driver.Navigate().GoToUrl(DesiredUserURL);
- StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\PostData.txt", true);
- IList<IWebElement> all = driver.FindElements(By.CssSelector("div[role=\"article\"]"));
- String[] allText = new String[all.Count];
- int i = 0;
- foreach (IWebElement element in all)
- {
- allText[i++] = element.Text;
- /** Crawling Time **/
- sw.WriteLine(String.Format("Crawling Time: {0}", DateTime.Now.ToString()));
- /** Crawling Time End **/
- /** Post ID**/
- String DataFtAttrValue = element.GetAttribute("data-ft");
- Console.WriteLine(String.Format("Data FT Value: {0}", DataFtAttrValue));
- String[] tokens = DataFtAttrValue.Split(',');
- tokens = tokens[1].Split(':');
- String PostId = tokens[1].Replace('"', ' ').Trim();
- Console.WriteLine(String.Format("Post Id: {0}", PostId));
- sw.WriteLine(String.Format("Post ID: {0}", PostId));
- /** Post ID End**/
- //Posted By
- if (IsElementPresent(element, By.CssSelector("div div:nth-child(1) h3 strong a")))
- {
- String PosterName = element.FindElement(By.CssSelector("div div:nth-child(1) h3 strong a")).Text;
- // String PostLikeCount = element.FindElement(By.XPath(".//*/a[1]")).Text;
- //Console.WriteLine(element.Text);
- sw.WriteLine(String.Format("Posted by: {0}\n", PosterName));
- // sw.WriteLine(String.Format("Post Like Count: {0}\n\n", PostLikeCount));
- }
- //Post Content
- if (IsElementPresent(element, By.CssSelector("div div:nth-child(2)")))
- {
- String PostContent = element.FindElement(By.CssSelector("div div:nth-child(2)")).Text;
- sw.WriteLine(String.Format("Post Content: {0}\n", PostContent));
- }
- // Post Time
- if (IsElementPresent(element, By.CssSelector("div:nth-child(2) div:nth-child(1) abbr")))
- {
- String PostedTime = element.FindElement(By.CssSelector("div:nth-child(2) div:nth-child(1) abbr")).Text;
- sw.WriteLine(String.Format("Posted Time: {0}\n", PostedTime));
- }
- // Post Privary
- if (IsElementPresent(element, By.CssSelector("div:nth-child(2) div:nth-child(1) span span span")))
- {
- String PostPrivacy = element.FindElement(By.CssSelector("div:nth-child(2) div:nth-child(1) span span span")).Text;
- sw.WriteLine(String.Format("Post Privacy: {0}\n\n", PostPrivacy));
- }
- }
- sw.Flush();
- sw.Close();
- /**
- StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\PostData.txt",true);
- IReadOnlyCollection<IWebElement> FbPosts = driver.FindElements(By.CssSelector("div[role=\"article\"]"));
- for (int i = 0; i < FbPosts.Count; i++)
- {
- var PostInfo = FbPosts.ElementAt(i);
- String Poster = PostInfo.FindElement(By.XPath("//div[1]/div[1]/table/tbody/tr/td[2]/div/h3/strong/a")).Text;
- String PostContent = PostInfo.FindElement(By.XPath("//div[2]/div[2]/span/p")).GetAttribute("innerHTML"); ;
- Console.WriteLine(String.Format("Posted By: {0}\n", Poster));
- sw.WriteLine(String.Format("Posted By: {0}\n", Poster));
- Console.WriteLine(String.Format("Post Content: {0}\n", PostContent));
- sw.WriteLine(String.Format("Post Content: {0}\n", PostContent));
- Console.WriteLine("\n\n\n");
- }
- sw.Flush();
- sw.Close();
- **/
- /**
- try
- {
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- }
- catch (Exception Ex)
- {
- Console.WriteLine(Ex.Message);
- } **/
- int numberOfElements = driver.FindElements(By.CssSelector("div[role=\"article\"]")).Count;
- Console.WriteLine(String.Format("Total Facebook Post: {0}", numberOfElements));
- ////Log Out
- //try{
- // WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
- // for (int i = 1; i < 6; i++)
- // {
- // By locator = By.CssSelector("a[href^=\"/logout.php\"]");
- // IWebElement element = driver.FindElement(locator);
- // element.Click();
- // wait.Until(ExpectedConditions.StalenessOf(element));
- // wait.Until(ExpectedConditions.ElementToBeClickable(locator));
- // }
- // //var LogOut = driver.FindElement(By.CssSelector("a[href^=\"/logout.php\"]"));
- // //LogOut.Click();
- //}
- //catch (Exception Ex)
- //{
- // Console.WriteLine(Ex.Message);
- //}
- // WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100)); // time out after 5 seconds
- // Console.WriteLine(String.Format("Page title is: {0}",driver.));
- driver.Close();
- driver.Quit();
- }
- else
- {
- Console.WriteLine("Error: Unable to login. Problem with page loading...");
- driver.Close();
- driver.Quit();
- }
- Console.ReadKey();
- }
- private static bool IsElementPresent(IWebElement Element, By by)
- {
- try
- {
- Element.FindElement(by);
- return true;
- }
- catch (NoSuchElementException)
- {
- return false;
- }
- }
- private static bool IsElementPresent(ChromeDriver Driver, By by)
- {
- try
- {
- Driver.FindElement(by);
- return true;
- }
- catch (NoSuchElementException)
- {
- return false;
- }
- }
- /**** Helper Functions ****/
- private static void ClearCurrentConsoleLine()
- {
- int currentLineCursor = Console.CursorTop;
- Console.SetCursorPosition(0, Console.CursorTop);
- Console.Write(new string(' ', Console.WindowWidth));
- Console.SetCursorPosition(0, currentLineCursor);
- }
- private void GetAndSaveImage(ChromeDriver driver, By ImageSelector)
- {
- ITakesScreenshot ssdriver = driver as ITakesScreenshot;
- Screenshot screenshot = ssdriver.GetScreenshot();
- Screenshot tempImage = screenshot;
- tempImage.SaveAsFile(@"full.png", ScreenshotImageFormat.Png);
- //replace with the XPath of the image element
- IWebElement my_image = driver.FindElement(ImageSelector);
- Point point = my_image.Location;
- int width = my_image.Size.Width;
- int height = my_image.Size.Height;
- Rectangle section = new Rectangle(point, new Size(width, height));
- Bitmap source = new Bitmap(@"full.png");
- Bitmap final_image = CropImage(source, section);
- final_image.Save(@"image.jpg");
- }
- private Bitmap CropImage(Bitmap source, Rectangle section)
- {
- Bitmap bmp = new Bitmap(section.Width, section.Height);
- Graphics g = Graphics.FromImage(bmp);
- g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
- return bmp;
- }
- private bool ImageDownload(String ImageSource)
- {
- Uri uri = new Uri(ImageSource);
- if (true)
- {
- string filename = System.IO.Path.GetFileName(uri.LocalPath);
- WebClient wb = new WebClient();
- wb.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.33 Safari/537.36");
- try
- {
- wb.DownloadFile(ImageSource, @"fbphoto\" + filename);
- return true;
- }
- catch (Exception Ex)
- {
- Console.WriteLine(Ex.Message);
- return false;
- }
- }
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment