|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +from typing import TYPE_CHECKING, NamedTuple |
| 5 | + |
| 6 | +from bs4 import BeautifulSoup |
| 7 | +from httpx import Response |
| 8 | + |
| 9 | +from twikit.utils import urlencode |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from ..client import Client |
| 13 | + |
| 14 | + |
| 15 | +class UnlockHTML(NamedTuple): |
| 16 | + authenticity_token: str |
| 17 | + assignment_token: str |
| 18 | + needs_unlock: bool |
| 19 | + start_button: bool |
| 20 | + finish_button: bool |
| 21 | + delete_button: bool |
| 22 | + blob: str |
| 23 | + |
| 24 | + |
| 25 | +class CaptchaSolver: |
| 26 | + client: Client |
| 27 | + max_attempts: int |
| 28 | + |
| 29 | + CAPTCHA_URL = 'https://twitter.com/account/access' |
| 30 | + CAPTCHA_SITE_KEY = '0152B4EB-D2DC-460A-89A1-629838B529C9' |
| 31 | + |
| 32 | + def get_unlock_html(self) -> tuple[Response, UnlockHTML]: |
| 33 | + headers = { |
| 34 | + 'X-Twitter-Client-Language': 'en-US', |
| 35 | + 'User-Agent': self.client._user_agent, |
| 36 | + 'Upgrade-Insecure-Requests': '1' |
| 37 | + } |
| 38 | + _, response = self.client.get( |
| 39 | + self.CAPTCHA_URL, headers=headers |
| 40 | + ) |
| 41 | + return response, parse_unlock_html(response.text) |
| 42 | + |
| 43 | + def ui_metrix(self) -> str: |
| 44 | + js, _ = self.client.get( |
| 45 | + 'https://twitter.com/i/js_inst?c_name=ui_metrics' |
| 46 | + ) |
| 47 | + return re.findall(r'return ({.*?});', js, re.DOTALL)[0] |
| 48 | + |
| 49 | + def confirm_unlock( |
| 50 | + self, |
| 51 | + authenticity_token: str, |
| 52 | + assignment_token: str, |
| 53 | + verification_string: str = None, |
| 54 | + ui_metrics: bool = False |
| 55 | + ) -> tuple[Response, UnlockHTML]: |
| 56 | + data = { |
| 57 | + 'authenticity_token': authenticity_token, |
| 58 | + 'assignment_token': assignment_token, |
| 59 | + 'lang': 'en', |
| 60 | + 'flow': '', |
| 61 | + } |
| 62 | + params = {} |
| 63 | + if verification_string: |
| 64 | + data['verification_string'] = verification_string |
| 65 | + data['language_code'] = 'en' |
| 66 | + params['lang'] = 'en' |
| 67 | + if ui_metrics: |
| 68 | + data['ui_metrics'] = self.ui_metrix() |
| 69 | + data = urlencode(data) |
| 70 | + headers = { |
| 71 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 72 | + 'Upgrade-Insecure-Requests': '1', |
| 73 | + 'Referer': self.CAPTCHA_URL |
| 74 | + } |
| 75 | + _, response = self.client.post( |
| 76 | + self.CAPTCHA_URL, params=params, data=data, headers=headers |
| 77 | + ) |
| 78 | + return response, parse_unlock_html(response.text) |
| 79 | + |
| 80 | + |
| 81 | +def parse_unlock_html(html: str) -> UnlockHTML: |
| 82 | + soup = BeautifulSoup(html, 'lxml') |
| 83 | + |
| 84 | + authenticity_token = None |
| 85 | + authenticity_token_element = soup.find( |
| 86 | + 'input', {'name': 'authenticity_token'} |
| 87 | + ) |
| 88 | + if authenticity_token_element is not None: |
| 89 | + authenticity_token: str = authenticity_token_element.get('value') |
| 90 | + |
| 91 | + assignment_token = None |
| 92 | + assignment_token_element = soup.find('input', {'name': 'assignment_token'}) |
| 93 | + if assignment_token_element is not None: |
| 94 | + assignment_token = assignment_token_element.get('value') |
| 95 | + |
| 96 | + verification_string = soup.find('input', id='verification_string') |
| 97 | + needs_unlock = bool(verification_string) |
| 98 | + start_button = bool(soup.find('input', value='Start')) |
| 99 | + finish_button = bool(soup.find('input', value='Continue to X')) |
| 100 | + delete_button = bool(soup.find('input', value='Delete')) |
| 101 | + |
| 102 | + iframe = soup.find(id='arkose_iframe') |
| 103 | + blob = re.findall(r'data=(.+)', iframe['src'])[0] if iframe else None |
| 104 | + |
| 105 | + return UnlockHTML( |
| 106 | + authenticity_token, |
| 107 | + assignment_token, |
| 108 | + needs_unlock, |
| 109 | + start_button, |
| 110 | + finish_button, |
| 111 | + delete_button, |
| 112 | + blob |
| 113 | + ) |
0 commit comments