touhid_xml

C# Selenifum for Facebook Monitoring

Mar 5th, 2018
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using OpenQA.Selenium.Chrome;
  8. //using OpenQA.Selenium.Firefox;
  9. using OpenQA.Selenium;
  10. using OpenQA.Selenium.Support.UI;
  11. using System.Drawing;
  12. using System.Drawing.Imaging;
  13. using System.Net;
  14.  
  15. namespace Facebook_Monitoring_System
  16. {
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             Console.OutputEncoding = Encoding.UTF8;
  22.             //Get User Input
  23.             //Console.WriteLine("------------- Enter Your Facebook User Name or Email or Phone -------------");
  24.             //String FbUsername = Console.ReadLine();
  25.             //Console.WriteLine("------------- Enter Your Facebook Password -------------");
  26.             //String FbPassword = Console.ReadLine();
  27.             //Console.SetCursorPosition(0, Console.CursorTop - 1);
  28.             //ClearCurrentConsoleLine();
  29.  
  30.             Console.WriteLine("------------- Enter Desired Username to get Information -------------");
  31.             String DesiredFbUser = Console.ReadLine();
  32.  
  33.             //Temp user
  34.             String FbUsername = "dev.dnj";
  35.             String FbPassword = "xxxxxxxxxx";
  36.  
  37.             //Desired user to get Information
  38.             String DesiredUserURL = String.Format("https://mbasic.facebook.com/{0}?v=timeline", DesiredFbUser);
  39.  
  40.             //var chromeOptions = new ChromeOptions();
  41.             //chromeOptions.AddArguments(new List<string>() { "headless" });
  42.             //var driver = new ChromeDriver(chromeOptions);
  43.  
  44.             var chrome_options = new ChromeOptions();
  45.             chrome_options.AddArgument("--disable-notifications");
  46.             // chrome_options.addargument("headless");
  47.             var driver = new ChromeDriver(chrome_options);
  48.  
  49.             // var driver = new FirefoxDriver();
  50.  
  51.  
  52.  
  53.             // Go to google
  54.             driver.Navigate().GoToUrl("https://mbasic.facebook.com/");
  55.             //IWebElement query = driver.FindElement(By.Name("q"));
  56.             // query.SendKeys("Rafusoft");
  57.  
  58.             /*******************
  59.             *  Login Page
  60.             ********************/
  61.  
  62.  
  63.             //Selectors
  64.  
  65.             //Login
  66.             if (IsElementPresent(driver, By.CssSelector("input[name=\"email\"]")) && IsElementPresent(driver, By.CssSelector("input[name=\"pass\"]")) && IsElementPresent(driver, By.CssSelector("input[value=\"Log In\"]")))
  67.             {
  68.                 var EmailInput = driver.FindElement(By.CssSelector("input[name=\"email\"]"));
  69.                 var PassInput = driver.FindElement(By.CssSelector("input[name=\"pass\"]"));
  70.                 var LoginButton = driver.FindElement(By.CssSelector("input[value=\"Log In\"]"));
  71.                 //Perform Input action and submit login
  72.                 EmailInput.SendKeys(FbUsername);
  73.                 // driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
  74.                 PassInput.SendKeys(FbPassword);
  75.                 //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
  76.                 LoginButton.Click();
  77.                 // driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
  78.  
  79.  
  80.                 if (IsElementPresent(driver, By.CssSelector("a[href^=\"/login/save-device\"]")))
  81.                 {
  82.                     var NotNowLink = driver.FindElement(By.CssSelector("a[href^=\"/login/save-device\"]"));
  83.                     NotNowLink.Click();
  84.                 }
  85.  
  86.                 /*************************
  87.                 * Desired User Profile
  88.                 ***************************/
  89.                 // Go to desired user profile
  90.                 driver.Navigate().GoToUrl(DesiredUserURL);
  91.  
  92.                 StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\PostData.txt", true);
  93.  
  94.                 IList<IWebElement> all = driver.FindElements(By.CssSelector("div[role=\"article\"]"));
  95.                 String[] allText = new String[all.Count];
  96.                 int i = 0;
  97.                 foreach (IWebElement element in all)
  98.                 {
  99.                     allText[i++] = element.Text;
  100.  
  101.                     /** Crawling Time **/
  102.                     sw.WriteLine(String.Format("Crawling Time: {0}", DateTime.Now.ToString()));
  103.                     /** Crawling Time End **/
  104.  
  105.                     /** Post ID**/
  106.                     String DataFtAttrValue = element.GetAttribute("data-ft");
  107.                     Console.WriteLine(String.Format("Data FT Value: {0}", DataFtAttrValue));
  108.  
  109.                     String[] tokens = DataFtAttrValue.Split(',');
  110.                     tokens = tokens[1].Split(':');
  111.                     String PostId = tokens[1].Replace('"', ' ').Trim();
  112.  
  113.                     Console.WriteLine(String.Format("Post Id: {0}", PostId));
  114.                     sw.WriteLine(String.Format("Post ID: {0}", PostId));
  115.  
  116.                     /** Post ID End**/
  117.  
  118.  
  119.  
  120.                     //Posted By
  121.  
  122.                     if (IsElementPresent(element, By.CssSelector("div div:nth-child(1) h3 strong a")))
  123.                     {
  124.  
  125.                         String PosterName = element.FindElement(By.CssSelector("div div:nth-child(1) h3 strong a")).Text;
  126.  
  127.                         // String PostLikeCount = element.FindElement(By.XPath(".//*/a[1]")).Text;
  128.                         //Console.WriteLine(element.Text);
  129.                         sw.WriteLine(String.Format("Posted by: {0}\n", PosterName));
  130.  
  131.                         // sw.WriteLine(String.Format("Post Like Count: {0}\n\n", PostLikeCount));
  132.                     }
  133.  
  134.  
  135.  
  136.                     //Post  Content
  137.                     if (IsElementPresent(element, By.CssSelector("div div:nth-child(2)")))
  138.                     {
  139.                         String PostContent = element.FindElement(By.CssSelector("div div:nth-child(2)")).Text;
  140.                         sw.WriteLine(String.Format("Post Content: {0}\n", PostContent));
  141.                     }
  142.  
  143.  
  144.                     // Post Time
  145.  
  146.                     if (IsElementPresent(element, By.CssSelector("div:nth-child(2) div:nth-child(1) abbr")))
  147.                     {
  148.                         String PostedTime = element.FindElement(By.CssSelector("div:nth-child(2) div:nth-child(1) abbr")).Text;
  149.                         sw.WriteLine(String.Format("Posted Time: {0}\n", PostedTime));
  150.  
  151.                     }
  152.  
  153.  
  154.  
  155.  
  156.                     // Post Privary
  157.                     if (IsElementPresent(element, By.CssSelector("div:nth-child(2) div:nth-child(1) span span span")))
  158.                     {
  159.                         String PostPrivacy = element.FindElement(By.CssSelector("div:nth-child(2) div:nth-child(1) span span span")).Text;
  160.                         sw.WriteLine(String.Format("Post Privacy: {0}\n\n", PostPrivacy));
  161.                     }
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.                 }
  169.                 sw.Flush();
  170.                 sw.Close();
  171.  
  172.                 /**
  173.                 StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\PostData.txt",true);
  174.                 IReadOnlyCollection<IWebElement> FbPosts = driver.FindElements(By.CssSelector("div[role=\"article\"]"));
  175.                 for (int i = 0; i < FbPosts.Count; i++)
  176.                 {
  177.                     var PostInfo = FbPosts.ElementAt(i);
  178.                     String Poster = PostInfo.FindElement(By.XPath("//div[1]/div[1]/table/tbody/tr/td[2]/div/h3/strong/a")).Text;
  179.                     String PostContent = PostInfo.FindElement(By.XPath("//div[2]/div[2]/span/p")).GetAttribute("innerHTML"); ;
  180.                     Console.WriteLine(String.Format("Posted By: {0}\n", Poster));
  181.                     sw.WriteLine(String.Format("Posted By: {0}\n", Poster));
  182.                     Console.WriteLine(String.Format("Post Content: {0}\n", PostContent));
  183.                     sw.WriteLine(String.Format("Post Content: {0}\n", PostContent));
  184.  
  185.                     Console.WriteLine("\n\n\n");
  186.                 }
  187.                 sw.Flush();
  188.                 sw.Close();
  189.                 **/
  190.  
  191.  
  192.  
  193.  
  194.                 /**
  195.  
  196.                 try
  197.                 {
  198.                     driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  199.                     driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  200.                     driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  201.                     driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  202.                     driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  203.                     driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  204.                     driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  205.                     driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  206.                     driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  207.  
  208.                 }
  209.                 catch (Exception Ex)
  210.                 {
  211.                     Console.WriteLine(Ex.Message);
  212.                 } **/
  213.  
  214.  
  215.                 int numberOfElements = driver.FindElements(By.CssSelector("div[role=\"article\"]")).Count;
  216.                 Console.WriteLine(String.Format("Total Facebook Post: {0}", numberOfElements));
  217.  
  218.  
  219.                 ////Log Out
  220.                 //try{
  221.                 //    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
  222.                 //    for (int i = 1; i < 6; i++)
  223.                 //    {
  224.                 //        By locator = By.CssSelector("a[href^=\"/logout.php\"]");
  225.                 //        IWebElement element = driver.FindElement(locator);
  226.                 //        element.Click();
  227.                 //        wait.Until(ExpectedConditions.StalenessOf(element));
  228.                 //        wait.Until(ExpectedConditions.ElementToBeClickable(locator));
  229.                 //    }
  230.                 //    //var LogOut = driver.FindElement(By.CssSelector("a[href^=\"/logout.php\"]"));
  231.                 //    //LogOut.Click();
  232.                 //}
  233.                 //catch (Exception Ex)
  234.                 //{
  235.                 //    Console.WriteLine(Ex.Message);
  236.                 //}
  237.  
  238.  
  239.  
  240.                // WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100)); // time out after 5 seconds
  241.  
  242.  
  243.                 // Console.WriteLine(String.Format("Page title is: {0}",driver.));
  244.                 driver.Close();
  245.                 driver.Quit();
  246.  
  247.             }
  248.             else
  249.             {
  250.                 Console.WriteLine("Error: Unable to login. Problem with page loading...");
  251.                 driver.Close();
  252.                 driver.Quit();
  253.                
  254.             }
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.             Console.ReadKey();
  263.            
  264.         }
  265.  
  266.         private static bool IsElementPresent(IWebElement Element,  By by)
  267.         {
  268.             try
  269.             {
  270.                 Element.FindElement(by);
  271.                 return true;
  272.             }
  273.             catch (NoSuchElementException)
  274.             {
  275.                 return false;
  276.             }
  277.         }
  278.  
  279.         private static bool IsElementPresent(ChromeDriver Driver, By by)
  280.         {
  281.             try
  282.             {
  283.                 Driver.FindElement(by);
  284.                 return true;
  285.             }
  286.             catch (NoSuchElementException)
  287.             {
  288.                 return false;
  289.             }
  290.         }
  291.  
  292.         /**** Helper Functions ****/
  293.  
  294.         private static void ClearCurrentConsoleLine()
  295.         {
  296.             int currentLineCursor = Console.CursorTop;
  297.             Console.SetCursorPosition(0, Console.CursorTop);
  298.             Console.Write(new string(' ', Console.WindowWidth));
  299.             Console.SetCursorPosition(0, currentLineCursor);
  300.         }
  301.  
  302.         private void GetAndSaveImage(ChromeDriver driver, By ImageSelector)
  303.         {
  304.             ITakesScreenshot ssdriver = driver as ITakesScreenshot;
  305.             Screenshot screenshot = ssdriver.GetScreenshot();
  306.  
  307.             Screenshot tempImage = screenshot;
  308.  
  309.             tempImage.SaveAsFile(@"full.png", ScreenshotImageFormat.Png);
  310.  
  311.             //replace with the XPath of the image element
  312.             IWebElement my_image = driver.FindElement(ImageSelector);
  313.  
  314.             Point point = my_image.Location;
  315.             int width = my_image.Size.Width;
  316.             int height = my_image.Size.Height;
  317.  
  318.             Rectangle section = new Rectangle(point, new Size(width, height));
  319.             Bitmap source = new Bitmap(@"full.png");
  320.             Bitmap final_image = CropImage(source, section);
  321.  
  322.             final_image.Save(@"image.jpg");
  323.         }
  324.  
  325.         private Bitmap CropImage(Bitmap source, Rectangle section)
  326.         {
  327.             Bitmap bmp = new Bitmap(section.Width, section.Height);
  328.             Graphics g = Graphics.FromImage(bmp);
  329.             g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
  330.             return bmp;
  331.         }
  332.  
  333.         private bool ImageDownload(String ImageSource)
  334.         {
  335.             Uri uri = new Uri(ImageSource);
  336.             if (true)
  337.             {
  338.                 string filename = System.IO.Path.GetFileName(uri.LocalPath);
  339.                 WebClient wb = new WebClient();
  340.                 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");
  341.                 try
  342.                 {
  343.                     wb.DownloadFile(ImageSource, @"fbphoto\" + filename);
  344.  
  345.                     return true;
  346.                 }
  347.                 catch (Exception Ex)
  348.                 {
  349.                     Console.WriteLine(Ex.Message);
  350.                     return false;
  351.                 }
  352.  
  353.  
  354.             }
  355.  
  356.             return false;
  357.  
  358.         }
  359.  
  360.  
  361.     }
  362. }
Add Comment
Please, Sign In to add comment