from ntpath import join 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-1 def toCode(x): return chr(x+ord('a')) def getKey(index): colCode = '' key = 'a' loop = index // len(string.ascii_uppercase); if(loop>0): colCode += getKey(loop-1); key = chr(ord(key)+index%len(string.ascii_uppercase)) colCode += key return colCode 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'{getKey(i)}.{x}' for x in top_domain], error_file, datas) else: for j in top_domain: domain = f'{getKey(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]) # 使用MultipartEncoder把表单内容转换为multipart/form-data类型 m = MultipartEncoder( fields=data) res = requests.post('https://www.namesilo.com/public/api/domains/bulk-check', data=m, headers={'Content-Type': m.content_type},timeout=10) 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,timeout=10) 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 = float(domain["currentPrice"]) writeInfo(f'{d}可注册,价格{price}$') datas.append((d, price)) 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)