import requests import sys import json import datetime # Provide your keys apikey = "paste_your_api_key_here" apisecret = "paste_your_api_secret_here" # apiurl = "https://api.codepunch.com/dnfeed/v2/" # get_api_token # token = get_api_token() # def get_api_token(): AUTHURLFMT = "{}auth/{}/{}" url = AUTHURLFMT.format(apiurl, apikey, apisecret) response = requests.get(url) if response.status_code == 200: apiresponse = response.json() else: raise Exception('Authentication: {}' . format(response.status_code)) if bool(apiresponse['status']) != True: raise Exception(apiresponse['error']) return apiresponse['token'] # get_api_data # parameters = {"date": "20200624", "format": "json", "limit": 50, "kw": "%apple%"} # thedata = get_api_data(token, "added", parameters) # domaindata = thedata['data'] # for d in domaindata['data']: # print (d['domain']) # def get_api_data(token, command, parameters): APIURLFMT = "{}{}/{}/" url = APIURLFMT.format(apiurl, token, command) response = requests.get(url, params=parameters) if(response.status_code != 200): raise Exception('Invalid response code: {}' . format(response.status_code)) apiresponse = response.json() if bool(apiresponse['status']) != True: raise Exception(apiresponse['error']) return apiresponse # get_api_zip_file # parameters = {"source": "added"} # get_api_zip_file(token, "dailyzip", parameters, "latest.zip") # def get_api_zip_file(token, command, parameters, filename): APIURLFMT = "{}{}/{}/" url = APIURLFMT.format(apiurl, token, command) response = requests.get(url, params=parameters) totalbits = 0 if response.status_code == 200: with open(filename, 'wb') as f: for chunk in response.iter_content(chunk_size=1024): if chunk: totalbits += 1024 f.write(chunk) print("Downloaded",totalbits*1025,"KB...") else: raise Exception('Invalid response code: {}' . format(response.status_code)) ######################################################################################### try: idx = 0 source_name = 'added' keywords = "%apple%|%paypal%" number_of_days = 1; only_domain_names = False show_information = True tlds_to_get = ''; show_raw_data = False #################################################################################### for i in sys.argv: if i == "-h": print("\ndnfeed-app.py [-d ] [-kw ] [-tlds ] [-s ] [-od] [-raw] [-v]\n"); print("\t-d days:\tNumber of preceding days (between 0 and 14) to get the data for.") print("\t-kw keywords:\tSpeify the keywords. Separate multiple keywords with a comma.\n\t\t\tUse % for wildcard searches.\n\t\t\tYou will need to enclose the keywords in double quotes if you use %.\n\t\t\tFor example, -kw \"%apple%,%amazon%\"") print("\t-tlds tlds:\tThe TLDs to get. Separate multiple keywords with a comma or |") print("\t-s name:\tThe source to get data from. The name should be added or deleted.") print("\t-od:\t\tPrint only the domain names in output") print("\t-raw:\t\tPrint raw data. If -raw is used the -od option will be ignored") print("\t-v:\t\tShow additional information") sys.exit() elif i == "-d": idx += 1; number_of_days = int(sys.argv[idx]) if number_of_days > 14: number_of_days = 14 elif i == "-kw": idx += 1; keywords = sys.argv[idx] keywords = keywords.replace(",", "|") elif i == "-od": idx += 1; only_domain_names = True elif i == "-raw": idx += 1; show_raw_data = True elif i == "-v": idx += 1; show_information = True elif i == "-s": idx += 1; source_name = sys.argv[idx] elif i == "-tlds": idx += 1; tlds_to_get = sys.argv[idx] else: idx += 1; #################################################################################### today_date = datetime.datetime.now() delta = datetime.timedelta(days = number_of_days) olddate = today_date - delta datecode = olddate.strftime("%Y%m%d") if source_name != 'added' and source_name != 'deleted': source_name = 'added' #################################################################################### if show_information: printdatecode = olddate.strftime("%Y-%m-%d") print('\nGetting domain data after ' + printdatecode + ' for keywords \'' + keywords + '\' from the ' + source_name + ' domains list.\n') #################################################################################### token = get_api_token() parameters = {"date": datecode, "limit": 5000, "kw": keywords, "dcm": 'gte', 'tlds': tlds_to_get, "format": "json"} thedata = get_api_data(token, source_name, parameters) domaindata = thedata['data'] if show_raw_data: json_formatted_str = json.dumps(domaindata, indent=2) print(json_formatted_str) else: for d in domaindata: if only_domain_names: print (d['domain']) else: print (d['domain'] + ', ' + d['date']) except Exception as e: print(repr(e));