def convert(amount, startingCurrencyCode, destinationCurrencyCode): errorValue = -1 validAmount = True decimalCount = 0 url_string = "https://www.google.com/finance/converter?a={}&from={}&" \ "to={}".format(amount, startingCurrencyCode, destinationCurrencyCode) result = web_utility.load_page(url_string) if type(amount) is str: for i in amount: if i not in ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."): validAmount = False if i in ".": decimalCount += 1 amount = float(amount) if amount < 1 or validAmount is False or decimalCount > 1: return errorValue elif startingCurrencyCode == destinationCurrencyCode: return errorValue else: resStr = result[result.index('result'):] convtAmt = resStr[resStr.index("d>") + 2:resStr.index(destinationCurrencyCode) - 1] return round(float(convtAmt), 2)
def convert(amount, home_currency, target_currency): from web_utility import load_page def remove_text_before(position_string, all_text): remove_start = all_text.find(position_string) if remove_start == -1: return '' remove_start += len(position_string) return all_text[remove_start:] def remove_span(all_text): remove_start = all_text.find('<') remove_end = all_text.find('>') + 1 return all_text[:remove_start] + all_text[remove_end:] format_string = 'https://www.google.com/finance/converter?a={}&from={}&to={}' url_string = format_string.format(amount, home_currency, target_currency) html_string = load_page(url_string) if not html_string: return -1 data_string = remove_text_before('converter_result>', html_string) if not data_string: return -1 data_string = remove_span(data_string) converted_amount_string = remove_text_before(" = ", data_string) if not converted_amount_string: return -1 end_amount = converted_amount_string.find(' ') return float(converted_amount_string[:end_amount])
def convert(self): import web_utility url_string = "https://www.google.com/finance/converter?a=1&from=AUD&to=JPY" url_string = url_string.replace('1', self.amount) url_string = url_string.replace('AUD', self.home_currency_code) url_string = url_string.replace('JPY', self.location_currency_code) if self.home_currency_code == self.location_currency_code: #print("-1") return -1.00, "Invalid Conversion" else: print() result = web_utility.load_page(url_string) #print(result[result.index('result'):]) store_result = result[result.index('result'):] #print(store_result) url_string = "https://www.google.com/finance/converter?a=1&from=AUD&to=JPY" new_store_result = store_result.replace('\\n', "") #print(new_store_result) conversion_num = (re.findall('\d+(?:\.\d+)?', new_store_result)) #regular expression #/d+ - any number, one or more #print(conversion_num) final_conversion_num = conversion_num[1] #print(final_conversion_num) return final_conversion_num, "Valid conversion"
def convert(amount, home_currency, location_currency): #function for getting the amount,home and location currency from web_utility import load_page def ignore_characters_at_beginning(index_character, all_other_string): #funtion definition for removing the text before ignore_commence = all_other_string.find(index_character) if ignore_commence == -1: return '' ignore_commence += len(index_character) return all_other_string[ignore_commence:] def ignore_space(all_strings): #function definition for removing the space before ignore_begin = all_strings.find('<') ignore_exit = all_strings.find('>') + 1 return all_strings[:ignore_begin] + all_strings[ignore_exit:] currency_url = 'https://www.google.com/finance/converter?a={}&from={}&to={}' #getting the url of the google finance converter url_string = currency_url.format(amount, home_currency, location_currency) http_url = load_page(url_string) if not http_url: return -1 info_text = ignore_characters_at_beginning('converter_result>', http_url) #remove text before url if not info_text: return -1 info_text = ignore_space(info_text) #remove space before url amount_after_conversion_text = ignore_characters_at_beginning(" = ", info_text) if not amount_after_conversion_text: return -1 end_amount = amount_after_conversion_text.find(' ') #final amount after currency conversion return float(amount_after_conversion_text[:end_amount])
def convert(amount, home_currency_code, location_currency_code): url_string = "https://www.google.com/finance/converter?a=" + str( amount ) + "&from=" + home_currency_code + "&to=" + location_currency_code result = web_utility.load_page(url_string) resStr = result[result.index('result'):] convtAmt = resStr[resStr.index("d>") + 2:resStr.index(location_currency_code)] return float(convtAmt)
def convert(amount, first_currency, second_currency): url_string = "https://www.google.com/finance/converter?a={}&from={}&to={" \ "}".format(amount, first_currency.upper(), second_currency.upper()) result = web_utility.load_page(url_string) if first_currency == second_currency: return incorrect_value elif error_string not in result: return incorrect_value else: return result[(result.find('class=bld>') + 10):(result.find('</span>') - 4)]
def convert(amount, home_currency_code, location_currency_code): """ Returns another equivalent currency specified amount of money """ try: url_string = "https://www.google.com/finance/converter?a={}&from={}&to={}".format(amount, home_currency_code, location_currency_code) result = web_utility.load_page(url_string) # accesses and reads web page extracted_string = (result[result.index('result'):]) listed_string = extracted_string.split(">") converted_result = listed_string[2].split(" ") return float(converted_result[0]) except: return -1
def convert(amount, home_currency_code, location_currency_code): url_string = "https://www.google.com/finance/converter?a=" + str( amount) + "&from=" + str(home_currency_code) + "&to=" + str( location_currency_code) results = web_utility.load_page(url_string) if (home_currency_code == location_currency_code ) or not home_currency_code + " = <span class=bld>" in results: return -1 else: result = ( results[results.index(home_currency_code + " = <span class=bld>") + 22:results.index(location_currency_code + "</span>")]) return "{:.3f}".format(float(result))
def convert(home_currency, location_currency_code): amount = float(1) number = 1000000.0000 try: if home_currency == location_currency_code: raise trip.Error("Same Currency") url_string = "https://www.google.com/finance/converter?a=1&from=" + home_currency + "&to=" + location_currency_code result = web_utility.load_page(url_string) result = result[result.index('<span class=bld>') + 16:] result = result[:result.index(location_currency_code)] except: result = -1 return result
def convert(amount, home_currency_code, location_currency_code ): #converts the home currency to foreign currency and returns it url_string = "https://www.google.com/finance/converter?a={}&from={}&to={}".format( amount, home_currency_code, location_currency_code) result = web_utility.load_page(url_string) if home_currency_code == location_currency_code: return -1 if not home_currency_code + " = <span class=bld>" in result: return -1 else: output_google = result[result.index('ld>'):result.index('</span>')] money = float(''.join(ele for ele in output_google if ele.isdigit() or ele == '.')) return money
def convert(amount, home_currency_code, location_currency_code): url = "https://www.google.com/finance/converter?a=" + str( amount ) + "&from=" + home_currency_code + "&to=" + location_currency_code try: result = web_utility.load_page(url) split_string = result[result.index('result'):] split_string = split_string.split( ">") # split the function into parts by using '>' split_string = split_string[2].split( " ") # split the parts again into smaller sections return float(split_string[0]) except: # if there is any error, eg. page doesn't load, will return -1 return -1
def convert(amount, home_currency_code, location_currency_code): """This function takes an amount of money from one currency and converts it to another.""" if not isinstance(home_currency_code, str) and isinstance(location_currency_code, str) \ and isinstance(amount, (float, int)): # Checking that the inputs for the function is in the correct format return -1 # if not in the correct format, return -1. try: import web_utility # Use the web_utility module. url_string = str("https://www.google.com/finance/converter?a=%s&from=%s&to=%s" % (amount, home_currency_code, location_currency_code)) result = web_utility.load_page(url_string) # Load the url with specified parameters and return the result URL except: # All errors, return -1. return -1 return float(result[(result.find('<span class=bld>') + 16):(result.find('</span>')-4)]) # Cut the returned URL
def convert(value, home_currency_code, location_currency_code): try: value = float(value) home_currency_code != location_currency_code url_string = "https://www.google.com/finance/converter?a={}&from={}&to={}".format(str(value), home_currency_code, location_currency_code) currency_outcome = web_utility.load_page(url_string) substring = (currency_outcome[currency_outcome.index('result'):]) currency_section = substring.split('>') end_index = (currency_section[2].find(' ')) currency_converted_amount = float(currency_section[2][0:end_index]) return currency_converted_amount except: return -1
def convert(amount, home_currency_code, location_currency_code): url_string = "https://www.google.com/finance/converter?a="+str(amount)+"&from="+home_currency_code+"&to="+location_currency_code print(url_string) result = web_utility.load_page(url_string) try : index_of_matched_data = result.index('<span class=bld>') newmatched_string = result[index_of_matched_data:] # This will return me this text <span class=bld>0.7426 AUD</span>\n<input type= ....> # Next I will take the data till </span> so as to get the currency newmatched_string = newmatched_string[newmatched_string.index('<span class=bld>')+16:newmatched_string.index('</span>')] # index of span class=bld + 16. 16 is length of <span class=bld> and hence start index to get value should be after +16. #output 10 USD # we just need 10 return newmatched_string[0:newmatched_string.index(" ")] except ValueError : return "-1"
def convert( amount, home_currency, target_currency ): #Takes the currency codes and amounts and converts them using the google finance conversion try: if home_currency == target_currency: return "-1" else: url_string = "https://www.google.com/finance/converter?a=%s&from=%s&to=%s" % ( amount, home_currency, target_currency) result = load_page(url_string) returned_numbers = re.findall( '\d+.\d+|\d+', (result[result.index("result"):]))[:2] if float(returned_numbers[0] or returned_numbers[1]) == amount: return returned_numbers[1] else: return "-1" except ValueError: return "-1"
def convert( amount, home_currency, target_currency ): # Takes the currency codes and amounts and converts them using the google finance conversion try: if home_currency == target_currency: return "-1" else: url_string = "https://www.google.com/finance/converter?a=%s&from=%s&to=%s" % ( amount, home_currency, target_currency, ) result = load_page(url_string) returned_numbers = re.findall("\d+.\d+|\d+", (result[result.index("result") :]))[:2] if float(returned_numbers[0] or returned_numbers[1]) == amount: return returned_numbers[1] else: return "-1" except ValueError: return "-1"
def convert(amount, startingCurrencyCode, destinationCurrencyCode): errorValue = -1 validAmount = True decimalCount = 0 url_string = "https://www.google.com/finance/converter?a={}&from={}&" \ "to={}".format(amount, startingCurrencyCode, destinationCurrencyCode) result = web_utility.load_page(url_string) if type(amount) is str: for i in amount: if i not in ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."): validAmount = False if i in ".": decimalCount += 1 amount = float(amount) if amount < 1 or validAmount is False or decimalCount > 1: return errorValue elif startingCurrencyCode == destinationCurrencyCode: return errorValue else: resStr = result[result.index('result'):] convtAmt = resStr[resStr.index("d>") + 2: resStr.index(destinationCurrencyCode) - 1] return round(float(convtAmt), 2)
def convert(amount, home_currency_code, location_currency_code): url_string = "https://www.google.com/finance/converter?a="+str(amount)+"&from="+home_currency_code+"&to="+location_currency_code result = web_utility.load_page(url_string) resStr = result[result.index('result'):] convtAmt = resStr[resStr.index("d>") + 2 :resStr.index(location_currency_code)] return float(convtAmt)
def convert(amount, home_currency_code, location_currency_code): url_string = ("https://www.google.com/finance/converter?a=" + amount + "&from=" + home_currency_code + "&to=" + location_currency_code) result = web_utility.load_page(url_string) result = (result[result.index('result'):]) return result
import web_utility amount = str('5') first_country = 'USD' sec_country = 'JPY' url_string = "https://www.google.com/finance/converter?a=1&from=AUD&to=JPY" url_string = url_string.replace('1', amount) url_string = url_string.replace('AUD', first_country) url_string = url_string.replace('JPY', sec_country) print (url_string) result = web_utility.load_page(url_string) print(result[result.index('result'):]) url_string = "https://www.google.com/finance/converter?a=1&from=AUD&to=JPY"
def convert(self, amount): url_string = "https://www.google.com/finance/converter?a={}&from={}&to={}".format(amount, self.get_currency_code(self.selected_country), self.get_currency_code('Australia')) result = web_utility.load_page(url_string) converted_amount = round(self.get_only_number(result[result.index('ld>'):result.index('</span>')]), 2) self.converted = converted_amount