Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- # -*- coding: utf-8 -*-
- """
- Created on Tue Jan 16 09:46:22 2018
- @author: archil
- """
- from selenium.webdriver.chrome.options import Options
- from selenium import webdriver
- import selenium.webdriver.chrome.service as service
- import json
- from flask import Flask,request,Request
- app = Flask(__name__)
- import datetime
- import uuid
- from random import randint
- #from selenium.webdriver.common import keys
- import time
- import zipfile
- from PIL import Image
- import numpy
- import pytesseract
- import cv2
- #from __future__ import print_function
- import os
- import sys
- import tensorflow as tf
- import keras.backend.tensorflow_backend as ktf
- from keras.models import load_model
- dirpath = os.getcwd()
- sys.path.append('..')
- from utils import display_examples, RotNetDataGenerator, angle_error
- #from pyvirtualdisplay.smartdisplay import Display
- global driver
- global canvas
- global svc
- #global display
- #from pyvirtualdisplay import Display
- gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.05,
- allow_growth=False)
- session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
- ktf.set_session(session)
- pytesseract.pytesseract.tesseract_cmd='/usr/local/bin/tesseract'
- model_location = os.path.join(dirpath, 'models', 'rotnet_funcaptcha_resnet50.hdf5', )
- model = load_model(model_location, custom_objects={'angle_error': angle_error})
- class captcha_status():
- SOLVE_CHALLENGE = 0
- COMPLETED_FAILURE = 1
- COMPLETED_SUCCESSFULLY = 2
- OUT_OF_TIME = 3
- def findfuncaptchacanvas(screenjson):
- global driver
- global canvas
- global svc
- # print (screenjson)
- resp = screenjson #json.loads(screenjson)
- print resp
- proxy = resp['proxy']
- useragent = resp['user-agent']
- windowsize = resp['window-size']
- proxy = proxy.split(':', 3)
- ip, port, user, pswd = proxy[0], proxy[1], proxy[2], proxy[3].replace('\n', '')
- manifest_json = """
- {
- "version": "1.0.0",
- "manifest_version": 2,
- "name": "Chrome Proxy",
- "permissions": [
- "proxy",
- "tabs",
- "unlimitedStorage",
- "storage",
- "<all_urls>",
- "webRequest",
- "webRequestBlocking"
- ],
- "background": {
- "scripts": ["background.js"]
- },
- "minimum_chrome_version":"22.0.0"
- }
- """
- background_js = """
- var config = {
- mode: "fixed_servers",
- rules: {
- singleProxy: {
- scheme: "http",
- host: "%(host)s",
- port: parseInt(%(port)d)
- },
- bypassList: ["foobar.com"]
- }
- };
- chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
- function callbackFn(details) {
- return {
- authCredentials: {
- username: "%(user)s",
- password: "%(pass)s"
- }
- };
- }
- chrome.webRequest.onAuthRequired.addListener(
- callbackFn,
- {urls: ["<all_urls>"]},
- ['blocking']
- );
- """ % {
- "host": ip,
- "port": int(port),
- "user": user,
- "pass": pswd,
- }
- pluginfile = 'proxy_auth_plugin'+str(uuid.uuid4())+'.zip'
- with zipfile.ZipFile(pluginfile, 'w') as zp:
- zp.writestr("manifest.json", manifest_json)
- zp.writestr("background.js", background_js)
- # remotedriver = webdriver.Remote(desired_capabilities=webdriver.Chrome)
- # display = Display(backend='xephyr',visible=0, size=(1920, 1080))
- # display.start()
- service_args = ["--verbose","--log-path=fakeapi.log"]#,"--remote-debugging-port=9222"
- co = Options()
- # co.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
- # co.add_argument('--url-base=/wd/hub')
- # co.add_argument('--headless')
- # co.add_argument('--disable-gpu')
- co.add_argument('--no-sandbox')
- co.add_argument('--disable-setuid-sandbox')
- co.add_argument('--user-data-dir=/tmp/cdriver')
- # co.add_argument("--disable-extensions")
- # co.add_argument('--no-proxy-server')
- # co.add_argument("--disable-infobars");
- # co.add_argument('--single-process')
- co.add_extension(pluginfile)
- co.add_argument(useragent)
- co.add_argument(windowsize)
- svc = service.Service(executable_path=dirpath+'/chromedriver',service_args=service_args)
- svc.start()
- driver = webdriver.Remote(command_executor=svc.service_url,desired_capabilities=co.to_capabilities())#,chrome_options=co,service_args=service_args)
- # driver.set_window_size(int(windowsize['width']), int(windowsize['height']))
- driver.get('file://' + dirpath + '/fun_loader.html')
- time.sleep(7.5)
- canvas = driver.find_element_by_id('CAPTCHA')
- os.remove(os.path.join(dirpath,pluginfile))
- def fun_click(x, y):
- # global driver
- action = webdriver.ActionChains(driver)
- action.move_to_element_with_offset(canvas, x, y)
- action.click()
- action.perform()
- def clockwise_rotate():
- # left arrow click rotates img clockwise
- fun_click(42, 138)
- time.sleep(2)
- def anti_clockwise_rotate():
- # right arrow click rotate img anticlockwise
- fun_click(255, 146)
- time.sleep(2)
- def image_done():
- # try again, done, etc
- fun_click(150, 225)
- time.sleep(2)
- def start_challenge():
- fun_click(150, 210)
- time.sleep(2)
- def get_captcha_status(picture):
- global driver
- # for i in range(1, 5):
- # grey scale for best results
- canvas_img = picture
- filename = "{}.png".format(os.getpid())
- cv2.imwrite(filename, canvas_img)
- # img= cv2.imread(filename, cv2.IMREAD_COLOR)
- # edges = cv2.Canny(img,100,200)
- # cv2.imwrite("img.png", edges)
- # img = Image.fromarray(edges)
- img = Image.open(filename)
- # print filename
- text_read = pytesseract.image_to_string(img).lower()
- os.remove(filename)
- print(text_read)
- if text_read.__contains__('too much time'):
- return captcha_status.OUT_OF_TIME
- elif text_read.__contains__('the ball'):
- return captcha_status.SOLVE_CHALLENGE
- elif text_read.__contains__('whoops'):
- return captcha_status.COMPLETED_FAILURE
- return captcha_status.COMPLETED_SUCCESSFULLY
- def getscreenshot():
- size = canvas.size
- location = canvas.location
- centerX=139
- centerY=151
- radius=53
- centerX1=112
- centerY1=112
- radius1=110
- time.sleep(2)
- firstscreenshotname = str(uuid.uuid4())+'.png'
- driver.save_screenshot(firstscreenshotname)
- im = Image.open(firstscreenshotname) # uses PIL library to open image in memory
- os.remove(firstscreenshotname )
- left = location['x']
- top = location['y']
- right = location['x'] + size['width']
- bottom = location['y'] + size['height']
- im = im.crop((left, top, right, bottom)) # defines crop points
- im = numpy.array(im)
- # mask = numpy.zeros((im.shape[0],im.shape[1]))
- # for i in range(im.shape[0]):
- # for j in range(im.shape[1]):
- # if (i-centerX)**2 + (j-centerY)**2 < radius**2:
- # mask[i,j] = 1
- # newIm = numpy.empty(im.shape,dtype='uint8')
- # newIm[:,:,:3] = im[:,:,:3]
- # newIm[:,:,3] = mask*255
- im = im[centerX-radius:centerX+radius,centerY-radius:centerY+radius]
- im = cv2.resize(im, (224,224))
- rows, cols, channels = im.shape
- newIm = numpy.empty((rows, cols,3),dtype='uint8')
- newIm[:,:,:3] = im[:,:,:3]
- for i in range(im.shape[0]):
- for j in range(im.shape[1]):
- if (i-centerX1)**2 + (j-centerY1)**2 >= radius1**2:
- newIm[i,j]= [255,255,255]
- # rows, cols, channels = newIm.shape
- # print('Rows:%s Cols:%s channels:%s',str(rows),str(cols),str(channels))
- newIma= Image.fromarray(newIm, "RGB")
- newIma.save(os.path.join(dirpath,firstscreenshotname))
- newIma.save(os.path.join(dirpath,'pictures',firstscreenshotname))
- return newIma
- def getfullscreenshot():
- firstscreenshotname = str(uuid.uuid4())+'.png'
- driver.save_screenshot(firstscreenshotname)
- pic = Image.open(firstscreenshotname)
- os.remove(firstscreenshotname )
- return numpy.asarray(pic)
- def getlog():
- entries = driver.get_log("browser")
- for entry in entries: # check the logged timestamp if it is logged in last sec
- print (datetime.datetime.utcnow() - datetime.timedelta(seconds=30))
- print entry["timestamp"]
- if (entry["timestamp"] > time.mktime(datetime.datetime.utcnow() - datetime.timedelta(seconds=15))) and (entry["level"]=="INFO") :
- return entry["message"]
- else:
- return None
- def correctangle(angle):
- # print angle
- for i in range(0,360,360/7):
- # print abs(i-numpy.argmax(angle))
- if(abs(i-numpy.argmax(angle, axis=1))<360/14):
- return numpy.argmax(angle, axis=1)
- #def resizepicture(picture):
- # cv.imwrite
- def getclicks (picture):
- screenname = str(uuid.uuid4())+'.png'
- cv2.imwrite( screenname,numpy.asarray(picture))
- picture = cv2.imread(screenname, cv2.IMREAD_COLOR)
- os.remove(screenname)
- picture = cv2.resize(picture, (224,224))
- ## picture = resizepicture(picture)
- ## if(request.files['file'] is None):
- ## return json.dumps({ "error": "file is not submitted" }), 500
- ## if(imghdr.what( request.files['file'])!='png'):
- ## return json.dumps({ "error": "file is not a PNG picture" }), 500
- rows, cols, channels = picture.shape
- # print('Rows:%s Cols:%s channels:%s', str(rows), str(cols), str(channels))
- # col = None
- angle = model.predict(numpy.expand_dims(picture,axis=0))
- print screenname
- correctedangle = correctangle(angle)
- print correctedangle
- return round(correctedangle/(360/7))
- @app.route("/solvefuncaptcha",methods=['POST'])
- def hello():
- print 'start'
- screenjson = request.get_json(force=True)
- # print screenjson
- # disp = Display(backend='xvfb',visible=0,size=(1920, 1080))
- # disp.start()
- findfuncaptchacanvas(screenjson)
- print 'start of method'
- start_challenge()
- # image_index =0
- while True:
- screenshot = getfullscreenshot()
- # cv2.imwrite (screenshot,'scr.png')'
- result = get_captcha_status(screenshot)
- if result is captcha_status.COMPLETED_SUCCESSFULLY:
- print 'completed successfully'
- message = getlog()
- driver.close()
- # disp.stop()
- if message:
- return message
- else:
- return 'failed'
- time.sleep(5)
- break
- elif result is captcha_status.SOLVE_CHALLENGE:
- print 'start of solve challenge'
- picture = getscreenshot()
- clicks = randint(1,6) #getclicks(picture)
- print clicks
- for i in range (0, int(clicks)):
- clockwise_rotate()
- image_done()
- print 'end of solve challenge'
- elif result is captcha_status.COMPLETED_FAILURE:
- print 'completed failure'
- image_done()
- time.sleep(5)
- elif result is captcha_status.OUT_OF_TIME:
- print 'out of time'
- image_done()
- time.sleep(5)
- # findfuncaptchacanvas(screenjson)
- #
- # checkmodelforpicture (picture)
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement