Facebook
From Sole Tortoise, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 135
  1. using OpenQA.Selenium;
  2. using OpenQA.Selenium.Chrome;
  3. using System;
  4. using System.Threading;
  5. using System.IO;
  6. using System.Text;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using net.openstack.Providers.Rackspace.Objects.Monitoring;
  10.  
  11. namespace botInstagram
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.  
  18.  
  19.             // Read the kind of login from kindoflogin.txt  
  20.             string kind_of_login;
  21.             System.IO.StreamReader file = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/kindoflogin.txt");
  22.             kind_of_login = file.ReadLine();
  23.  
  24.  
  25.             // Read the hasztag from hasztag.txt    
  26.             string hashtag_name;
  27.             System.IO.StreamReader file1 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/hasztag.txt");
  28.             hashtag_name = file1.ReadLine();
  29.  
  30.  
  31.             // Read the email from email.txt.
  32.             string facebook_or_instagram_email;
  33.             System.IO.StreamReader file2 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/email.txt");
  34.             facebook_or_instagram_email = file2.ReadLine();
  35.  
  36.  
  37.             // Read the password from password.txt.
  38.             string facebook_or_instagram_password;
  39.             System.IO.StreamReader file3 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/password.txt");
  40.             facebook_or_instagram_password = file3.ReadLine();
  41.  
  42.             // Read the comment1 from comment1.txt.
  43.             string comment1_name;
  44.             System.IO.StreamReader file4 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment1.txt");
  45.             comment1_name = file4.ReadLine();
  46.  
  47.             // Read the comment2 from comment2.txt.
  48.             string comment2_name;
  49.             System.IO.StreamReader file5 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment2.txt");
  50.             comment2_name = file5.ReadLine();
  51.  
  52.             // Read the comment3 from comment3.txt.
  53.             string comment3_name;
  54.             System.IO.StreamReader file6 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment3.txt");
  55.             comment3_name = file6.ReadLine();
  56.  
  57.             // Read the number of comments from comment_number.txt.
  58.             string number_posts_to_comment;
  59.             System.IO.StreamReader file10 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment_number.txt");
  60.             number_posts_to_comment = file10.ReadLine();
  61.             int number_posts_to_coment_int = Convert.ToInt32(number_posts_to_comment);
  62.  
  63.             // Read the number of likes from likes.txt.
  64.             string number_of_likes;
  65.             System.IO.StreamReader file11 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/likes_number.txt");
  66.             number_of_likes = file11.ReadLine();
  67.             int number_of_likes_int = Convert.ToInt32(number_of_likes);
  68.  
  69.             // Read the comment (comm or no) of likes from comment_yes_or_no.txt.
  70.             string comment_yes_or_no;
  71.             System.IO.StreamReader file12 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment_yes_or_no.txt");
  72.             comment_yes_or_no = file12.ReadLine();
  73.  
  74.             // Read the follow (comm or no) of likes from follow_yes_or_no.txt.
  75.             string follow_yes_or_no;
  76.             System.IO.StreamReader file13 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/follow_yes_or_no.txt");
  77.             follow_yes_or_no = file13.ReadLine();
  78.  
  79.  
  80.  
  81.  
  82.             // create the reference for browser
  83.             ChromeDriver driver = new ChromeDriver(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/Drivers/");
  84.  
  85.             // navigate to instagram
  86.             driver.Navigate().GoToUrl("https://www.instagram.com");
  87.  
  88.             //get width and geiht of chrome
  89.             Thread.Sleep(3000);
  90.             driver.Manage().Window.Size = new Size(800, 600);
  91.  
  92.  
  93.  
  94.             // kind of login
  95.             // facebook login
  96.             if (kind_of_login == "1")
  97.             {
  98.                 // facebook login button ( from instagram)
  99.                 Thread.Sleep(5000);
  100.                 IWebElement facebook_login_button_in_instagram = driver.FindElement(By.ClassName("KPnG0"));
  101.                 facebook_login_button_in_instagram.Click();
  102.  
  103.                 // facebook email
  104.                 Thread.Sleep(5000);
  105.                 IWebElement facebook_login_email = driver.FindElement(By.XPath("/html/body/div[1]/div[4]/div[1]/div/div/div[2]/div[1]/form/div/div[1]/input"));
  106.                 facebook_login_email.SendKeys(facebook_or_instagram_email);
  107.  
  108.                 // facebokk password
  109.                 IWebElement facebook_login_password = driver.FindElement(By.XPath("/html/body/div[1]/div[4]/div[1]/div/div/div[2]/div[1]/form/div/div[2]/input"));
  110.                 facebook_login_password.SendKeys(facebook_or_instagram_password);
  111.  
  112.                 // facebook login button
  113.                 Thread.Sleep(5000);
  114.                 IWebElement facebook_login_button = driver.FindElement(By.XPath("/html/body/div[1]/div[4]/div[1]/div/div/div[2]/div[1]/form/div/div[3]/button"));
  115.                 facebook_login_button.Click();
  116.  
  117.             }
  118.  
  119.             // instagram login
  120.             else if (kind_of_login == "2")
  121.             {
  122.  
  123.                 // instagram email
  124.                 Thread.Sleep(5000);
  125.                 IWebElement facebook_login_email = driver.FindElement(By.XPath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input"));
  126.                 facebook_login_email.SendKeys(facebook_or_instagram_email);
  127.  
  128.                 // instagram password
  129.                 IWebElement instagram_login_password = driver.FindElement(By.XPath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input"));
  130.                 instagram_login_password.SendKeys(facebook_or_instagram_password);
  131.  
  132.                 // instagram login button
  133.                 Thread.Sleep(5000);
  134.                 IWebElement facebook_login_button = driver.FindElement(By.XPath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button"));
  135.                 facebook_login_button.Click();
  136.  
  137.             }
  138.  
  139.             // searching hasztags
  140.             Thread.Sleep(8000);
  141.             driver.Navigate().GoToUrl("https://www.instagram.com/explore/tags/" + hashtag_name + "/");
  142.  
  143.  
  144.  
  145.             // number of post to like
  146.             int next_column = 1;
  147.             int current_like;
  148.             int number_of_photo_in_a_row = 1;
  149.  
  150.             IJavaScriptExecutor js = driver as IJavaScriptExecutor;
  151.  
  152.             js.ExecuteScript("window.scrollBy(0,939);");
  153.             Thread.Sleep(1000);
  154.             for (current_like = 1; current_like <= number_of_likes_int; current_like++)
  155.             {
  156.                 Random rnd = new Random();
  157.                 int time = rnd.Next(5000, 8000);
  158.                 int time2 = rnd.Next(5000, 8000);
  159.                 int time3 = rnd.Next(5000, 8000);
  160.                 int time4 = rnd.Next(5000, 8000);
  161.                 int time5 = rnd.Next(5000, 8000);
  162.                 int time6 = rnd.Next(5000, 8000);
  163.                 int time7 = rnd.Next(5000, 8000);
  164.  
  165.                 // open posts
  166.                 Thread.Sleep(time);
  167.                 Thread.Sleep(4000);
  168.                 IWebElement instagram_open_photo = driver.FindElement(By.XPath("/html/body/div[1]/section/main/article/div[2]/div/div[" + next_column + "]/div[" + number_of_photo_in_a_row + "]/a/div"));
  169.  
  170.                 // / html / body / div[1] / section / main / article / div[2] / div / div[1] / div[1] / a / div
  171.                 // /html / body / div[1 ]/ section / main / article / div[2] / div /div[2] / div[1]  / a / div
  172.  
  173.                 instagram_open_photo.Click();
  174.  
  175.  
  176.  
  177.                 //check status of like  
  178.                 Thread.Sleep(time2);
  179.                 string check_status_of_like = driver.FindElement(By.CssSelector("body > div._2dDPU.CkGkG > div.zZYga > div > article > div.eo2As > section.ltpMr.Slqrh > span.fr66n > button > svg")).GetAttribute("aria-label"); ;
  180.  
  181.  
  182.  
  183.                 if (check_status_of_like == "LubiÄ™ to!")
  184.                 {
  185.  
  186.  
  187.  
  188.                     // like posts
  189.                     Thread.Sleep(time3);
  190.                     IWebElement instagram_like_photo = driver.FindElement(By.XPath("/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]"));
  191.                     instagram_like_photo.Click();
  192.  
  193.                     //follow
  194.                     if (follow_yes_or_no == "1")
  195.                     {
  196.                         Thread.Sleep(time4);
  197.                         IWebElement click_follow_button = driver.FindElement(By.XPath("/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button"));
  198.                         click_follow_button.Click();
  199.                     }
  200.  
  201.  
  202.  
  203.                     if (comment_yes_or_no == "1")
  204.                     {
  205.  
  206.                         // comment
  207.                         if (current_like % number_posts_to_coment_int == 0)
  208.                         {
  209.                             Thread.Sleep(time5);
  210.  
  211.                             int random_number = rnd.Next(1, 4);
  212.                             string comment = "NULL";
  213.                             if (random_number == 1)
  214.                             {
  215.                                 comment = comment1_name;
  216.                             }
  217.                             else if (random_number == 2)
  218.                             {
  219.                                 comment = comment2_name;
  220.                             }
  221.                             else if (random_number == 3)
  222.                             {
  223.                                 comment = comment3_name;
  224.                             }
  225.                             Thread.Sleep(3000);
  226.                             IWebElement click_comment_instagram = driver.FindElement(By.ClassName("Ypffh"));
  227.                             Thread.Sleep(time6);
  228.                             click_comment_instagram.Click();
  229.                             Thread.Sleep(time7);
  230.                             IWebElement write_comment_instagram = driver.FindElement(By.ClassName("focus-visible"));
  231.                             write_comment_instagram.SendKeys(comment);
  232.                             Thread.Sleep(time3);
  233.                             IWebElement send_comment_instagram = driver.FindElement(By.XPath("/html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/button"));
  234.                             send_comment_instagram.Click();
  235.                         }
  236.  
  237.  
  238.                     }
  239.                 }
  240.                 else
  241.                 {
  242.                     current_like--;
  243.                 }
  244.                 Thread.Sleep(4500);
  245.                 // /html/body/div[5]/div/div/div[2]/button[2]
  246.                 // instagram error
  247.                 IWebElement instagram_close_error;
  248.                 try
  249.                 {
  250.                     instagram_close_error = driver.FindElement(By.XPath("/html/body/div[5]/div/div/div[2]/button[2]"));
  251.  
  252.                 }
  253.                 catch (Exception)
  254.                 {
  255.                     continue;
  256.                    
  257.                 }
  258.  
  259.                 if (instagram_close_error.Displayed)
  260.                 {
  261.                     instagram_close_error.Click();
  262.                     Thread.Sleep(4000);
  263.                    
  264.                 }
  265.  
  266.                
  267.                 // close posts
  268.                 Thread.Sleep(time2);
  269.                 IWebElement instagram_close_photo = driver.FindElement(By.XPath("/html/body/div[4]/div[3]/button"));
  270.                 instagram_close_photo.Click();
  271.                
  272.  
  273.  
  274.  
  275.                 // scroll down after 3 posts
  276.                 if (number_of_photo_in_a_row % 3 == 0)
  277.                 {
  278.                     next_column++;
  279.                     Thread.Sleep(time);
  280.  
  281.  
  282.                     js.ExecuteScript("window.scrollBy(0,254);");
  283.                     Thread.Sleep(time6);
  284.                 }
  285.  
  286.                 // resetting rows fater 3 posts
  287.                 if (number_of_photo_in_a_row == 3)
  288.                 {
  289.  
  290.                     number_of_photo_in_a_row = 0;
  291.                     Thread.Sleep(1000);
  292.                 }
  293.                 number_of_photo_in_a_row++;
  294.             }
  295.  
  296.         }
  297.  
  298.     }
  299. }