- using OpenQA.Selenium;
- using OpenQA.Selenium.Chrome;
- using System;
- using System.Threading;
- using System.IO;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Imaging;
- using net.openstack.Providers.Rackspace.Objects.Monitoring;
- namespace botInstagram
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Read the kind of login from kindoflogin.txt
- string kind_of_login;
- System.IO.StreamReader file = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/kindoflogin.txt");
- kind_of_login = file.ReadLine();
- // Read the hasztag from hasztag.txt
- string hashtag_name;
- System.IO.StreamReader file1 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/hasztag.txt");
- hashtag_name = file1.ReadLine();
- // Read the email from email.txt.
- string facebook_or_instagram_email;
- System.IO.StreamReader file2 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/email.txt");
- facebook_or_instagram_email = file2.ReadLine();
- // Read the password from password.txt.
- string facebook_or_instagram_password;
- System.IO.StreamReader file3 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/password.txt");
- facebook_or_instagram_password = file3.ReadLine();
- // Read the comment1 from comment1.txt.
- string comment1_name;
- System.IO.StreamReader file4 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment1.txt");
- comment1_name = file4.ReadLine();
- // Read the comment2 from comment2.txt.
- string comment2_name;
- System.IO.StreamReader file5 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment2.txt");
- comment2_name = file5.ReadLine();
- // Read the comment3 from comment3.txt.
- string comment3_name;
- System.IO.StreamReader file6 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment3.txt");
- comment3_name = file6.ReadLine();
- // Read the number of comments from comment_number.txt.
- string number_posts_to_comment;
- System.IO.StreamReader file10 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment_number.txt");
- number_posts_to_comment = file10.ReadLine();
- int number_posts_to_coment_int = Convert.ToInt32(number_posts_to_comment);
- // Read the number of likes from likes.txt.
- string number_of_likes;
- System.IO.StreamReader file11 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/likes_number.txt");
- number_of_likes = file11.ReadLine();
- int number_of_likes_int = Convert.ToInt32(number_of_likes);
- // Read the comment (comm or no) of likes from comment_yes_or_no.txt.
- string comment_yes_or_no;
- System.IO.StreamReader file12 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/comment_yes_or_no.txt");
- comment_yes_or_no = file12.ReadLine();
- // Read the follow (comm or no) of likes from follow_yes_or_no.txt.
- string follow_yes_or_no;
- System.IO.StreamReader file13 = new System.IO.StreamReader(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/follow_yes_or_no.txt");
- follow_yes_or_no = file13.ReadLine();
- // create the reference for browser
- ChromeDriver driver = new ChromeDriver(@"C:/Users/" + Environment.UserName + "/Desktop/botInstagram/Drivers/");
- // navigate to instagram
- driver.Navigate().GoToUrl("https://www.instagram.com");
- //get width and geiht of chrome
- Thread.Sleep(3000);
- driver.Manage().Window.Size = new Size(800, 600);
- // kind of login
- // facebook login
- if (kind_of_login == "1")
- {
- // facebook login button ( from instagram)
- Thread.Sleep(5000);
- IWebElement facebook_login_button_in_instagram = driver.FindElement(By.ClassName("KPnG0"));
- facebook_login_button_in_instagram.Click();
- // facebook email
- Thread.Sleep(5000);
- 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"));
- facebook_login_email.SendKeys(facebook_or_instagram_email);
- // facebokk password
- 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"));
- facebook_login_password.SendKeys(facebook_or_instagram_password);
- // facebook login button
- Thread.Sleep(5000);
- 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"));
- facebook_login_button.Click();
- }
- // instagram login
- else if (kind_of_login == "2")
- {
- // instagram email
- Thread.Sleep(5000);
- 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"));
- facebook_login_email.SendKeys(facebook_or_instagram_email);
- // instagram password
- 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"));
- instagram_login_password.SendKeys(facebook_or_instagram_password);
- // instagram login button
- Thread.Sleep(5000);
- IWebElement facebook_login_button = driver.FindElement(By.XPath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button"));
- facebook_login_button.Click();
- }
- // searching hasztags
- Thread.Sleep(8000);
- driver.Navigate().GoToUrl("https://www.instagram.com/explore/tags/" + hashtag_name + "/");
- // number of post to like
- int next_column = 1;
- int current_like;
- int number_of_photo_in_a_row = 1;
- IJavaScriptExecutor js = driver as IJavaScriptExecutor;
- js.ExecuteScript("window.scrollBy(0,939);");
- Thread.Sleep(1000);
- for (current_like = 1; current_like <= number_of_likes_int; current_like++)
- {
- Random rnd = new Random();
- int time = rnd.Next(5000, 8000);
- int time2 = rnd.Next(5000, 8000);
- int time3 = rnd.Next(5000, 8000);
- int time4 = rnd.Next(5000, 8000);
- int time5 = rnd.Next(5000, 8000);
- int time6 = rnd.Next(5000, 8000);
- int time7 = rnd.Next(5000, 8000);
- // open posts
- Thread.Sleep(time);
- Thread.Sleep(4000);
- 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"));
- // / html / body / div[1] / section / main / article / div[2] / div / div[1] / div[1] / a / div
- // /html / body / div[1 ]/ section / main / article / div[2] / div /div[2] / div[1] / a / div
- instagram_open_photo.Click();
- //check status of like
- Thread.Sleep(time2);
- 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"); ;
- if (check_status_of_like == "LubiÄ™ to!")
- {
- // like posts
- Thread.Sleep(time3);
- IWebElement instagram_like_photo = driver.FindElement(By.XPath("/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]"));
- instagram_like_photo.Click();
- //follow
- if (follow_yes_or_no == "1")
- {
- Thread.Sleep(time4);
- IWebElement click_follow_button = driver.FindElement(By.XPath("/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button"));
- click_follow_button.Click();
- }
- if (comment_yes_or_no == "1")
- {
- // comment
- if (current_like % number_posts_to_coment_int == 0)
- {
- Thread.Sleep(time5);
- int random_number = rnd.Next(1, 4);
- string comment = "NULL";
- if (random_number == 1)
- {
- comment = comment1_name;
- }
- else if (random_number == 2)
- {
- comment = comment2_name;
- }
- else if (random_number == 3)
- {
- comment = comment3_name;
- }
- Thread.Sleep(3000);
- IWebElement click_comment_instagram = driver.FindElement(By.ClassName("Ypffh"));
- Thread.Sleep(time6);
- click_comment_instagram.Click();
- Thread.Sleep(time7);
- IWebElement write_comment_instagram = driver.FindElement(By.ClassName("focus-visible"));
- write_comment_instagram.SendKeys(comment);
- Thread.Sleep(time3);
- IWebElement send_comment_instagram = driver.FindElement(By.XPath("/html/body/div[4]/div[2]/div/article/div[2]/section[3]/div/form/button"));
- send_comment_instagram.Click();
- }
- }
- }
- else
- {
- current_like--;
- }
- Thread.Sleep(4500);
- // /html/body/div[5]/div/div/div[2]/button[2]
- // instagram error
- IWebElement instagram_close_error;
- try
- {
- instagram_close_error = driver.FindElement(By.XPath("/html/body/div[5]/div/div/div[2]/button[2]"));
- }
- catch (Exception)
- {
- continue;
- }
- if (instagram_close_error.Displayed)
- {
- instagram_close_error.Click();
- Thread.Sleep(4000);
- }
- // close posts
- Thread.Sleep(time2);
- IWebElement instagram_close_photo = driver.FindElement(By.XPath("/html/body/div[4]/div[3]/button"));
- instagram_close_photo.Click();
- // scroll down after 3 posts
- if (number_of_photo_in_a_row % 3 == 0)
- {
- next_column++;
- Thread.Sleep(time);
- js.ExecuteScript("window.scrollBy(0,254);");
- Thread.Sleep(time6);
- }
- // resetting rows fater 3 posts
- if (number_of_photo_in_a_row == 3)
- {
- number_of_photo_in_a_row = 0;
- Thread.Sleep(1000);
- }
- number_of_photo_in_a_row++;
- }
- }
- }
- }