xchangerate

reserve bank exchange rate

downloads excel file from rbnz. Saves to to local file system. Convert content to json and serve.

In [2]:
import xmltodict
import requests
import shutil
import getpass
import arrow
#import pandas
import json
In [3]:
myusr = getpass.getuser()
In [5]:
reqdaily = requests.get('http://www.rbnz.govt.nz/-/media/ReserveBank/Files/Statistics/tables/b1/hb1-daily.xls', stream=True)
---------------------------------------------------------------------------
SSLError                                  Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    599                                                   body=body, headers=headers,
--> 600                                                   chunked=chunked)
    601 

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    344         try:
--> 345             self._validate_conn(conn)
    346         except (SocketTimeout, BaseSSLError) as e:

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py in _validate_conn(self, conn)
    843         if not getattr(conn, 'sock', None):  # AppEngine might not have  `.sock`
--> 844             conn.connect()
    845 

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connection.py in connect(self)
    325             server_hostname=hostname,
--> 326             ssl_context=context)
    327 

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/util/ssl_.py in ssl_wrap_socket(sock, keyfile, certfile, cert_reqs, ca_certs, server_hostname, ssl_version, ciphers, ssl_context, ca_cert_dir)
    324     if HAS_SNI:  # Platform-specific: OpenSSL with enabled SNI
--> 325         return context.wrap_socket(sock, server_hostname=server_hostname)
    326 

/usr/lib/python3.5/ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname)
    384                          server_hostname=server_hostname,
--> 385                          _context=self)
    386 

/usr/lib/python3.5/ssl.py in __init__(self, sock, keyfile, certfile, server_side, cert_reqs, ssl_version, ca_certs, do_handshake_on_connect, family, type, proto, fileno, suppress_ragged_eofs, npn_protocols, ciphers, server_hostname, _context)
    759                         raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
--> 760                     self.do_handshake()
    761 

/usr/lib/python3.5/ssl.py in do_handshake(self, block)
    995                 self.settimeout(None)
--> 996             self._sslobj.do_handshake()
    997         finally:

/usr/lib/python3.5/ssl.py in do_handshake(self)
    640         """Start the SSL/TLS handshake."""
--> 641         self._sslobj.do_handshake()
    642         if self.context.check_hostname:

SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)

During handling of the above exception, another exception occurred:

SSLError                                  Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    437                     retries=self.max_retries,
--> 438                     timeout=timeout
    439                 )

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    629             clean_exit = False
--> 630             raise SSLError(e)
    631 

SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)

During handling of the above exception, another exception occurred:

SSLError                                  Traceback (most recent call last)
<ipython-input-5-1e3388871684> in <module>()
----> 1 reqdaily = requests.get('http://www.rbnz.govt.nz/-/media/ReserveBank/Files/Statistics/tables/b1/hb1-daily.xls', stream=True)

/usr/local/lib/python3.5/dist-packages/requests/api.py in get(url, params, **kwargs)
     70 
     71     kwargs.setdefault('allow_redirects', True)
---> 72     return request('get', url, params=params, **kwargs)
     73 
     74 

/usr/local/lib/python3.5/dist-packages/requests/api.py in request(method, url, **kwargs)
     56     # cases, and look like a memory leak in others.
     57     with sessions.Session() as session:
---> 58         return session.request(method=method, url=url, **kwargs)
     59 
     60 

/usr/local/lib/python3.5/dist-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    516         }
    517         send_kwargs.update(settings)
--> 518         resp = self.send(prep, **send_kwargs)
    519 
    520         return resp

/usr/local/lib/python3.5/dist-packages/requests/sessions.py in send(self, request, **kwargs)
    659 
    660         # Resolve redirects if allowed.
--> 661         history = [resp for resp in gen] if allow_redirects else []
    662 
    663         # Shuffle things around if there's history.

/usr/local/lib/python3.5/dist-packages/requests/sessions.py in <listcomp>(.0)
    659 
    660         # Resolve redirects if allowed.
--> 661         history = [resp for resp in gen] if allow_redirects else []
    662 
    663         # Shuffle things around if there's history.

/usr/local/lib/python3.5/dist-packages/requests/sessions.py in resolve_redirects(self, resp, req, stream, timeout, verify, cert, proxies, **adapter_kwargs)
    212                 proxies=proxies,
    213                 allow_redirects=False,
--> 214                 **adapter_kwargs
    215             )
    216 

/usr/local/lib/python3.5/dist-packages/requests/sessions.py in send(self, request, **kwargs)
    637 
    638         # Send the request
--> 639         r = adapter.send(request, **kwargs)
    640 
    641         # Total elapsed time of the request (approximately)

/usr/local/lib/python3.5/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    510         except (_SSLError, _HTTPError) as e:
    511             if isinstance(e, _SSLError):
--> 512                 raise SSLError(e, request=request)
    513             elif isinstance(e, ReadTimeoutError):
    514                 raise ReadTimeout(e, request=request)

SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)
In [ ]:
usrpat = '/home/{}'.format(myusr)
In [46]:
#import requests
#dls = "http://www.muellerindustries.com/uploads/pdf/UW SPD0114.xls"
#resp = requests.get(dls)
with open('{}/daily.xls'.format(usrpat), 'wb') as output:
    output.write(reqdaily.content)
In [49]:
dailyrd = pandas.read_excel('{}/daily.xls'.format(usrpat), header=1, index_col=0, skiprows=[2,3,4])
In [53]:
tojsn = dailyrd.to_json()
In [55]:
jsz = json.loads(tojsn)
In [41]:
with open('/home/{}/artctrl/exchange.json'.format(myusr), 'w') as exchan:
          exchan.write(str(jsz))

          

Comments

Comments powered by Disqus