import os import string import time import requests import json from openpyxl import Workbook, load_workbook from config.log import writeInfo from requests_toolbelt.multipart.encoder import MultipartEncoder def colname_to_num(colname): if type(colname) is not str: return colname col = 0 power = 1 for i in range(len(colname) - 1, -1, -1): ch = colname[i] col += (ord(ch) - ord('a') + 1) * power power *= len(string.ascii_lowercase) return col def column_to_name(colnum): if type(colnum) is not int: return colnum str = '' str_l = len(string.ascii_lowercase) while (not (colnum // str_l == 0 and colnum % str_l == 0)): temp = str_l - 1 if (colnum % str_l == 0): str += chr(temp + ord('a')) else: str += chr(colnum % str_l - 1 + ord('a')) colnum //= str_l # 倒序输出拼写的字符串 return str[::-1] def aliyun(domain: str, error_file: str, datas: list): url = f'https://checkapi.aliyun.com/check/checkdomain?domain={domain}&command=&token=Ybd4a4cee48911b5a42fb04958c7fd25f' writeInfo(f'url={url}') res = requests.get(url) if res.status_code == 200: res_json = json.loads(res.content) try: if res_json['module'][0]['avail'] == 1: price = res_json['module'][0]['price'] writeInfo(f'{domain}可注册,价格{price}') time.sleep(0.5) datas.append((domain, f'{price}¥')) else: writeInfo(f'{domain}已注册') except: with open(error_file, 'a', encoding='utf-8') as f: f.write(f'{domain}\n') else: raise Exception('请求异常') def check(top_domain: list, start: str, end: str, supportMany: bool, todo): if not isinstance(top_domain, list) or len(top_domain) == 0: raise Exception('top_domain不合法') if not str.isalpha(start): raise Exception('start不合法') if not str.isalpha(end) or colname_to_num(end) <= colname_to_num(start): raise Exception('end不合法') if not isinstance(supportMany, bool): raise Exception('isExec不合法') domain_file = 'domain.xlsx' error_file = 'error.txt' if os.path.exists(domain_file): wb = load_workbook(domain_file) else: wb = Workbook() ws = wb.active if ws.max_row == 1: ws.cell(1, 1, '域名') ws.cell(1, 2, '价格') if os.path.exists(error_file): os.remove(error_file) for i in range(colname_to_num(start), colname_to_num(end)+1): datas = list() if supportMany: todo([f'{column_to_name(i)}.{x}' for x in top_domain], error_file, datas) else: for j in top_domain: domain = f'{column_to_name(i)}.{j}' todo(domain, error_file, datas) for index, data in enumerate(datas): domain, price = data row = ws.max_row + 1 ws.cell(row, 1, domain) ws.cell(row, 2, price) wb.save(domain_file) def namesilo(domains: list, error_file: str, datas: list): data = tuple([('tlds[]', x.split('.')[1]) for x in domains] + [('domains[]', x) for x in domains]) m = MultipartEncoder( fields=data) res = requests.post('https://www.namesilo.com/public/api/domains/bulk-check', data=m, headers={'Content-Type': m.content_type}) if res.status_code == 200: json_res = json.loads(res.content) if 'result' in json_res and json_res['result'] == 'success' and 'data' in json_res and 'checkId' in json_res[ 'data']: checkId = json_res['data']['checkId'] url = f'https://www.namesilo.com/public/api/domains/results/{checkId}' res = requests.get(url) if res.status_code == 200: json_res = json.loads(res.content) for domain in json_res['data']['domains']: d = domain['domain'] if domain['available']: price = domain['currentPrice'] writeInfo(f'{d}可注册,价格{price}') datas.append((d, f'{round(price, 2)}$')) else: writeInfo(f'{d}已注册') else: raise Exception('请求异常') else: writeInfo(json_res) else: with open(error_file, 'a', encoding='utf-8') as f: for domain in domains: f.write(f'{domain}\n') raise Exception('请求异常') # check(['top', 'fun', 'online', 'ink', 'ren', 'site', 'asia'], 'aaa', 'aag', False, aliyun)