def follow_people(amount): # Click followers driver.find_element_by_xpath( '''//*[@id="react-root"]/section/main/article/header/section/ul/li[2]/a''' ).click() sleep() for future_followers in range(amount): data_names = pickle.load(open("../../data/Instagram_data.p", "rb")) username_list = data_names['username'].tolist() name = driver.find_elements_by_class_name('_2g7d5') buttons = "../../../../div[2]/span" if name[future_followers].text == 'linethmm': continue if name[future_followers].text in username_list: continue if name[future_followers].find_element_by_xpath( buttons).text == 'Follow': name[future_followers].find_element_by_xpath(buttons).click() sleep() else: continue print("Now following: ", name[future_followers].text) write_to_database(name, future_followers)
def check_if_account_is_private(): try: if driver.find_element_by_xpath('''//*[@id="react-root"]/section/main/div/div/article/div/div/h2'''): print('Account is Private') sleep() return True except NoSuchElementException: return False
def check_if_image_is_not_a_video(): try: if driver.find_element_by_class_name('QvAa1'): driver.find_element_by_class_name( '''coreSpriteRightPaginationArrow''').click() sleep() except: pass
def check_if_image_is_not_a_video(): try: driver.find_element_by_class_name('''_gu6vm''').click() return True except NoSuchElementException: # Skips the video and goes to the next image to check if it's not a video driver.find_element_by_class_name('''coreSpriteRightPaginationArrow''').click() sleep() return False
def open_chrome(): global driver global client options = webdriver.ChromeOptions() options.add_argument( "user-data-dir=C:/Users/jamie/PycharmProjects/Instagram/Profiles/Liking_people_following_Profile" ) # Path to your chrome profile driver = webdriver.Chrome(executable_path='../../assets/chromedriver', chrome_options=options) driver.get("https://www.instagram.com/") sleep()
def write_to_database(name, future_followers): # Begin pickle data = pickle.load(open("../../data/Instagram_data.p", "rb")) df = pd.DataFrame( [[name[future_followers].text, 'Following', str(datetime.datetime.now()), 'Follow_influencer_person_' + str(influencer)]], columns=['username', 'status', 'time_stamp', 'acquisition']) data = data.append(df) pickle.dump(data, open("../../data/Instagram_data.p", "wb")) # End pickle sleep()
def search_famous_person(ursname): # Search bar search = driver.find_element_by_xpath('''//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input''') search.clear() search.send_keys(ursname) search.send_keys(Keys.ENTER) sleep() # Goes to first person in search search_results = driver.find_elements_by_class_name('_ndl3t') search_results[0].click() sleep()
def check_if_account_exists(): try: availability = driver.find_elements_by_xpath( '''/html/body/div/div[1]/div/div/h2''') if availability.text == 'Sorry, this page isn\'t available.': return True except NoSuchElementException: driver.back() sleep() driver.refresh() sleep() return False
def likes_persons_posts(num_images_to_like): count_posts = 0 not_pic_count = 0 while count_posts <= num_images_to_like: count_posts += 1 sleep() # if statement looks for a video if count_posts >= num_images_to_like: break try: like_unlike_check() sleep() # right click on images to scroll driver.find_element_by_class_name('''HBoOv''').click() sleep() except NoSuchElementException: print('Image is not a picture!') not_pic_count += 1 count_posts =- 1 if not_pic_count == 3: break if not_pic_count == 3: driver.back() else: driver.back() sleep() driver.back()
def likes_persons_posts(num_images_to_like): count_posts = 0 while count_posts < num_images_to_like: # if statement looks for a video try: like_unlike_check() sleep() # right click on images to scroll right_arrow() sleep() count_posts += 1 except NoSuchElementException: print('Image is not a picture!') count_posts += 1
def tab_and_space(num_spacebars): tab = 0 while tab <= 2: actions_tab = ActionChains(driver) actions_tab.send_keys(Keys.TAB) actions_tab.perform() time.sleep(.5) tab += 1 sleep() count = 0 while count < int(num_spacebars / 3): # Spacebar X number of times actions_space = ActionChains(driver) actions_space.send_keys(Keys.SPACE) actions_space.perform() time.sleep(.75) count += 1
def num_posts_to_like(num_images_to_like): count_posts = 0 while count_posts < num_images_to_like: # if statement looks for a video try: like_unlike_check() sleep() # right click on images to scroll driver.find_element_by_class_name( '''coreSpriteRightPaginationArrow''').click() sleep() count_posts += 1 except NoSuchElementException: print('Image is not a picture!') count_posts += 1
def follow_people(num_of_people, num_of_their_followers, sleep_time_minutes): for people in range(num_of_people): name = driver.find_elements_by_class_name('FPmhX') user_followers_list.append(name[people].text) driver.back() sleep() ################################Get's list of people to follow COMPLETE############################################ ppl_followed = 0 for person in user_followers_list[7:]: # Goes directly to person's profile driver.get("https://www.instagram.com/" + person) sleep() if check_if_account_is_private(): continue # clicks followers try: click_posts_followers_followings('followers') sleep() except: continue repeat_space_bar(num_of_their_followers/3) people_to_follow_from_friend = [] for future_followers in range(num_of_their_followers): name = driver.find_elements_by_class_name('FPmhX') people_to_follow_from_friend.append('https://www.instagram.com/'+name[future_followers].text) data_names = pickle.load(open("../../data/Instagram_data.p", "rb")) username_list = data_names['user_url'].tolist() # TODO - filter by number of interactions! for person_url in people_to_follow_from_friend: if person_url in username_list: continue if isEnglish(person_url[26:]) == False: continue driver.get(person_url) if check_if_account_is_private(): continue # Make sure page is available try: if driver.find_element_by_class_name('error-container'): continue except NoSuchElementException: pass # Check stats to make sure they are "acceptable" if stats_range(follower_min=150, follower_max=25000, following_min=150, following_max=6000, posts_min=35, posts_max=99999999) == False: sleep() continue flw_btn = driver.find_element_by_class_name('_5f5mN') if flw_btn.text == 'Follow': flw_btn.click() ppl_followed += 1 sleep() else: continue print("Now following: ", person_url) posts = driver.find_elements_by_class_name('g47SY')[0].text followers = driver.find_elements_by_class_name('g47SY')[1].text followings = driver.find_elements_by_class_name('g47SY')[2].text # Begin pickle data = pickle.load(open("../../data/Instagram_data.p", "rb")) df = pd.DataFrame([['No', os.path.basename(__file__)[:-3], 'Following', driver.current_url, str(datetime.datetime.now()), str(datetime.datetime.now()), 1, posts, followers, followings]], columns=['Official_Friend', 'acquisition', 'status', 'user_url', 'first_interacted_time', 'last_interacted_time', 'number_of_interactions', 'posts', 'followers', 'following']) data = data.append(df) pickle.dump(data, open("../../data/Instagram_data.p", "wb")) # End pickle if (ppl_followed + 1) % 11 == 0: # Sleeps for 6 minutes every 10 unfollow print(ppl_followed, 'Followed: Waiting', sleep_time_minutes, 'minutes') time.sleep(sleep_time_minutes * 60) else: continue driver.back() time.sleep(3) driver.back() sleep()
def follow_people(amount, num_pics_to_like): # Click followers click_posts_followers_followings('followers') sleep() for future_followers in range(amount): data_names = pickle.load(open("../../data/Instagram_data.p", "rb")) username_list = data_names['username'].tolist() # TODO - update 1 name = driver.find_elements_by_class_name('FPmhX') buttons = "../../../../div[2]/span" # Makes sure there are only english characters if isEnglish(name[future_followers].text) == False: continue # Making sure we haven't already iteracted with them or is our own account if name[future_followers].text == 'linethmm' or name[future_followers].text in username_list: # TODO - update 2 to check print(name[future_followers].text) continue if name[future_followers].find_element_by_xpath(buttons).text == 'Follow': name[future_followers].find_element_by_xpath(buttons).click() print("Now following: ", name[future_followers].text) write_to_database(name, future_followers) sleep() #Deletethis # TODO - THIS IS WHERE TO TURN ON AND OFF THE LIKING STUFFFFFF # continue else: continue # Click person in list order (goes to their profile) name[future_followers].click() sleep() # Check number of posts they have! # Makes sure that the user has enough images to like! try: total_images = driver.find_element_by_xpath( '''//*[@id="react-root"]/section/main/div/header/section/ul/li[1]/span/span''').text total_images = remove_k_m_periods_commas(total_images) if total_images >= num_pics_to_like: total_images = num_pics_to_like except NoSuchElementException: print('unavailable page?') driver.back() continue # Clicks the person's first image try: # TODO - update 3... change class name from _e3il2 to eLAPa driver.find_element_by_class_name('''eLAPa''').click() sleep() except NoSuchElementException: print('Private account') driver.back() continue likes_persons_posts(total_images) sleep()
def like_peoples_stuffs(people_to_follow, number_of_pics_to_like): # go to profile driver.find_element_by_xpath( '''//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/a''' ).click() sleep() # Select following people driver.find_element_by_xpath( '''//*[@id="react-root"]/section/main/article/header/section/ul/li[2]/a''' ).click() sleep() # ########################7################begin space bar!################################################# tab = 0 while tab <= 2: variable = driver.find_element_by_class_name('_si7dy') actions = webdriver.ActionChains(driver) actions.move_to_element(variable) # actions.click() actions.send_keys(Keys.TAB) actions.perform() time.sleep(.5) tab += 1 count = 0 while count < int(people_to_follow / 3): # Spacebar X number of times variable = driver.find_element_by_class_name('_4rbun') actions = webdriver.ActionChains(driver) actions.move_to_element(variable) # actions.click() actions.send_keys(Keys.SPACE) actions.perform() time.sleep(.25) count += 1 # #################################End repeat space bar############################################### people_list = [] for people in range(people_to_follow): selenium_list = driver.find_elements_by_class_name('''_2g7d5''') people_list.append(selenium_list[people].text) print(people_list) driver.back() sleep() # Likes the first x people! for x in people_list: # Still need to click out of image to get to the search bar!! print(people_list.index(x), x) search = driver.find_element_by_xpath( '''//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input''' ) search.clear() search.send_keys(x) search.send_keys(Keys.ENTER) sleep() # Goes to first person in search search_results = driver.find_elements_by_class_name('_gimca') # checks if results are found try: search_results[0].click() sleep() except: driver.find_element_by_class_name('_oznku') search.clear() # if statement that breaks for loop if were at the end... if people_list.index(x) == len(people_list) - 1: break continue # Check if they found hashtag try: driver.find_element_by_class_name('_kwqc3') continue except NoSuchElementException: if people_list.index(x) == len(people_list) - 1: break pass # Check if they found a location try: driver.find_element_by_class_name('_thew0') continue except NoSuchElementException: if people_list.index(x) == len(people_list) - 1: break pass # Check if they found a Sorry, this page isn't available. try: driver.find_element_by_class_name('error-container') driver.find_element_by_xpath( '''/html/body/div/div[1]/header/div/div[1]/a''').click() time.sleep(3) continue except NoSuchElementException: if people_list.index(x) == len(people_list) - 1: continue pass # Makes sure we are liking the right person! username_found = driver.find_element_by_xpath( '''//*[@id="react-root"]/section/main/article/header/section/div[1]/h1''' ).text if x != username_found: if people_list.index(x) == len(people_list) - 1: break continue if check_if_account_is_private() is True: if people_list.index(x) == len(people_list) - 1: break continue # Makes sure that the user has enough images to like! total_images = driver.find_element_by_xpath( '''//*[@id="react-root"]/section/main/article/header/section/ul/li[1]/span/span''' ).text total_images = remove_k_m_periods_commas(total_images) total_images = int(total_images) # Clicks the person's first image try: driver.find_element_by_class_name('''_e3il2''').click() sleep() except NoSuchElementException: driver.back() continue if total_images >= number_of_pics_to_like: total_images = number_of_pics_to_like sleep() likes_persons_posts(total_images) sleep() # Below goes back twice to profile to start for loop over again driver.back() sleep() driver.back() sleep() driver.back() sleep() # go to profile driver.find_element_by_xpath( '''//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/a''' ).click() sleep()
driver.back() time.sleep(3) driver.back() sleep() errors = 3 while errors > 0: try: global driver driver = open_chrome('Follow_Profile') twilio() # go to profile driver.get('https://www.instagram.com/linethmm') sleep() # Get's people to follow! click_posts_followers_followings('followers') sleep() number_of_people = 12 repeat_space_bar(number_of_people/2) # Create new list of people to follow! user_followers_list = [] follow_people(number_of_people, 25, 20) # Number of people, number of followings, time to wait print(user_followers_list) # ###################################Check # of followings########################################################## # go to profile
def like_and_follow_people_then_unfollow(people_list, number_of_pics_to_like=3): # Search for users followed = 0 people_followed_list = [] for person in people_list: print('---searching:', person) #Checks to make sure it is not an official friend! person_url = 'https://www.instagram.com/' + person if person_url in official_friend_urls_list: continue driver.get(person_url) sleep() # Make sure they are not private try: if driver.find_element_by_class_name('rkEop'): continue except NoSuchElementException: pass # Make sure page is available try: if driver.find_element_by_class_name('error-container'): continue except NoSuchElementException: pass # Check stats to make sure they are "acceptable" if stats_range(follower_min=150, follower_max=25000, following_min=150, following_max=5000, posts_min=75, posts_max=99999999) == False: print('out of stat range') continue # Like their first 3 things click_first_post() likes_persons_posts(number_of_pics_to_like) sleep() # Gets out of pic window to go to their account driver.back() sleep() # TODO - turn off/on following them driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME) sleep() try: driver.find_element_by_class_name('_6VtSN').click() except: try: driver.find_elements_by_class_name('BY3EC').click() except: print('cant find follow btn!! :(') people_followed_list.append(driver.current_url) print('following person:', driver.current_url) followed += 1 sleep() # Wait time after 10 follows if (followed + 1) % 10 == 0: print('Waiting 10 minutes') time.sleep(60 * 10) # BREAK - make sure we get to 150 follow if len(people_followed_list) == max_to_follow: print('followed 150, time to unfollow') break print('#' * 20) # Begin pickle posts = driver.find_elements_by_class_name('g47SY')[0].text followers = driver.find_elements_by_class_name('g47SY')[1].text followings = driver.find_elements_by_class_name('g47SY')[2].text data = pickle.load(open("../../data/Instagram_data.p", "rb")) df = pd.DataFrame([[ 'No', os.path.basename(__file__)[:-3], 'Following', driver.current_url, str(datetime.datetime.now()), str(datetime.datetime.now()), 1, posts, followers, followings ]], columns=[ 'Official_Friend', 'acquisition', 'status', 'user_url', 'first_interacted_time', 'last_interacted_time', 'number_of_interactions', 'posts', 'followers', 'following' ]) data = data.append(df) pickle.dump(data, open("../../data/Instagram_data.p", "wb")) # End pickle print(people_followed_list) print('WAIT 1 HOUR TO START UNFOLLOWING!') #time.sleep(60*60*1) # UNFOLLOW SECTION!!! unfollow_count = 0 for people_url in people_followed_list: driver.get(people_url) sleep() driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME) sleep() try: driver.find_element_by_class_name('_5f5mN').click() except: try: driver.find_element_by_class_name('_6VtSN').click() except: try: driver.find_elements_by_class_name('BY3EC').click() except: print('cant find follow btn!! :(') print('unfollowed person', people_url) sleep() unfollow_count += 1 # Begin pickle data = pickle.load(open("../../data/Instagram_data.p", "rb")) df = pd.DataFrame( [[ driver.current_url, 'Unfollowed', str(datetime.datetime.now()), 'Friends_of_friends' ]], columns=['username', 'status', 'time_stamp', 'acquisition']) data = data.append(df) pickle.dump(data, open("../../data/Instagram_data.p", "wb")) # End pickle # Wait time after 10 unfollows if (unfollow_count + 1) % 10 == 0: print('Waiting 10 minutes') time.sleep(60 * 10)
def like_peoples_stuffs(number_of_valeria_pictures, people_to_follow, number_of_pics_to_like, post_number): pic_counter = 1 post_number = post_number for y in range(number_of_valeria_pictures): pic_count = 0 print('picture number:', y) # We don't do vidoes... But this looks for the play button and works check_if_image_is_not_a_video() # Click liking button driver.find_element_by_class_name('zV_Nj').click() sleep() # ########################################begin space bar!################################################# tab = 0 while tab <= 2: actions_tab = ActionChains(driver) actions_tab.send_keys(Keys.TAB) actions_tab.perform() time.sleep(.5) tab += 1 sleep() count = 0 while count < int(people_to_follow / 3): # Spacebar X number of times actions_space = ActionChains(driver) actions_space.send_keys(Keys.SPACE) actions_space.perform() time.sleep(.75) count += 1 # #################################End repeat space bar############################################### people_list = [] for people in range(people_to_follow): selenium_list = driver.find_elements_by_class_name('FPmhX') people_list.append(selenium_list[people].text) print(people_list) driver.back() # Likes the first x people! for x in people_list: # Still need to click out of image to get to the search bar!! sleep() print(people_list.index(x), x) search(x) # Check if they found a Sorry, this page isn't available. try: driver.find_element_by_class_name('error-container') driver.find_element_by_xpath( '''/html/body/div/div[1]/header/div/div[1]/a''').click() time.sleep(3) continue except NoSuchElementException: if people_list.index(x) == len(people_list) - 1: continue pass if check_if_account_is_private() is True: if people_list.index(x) == len(people_list) - 1: break continue # Makes sure that the user has enough images to like! total_images = driver.find_elements_by_class_name('g47SY')[0].text total_images = remove_k_m_periods_commas(total_images) # Clicks the person's first image click_specific_post(0) if total_images >= number_of_pics_to_like: total_images = number_of_pics_to_like sleep() likes_persons_posts(total_images) sleep() # Below goes back twice to profile to start for loop over again driver.back() sleep() # driver.back() # sleep() driver.back() sleep() # go to profile go_to_profile() sleep() post_number += 1 # Clicks image we started with and then loops through where to go... (not the best) click_specific_post(post_number) sleep() while pic_count < pic_counter: driver.find_element_by_class_name( '''coreSpriteRightPaginationArrow''').click() pic_count += 1 sleep() pic_counter += 1
def like_people(number_of_people, number_pics_to_like): count = 0 followed = 0 while count < number_of_people: #clicks person's name to go their profile try: driver.find_element_by_class_name('FPmhX').click() sleep() except: right_arrow() count += 1 continue print('liking:' + driver.find_element_by_class_name('AC5d8').text) try: # If out of range. Go back and select next picture if stats_range() is False: driver.back() sleep() right_arrow() sleep() continue except: # if you can't find the stats still go back... driver.back() sleep() right_arrow() sleep() continue click_specific_post(0) sleep() likes_persons_posts(number_pics_to_like) sleep() #Goes back twice to get back to hashtag driver.back() sleep() driver.back() sleep() # right click on images to scroll right_arrow() count += 1 sleep()