|
| 1 | +import zlib |
| 2 | +import json |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import time |
| 6 | +from pandas.compat import StringIO |
| 7 | + |
| 8 | +from pandas import DataFrame |
| 9 | +import pandas.compat as compat |
| 10 | +import pandas as pd |
| 11 | +import requests |
| 12 | + |
| 13 | +from pandas_datareader.base import _BaseReader |
| 14 | + |
| 15 | + |
| 16 | +class EnigmaReader(_BaseReader): |
| 17 | + """ |
| 18 | + Collects Enigma data located at the specified datapath and returns a pandas DataFrame. |
| 19 | +
|
| 20 | + Usage (high-level): |
| 21 | + ``` |
| 22 | + import pandas_datareader as pdr |
| 23 | + df = pdr.get_data_enigma('enigma.inspections.restaurants.fl') |
| 24 | +
|
| 25 | + #in the event that ENIGMA_API_KEY does not exist in your env, it can be supplied as the second arg: |
| 26 | + df = prd.get_data_enigma('enigma.inspections.restaurants.fl', 'ARIAMFHKJMISF38UT') |
| 27 | + ``` |
| 28 | +
|
| 29 | + Usage: |
| 30 | + ``` |
| 31 | + df = EnigmaReader(datapath='enigma.inspections.restaurants.fl', api_key='ARIAMFHKJMISF38UT').read() |
| 32 | + ``` |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self, |
| 36 | + datapath=None, |
| 37 | + api_key=None, |
| 38 | + retry_count=5, |
| 39 | + pause=0.250, |
| 40 | + session=None): |
| 41 | + |
| 42 | + super(EnigmaReader, self).__init__(symbols=[], |
| 43 | + retry_count=retry_count, |
| 44 | + pause=pause) |
| 45 | + if api_key == None: |
| 46 | + self._api_key = os.getenv('ENIGMA_API_KEY') |
| 47 | + if self._api_key == None: |
| 48 | + raise ValueError( |
| 49 | + """Please provide an Enigma API key or set the ENIGMA_API_KEY environment variable\n |
| 50 | + If you do not have an API key, you can get one here: https://app.enigma.io/signup""") |
| 51 | + else: |
| 52 | + self._api_key = api_key |
| 53 | + |
| 54 | + self._datapath = datapath |
| 55 | + if not isinstance(self._datapath, compat.string_types): |
| 56 | + raise ValueError( |
| 57 | + "The Enigma datapath must be a string (ex: 'enigma.inspections.restaurants.fl')") |
| 58 | + |
| 59 | + |
| 60 | + @property |
| 61 | + def url(self): |
| 62 | + return 'https://api.enigma.io/v2/export/{}/{}'.format(self._api_key, |
| 63 | + self._datapath) |
| 64 | + |
| 65 | + @property |
| 66 | + def export_key(self): |
| 67 | + return 'export_url' |
| 68 | + |
| 69 | + |
| 70 | + @property |
| 71 | + def _head_key(self): |
| 72 | + return 'head_url' |
| 73 | + |
| 74 | + |
| 75 | + def _request(self, url): |
| 76 | + self.session.headers.update({'User-Agent': 'pandas-datareader'}) |
| 77 | + resp = self.session.get(url) |
| 78 | + resp.raise_for_status() |
| 79 | + return resp |
| 80 | + |
| 81 | + |
| 82 | + def _decompress_export(self, compressed_export_data): |
| 83 | + return zlib.decompress(compressed_export_data, 16 + zlib.MAX_WBITS) |
| 84 | + |
| 85 | + |
| 86 | + def extract_export_url(self, delay=10, max_attempts=10): |
| 87 | + """ |
| 88 | + Performs an HTTP HEAD request on 'head_url' until it returns a `200`. |
| 89 | + This allows the Enigma API time to export the requested data. |
| 90 | + """ |
| 91 | + resp = self._request(self.url) |
| 92 | + attempts = 0 |
| 93 | + while True: |
| 94 | + try: |
| 95 | + requests.head(resp.json()[self._head_key]).raise_for_status() |
| 96 | + except Exception as e: |
| 97 | + attempts += 1 |
| 98 | + if attempts > max_attempts: |
| 99 | + raise e |
| 100 | + time.sleep(delay) |
| 101 | + continue |
| 102 | + return resp.json()[self.export_key] |
| 103 | + |
| 104 | + def read(self): |
| 105 | + export_gzipped_req = self._request(self.extract_export_url()) |
| 106 | + decompressed_data = self._decompress_export(export_gzipped_req.content) |
| 107 | + return pd.read_csv(StringIO(decompressed_data)) |
0 commit comments