def uploadPost(self): posts = usersDB.loadState('uploaded.pickle') babes = list(posts.keys()) i = 0 for babe in babes: try: i += 1 print(posts[babe]["iteration"]) print('babe has already been uploaded') continue except: try: print('new Babe uploading...') post = list(posts[babe].keys())[0] print('fetching posturl', post) image = posts[babe][post]["src"] urllib.request.urlretrieve(image, os.getcwd() + f"/0images/0.png") sleep(2) self.post() posts[babe]["iteration"] = 0 print('delete again') os.remove(os.getcwd() + '/0images/0.png') usersDB.saveState(posts, 'uploaded.pickle') break except: print("empty user... skipping user") continue print('delete again') os.remove(os.getcwd() + '/0images/0.png') usersDB.saveState(posts, 'uploaded.pickle') break
def scrapePrices(self, orderID): #self.driver.find_element_by_xpath("//span[contains(text(), 'Orders')").click() sleep(2) i = 0 while True: try: i += 1 stringID = '#' + str(orderID) order = self.driver.execute_script( """ array = document.body.getElementsByTagName('span') let found for (let i = 0 ; i< array.length ; i++){ if(array[i].innerText == arguments[0]){found = array[i]; break;} } return found """, stringID) price = self.driver.execute_script( """ return arguments[0].parentElement.parentNode.parentNode.children[5].textContent """, order) price = price[1:] print(price) products[orderID] = {} products[orderID]["price"] = float(price) # put all prices to orderID orderID += 1 except: print('reached the end') break print(products) print('end of loop') usersDB.saveState(products, 'revenue.pickle')
def addPictureSrc(): babes = usersDB.loadState('postingBabes.pickle') babeArr = list(babes.keys()) """ posting = usersDB.loadState('posts.pickle') for elem in list(posting.keys()): babeArr.remove(elem) # babes = list(posting.keys()) print('startValue', babes) """ for babe in babeArr: posts = list(babes[babe].keys()) for post in posts: try: print(babes[babe][post]["src"]) print('already downloaded') break except: babes[babe][post] = {} print('enter loop') try: driver.get(post) sleep(2) src = driver.execute_script(""" return document.getElementsByClassName("_97aPb ")[0].querySelector('img').src """) babes[babe][post]["posted"] = False babes[babe][post]["src"] = src # print(posting[babe]) print(babes[babe][post]) # urllib.request.urlretrieve(src, os.getcwd()+ f"/images/{post}.png") usersDB.saveState(babes, 'posts.pickle') print('SAVED') except: continue
def saveAllPostLinks(self): last_height = 0 end = 0 i = 0 while True: results = self.getAllRows(posts) last_height = last_height + 1500 self.driver.execute_script("window.scrollTo(0, arguments[0]);", last_height) sleep(1) new_height = self.driver.execute_script( "return document.documentElement.scrollHeight") if end == new_height: print('reached end of DIV') break else: end = new_height i += 1 print(i) print('new Height', new_height) print('last_height', last_height) if i == 600: i = 0 break usersDB.saveState(results, 'users.pickle')
def extractAllUsers(self): posts = usersDB.loadState('users.pickle') links = list(posts.keys()) for link in links: self.driver.switch_to.window(self.driver.window_handles[0]) self.getPost(link, self.getUserHandle) usersDB.saveState(users, 'babes.pickle') print('saved state to file')
def findQualityPicture(self): babes = usersDB.loadState('babes.pickle') babesArr = list(babes.keys()) for babe in babesArr: # from the first page get all pictures and their likes # result result = self.getPost(babe, self.getAllRows, babes[babe]) babes[babe] = result usersDB.saveState(babes, 'babes.pickle') print('final result', babes)
def getLikesFromAll(self): babes = usersDB.loadState('babes.pickle') babeArr = list(babes.keys()) posting = usersDB.loadState('postingBabes.pickle') for elem in list(posting.keys()): babeArr.remove(elem) for babe in babeArr: pictures = list(babes[babe].keys()) for picture in pictures: # get all likes likes = self.getPost(picture, self.getLikes, {}) babes[babe][picture] = {"processed": False} babes[babe][picture]["likes"] = likes usersDB.saveState(babes, 'babes.pickle') print('saved babe in DB', babes[babe])
def deFollowUsers(): following = usersDB.loadState('following.pickle') # run once at the end of a day # today = datetime.date.today() today = date(2021, 1, 12) weekBefore = today - datetime.timedelta(days=7) print(weekBefore) try: print('access week ago', following[str(weekBefore)]) for elem in following[str(weekBefore)]: print('deFollow', elem) following[str(weekBefore)].append('defollowed') usersDB.saveState(following, 'following.pickle') except: print('one week has not passed yet')
def deFollowUsers(self, seconds): following = usersDB.loadState('following.pickle') # run once at the end of a day # today = datetime.date.today() today = datetime.date.today() weekBefore = today - datetime.timedelta(days=2) print(weekBefore) try: print('access week ago', following[str(weekBefore)]) for elem in following[str(weekBefore)]: print('enter loop') sleep(seconds) self.getUser(elem, self.deFollow) following[str(weekBefore)].append('defollowed') usersDB.saveState(following, 'following.pickle') print('defollowed All users from day') except: print('one week has not passed yet')
def followUser(self, user): print('click Follow button of user') # load userList following = usersDB.loadState('following.pickle') # following is false and empty object in first iteration today = datetime.date.today() # check the object wether elements for this date exist try: print('accessing date and show length', len(following[str(today)])) following[str(today)].append(user) except: # if not existent, init empty array for that day print('first user of today. Init new array and save user') following[str(today)] = [] following[str(today)].append(user) print('saving new followers...') usersDB.saveState(following, 'following.pickle')
def test(): babes = usersDB.loadState('babes.pickle') babeArr = list(babes.keys()) print('RESULT LÄNGE', len(babeArr)) print(babeArr.index('https://www.instagram.com/juuleechkaa/')) print(babes[babeArr[87]]) posting = {} print('POSTED', list(posting.keys())) print(len(list(posting.keys()))) n = 0 for babe in babeArr: try: n += 1 print(n) pictures = list(babes[babe].keys()) likesArr = [] obj = {} posting[babe] = {} for picture in pictures: # get all likes likes = babes[babe][picture]["likes"] likesArr.append(likes) obj[likes] = picture likesArr.sort(reverse=True) #print(likesArr) for i in range(3): babes[babe][obj[likesArr[i]]]["passed"] = True posting[babe][obj[likesArr[i]]] = False # print(babes[babe][obj[likesArr[i]]]["passed"]) print('babes sucessfully selected') usersDB.saveState(posting, 'postingBabes.pickle') # print(posting) """ posting.pop(list(posting.keys())[-1], None) print(len(babeArr)) print(list(posting.keys())) print(len(list(posting.keys()))) """ #usersDB.saveState(posting, 'postingBabes.pickle') # usersDB.saveState(posting, 'postingBabes.pickle') except: continue
def getCosts(self, orderID, endID): products = usersDB.loadState('revenue.pickle') self.driver.get('https://printify.com/app/orders/2') difference = endID - orderID sleep(3) i = 0 while i < difference: self.driver.switch_to.window(self.driver.window_handles[0]) stringID = '#' + str(orderID) try: print('processing product', stringID) orderLink = self.driver.execute_script( """ array = document.body.getElementsByTagName('div') let found for (let i = 0 ; i< array.length ; i++){ if(array[i].innerText.substr(0,5) == arguments[0]){found = array[i]; break;} } return found.parentElement.href """, stringID) except: print('stringID not found') orderID += 1 continue self.openProductInNewTab(orderLink) totalCosts = self.driver.execute_script(""" let totalCosts = document.body.getElementsByClassName('order__billing__entry order__billing__total')[0].children[1].innerText.substr(4) return totalCosts """) print(totalCosts) products[orderID]["costs"] = float(totalCosts) # put all prices to orderID orderID += 1 i += 1 usersDB.saveState(products, 'revenue.pickle') print(products) self.driver.close()
def scrapeUsers(self): users = usersDB.loadState('userHQ.pickle') for link in highQualityPictures: # self.driver.get('https://www.instagram.com/explore/tags/'+ hashtag + '/') if (highQualityPictures[link]): # if image already has been assigned once or went through, skip it print( 'image has already been scraped run through, skipp iteration' ) continue self.driver.get(link) sleep(5) imageSrc = self.driver.find_element_by_xpath( '//div[@class="KL4Bh"]/img').get_attribute('src') print(imageSrc) print('starting insta bot execution') #first_thumbnail.click() sleep(2) # click liked by users button section = self.driver.find_element_by_xpath( "//section[@class='EDfFK ygqzn']") section.find_element_by_xpath(".//button").click() sleep(7) last_height = 0 # save all usernames to run through pictureArr = [] i = 0 end = 0 while True: # save usernames in object # usernames = self.driver.find_elements_by_xpath('//div[@class="_1XyCr"]/div[position()=2]/div/div/*') # sleep(3) # print(usernames) sleep(1) userData = self.driver.execute_script( """ let obj let arr=[] var list = document.querySelector('._1XyCr div:nth-child(2) div div').children for (i = 0; i < list.length; i++) { var title = list[i].querySelector('div:nth-child(2) div div span a').getAttribute('title') if (arguments[0][title] == undefined ){ arguments[0][title] = false arguments[1].push(title) } } obj = { "users": arguments[0], "userPictureList":arguments[1] } return obj """, users, pictureArr) users = userData["users"] print(len(list(users.keys()))) last_height = last_height + 1000 container = self.driver.find_element_by_xpath( '//div[@class="_1XyCr"]/div[position()=2]/div') self.driver.execute_script( "arguments[1].scrollTo(0, arguments[0]);", last_height, container) sleep(2) new_height = self.driver.execute_script( "return arguments[0].scrollHeight", container) if end == new_height: print('reached end of DIV') break else: end = new_height i += 1 print(i) print('new Height', new_height) print('last_height', last_height) usersDB.saveState(users, 'userHQ.pickle') print('saved users to DB') if i == 40000: i = 0 break # comment and like all these users print('länge objekt', len(list(users.keys()))) result = usersDB.loadState('userHQ.pickle') highQualityPictures[link] = True print('saved result', result)
def commentAndLike(self, comment, follow, seconds): users = usersDB.loadState('userHQ.pickle') randomList = [k for k, v in users.items() if v == False] print('length of userDB', len(randomList)) random.shuffle(randomList) selected = [] for k in range(0, 400): selected.append(random.choice(randomList)) print('random selected users', selected) n = 0 # x = number followers x = 0 for k in selected: # Wait 20 seconds between each request sleep(seconds) print('number of users liked, commented, and followed', x) if n < comment: if users[k] == "PRIVATE": print('useraccount is private') continue if users[k] == True: print('user has already been commented') continue print(k) self.driver.switch_to.window(self.driver.window_handles[0]) sleep(7) self.driver.execute_script("window.open('','_blank');") sleep(2) self.driver.switch_to.window(self.driver.window_handles[1]) sleep(2) self.driver.get("https://instagram.com/" + k + '/') sleep(2) try: print('detect private or public profile') firstPic = self.driver.find_element_by_xpath( '//article[@class="ySN3v"]/div/div/div/div/a/div') print('number of users followed', x) if (x < follow): print('following User') followButton = self.driver.execute_script(""" return document.getElementsByClassName('BY3EC')[0].children[0] """) followButton.click() sleep(2) self.followUser(k) x += 1 # followUser else: print('limit followers reached') except: print('account cannot be followed') pass try: self.driver.find_element_by_xpath( '//article[@class="ySN3v"]/div/div/div/div/a/div' ).click() sleep(2) except: print('error', 'weird stuff going on') pass self.likePicture() try: self.driver.find_element_by_xpath( '//textarea[@class="Ypffh"]').click() except NoSuchElementException: print("This account is private. Flagging as private") users[k] = "PRIVATE" usersDB.saveState(users, 'userHQ.pickle') pass sleep(2) try: self.driver.find_element_by_xpath( '//form[@class="X7cDz"]/textarea').send_keys( "@" + k + " " + random.choice(comments)) sleep(2) self.driver.find_element_by_xpath( '//form[@class="X7cDz"]/textarea').send_keys( Keys.RETURN) sleep(4) try: postButton = self.driver.execute_script(""" let buttons = document.body.getElementsByTagName("button") for (let i = 0; i< buttons.length; i++){ if (buttons[i].textContent == "Post"){ return buttons[i] } } """) postButton.click() sleep(2) print('liked and commented sucessfully') except: print("comment failed") users[k] = True usersDB.saveState(users, 'userHQ.pickle') n += 1 except NoSuchElementException: print(NoSuchElementException) pass self.driver.close() else: print('limit of public comments reached. Exit') break print([k for k, v in users.items() if v == True])
def commentAndLike(self, comment, seconds): users = usersDB.loadState('userHQ.pickle') randomList = [k for k, v in users.items() if v == False] print('length of userDB', len(randomList)) random.shuffle(randomList) selected = [] for k in range(0, 200): selected.append(random.choice(randomList)) print('random selected users', selected) n = 0 for k in selected: sleep(seconds) print('processing userList', len(selected)) if n < comment: if users[k] == "PRIVATE": print('useraccount is private') continue if users[k] == True: print('user has already been commented') continue print(k) self.driver.switch_to.window(self.driver.window_handles[0]) sleep(7) self.driver.execute_script("window.open('','_blank');") sleep(2) self.driver.switch_to.window(self.driver.window_handles[1]) sleep(2) self.driver.get("https://instagram.com/" + k + '/') sleep(2) try: self.driver.find_element_by_xpath( '//article[@class="ySN3v"]/div/div/div/div/a/div' ).click() sleep(2) except: print('error', 'weird stuff going on') pass try: #like picture self.driver.execute_script(""" document.querySelector('svg[aria-label="Like"]').parentNode.click() """) # write a comment except: pass try: self.driver.find_element_by_xpath( '//textarea[@class="Ypffh"]').click() except NoSuchElementException: print("This account is private. Flagging as private") users[k] = "PRIVATE" usersDB.saveState(users, 'userHQ.pickle') pass sleep(2) try: self.driver.find_element_by_xpath( '//form[@class="X7cDz"]/textarea').send_keys( "@" + k + " " + random.choice(comments)) sleep(2) self.driver.find_element_by_xpath( '//form[@class="X7cDz"]/textarea').send_keys( Keys.RETURN) sleep(4) try: postButton = self.driver.execute_script(""" let buttons = document.body.getElementsByTagName("button") for (let i = 0; i< buttons.length; i++){ if (buttons[i].textContent == "Post"){ return buttons[i] } } """) postButton.click() sleep(2) print('liked and commented sucessfully') except: print("comment failed") users[k] = True usersDB.saveState(users, 'userHQ.pickle') n += 1 except NoSuchElementException: print(NoSuchElementException) pass self.driver.close() else: print('limit of public comments reached. Exit') break print([k for k, v in users.items() if v == True]) print(selected)
def followUsers(self): i = 0 n = 0 while n < 2: self.driver.switch_to.window(self.driver.window_handles[0]) sleep(2) # load the hashtagDB hashtags = hashTagDB.loadState() print("HASHTAGDB", hashtags) hashtag = random.choice(list(hashtags.keys())) # hashtag="princessdress" # hashtag = hashtag[1:] print(hashtag) self.driver.get('https://www.instagram.com/explore/tags/' + hashtag + '/') sleep(5) first_thumbnail = self.driver.find_element_by_xpath( '//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div' ) imageSrc = first_thumbnail.find_element_by_xpath( './/img').get_attribute('src') print(imageSrc) try: if (hashtags[hashtag][imageSrc]): # if image already has been assigned once or went through, skip it print( 'image has already been scraped run through, skipp iteration' ) continue except: print('starting insta bot execution') first_thumbnail.click() sleep(2) # click liked by users button section = self.driver.find_element_by_xpath( "//section[@class='EDfFK ygqzn']") section.find_element_by_xpath(".//button").click() last_height = 0 # save all usernames to run through pictureArr = [] while True: # save usernames in object # usernames = self.driver.find_elements_by_xpath('//div[@class="_1XyCr"]/div[position()=2]/div/div/*') # sleep(3) # print(usernames) sleep(3) global users userData = self.driver.execute_script( """ let obj let arr=[] var list = document.querySelector('._1XyCr div:nth-child(2) div div').children for (i = 0; i < list.length; i++) { var title = list[i].querySelector('div:nth-child(2) div div span a').getAttribute('title') if (arguments[0][title] == undefined ){ arguments[0][title] = false arguments[1].push(title) } } obj = { "users": arguments[0], "userPictureList":arguments[1] } return obj """, users, pictureArr) users = userData["users"] for elem in userData["userPictureList"]: pictureArr.append(elem) userData["userPictureList"] = pictureArr print(userData["userPictureList"]) last_height = last_height + 1000 container = self.driver.find_element_by_xpath( '//div[@class="_1XyCr"]/div[position()=2]/div') self.driver.execute_script( "arguments[1].scrollTo(0, arguments[0]);", last_height, container) sleep(1) new_height = self.driver.execute_script( "return arguments[0].scrollHeight", container) i += 1 if i == 10: i = 0 break if new_height == last_height: break # comment and like all these users for k in userData["userPictureList"]: print('processing userList', len(userData["userPictureList"])) if userData["users"][k] == "PRIVATE": print('useraccount is private') continue if userData["users"][k] == True: print('user has already been commented') continue print(k) self.driver.switch_to.window(self.driver.window_handles[0]) sleep(7) self.driver.execute_script("window.open('','_blank');") sleep(2) self.driver.switch_to.window(self.driver.window_handles[1]) sleep(2) self.driver.get("https://instagram.com/" + k + '/') sleep(2) try: self.driver.find_element_by_xpath( '//article[@class="ySN3v"]/div/div/div/div/a/div' ).click() sleep(2) except NoSuchElementException: print(NoSuchElementException) pass try: #like picture self.driver.execute_script(""" document.querySelector('svg[aria-label="Like"]').parentNode.click() """) # write a comment except: pass try: self.driver.find_element_by_xpath( '//textarea[@class="Ypffh"]').click() except NoSuchElementException: print("This account is private. Flagging as private") userData["users"][k] = "PRIVATE" usersDB.saveState(userData["users"]) pass sleep(2) try: #self.driver.find_element_by_xpath('//form[@class="X7cDz"]/textarea').send_keys(random.choice(comments)) #sleep(2) #self.driver.find_element_by_xpath('//form[@class="X7cDz"]/textarea').send_keys(Keys.RETURN) #sleep(4) print('liked sucessfully') userData["users"][k] = True usersDB.saveState(userData["users"]) except NoSuchElementException: print(NoSuchElementException) pass self.driver.close() hashtags[hashtag][imageSrc] = True hashTagDB.saveState(hashtags) n += 1
def commentAndLike(self): # users = usersDB.loadState('userHQ.pickle') randomList = [k for k, v in users.items() if v == False] print('length of userDB', len(randomList)) random.shuffle(randomList) selected = [] for k in range(0, 50): selected.append(random.choice(randomList)) print('random selected users', selected) for k in selected: print('processing userList', len(selected)) if users[k] == "PRIVATE": print('useraccount is private') continue if users[k] == True: print('user has already been commented') continue print(k) self.driver.switch_to.window(self.driver.window_handles[0]) sleep(7) self.driver.execute_script("window.open('','_blank');") sleep(2) self.driver.switch_to.window(self.driver.window_handles[1]) sleep(2) self.driver.get("https://instagram.com/" + k + '/') sleep(2) try: self.driver.find_element_by_xpath( '//article[@class="ySN3v"]/div/div/div/div/a/div').click() sleep(2) except: print('error', 'weird stuff going on') pass try: #like picture self.driver.execute_script(""" document.querySelector('svg[aria-label="Like"]').parentNode.click() """) # write a comment except: pass try: self.driver.find_element_by_xpath( '//textarea[@class="Ypffh"]').click() except NoSuchElementException: print("This account is private. Flagging as private") users[k] = "PRIVATE" usersDB.saveState(users, 'userHQ.pickle') pass sleep(2) try: self.driver.find_element_by_xpath( '//form[@class="X7cDz"]/textarea').send_keys( random.choice(comments)) sleep(2) self.driver.find_element_by_xpath( '//form[@class="X7cDz"]/textarea').send_keys(Keys.RETURN) sleep(4) print('commented and liked sucessfully') users[k] = True usersDB.saveState(users, 'userHQ.pickle') except NoSuchElementException: print(NoSuchElementException) pass self.driver.close() print([k for k, v in users.items() if v == True]) print(selected)