Skip to content

Instantly share code, notes, and snippets.

@AhmedSoliman
Created January 8, 2023 22:30
Show Gist options
  • Save AhmedSoliman/e77ea036e36fbaf33363bed3199e6b4e to your computer and use it in GitHub Desktop.
Save AhmedSoliman/e77ea036e36fbaf33363bed3199e6b4e to your computer and use it in GitHub Desktop.
A tiny little script to get USD to GBP conversion rate for a given date from HMRC
import sys
from typing import Dict, List
from datetime import datetime
import requests
from xml.etree import ElementTree
BASE_HMRC_URL = 'http://www.hmrc.gov.uk/softwaredevelopers/rates/' #exrates-monthly-0123.XML
def main():
if len(sys.argv) < 2:
raise Exception("You must supply the date in DD/MM/YYYY format")
parsed_date = datetime.strptime(sys.argv[1], "%d/%m/%Y")
f_month = parsed_date.strftime("%m")
f_year = parsed_date.strftime("%y")
# print(f"Getting USD -> GBP rates for {parsed_date.month}/{parsed_date.year}")
url = f"{BASE_HMRC_URL}/exrates-monthly-{f_month}{f_year}.XML"
# print(f"URL is {url}")
response = requests.get(url)
tree = ElementTree.fromstring(response.content)
for child in tree:
if child.tag == 'exchangeRate' and child.find('./currencyCode').text == 'USD':
rate = float(child.find('./rateNew').text)
print(f"{1/rate}")
# Run this with input `02/1/2022` and get the USD -> GBP exchange rate from HMRC
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment