<s> from abc import ABCMeta , abstractmethod <EOL> from io import BytesIO <EOL> import random <EOL> import requests <EOL> import matplotlib . image as mpimg <EOL> from helper import show_image <EOL> class HttpCaptchaProvider : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = ABCMeta <EOL> def __init__ ( self , fetch_method , fetch_url , fetch_headers , <EOL> verify_method , verify_url , verify_headers ) : <EOL> self . __fetch_method = fetch_method <EOL> self . __fetch_url = fetch_url <EOL> self . __fetch_headers = fetch_headers <EOL> self . __verify_method = verify_method <EOL> self . __verify_url = verify_url <EOL> self . __verify_headers = verify_headers <EOL> self . __session = requests . session ( ) <EOL> self . __is_virgin = True <EOL> def fetch ( self , retry_limit = <NUM_LIT:3> ) : <EOL> self . __is_virgin = False <EOL> print ( '<STR_LIT>' . format ( self . __fetch_url ) ) <EOL> r = None <EOL> for num_retries in range ( retry_limit ) : <EOL> if num_retries > <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' . format ( num_retries ) ) <EOL> try : <EOL> r = self . __session . request ( <EOL> self . __fetch_method , <EOL> self . __fetch_url , <EOL> headers = self . __fetch_headers <EOL> ) <EOL> break <EOL> except Exception as e : <EOL> print ( '<STR_LIT>' , e ) <EOL> return None if r is None else mpimg . imread ( BytesIO ( r . content ) ) <EOL> def verify ( self , seq ) : <EOL> if self . __is_virgin : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> r = self . __session . request ( <EOL> self . __verify_method , <EOL> self . __verify_url , <EOL> headers = self . __verify_headers , <EOL> data = self . _get_data_from_seq ( seq ) <EOL> ) <EOL> return self . _is_correct_response ( r ) <EOL> @ abstractmethod <EOL> def _get_data_from_seq ( self , seq ) : <EOL> raise NotImplementedError ( ) <EOL> @ abstractmethod <EOL> def _is_correct_response ( self , r ) : <EOL> raise NotImplementedError ( ) <EOL> class NormalSeqSet : <EOL> def __init__ ( self , chars , seq_length ) : <EOL> self . chars = chars <EOL> self . charset = set ( chars ) <EOL> self . seq_length = seq_length <EOL> def canonicalize_seq ( self , seq ) : <EOL> return seq <EOL> def is_valid_seq ( self , seq ) : <EOL> return ( len ( seq ) == self . seq_length and <EOL> all ( char in self . charset for char in seq ) ) <EOL> class BilibiliCaptchaProvider ( HttpCaptchaProvider , NormalSeqSet ) : <EOL> __GET = '<STR_LIT:GET>' <EOL> __POST = '<STR_LIT:POST>' <EOL> __USER_AGENT = '<STR_LIT>' <EOL> __HOST = '<STR_LIT>' <EOL> __REFERER = '<STR_LIT>' <EOL> __user_agent = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> __host = '<STR_LIT>' <EOL> __verify_method = __POST <EOL> __verify_url = '<STR_LIT>' <EOL> __verify_headers = { <EOL> __USER_AGENT : __user_agent , <EOL> __HOST : __host , <EOL> __REFERER : __verify_url <EOL> } <EOL> __fetch_method = __GET <EOL> __fetch_url = '<STR_LIT>' <EOL> __fetch_headers = { <EOL> __USER_AGENT : __user_agent , <EOL> __HOST : __host , <EOL> __REFERER : __verify_url <EOL> } <EOL> def __init__ ( self ) : <EOL> chars = '<STR_LIT>' . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> NormalSeqSet . __init__ ( self , chars , <NUM_LIT:5> ) <EOL> HttpCaptchaProvider . __init__ ( <EOL> self , <EOL> self . __fetch_method , self . __fetch_url , self . __fetch_headers , <EOL> self . __verify_method , self . __verify_url , self . __verify_headers , <EOL> ) <EOL> def verify ( self , seq ) : <EOL> return self . is_valid_seq ( seq ) and HttpCaptchaProvider . verify ( self , seq ) <EOL> def _get_data_from_seq ( self , seq ) : <EOL> return { '<STR_LIT>' : seq , '<STR_LIT:action>' : "<STR_LIT>" } <EOL> def _is_correct_response ( self , r ) : <EOL> r_json = r . json ( ) <EOL> if r_json [ '<STR_LIT:status>' ] : <EOL> return True <EOL> else : <EOL> print ( '<STR_LIT>' , r_json [ '<STR_LIT:message>' ] ) <EOL> return False <EOL> def canonicalize_seq ( self , seq ) : <EOL> return seq . upper ( ) <EOL> def _test_bilibili ( ) : <EOL> captcha_provider = BilibiliCaptchaProvider ( ) <EOL> print ( captcha_provider . is_valid_seq ( <EOL> '<STR_LIT>' . join ( random . sample ( captcha_provider . chars , <NUM_LIT:5> ) ) <EOL> ) ) <EOL> print ( captcha_provider . is_valid_seq ( <EOL> '<STR_LIT>' . join ( random . sample ( captcha_provider . chars , <NUM_LIT:4> ) ) + '<STR_LIT:U+0020>' <EOL> ) ) <EOL> show_image ( captcha_provider . fetch ( ) ) <EOL> seq = captcha_provider . canonicalize_seq ( <EOL> input ( '<STR_LIT>' ) <EOL> ) <EOL> print ( captcha_provider . verify ( seq ) ) <EOL> def main ( ) : <EOL> _test_bilibili ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
