Last active
August 20, 2018 07:55
-
-
Save codenoid/4ae9059079b5b0ef65f157bc7e3026fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time, os, json, sys, pprint, logging, hashlib, re, base64 | |
| username = sys.argv[1] | |
| from PIL import Image | |
| from selenium import webdriver | |
| from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException | |
| from selenium.webdriver.common.keys import Keys | |
| def main(): | |
| driverPath = '/usr/bin/chromedriver' | |
| dataPath = './profile/'+username | |
| options = webdriver.ChromeOptions() | |
| options.add_argument("--user-data-dir=" + dataPath) | |
| options.add_argument('no-sandbox') | |
| options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36') | |
| driver = webdriver.Chrome(chrome_options=options, executable_path=driverPath) | |
| driver.get("https://web.whatsapp.com") | |
| time.sleep(5) | |
| is_logged = False | |
| try: | |
| img = driver.find_element_by_css_selector("img[alt=\"Scan me!\"]") | |
| except NoSuchElementException: | |
| is_logged = True | |
| if is_logged is False: | |
| while True: | |
| try: | |
| try: | |
| reload = driver.find_element_by_css_selector("span > div[role=\"button\"]") | |
| reload.click() | |
| time.sleep(5) | |
| except NoSuchElementException: | |
| reloadQR(driver) | |
| except (NoSuchElementException, StaleElementReferenceException): | |
| is_logged = True | |
| break | |
| if is_logged is True: | |
| with open("./whatsapp_number.txt") as f: | |
| content = f.readlines() | |
| content = [x.strip() for x in content] | |
| for number in content: | |
| number = str(number) | |
| driver.get("https://web.whatsapp.com/send?phone="+number) | |
| time.sleep(10) | |
| head = driver.find_elements_by_css_selector('div#main header span[dir="auto"]') | |
| if len(head) > 0: | |
| head[0].click() | |
| time.sleep(5) | |
| picture = driver.find_elements_by_css_selector('div[tabindex] .copyable-area img[src*="https://dy"]') | |
| if len(picture) > 0: | |
| image_src = picture[0].get_attribute("src") | |
| number_only = re.sub("[^0-9]", "", number) | |
| filename = str(number_only) + ".png" | |
| downloadBlob(driver, image_src, filename) | |
| time.sleep(2) | |
| def is_loggedx(driver): | |
| try: | |
| img = driver.find_element_by_css_selector("img[alt=\"Scan me!\"]") | |
| return "yes" | |
| except NoSuchElementException: | |
| return "no" | |
| def generateQR(driver): | |
| img = driver.find_element_by_css_selector("img[alt=\"Scan me!\"]") | |
| img64 = bytes(img.get_attribute("src").replace("data:image/png;base64,", "")) | |
| fh = open("./qrcode.png", "wb") | |
| fh.write(img64.decode('base64')) | |
| fh.close() | |
| def reloadQR(driver): | |
| img = driver.find_element_by_css_selector("img[alt=\"Scan me!\"]") | |
| img64 = bytes(img.get_attribute("src").replace("data:image/png;base64,", "")) | |
| fh = open("./qrcode.png", "wb") | |
| fh.write(img64.decode('base64')) | |
| fh.close() | |
| def downloadBlob(driver, uri, filename): | |
| result = driver.execute_async_script(""" | |
| var uri = arguments[0]; | |
| var callback = arguments[1]; | |
| var toBase64 = function(buffer){for(var r,n=new Uint8Array(buffer),t=n.length,a=new Uint8Array(4*Math.ceil(t/3)),i=new Uint8Array(64),o=0,c=0;64>c;++c)i[c]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(c);for(c=0;t-t%3>c;c+=3,o+=4)r=n[c]<<16|n[c+1]<<8|n[c+2],a[o]=i[r>>18],a[o+1]=i[r>>12&63],a[o+2]=i[r>>6&63],a[o+3]=i[63&r];return t%3===1?(r=n[t-1],a[o]=i[r>>2],a[o+1]=i[r<<4&63],a[o+2]=61,a[o+3]=61):t%3===2&&(r=(n[t-2]<<8)+n[t-1],a[o]=i[r>>10],a[o+1]=i[r>>4&63],a[o+2]=i[r<<2&63],a[o+3]=61),new TextDecoder("ascii").decode(a)}; | |
| var xhr = new XMLHttpRequest(); | |
| xhr.responseType = 'arraybuffer'; | |
| xhr.onload = function(){ callback(toBase64(xhr.response)) }; | |
| xhr.onerror = function(){ callback(xhr.status) }; | |
| xhr.open('GET', uri); | |
| xhr.send(); | |
| """, uri) | |
| if type(result) == int: | |
| return "none" | |
| else: | |
| path = "./" + filename | |
| file = open(path, "w") | |
| file.write(base64.b64decode(result)) | |
| file.close() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment