1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| import re import sys import json import argparse import itertools import threading from typing import List, Tuple import xlrd import xlwt from threading import Lock from multiprocessing import Value from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.faceid.v20180301 import faceid_client as faceid from tencentcloud.faceid.v20180301.models import IdCardVerificationRequest
SECRET_ID = "xxx" SECRET_KEY = "xxx"
def get_checkbit(id_number: str) -> str: weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
sum_ = sum([int(id_number[i]) * weight[i] for i in range(17)]) return check_code[sum_ % 11]
def check_number(id_number: str) -> bool: if not re.match(r'^\d{17}[\dXx]$', id_number): return False
if id_number[-1].upper() == get_checkbit(id_number): return True
return False
def is_valid_id_number(id_number: str, name: str, counter: dict, lock: Lock) -> bool: cred = credential.Credential(SECRET_ID, SECRET_KEY) httpProfile = HttpProfile() httpProfile.endpoint = "faceid.tencentcloudapi.com"
clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = faceid.FaceidClient(cred, "ap-beijing", clientProfile)
req = IdCardVerificationRequest() params = { "IdCard": id_number, "Name": name } req.from_json_string(json.dumps(params)) with lock: counter.value += 1 try: resp = client.IdCardVerification(req) if resp and resp.Result == "0": print(resp) return True except Exception as e: print(f"Error occurred while validating ID {id_number}: {e}")
return False
def generate_id_numbers(partial_id: str) -> List[str]: missing_digits = 18 - len(partial_id.replace('*', '')) id_numbers = []
for combination in itertools.product(range(10), repeat=missing_digits): id_number = partial_id for digit in combination: id_number = id_number.replace('*', str(digit), 1)
year = int(id_number[6:10]) if year < 1970 or year > 2000: continue tens_digit_month1 = int(id_number[10]) if tens_digit_month1 not in [0, 1]: continue
tens_digit_month2 = int(id_number[11]) if tens_digit_month1 == 1 and tens_digit_month2 not in [0, 1, 2]: continue month = int(id_number[10:12]) if month == 0: continue
tens_digit_day = int(id_number[12]) if tens_digit_day not in [0, 1, 2, 3]: continue
ones_digit_day = int(id_number[13]) if ones_digit_day < 1 or ones_digit_day > 9: continue
id_numbers.append(id_number)
return id_numbers
def read_excel(file_path: str) -> List[Tuple[str, str]]: data = [] workbook = xlrd.open_workbook(file_path) sheet = workbook.sheet_by_index(0)
for row in range(1, sheet.nrows): name = sheet.cell_value(row, 1) id_number = sheet.cell_value(row, 2)
if not name.strip(): continue if len(id_number) != 18: continue if '*' in id_number[:6] or '*' in id_number[12:]: continue
data.append((name, id_number))
return data
def write_excel(data: List[Tuple[str, str]], file_path: str): workbook = xlwt.Workbook() sheet = workbook.add_sheet('Result')
for row, (name, id_number) in enumerate(data): sheet.write(row, 0, name) sheet.write(row, 1, id_number)
workbook.save(file_path) def process_id_number(name: str, partial_id: str, result: List[Tuple[str, str]], index: int, counter1: dict, lock: Lock): print(f"Processing {name} with partial ID {partial_id} in thread {index}") counter = 0 for id_number in generate_id_numbers(partial_id): counter += 1 if counter % 1000 == 0: print(f"Thread {index} progress: {counter} IDs checked") if check_number(id_number): print(id_number) check = is_valid_id_number(id_number, name, counter1, lock) print(check) if check: print(f"{name}正确的身份证{id_number}") result.append((name, id_number)) print(f"Thread {index} found valid ID: {name}, {id_number}") break
def main(file_path: str): data = read_excel(file_path) result = []
threads = [] counter = Value('i', 0) lock = Lock() for index, (name, partial_id) in enumerate(data): thread = threading.Thread(target=process_id_number, args=(name, partial_id, result, index, counter, lock)) thread.start() threads.append(thread)
for thread in threads: thread.join() print(f"\n总共请求接口次数: {counter.value}") print("\n成功了:") for name, id_number in result: print(f"{name}, {id_number}")
write_excel(result, 'result.xls')
if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--data', required=True, help='Path to the input Excel file (.xls)') args = parser.parse_args() main(args.data)
|