Facebook
From Colorant Crane, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 148
  1. using OpenQA.Selenium;
  2. using OpenQA.Selenium.Chrome;
  3. using System;
  4. using System.Drawing;
  5. using System.Threading;
  6. using System.IO;
  7. using Microsoft.VisualBasic.FileIO;
  8.  
  9. namespace botInstagram
  10. {
  11.     class Program
  12.     {
  13.  
  14.         static string comment1_name;
  15.         static string comment2_name;
  16.         static string comment3_name;
  17.  
  18.         static ChromeDriver driver;
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.  
  23.             Thread.Sleep(150);
  24.  
  25.             // Read the kind of login from kindoflogin.txt  
  26.  
  27.             string kind_of_login;
  28.             System.IO.StreamReader file = new System.IO.StreamReader("InstagramManager_Data/Bot/kindoflogin.txt");
  29.             kind_of_login = file.ReadLine();
  30.  
  31.             Thread.Sleep(150);
  32.  
  33.             // Read the hasztag from hasztag.txt    
  34.             string hashtag_name;
  35.             System.IO.StreamReader file1 = new System.IO.StreamReader("InstagramManager_Data/Bot/hasztag.txt");
  36.             hashtag_name = file1.ReadLine();
  37.  
  38.             Thread.Sleep(150);
  39.  
  40.             // Read the email from email.txt.
  41.             string facebook_or_instagram_email;
  42.             System.IO.StreamReader file2 = new System.IO.StreamReader("InstagramManager_Data/Bot/email.txt");
  43.             facebook_or_instagram_email = file2.ReadLine();
  44.  
  45.             Thread.Sleep(150);
  46.  
  47.             // Read the password from password.txt.
  48.             string facebook_or_instagram_password;
  49.             System.IO.StreamReader file3 = new System.IO.StreamReader("InstagramManager_Data/Bot/password.txt");
  50.             facebook_or_instagram_password = file3.ReadLine();
  51.  
  52.             Thread.Sleep(150);
  53.  
  54.             // Read the comment1 from comment1.txt.
  55.             System.IO.StreamReader file4 = new System.IO.StreamReader("InstagramManager_Data/Bot/comment1.txt");
  56.             comment1_name = file4.ReadLine();
  57.  
  58.             Thread.Sleep(150);
  59.  
  60.             // Read the comment2 from comment2.txt.
  61.             System.IO.StreamReader file5 = new System.IO.StreamReader("InstagramManager_Data/Bot/comment2.txt");
  62.             comment2_name = file5.ReadLine();
  63.  
  64.             Thread.Sleep(150);
  65.  
  66.             // Read the comment3 from comment3.txt.
  67.             System.IO.StreamReader file6 = new System.IO.StreamReader("InstagramManager_Data/Bot/comment3.txt");
  68.             comment3_name = file6.ReadLine();
  69.  
  70.             Thread.Sleep(150);
  71.  
  72.             // Read the number of comments from comment_number.txt.
  73.             string number_posts_to_comment;
  74.             System.IO.StreamReader file10 = new System.IO.StreamReader("InstagramManager_Data/Bot/comment_number.txt");
  75.             number_posts_to_comment = file10.ReadLine();
  76.             int number_posts_to_coment_int = Convert.ToInt32(number_posts_to_comment);
  77.  
  78.             Thread.Sleep(150);
  79.  
  80.             // Read the number of likes from likes.txt.
  81.             string number_of_likes;
  82.             System.IO.StreamReader file11 = new System.IO.StreamReader("InstagramManager_Data/Bot/likes_number.txt");
  83.             number_of_likes = file11.ReadLine();
  84.             int number_of_likes_int = Convert.ToInt32(number_of_likes);
  85.  
  86.             Thread.Sleep(150);
  87.  
  88.             // Read the comment (comm or no) of likes from comment_yes_or_no.txt.
  89.             string comment_yes_or_no;
  90.             System.IO.StreamReader file12 = new System.IO.StreamReader("InstagramManager_Data/Bot/comment_yes_or_no.txt");
  91.             comment_yes_or_no = file12.ReadLine();
  92.  
  93.             Thread.Sleep(150);
  94.  
  95.             // Read the follow (comm or no) of likes from follow_yes_or_no.txt.
  96.             string follow_yes_or_no;
  97.             System.IO.StreamReader file13 = new System.IO.StreamReader("InstagramManager_Data/Bot/follow_yes_or_no.txt");
  98.             follow_yes_or_no = file13.ReadLine();
  99.  
  100.             string secretCode;
  101.             System.IO.StreamReader file14 = new System.IO.StreamReader("InstagramManager_Data/Bot/papa.txt");
  102.             secretCode = file14.ReadLine();
  103.  
  104.            
  105.             string createCode = (DateTime.Now.DayOfYear * 2137 * DateTime.DaysInMonth(2001, 2)).ToString();
  106.  
  107.            
  108.             if (secretCode.Equals(createCode))
  109.             {
  110.                 Console.WriteLine("Weryfikacja przebiegła pomyślnie");
  111.  
  112.                 Thread.Sleep(2000);
  113.                 File.Delete("InstagramManager_Data/Bot/papa.txt");
  114.                
  115.  
  116.                 // create the reference for browser
  117.                 ChromeOptions option = new ChromeOptions();
  118.                 option.AddArgument("--headless");
  119.  
  120.                 driver = new ChromeDriver(option);
  121.  
  122.                 // navigate to instagram
  123.                 driver.Navigate().GoToUrl("https://www.instagram.com");
  124.  
  125.                 //get width and geiht of chrome
  126.                 Thread.Sleep(useRandomTime(1000, 10000));
  127.                 driver.Manage().Window.Size = new Size(800, 600);
  128.  
  129.                 Random rnd = new Random();
  130.  
  131.                 // kind of login
  132.                 // facebook login
  133.                 if (kind_of_login == "1")
  134.                 {
  135.                     // facebook login button ( from instagram)
  136.                     Thread.Sleep(useRandomTime(3000, 7000));
  137.                     IWebElement facebook_login_button_in_instagram = driver.FindElement(By.ClassName("KPnG0"));
  138.                     facebook_login_button_in_instagram.Click();
  139.  
  140.                     // facebook email
  141.                     Thread.Sleep(useRandomTime(3000, 7000));
  142.                     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"));
  143.                     facebook_login_email.SendKeys(facebook_or_instagram_email);
  144.  
  145.                     // facebokk password
  146.                     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"));
  147.                     facebook_login_password.SendKeys(facebook_or_instagram_password);
  148.  
  149.                     // facebook login button
  150.                     Thread.Sleep(useRandomTime(3000, 7000));
  151.                     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"));
  152.                     facebook_login_button.Click();
  153.  
  154.                 }
  155.  
  156.                 // instagram login
  157.                 else if (kind_of_login == "2")
  158.                 {
  159.  
  160.                     // instagram email
  161.                     Thread.Sleep(useRandomTime(2000, 2500));
  162.                     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"));
  163.                     facebook_login_email.SendKeys(facebook_or_instagram_email);
  164.  
  165.                     // instagram password
  166.                     Thread.Sleep(useRandomTime(500, 1000));
  167.                     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"));
  168.                     instagram_login_password.SendKeys(facebook_or_instagram_password);
  169.  
  170.                     // instagram login button
  171.                     Thread.Sleep(useRandomTime(1000, 2000));
  172.                     IWebElement facebook_login_button = driver.FindElement(By.XPath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button"));
  173.                     facebook_login_button.Click();
  174.  
  175.                 }
  176.  
  177.                 // searching hasztags
  178.                 Thread.Sleep(useRandomTime(6000, 9000));
  179.                 driver.Navigate().GoToUrl("https://www.instagram.com/explore/tags/" + hashtag_name + "/");
  180.  
  181.  
  182.                 Console.Clear();
  183.                 // number of post to like
  184.  
  185.                 int current_like;
  186.                 int followsAmount = 0;
  187.                 int likesAmount = 0;
  188.  
  189.                 IJavaScriptExecutor js = driver as IJavaScriptExecutor;
  190.  
  191.                 js.ExecuteScript("window.scrollBy(0,939);");
  192.                 Thread.Sleep(useRandomTime(2000, 5000));
  193.  
  194.                 // open posts
  195.                 Thread.Sleep(useRandomTime(6000, 8000));
  196.                 IWebElement instagram_open_photo = driver.FindElement(By.XPath("/html/body/div[1]/section/main/article/div[2]/div/div[1]/div[1]/a/div"));
  197.  
  198.  
  199.  
  200.                 instagram_open_photo.Click();
  201.                 Console.Clear();
  202.  
  203.                 for (current_like = 1; current_like <= number_of_likes_int; current_like++)
  204.                 {
  205.                     int alert = useRandomTime(1, 1000);
  206.                     int alert_div_number;
  207.                     if (alert <= 500)
  208.                     {
  209.                         alert_div_number = 1;
  210.                     }
  211.                     else
  212.                     {
  213.                         alert_div_number = 2;
  214.                     }
  215.  
  216.  
  217.                     //check status of like  
  218.                     Thread.Sleep(useRandomTime(2000, 2500));
  219.  
  220.                     string check_status_of_like = "";
  221.  
  222.                     try
  223.                     {
  224.                         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");
  225.                         debugLog("check_status_of_like");
  226.                     }
  227.                     catch (Exception)
  228.                     {
  229.                         Thread.Sleep(useRandomTime(2000, 3000));
  230.                         IWebElement instagram_next_photo = driver.FindElement(By.ClassName("coreSpriteRightPaginationArrow"));
  231.                         instagram_next_photo.Click();
  232.                         debugLog("Next photo");
  233.                     }
  234.  
  235.  
  236.                     if (check_status_of_like == "Lubię to!")
  237.                     {
  238.  
  239.  
  240.  
  241.                         try
  242.                         {
  243.  
  244.                             IWebElement instagram_like_photo = driver.FindElement(By.XPath("/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]"));
  245.                             instagram_like_photo.Click();
  246.                             debugLog("Like photo");
  247.                             likesAmount++;
  248.  
  249.  
  250.                             try
  251.                             {
  252.                                 Thread.Sleep(useRandomTime(1500, 2000));
  253.                                 IWebElement instagram_close_error;
  254.                                 instagram_close_error = driver.FindElement(By.XPath("/html/body/div[5]/div/div/div[2]/button[" + alert_div_number + "]"));
  255.                                 instagram_close_error.Click();
  256.                                 debugLog("Close Error");
  257.  
  258.                                 if (comment_yes_or_no == "1")
  259.                                 {
  260.                                     if (current_like % number_posts_to_coment_int == 0)
  261.                                     {
  262.                                         addComment();
  263.                                     }
  264.                                 }
  265.                             }
  266.                             catch (Exception)
  267.                             {
  268.                                 if (comment_yes_or_no == "1")
  269.                                 {
  270.                                     if (current_like % number_posts_to_coment_int == 0)
  271.                                     {
  272.                                         addComment();
  273.                                     }
  274.                                 }
  275.                             }
  276.  
  277.  
  278.  
  279.                             try
  280.                             {
  281.                                 Thread.Sleep(useRandomTime(1500, 2000));
  282.                                 IWebElement instagram_close_error;
  283.                                 instagram_close_error = driver.FindElement(By.XPath("/html/body/div[5]/div/div/div[2]/button[" + alert_div_number + "]"));
  284.                                 instagram_close_error.Click();
  285.                                 debugLog("Close Error");
  286.                                 if (follow_yes_or_no == "1")
  287.                                 {
  288.  
  289.                                     addFollow();
  290.                                     followsAmount++;
  291.  
  292.  
  293.                                 }
  294.                             }
  295.                             catch (Exception)
  296.                             {
  297.                                 if (follow_yes_or_no == "1")
  298.                                 {
  299.  
  300.                                     addFollow();
  301.                                     followsAmount++;
  302.  
  303.  
  304.                                 }
  305.                             }
  306.  
  307.  
  308.  
  309.  
  310.  
  311.                         }
  312.                         catch
  313.                         {
  314.  
  315.                             Thread.Sleep(useRandomTime(2000, 3000));
  316.                             IWebElement instagram_next_photo = driver.FindElement(By.ClassName("coreSpriteRightPaginationArrow"));
  317.                             instagram_next_photo.Click();
  318.                             debugLog("Next photo");
  319.  
  320.                         }
  321.                     }
  322.                     else
  323.                     {
  324.                         current_like--;
  325.                     }
  326.  
  327.                     // next post
  328.                     try
  329.                     {
  330.                         Thread.Sleep(useRandomTime(1500, 2000));
  331.                         IWebElement instagram_close_error;
  332.                         instagram_close_error = driver.FindElement(By.XPath("/html/body/div[5]/div/div/div[2]/button[" + alert_div_number + "]"));
  333.                         instagram_close_error.Click();
  334.                         debugLog("Close Error");
  335.  
  336.  
  337.                         Thread.Sleep(useRandomTime(1500, 2000));
  338.                         IWebElement instagram_next_photo = driver.FindElement(By.ClassName("coreSpriteRightPaginationArrow"));
  339.                         instagram_next_photo.Click();
  340.                         debugLog("Next photo");
  341.  
  342.                     }
  343.                     catch (Exception)
  344.                     {
  345.  
  346.                         Thread.Sleep(useRandomTime(1500, 2000));
  347.                         IWebElement instagram_next_photo = driver.FindElement(By.ClassName("coreSpriteRightPaginationArrow"));
  348.                         instagram_next_photo.Click();
  349.                         debugLog("Next photo");
  350.  
  351.                     }
  352.  
  353.                     if (follow_yes_or_no == "1") Console.Write(DateTime.Now + " || Current likes: " + likesAmount.ToString() + " || Current follows: " + followsAmount.ToString() + "\n");
  354.                     else Console.Write(DateTime.Now + " || Current likes: " + likesAmount.ToString() + "\n");
  355.  
  356.  
  357.                     Thread.Sleep(useRandomTime(2000, 2500));
  358.  
  359.                     // /html/body/div[5]/div/div/div[2]/button[2]
  360.                     // instagram error
  361.  
  362.                 }
  363.             }
  364.             else
  365.             {
  366.                 Console.WriteLine("Błędny kod weryfikacji!");
  367.             }
  368.  
  369.         }
  370.  
  371.  
  372.  
  373.  
  374.         static int useRandomTime(int _minTime, int _maxTime)
  375.         {
  376.             Random rnd = new Random();
  377.             int time = rnd.Next(_minTime, _maxTime);
  378.  
  379.             return time;
  380.         }
  381.  
  382.         static void debugLog(string _logText)
  383.         {
  384.             Console.Write(DateTime.Now + " || " + _logText + "\n");
  385.         }
  386.  
  387.         static void addComment()
  388.         {
  389.  
  390.  
  391.  
  392.             Thread.Sleep(useRandomTime(1500, 2000));
  393.  
  394.             debugLog("Comment");
  395.  
  396.             Random rnd = new Random();
  397.  
  398.             int random_number = rnd.Next(1, 4);
  399.             string comment = "NULL";
  400.             if (random_number == 1)
  401.             {
  402.                 comment = comment1_name;
  403.             }
  404.             else if (random_number == 2)
  405.             {
  406.                 comment = comment2_name;
  407.             }
  408.             else if (random_number == 3)
  409.             {
  410.                 comment = comment3_name;
  411.             }
  412.             Thread.Sleep(useRandomTime(1500, 2000));
  413.             IWebElement click_comment_instagram = driver.FindElement(By.ClassName("Ypffh"));
  414.             Thread.Sleep(useRandomTime(1500, 2000));
  415.             click_comment_instagram.Click();
  416.             Thread.Sleep(useRandomTime(1500, 2000));
  417.             IWebElement write_comment_instagram = driver.FindElement(By.ClassName("focus-visible"));
  418.             write_comment_instagram.SendKeys(comment);
  419.             Thread.Sleep(useRandomTime(1500, 2000));
  420.             IWebElement send_comment_instagram = driver.FindElement(By.XPath("/html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/button"));
  421.             send_comment_instagram.Click();
  422.             Thread.Sleep(useRandomTime(1500, 2000));
  423.         }
  424.  
  425.         static void addFollow()
  426.         {
  427.             //follow
  428.             Thread.Sleep(useRandomTime(1500, 2000));
  429.             string check_follow_button = driver.FindElement(By.CssSelector("body > div._2dDPU.CkGkG > div.zZYga > div > article > header > div.o-MQd.z8cbW > div.PQo_0.RqtMr > div.bY2yH > button")).GetAttribute("class");
  430.             if (check_follow_button == "oW_lN sqdOP yWX7d    y3zKF     ")
  431.             {
  432.                 IWebElement click_follow_button = driver.FindElement(By.XPath("/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button"));
  433.                 click_follow_button.Click();
  434.  
  435.                 debugLog("Follow");
  436.             }
  437.  
  438.         }
  439.     }
  440. }