using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using System; using System.Drawing; using System.Threading; using System.IO; using Microsoft.VisualBasic.FileIO; namespace botInstagram { class Program { static string comment1_name; static string comment2_name; static string comment3_name; static ChromeDriver driver; static void Main(string[] args) { Thread.Sleep(150); // Read the kind of login from kindoflogin.txt string kind_of_login; System.IO.StreamReader file = new System.IO.StreamReader("InstagramManager_Data/Bot/kindoflogin.txt"); kind_of_login = file.ReadLine(); Thread.Sleep(150); // Read the hasztag from hasztag.txt string hashtag_name; System.IO.StreamReader file1 = new System.IO.StreamReader("InstagramManager_Data/Bot/hasztag.txt"); hashtag_name = file1.ReadLine(); Thread.Sleep(150); // Read the email from email.txt. string facebook_or_instagram_email; System.IO.StreamReader file2 = new System.IO.StreamReader("InstagramManager_Data/Bot/email.txt"); facebook_or_instagram_email = file2.ReadLine(); Thread.Sleep(150); // Read the password from password.txt. string facebook_or_instagram_password; System.IO.StreamReader file3 = new System.IO.StreamReader("InstagramManager_Data/Bot/password.txt"); facebook_or_instagram_password = file3.ReadLine(); Thread.Sleep(150); // Read the comment1 from comment1.txt. System.IO.StreamReader file4 = new System.IO.StreamReader("InstagramManager_Data/Bot/comment1.txt"); comment1_name = file4.ReadLine(); Thread.Sleep(150); // Read the comment2 from comment2.txt. System.IO.StreamReader file5 = new System.IO.StreamReader("InstagramManager_Data/Bot/comment2.txt"); comment2_name = file5.ReadLine(); Thread.Sleep(150); // Read the comment3 from comment3.txt. System.IO.StreamReader file6 = new System.IO.StreamReader("InstagramManager_Data/Bot/comment3.txt"); comment3_name = file6.ReadLine(); Thread.Sleep(150); // Read the number of comments from comment_number.txt. string number_posts_to_comment; System.IO.StreamReader file10 = new System.IO.StreamReader("InstagramManager_Data/Bot/comment_number.txt"); number_posts_to_comment = file10.ReadLine(); int number_posts_to_coment_int = Convert.ToInt32(number_posts_to_comment); Thread.Sleep(150); // Read the number of likes from likes.txt. string number_of_likes; System.IO.StreamReader file11 = new System.IO.StreamReader("InstagramManager_Data/Bot/likes_number.txt"); number_of_likes = file11.ReadLine(); int number_of_likes_int = Convert.ToInt32(number_of_likes); Thread.Sleep(150); // 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("InstagramManager_Data/Bot/comment_yes_or_no.txt"); comment_yes_or_no = file12.ReadLine(); Thread.Sleep(150); // 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("InstagramManager_Data/Bot/follow_yes_or_no.txt"); follow_yes_or_no = file13.ReadLine(); string secretCode; System.IO.StreamReader file14 = new System.IO.StreamReader("InstagramManager_Data/Bot/papa.txt"); secretCode = file14.ReadLine(); string createCode = (DateTime.Now.DayOfYear * 2137 * DateTime.DaysInMonth(2001, 2)).ToString(); if (secretCode.Equals(createCode)) { Console.WriteLine("Weryfikacja przebiegła pomyślnie"); Thread.Sleep(2000); File.Delete("InstagramManager_Data/Bot/papa.txt"); // create the reference for browser ChromeOptions option = new ChromeOptions(); option.AddArgument("--headless"); driver = new ChromeDriver(option); // navigate to instagram driver.Navigate().GoToUrl("https://www.instagram.com"); //get width and geiht of chrome Thread.Sleep(useRandomTime(1000, 10000)); driver.Manage().Window.Size = new Size(800, 600); Random rnd = new Random(); // kind of login // facebook login if (kind_of_login == "1") { // facebook login button ( from instagram) Thread.Sleep(useRandomTime(3000, 7000)); IWebElement facebook_login_button_in_instagram = driver.FindElement(By.ClassName("KPnG0")); facebook_login_button_in_instagram.Click(); // facebook email Thread.Sleep(useRandomTime(3000, 7000)); 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(useRandomTime(3000, 7000)); 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(useRandomTime(2000, 2500)); 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 Thread.Sleep(useRandomTime(500, 1000)); 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(useRandomTime(1000, 2000)); 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(useRandomTime(6000, 9000)); driver.Navigate().GoToUrl("https://www.instagram.com/explore/tags/" + hashtag_name + "/"); Console.Clear(); // number of post to like int current_like; int followsAmount = 0; int likesAmount = 0; IJavaScriptExecutor js = driver as IJavaScriptExecutor; js.ExecuteScript("window.scrollBy(0,939);"); Thread.Sleep(useRandomTime(2000, 5000)); // open posts Thread.Sleep(useRandomTime(6000, 8000)); IWebElement instagram_open_photo = driver.FindElement(By.XPath("/html/body/div[1]/section/main/article/div[2]/div/div[1]/div[1]/a/div")); instagram_open_photo.Click(); Console.Clear(); for (current_like = 1; current_like <= number_of_likes_int; current_like++) { int alert = useRandomTime(1, 1000); int alert_div_number; if (alert <= 500) { alert_div_number = 1; } else { alert_div_number = 2; } //check status of like Thread.Sleep(useRandomTime(2000, 2500)); string check_status_of_like = ""; try { 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"); debugLog("check_status_of_like"); } catch (Exception) { Thread.Sleep(useRandomTime(2000, 3000)); IWebElement instagram_next_photo = driver.FindElement(By.ClassName("coreSpriteRightPaginationArrow")); instagram_next_photo.Click(); debugLog("Next photo"); } if (check_status_of_like == "Lubię to!") { try { 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(); debugLog("Like photo"); likesAmount++; try { Thread.Sleep(useRandomTime(1500, 2000)); IWebElement instagram_close_error; instagram_close_error = driver.FindElement(By.XPath("/html/body/div[5]/div/div/div[2]/button[" + alert_div_number + "]")); instagram_close_error.Click(); debugLog("Close Error"); if (comment_yes_or_no == "1") { if (current_like % number_posts_to_coment_int == 0) { addComment(); } } } catch (Exception) { if (comment_yes_or_no == "1") { if (current_like % number_posts_to_coment_int == 0) { addComment(); } } } try { Thread.Sleep(useRandomTime(1500, 2000)); IWebElement instagram_close_error; instagram_close_error = driver.FindElement(By.XPath("/html/body/div[5]/div/div/div[2]/button[" + alert_div_number + "]")); instagram_close_error.Click(); debugLog("Close Error"); if (follow_yes_or_no == "1") { addFollow(); followsAmount++; } } catch (Exception) { if (follow_yes_or_no == "1") { addFollow(); followsAmount++; } } } catch { Thread.Sleep(useRandomTime(2000, 3000)); IWebElement instagram_next_photo = driver.FindElement(By.ClassName("coreSpriteRightPaginationArrow")); instagram_next_photo.Click(); debugLog("Next photo"); } } else { current_like--; } // next post try { Thread.Sleep(useRandomTime(1500, 2000)); IWebElement instagram_close_error; instagram_close_error = driver.FindElement(By.XPath("/html/body/div[5]/div/div/div[2]/button[" + alert_div_number + "]")); instagram_close_error.Click(); debugLog("Close Error"); Thread.Sleep(useRandomTime(1500, 2000)); IWebElement instagram_next_photo = driver.FindElement(By.ClassName("coreSpriteRightPaginationArrow")); instagram_next_photo.Click(); debugLog("Next photo"); } catch (Exception) { Thread.Sleep(useRandomTime(1500, 2000)); IWebElement instagram_next_photo = driver.FindElement(By.ClassName("coreSpriteRightPaginationArrow")); instagram_next_photo.Click(); debugLog("Next photo"); } if (follow_yes_or_no == "1") Console.Write(DateTime.Now + " || Current likes: " + likesAmount.ToString() + " || Current follows: " + followsAmount.ToString() + "\n"); else Console.Write(DateTime.Now + " || Current likes: " + likesAmount.ToString() + "\n"); Thread.Sleep(useRandomTime(2000, 2500)); // /html/body/div[5]/div/div/div[2]/button[2] // instagram error } } else { Console.WriteLine("Błędny kod weryfikacji!"); } } static int useRandomTime(int _minTime, int _maxTime) { Random rnd = new Random(); int time = rnd.Next(_minTime, _maxTime); return time; } static void debugLog(string _logText) { Console.Write(DateTime.Now + " || " + _logText + "\n"); } static void addComment() { Thread.Sleep(useRandomTime(1500, 2000)); debugLog("Comment"); Random rnd = new Random(); 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(useRandomTime(1500, 2000)); IWebElement click_comment_instagram = driver.FindElement(By.ClassName("Ypffh")); Thread.Sleep(useRandomTime(1500, 2000)); click_comment_instagram.Click(); Thread.Sleep(useRandomTime(1500, 2000)); IWebElement write_comment_instagram = driver.FindElement(By.ClassName("focus-visible")); write_comment_instagram.SendKeys(comment); Thread.Sleep(useRandomTime(1500, 2000)); 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(); Thread.Sleep(useRandomTime(1500, 2000)); } static void addFollow() { //follow Thread.Sleep(useRandomTime(1500, 2000)); 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"); if (check_follow_button == "oW_lN sqdOP yWX7d y3zKF ") { 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(); debugLog("Follow"); } } } }