Advertisement
Guest User

Untitled

a guest
May 29th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. from selenium.webdriver.common.keys import Keys
  2. import webbrowser
  3. import re
  4. import os
  5. import json
  6. import requests
  7. import random
  8. import selenium
  9. from robobrowser import RoboBrowser
  10. import time
  11.  
  12. class vk_captcha:
  13.     """Класс для работы с каптчей. Вырезает img со страницы, отправляет
  14.     в antigate, получает результат, возвращает введенную каптчу в виде строки"""
  15.  
  16.     def decode(page, root_path):
  17.         """Объединение функций _send_captcha и _check_captcha"""
  18.         print("Каптча...")
  19.  
  20.         key = "4517fb23f100hvyg87gy87gvhj"
  21.         a_captchaID = vk_captcha._send_captcha(page, root_path, key) #antigate captcha ID [OK, 406704123]
  22.         if a_captchaID[0] != "OK":
  23.             print("Неудалось отправить запрос на antigate\n%s" % a_captchaID)
  24.             return 0
  25.         while True:
  26.             time.sleep(7)
  27.             a_captchaRESULT = vk_captcha._check_captcha(a_captchaID[1], key)  #antigate captcah RESULT [OK, Fgv4Kl]
  28.             if a_captchaRESULT[0] != "OK":
  29.                 continue
  30.             print("Готово. %s" % a_captchaRESULT[1])
  31.             return a_captchaRESULT[1]
  32.        
  33.  
  34.     def _send_captcha(page, root_path, key):
  35.         """Получает страницу, возвращает массив формата [OK, 406704123]"""
  36.         img_tag = page.find(id="captcha")
  37.         if not img_tag:
  38.             img_tag = page.find("img", {"class":"captcha_img"})
  39.         if not img_tag:
  40.             print("Тега с id=captcha не найдено")
  41.             return 0
  42.         img = vk_captcha.request_with_retry(root_path + img_tag["src"]).content
  43.         data = {
  44.             "key": key,
  45.             "method": "post",
  46.         }
  47.         response = vk_captcha.request_with_retry('http://antigate.com/in.php', data=data, files={"file": img})
  48.         return (response.text.split("|"))
  49.  
  50.     def _check_captcha(captcha_id, key):
  51.         response = vk_captcha.request_with_retry("http://antigate.com/res.php?key=" + key + "&action=get&id=" + captcha_id)
  52.         return (response.text.split('|'))
  53.  
  54.     def request_with_retry(url, data="", files=""):
  55.         TIMEOUT = 0.5
  56.         while True:
  57.             try:
  58.                 if data or files:
  59.                     response = requests.post(url, data=data, files=files, timeout=TIMEOUT)
  60.                 else:
  61.                     response = requests.get(url, timeout=TIMEOUT)
  62.             except requests.exceptions.ReadTimeout:
  63.                 continue
  64.             break
  65.         return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement