Commits

Virgil Dupras committed 11b5797

Fixed fetch_boc_rates so that it works with the new interface of the Bank of Canada.

Comments (0)

Files changed (3)

-98b95abd14944000cf8712cbf0e5ea425e60b43a hscommon
+1210f8dd2ee345dceabbbb51f041d2be23f7ec3a hscommon
             rate_element = currency_element.find('Observation_data')
             try:
                 rate = float(rate_element.text.strip())
-            except ValueError: # probably n/a
+            except (ValueError, AttributeError): # probably n/a
                 continue
             year, month, day = date_element.text.strip().split('-')
             self.set_CAD_value(date(int(year), int(month), int(day)), currency_code, rate)

fetch_boc_rates.py

-#!/usr/bin/env python
 # This script fetches today's rates from the Bank of Canada website
 # Created By: Eric Mc Sween
 # Created On: 2008-05-19
 
 from currency_server.db import RatesDB
 
+def main():
+    # Open the db
+    db = RatesDB()
 
-# Open the db
-db = RatesDB()
+    # Make the first query
 
-# Make the first query
+    db_min, db_max = db.date_range('USD')
+    start = db_max # We'll always refetch yesterday's data in case there was some missing.
+    end = date.today()
 
-db_min, db_max = db.date_range('USD')
-start = db_max # We'll always refetch yesterday's data in case there was some missing.
-end = date.today()
-form_data = {
-    # Hidden
-    'cgi': 'famescript',
-    'pageName': 'exchange-look.html',
-    'famescript_formname': 'exchange-look.html',
-    'famescript_html_template': 'fxlook-nojs.html',
-    'ExchangeRates_frequency': 'business',
-    'ExchangeRates_daterange': '',
-    'ExchangeRates_replace_NC': 'Not available',
-    'ExchangeRates_replace_ND': 'Not available',
-    'ExchangeRates_replace_NA': 'Bank holiday',
+    form_data = {
+        # Hidden
+        'lookupPage': 'lookup_daily_exchange_rates.php',
+        'startRange': '2001-07-07',
+        'dFrom': start.strftime('%Y-%m-%d'),
+        'dTo': end.strftime('%Y-%m-%d'),
 
-    # Select options
-    'show_xml': 'TRUE',
+        # Select currencies
+        'series[]': [
+            'LOOKUPS_IEXE0101',  # U.S. dollar (close)
+            'LOOKUPS_IEXE2702',  # Argentine peso (floating rate)
+            'LOOKUPS_IEXE1601',  # Australian dollar
+            'LOOKUPS_IEXE6001',  # Bahamian dollar
+            'LOOKUPS_IEXE2801',  # Brazilian real
+            'LOOKUPS_IEXE4501',  # CFA franc
+            'LOOKUPS_IEXE4601',  # CFP franc
+            'LOOKUPS_IEXE2901',  # Chilean peso
+            'LOOKUPS_IEXE2201',  # Chinese renminbi (yuan)
+            'LOOKUPS_IEXE3901',  # Colombian peso
+            'LOOKUPS_IEXE6101',  # Croatian kuna
+            'LOOKUPS_IEXE2301',  # Czech Republic koruna
+            'LOOKUPS_IEXE0301',  # Danish krone
+            'LOOKUPS_IEXE4001',  # East Caribbean dollar
+            'LOOKUPS_EUROCAE01', # European Euro
+            'LOOKUPS_IEXE4101',  # Fiji dollar
+            'LOOKUPS_IEXE4702',  # Ghanaian cedi
+            'LOOKUPS_IEXE6501',  # Guatemalan quetzal
+            'LOOKUPS_IEXE4301',  # Honduran lempira
+            'LOOKUPS_IEXE1401',  # Hong Kong dollar
+            'LOOKUPS_IEXE2501',  # Hungarian forint
+            'LOOKUPS_IEXE4401',  # Icelandic krona
+            'LOOKUPS_IEXE3001',  # Indian rupee
+            'LOOKUPS_IEXE2601',  # Indonesian rupiah
+            'LOOKUPS_IEXE5301',  # Israeli new shekel
+            'LOOKUPS_IEXE6401',  # Jamaican dollar
+            'LOOKUPS_IEXE0701',  # Japanese yen
+            'LOOKUPS_IEXE3201',  # Malaysian ringgit
+            'LOOKUPS_IEXE2001',  # Mexican peso
+            'LOOKUPS_IEXE4801',  # Moroccan dirham
+            'LOOKUPS_IEXE3801',  # Myanmar (Burma) kyat
+            'LOOKUPS_IEXE4901',  # Neth. Antilles florin
+            'LOOKUPS_IEXE1901',  # New Zealand dollar
+            'LOOKUPS_IEXE0901',  # Norwegian krone
+            'LOOKUPS_IEXE5001',  # Pakistan rupee
+            'LOOKUPS_IEXE5101',  # Panamanian balboa
+            'LOOKUPS_IEXE5201',  # Peruvian new sol
+            'LOOKUPS_IEXE3301',  # Philippine peso
+            'LOOKUPS_IEXE2401',  # Polish zloty
+            'LOOKUPS_IEXE6505',  # Romanian new leu
+            'LOOKUPS_IEXE2101',  # Russian rouble
+            'LOOKUPS_IEXE6504',  # Serbian dinar
+            'LOOKUPS_IEXE3701',  # Singapore dollar
+            'LOOKUPS_IEXE6201',  # Slovak koruna
+            'LOOKUPS_IEXE3401',  # South African rand
+            'LOOKUPS_IEXE3101',  # South Korean won
+            'LOOKUPS_IEXE5501',  # Sri Lanka rupee
+            'LOOKUPS_IEXE1001',  # Swedish krona
+            'LOOKUPS_IEXE1101',  # Swiss franc
+            'LOOKUPS_IEXE3501',  # Taiwanese new dollar
+            'LOOKUPS_IEXE3601',  # Thai baht
+            'LOOKUPS_IEXE5701',  # Tunisian dinar
+            'LOOKUPS_IEXE6506',  # U.A.E. dirham
+            'LOOKUPS_IEXE1201',  # U.K. pound sterling
+            'LOOKUPS_IEXE5902',  # Venezuelan bolivar fuerte
+            'LOOKUPS_IEXE6503',  # Vietnamese dong
+        ],
+    }
 
-    # Select dates
-    'ExchangeRates_dateclause': '=ExchangeRatesByDates',
-    'ExchangeRatesByDates_frequency': 'daily',
-    'ExchangeRatesByDates_daterange_start_Month': start.strftime('%b'),
-    'ExchangeRatesByDates_daterange_start_Day': start.day,
-    'ExchangeRatesByDates_daterange_start_Year': start.year,
-    'ExchangeRatesByDates_daterange_end_Month': end.strftime('%b'),
-    'ExchangeRatesByDates_daterange_end_Day': end.day,
-    'ExchangeRatesByDates_daterange_end_Year': end.year,
+    with urlopen('http://www.bankofcanada.ca/rates/exchange/10-year-lookup/', urlencode(form_data, True).encode('ascii')) as response:
+        contents = response.read().decode('latin-1')
 
-    # Select currencies
-    'ExchangeRates': [
-        'IEXE0101',  # U.S. dollar (noon)
-        'IEXE2702',  # Argentine peso (floating rate)
-        'IEXE1601',  # Australian dollar
-        'IEXE6001',  # Bahamian dollar
-        'IEXE2801',  # Brazilian real
-        'IEXE4501',  # CFA franc
-        'IEXE4601',  # CFP franc
-        'IEXE2901',  # Chilean peso
-        'IEXE2201',  # Chinese renminbi (yuan)
-        'IEXE3901',  # Colombian peso
-        'IEXE6101',  # Croatian kuna
-        'IEXE2301',  # Czech Republic koruna
-        'IEXE0301',  # Danish krone
-        'IEXE4001',  # East Caribbean dollar
-        'EUROCAE01', # European Euro
-        'IEXE4101',  # Fiji dollar
-        'IEXE4702',  # Ghanaian cedi (new)
-        'IEXE6501',  # Guatemalan quetzal
-        'IEXE4301',  # Honduran lempira
-        'IEXE1401',  # Hong Kong dollar
-        'IEXE2501',  # Hungarian forint
-        'IEXE4401',  # Icelandic krona
-        'IEXE3001',  # Indian rupee
-        'IEXE2601',  # Indonesian rupiah
-        'IEXE5301',  # Israeli new shekel
-        'IEXE6401',  # Jamaican dollar
-        'IEXE0701',  # Japanese yen
-        'IEXE3201',  # Malaysian ringgit
-        'IEXE2001',  # Mexican peso
-        'IEXE4801',  # Moroccan dirham
-        'IEXE3801',  # Myanmar (Burma) kyat
-        'IEXE4901',  # Neth. Antilles florin
-        'IEXE1901',  # New Zealand dollar
-        'IEXE0901',  # Norwegian krone
-        'IEXE5001',  # Pakistan rupee
-        'IEXE5101',  # Panamanian balboa
-        'IEXE5201',  # Peruvian new sol
-        'IEXE3301',  # Philippine peso
-        'IEXE2401',  # Polish zloty
-        'IEXE6505',  # Romanian new leu
-        'IEXE2101',  # Russian rouble
-        'IEXE6504',  # Serbian dinar
-        'IEXE3701',  # Singapore dollar
-        'IEXE6201',  # Slovak koruna
-        'IEXE3401',  # South African rand
-        'IEXE3101',  # South Korean won
-        'IEXE5501',  # Sri Lanka rupee
-        'IEXE1001',  # Swedish krona
-        'IEXE1101',  # Swiss franc
-        'IEXE3501',  # Taiwanese new dollar
-        'IEXE3601',  # Thai baht
-        #'IEXE5601',  # Trinidad & Tobago dollar
-        'IEXE5701',  # Tunisian dinar
-        #'IEXE5802',  # Turkish new lira
-        'IEXE6506',  # U.A.E. dirham
-        'IEXE1201',  # U.K. pound sterling
-        'IEXE5902',  # Venezuelan bolivar fuerte
-        'IEXE6503',  # Vietnamese dong
-    ],
-}
-response = urlopen('http://www.bankofcanada.ca/cgi-bin/famecgi_fdps', urlencode(form_data, True))
+    # search for a link to XML data
+    match = re.search(r'http://www\.bankofcanada\.ca/stats/results/p_xml\?session_id=.+?&session_key=lookup_daily_exchange_rates', contents)
 
-# Find the XML file's URL
+    if not match:
+        print('Invalid response')
+        sys.exit(1)
 
-url_re = re.compile(r'/databank/client_output/.*\.xml')
-for line in response:
-    match = url_re.search(line)
-    if match:
-        xml_url = match.group()
-        break
-else:
-    sys.exit("Error: couldn't find the XML file's URL in the response: %s" % response.read())
+    # Fetch the XML file
+    xml_url = contents[match.start():match.end()]
+    with urlopen(xml_url) as xml_file:
+        # print(repr(xml_file.read()))
+        db.import_bank_of_canada_rates(xml_file)
 
-# Fetch the XML file
-
-xml_file = urlopen('http://www.bankofcanada.ca' + xml_url)
-db.import_bank_of_canada_rates(xml_file)
+if __name__ == '__main__':
+    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.