Friday, January 30, 2015

USD to INR Exchange Rate Calculator (Xoom, PayPal) Script in Python

Written by Pranshu Bajpai |  | LinkedIn

I frequently transfer money to India. For this reason, I find myself calculating amounts pertaining to USD to INR conversions using major remit websites such as the following:
  • Xoom
  • PayPal
  • Remitly
  • RIA Money Transfer
  • MoneyDart - Money2Anywhere
  • Trans-Fast
  • USForex Money Transfer
  • IndusInd Bank - Indus Fast Remit
  • State Bank of India
  • ICICI Money2India
  • Axis Bank - AxisRemit
  • Western Union

Since almost all of these websites offer more or less similar services, I usually choose the one that is offering me the best exchange rate. Having said that, I was quickly annoyed by having to visit each of these websites to compare them and determine the one that is offering the best USD to INR exchange rate on the present day. For this reason, I decided to write a Python web scrapping script that would go online, locate the exchange rates germane to all of these major websites and then show them to me for comparison.

Usage: python usdtoinr.py

Download: Github: https://github.com/lifeofpentester/usdtoinr




By default, it shows you the exchange rate grabbed from xoom (without any switch). Or, you can use '-x' or '--xoom' switch to do the same thing.

By using the '-a' or '--all' switch, you can view the current exchange rates corresponding to all major remit websites.



Initially, my 'usdtoinr' python script only displayed the current exchange rate from the major websites, but then I realized that it would be better if they could convert an amount in USD (US dollars) to an amount in INR (Indian Rupees). Accordingly, I added this functionality in the script. You can use the '-c' or '--convert' switch for this purpose.



As I started using PayPal, I added another function in my script that would show me an amount in USD (US dollars) converted to an amount in INR (Indian Rupees) and then deduct PayPal's fees of 4 percent from this amount to show me the money that someone would actually receive in India. To invoke this function, use the '-p' or '--paypal' switch.


Of course, '-h' or '--help' is to see the usage information.

That's all the functionality that is coded in the script at the moment since that's all I needed. With time, I may add more functions should I need them. As with all web scrapping scripts, the functionality of the script depends on the websites from where it is capturing the information pertaining to the exchange rates. If those websites change with time, it might result in the script breaking down. If that is the case, or if you want some other functionality added, feel free to modify the code.

Note: argparse is a nice library that you can use to allow for command line arguments or switches in your scripts.

Note: The ASCII text banner in the script has been generated with a utility called 'figlet'

Source Code:


If you would like to read the code, here it is:


#!/usr/bin/python

import requests
from bs4 import BeautifulSoup
import argparse
import re
import time

def _xoom():
 r = requests.get('https://www.xoom.com/india/send-money')
 data = r.text

 soup = BeautifulSoup(data)

 for rate in soup.find_all('em'):
  return rate.text

def _all():
 r = requests.get('http://www.compareremit.com')
 print "[+] Requested information!"
 data = r.text
 print "[+] Grabbed all exchange rates online!"
 soup = BeautifulSoup(data)
 for rate in soup.find_all('div',{"class":"c_logo_box"}):
      print rate.a.img['alt'] 
      print rate.span.text

def _rate_calc():
 ratetext = _xoom()
 print "[+] Requested exchange rate from Xoom!"
 found = re.search("(?<=\=)(.*?)(?=I)", ratetext)
 print "[+] Located today's exhange rate!" 
 rate = float(found.group())
 print "[+] Converting USD to INR now..."
 amount = args.convert * rate
 return amount

def _paypal():
 ratetext = _xoom()
 print "[+] Requested exchange rate from Xoom!"
 found = re.search("(?<=\=)(.*?)(?=I)", ratetext)
 print "[+] Located today's exchange rate!" 
 rate = float(found.group())
 print "[+] Converting USD to INR now..."
 print "[+] Calculating amount left after PayPal's 4 percent fee..."
 amount = 0.96*(args.paypal*rate)
 return amount
 

parser = argparse.ArgumentParser(description="Script for USD to INR calculations")
parser.add_argument('-x', '--xoom', help='exchange rate from xoom.com', action='store_true')
parser.add_argument('-a', '--all', help='exchange rate from all major remit websites', action='store_true')
parser.add_argument('-c', '--convert', help='USD to INR conversion using current exchange rate', type=float)
parser.add_argument('-p', '--paypal', help='amount after deducting PayPal\'s 4 percent fees', type=float)
args = parser.parse_args()




print """               _ _        _           
 _   _ ___  __| | |_ ___ (_)_ __  _ __ 
| | | / __|/ _` | __/ _ \| | '_ \| '__|
| |_| \__ \ (_| | || (_) | | | | | |   
 \__,_|___/\__,_|\__\___/|_|_| |_|_|   

                          --by Pranshu
"""

_time = time.asctime(time.localtime(time.time()))

print "[i] " + _time


if args.xoom:
 rate = _xoom()
 print "[i] Exchange Rate: " + rate
elif args.all:
 _all()
elif args.convert:
 amount = _rate_calc()
 print "\n[i]Amount in Rupees according to the exchange rate today: %f" %amount
elif args.paypal:
 amount = _paypal()
 print "\n[i]Amount in Rupees after deduction of Paypal's fees: %f" %amount
else:
 rate = _xoom()
 print "[i] Exchange Rate: " + rate
 #parser.print_help()

2 comments:

  1. It's a well scripted page i like it...

    ReplyDelete

  2. There are many sites available to transfer money to India, But Transferwise and Ria money as they pay the maximum with very good service. You can compare with sites like compateremit.com and Inr2day

    ReplyDelete