def whois_history(self, domain, api_key): """ :param domain: whois_history domain requested :param api_key: optional, for whoxy.com api return whois info of domain """ if not (validators.domain(domain)): raise InvalidInputError('invalid domain') try: url_request = "http://api.whoxy.com/?key=%s&history=%s" % (api_key, domain) with urllib.request.urlopen(url_request) as url: data = json.loads(url.read().decode()) if 'status_reason' in data: if data['status_reason'] == 'Incorrect API Key': raise WrongApiKeyError('wrong or invalid api key') else: raise InvalidInputError(data['status_reason']) elif 'status' in data: if data['status'] == 0: raise InternalModuleError( ' something goes wrong in api') if 'whois_records' not in data: raise ResultNotFoundError( 'cant find any result for your request') except URLError: raise NetworkError('can not access whoxy.com') if self.parent: self.parent.check_point() result = {'results': self.parse_history(data['whois_records'])} return result
def get_result(self, search_term, api_key_value, cse_id_value, pages, **kwargs): """ :param search_term: query for search :param api_key_value: google api key :param cse_id_value: custom search engine id :param pages: number of google pages wants to search :param kwargs: :return: list of result from custom google search api """ return_result = {} # build a google custom search v1 try: service = build("customsearch", "v1", developerKey=api_key_value, cache_discovery=False) except httplib2.ServerNotFoundError: raise NetworkError('Unable to find the server at www.googleapis.com') result_list = [] if pages: self.pages = pages # iterate on number of excepted result, save one page on each iteration for i in range(pages): start_point_of_search_result = (i * 10) + 1 self.parent.check_point() try: # dictionary which is produced by google custom search api result_dict_temp = ( service.cse().list(q=search_term, cx=cse_id_value, num=10, start=start_point_of_search_result, **kwargs).execute()) # extract items from dictionary and reformat it if 'items' in result_dict_temp: temp_parsed_result = self.parse_result( result_dict_temp['items']) for element in temp_parsed_result: result_list.append(element) # update progressbar value self.update_progressbar(" Pages has been searched: " + str(i + 1), (100 * (i + 1) / self.pages)) # invalid api_key or cse_id except HttpError as e: if "Invalid Value" in str(e): raise WrongCseKeyError("wrong cse_id number") elif "Bad Request" in str(e): raise WrongApiKeyError("wrong api_key number") elif 'Forbidden' in str(e): raise WrongCseKeyError('wrong api_key or wrong cse_key') else: raise NetworkError(e) except Exception as e: raise NetworkError(e) # no result found if len(result_list) == 0: raise ResultNotFoundError(' can not find result for your query') return_result['results'] = result_list return return_result
def api_searching(self, query, page_number): """ :param page_number: the number of requested page for result of searching term :param query: query for bing search :return:list of searching result in form of entity_property """ return_result = {} try: headers, result = self.__bing_web_search(query, page_number) jres = json.loads(result) self.parent.check_point() # saving and parsing result, if status code = 401, wrong api key if 'statusCode' in jres and jres['statusCode'] == 401: raise WrongApiKeyError('Wrong or Invalid API') web_result = jres["webPages"] value = web_result["value"] self.update_progressbar(" parsing result ", 90) temp_parsed_result = self.parse_result(value) return_result['results'] = temp_parsed_result # no result found if len(return_result) == 0: return_result["results"] = [{ "data": " ", "properties": [{ 'title': '', 'type': 0 }, { 'description': '', 'type': 0 }], "type": 1 }] return return_result except Exception as e: raise e return return_result
def get_ip_info_balance(self): """ this function return the get_ip_info balance :return Returns api balance as a dictionary """ api_key = self.api_key self.update_progressbar('set request to get ip_info balance', 50) if self.parent: self.parent.check_point() try: r = requests.get('https://api.ip2location.com/?key=' + api_key + '&check=1') except (requests.exceptions.RequestException, requests.exceptions.ConnectionError): raise NetworkError('can not access api') if r.text == 'INVALID ACCOUNT': raise WrongApiKeyError(r.text) else: return {'ip_info_balance': r.text, 'type': 0}
def get_whois_by_name(name, api_key): """ :param name: whois name requested :param api_key: optional, for whoxy.com api return whois info of name """ try: if ' ' in name: name = name.replace(' ', '+') url_request = "http://api.whoxy.com/?key=%s&reverse=whois&name=%s" % ( api_key, name) with urllib.request.urlopen(url_request) as url: data = json.loads(url.read().decode()) if 'status_reason' in data: if data['status_reason'] == 'Incorrect API Key': raise WrongApiKeyError('wrong or invalid api key') else: raise InvalidInputError(data['status_reason']) except URLError: raise NetworkError('can not access whoxy.com') result = {'results': parse_reverse(data)} return result
def get_whois_by_email(email, api_key): """ :param email: whois email requested :param api_key: optional, for whoxy.com api return whois info of domain """ pattern = re.compile("[\w\.-]+@[\w\.-]+\.\w+") if (pattern.match(email)) == None: raise InvalidInputError('email format is invalid') try: url_request = "http://api.whoxy.com/?key=%s&reverse=whois&email=%s" % ( api_key, email) with urllib.request.urlopen(url_request) as url: data = json.loads(url.read().decode()) if 'status_reason' in data: if data['status_reason'] == 'Incorrect API Key': raise WrongApiKeyError('wrong or invalid api key') else: raise InvalidInputError(data['status_reason']) except URLError: raise NetworkError('can not access whoxy.com') result = {'results': parse_reverse(data)} return result
def check_balance(api_key): try: balance = '' key = api_key url_request = "http://api.whoxy.com/?key=%s&account=balance" % (key) with urllib.request.urlopen( url_request) as url: data = json.loads(url.read().decode()) if 'status_reason' in data: if data['status_reason'] == 'Incorrect API Key': raise WrongApiKeyError('wrong or invalid api key') else: raise InvalidInputError(data['status_reason']) if 'live_whois_balance' in data: balance = data['live_whois_balance'] if balance: return balance else: return 'can not get balance' except URLError: raise NetworkError('can not access whoxy.com')
def get_ip_is_proxy_balance(self): """ this function return the ip_is_proxy balance :return Returns api balance as a dictionary """ api_key = self.api_key self.update_progressbar('set request to get ip_proxy balance', 50) if self.parent: self.parent.check_point() try: r = requests.get('https://api.ip2proxy.com/?key=' + api_key + '&check=1') except (requests.exceptions.RequestException, requests.exceptions.ConnectionError): raise NetworkError(' can not access api') if 'response' in json.loads(r.text): if json.loads(r.text)['response'] == "INVALID ACCOUNT": raise WrongApiKeyError(json.loads(r.text)['response']) else: return { 'ip_is_proxy_balance': json.loads(r.text)['response'], 'type': 0 }
def get_result(self): """ :return: whois info of domain """ # build a whois request try: key = self.api_key domain = self.domain url_request = "http://api.whoxy.com/?key=%s&whois=%s" % (key, domain) with urllib.request.urlopen( url_request) as url: data = json.loads(url.read().decode()) if 'status_reason' in data: if data['status_reason'] == 'Incorrect API Key': raise WrongApiKeyError(data['status_reason']) else: raise InvalidInputError(data['status_reason']) except URLError: raise NetworkError('can not access whoxy.com') self.parent.check_point() self.update_progressbar('parsing result', 80) unstructured_res = WhoisApi.parse_unstructure(data) email_res = WhoisApi.get_emails(data) phone_res = WhoisApi.parse_phone(data) name_res = WhoisApi.get_names(data) result_list = [] properties_list = [] special_properties_list = [] result = {'results': [], 'special_properties': [], 'properties': []} # create structure data from result if unstructured_res: result_list.append(unstructured_res['results']) special_properties_list = (unstructured_res['special_properties']) properties_list = (unstructured_res['properties']) # saving emails, phone, names in list of results for email in email_res: if email: result_list.append(email) for phone in phone_res: if phone: result_list.append(phone) for name in name_res: if name: result_list.append(name) if len(special_properties_list) == 0: result['special_properties'] = '' else: result['special_properties'] = special_properties_list if len(properties_list) == 0: result['properties'] = '' else: result['properties'] = properties_list result['results'] = result_list return result
def get_ip_info(self, ip): """ this function get the given ip information :return Returns ip information in json format """ api_key = self.api_key if self.parent: self.parent.check_point() self.update_progressbar('set request to get ip info', 20) try: r = requests.get('https://api.ip2location.com/?ip=' + ip + '&key=' + api_key + '&package=WS24&format=json') except (requests.exceptions.RequestException, requests.exceptions.ConnectionError): raise NetworkError(' can no access api') if r.status_code == 200: result = json.loads(r.content.decode()) temp = list() self.update_progressbar('parsing result', 60) if self.parent: self.parent.check_point() if 'response' in result: if result['response'] == 'INSUFFICIENT CREDIT': raise InsufficientCredit(result['response']) elif result['response'] == 'INVALID ACCOUNT': raise WrongApiKeyError(result['response']) else: raise InternalModuleError(result['response']) else: longitude = 'unknown' latitude = 'unknown' if result.items(): for k, v in result.items(): # parsing result if k in [ 'country_name', 'region_name', 'city_name', 'isp', 'mobile_brand' ]: temp.append({ self.get_ip_info_keys[k]: v, "type": 11 }) elif k in ['domain']: temp.append({ self.get_ip_info_keys[k]: v, "type": 12 }) elif k in ['usage_type']: for each in v.split('/'): try: temp.append({ self.get_ip_info_keys[k]: self.usage_and_proxy_type[each], "type": 0 }) except KeyError: pass elif k in [ 'latitude', 'zip_code', 'time_zone', 'idd_code', 'area_code', 'elevation', 'mcc', 'mnc', 'net_speed', 'longitude' ]: temp.append({k: v, "type": 0}) if k == 'longitude': longitude = v if k == 'latitude': latitude = v else: pass # new key is founded geo_coordinates = str(latitude) + ',' + str(longitude) geo_record = { 'geo_coordinates': geo_coordinates, 'type': 14 } final = { "properties": [], "special_properties": temp, "results": [geo_record] } return final else: raise ResultNotFoundError( 'no result found for your request') else: if r.status_code == 404: raise ResultNotFoundError('no result found for your request') NetworkError('status code: ', r.status_code)
def ip_is_proxy(self, ip): """ this function specifies whether the given ip is proxy or not :return Returns ip information as a dictionary """ api_key = self.api_key self.update_progressbar('set request to is it proxy', 20) if self.parent: self.parent.check_point() try: r = requests.get('http://api.ip2proxy.com/?ip=' + ip + '&key=' + api_key + '&package=PX4&format=json') except (requests.exceptions.RequestException, requests.exceptions.ConnectionError): raise NetworkError(' can not access api') if r.status_code == 200: result = json.loads(r.content.decode()) self.update_progressbar('parsing result', 60) if self.parent: self.parent.check_point() if 'response' in result: temp = list() if result['response'] == 'OK': if result.items(): # parsing result for k, v in result.items(): if k in [ 'countryName', 'regionName', 'cityName', 'isp' ]: temp.append({ self.ip_is_proxy_keys[k]: v, "type": 11 }) elif k in ['proxyType']: for each in v.split('/'): try: temp.append({ self.ip_is_proxy_keys[k]: self.usage_and_proxy_type[each], "type": 0 }) except KeyError: temp.append({ self.ip_is_proxy_keys[k]: v, "type": 0 }) elif k in ['isProxy']: temp.append({ self.ip_is_proxy_keys[k]: v, "type": 0 }) else: pass final = { "properties": [], "special_properties": temp, "results": [] } return final else: raise ResultNotFoundError( 'no result found for your request') else: if result['response'] == 'INSUFFICIENT CREDIT': raise InsufficientCredit(result['response']) elif result['response'] == 'INVALID ACCOUNT': raise WrongApiKeyError(result['response']) else: raise InternalModuleError(result['response']) else: raise NetworkError(r.status_code) else: if r.status_code == 404: raise ResultNotFoundError('no result found for your request') else: raise NetworkError('status_code ' + str(r.status_code))