From cfa33db624d6ac4ca859a6b979d7ad664730f7c6 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 16 Aug 2016 17:34:53 -0400 Subject: [PATCH] Remove Bloomberg Planning to get data from other locations --- metrik/tasks/bloomberg.py | 70 ------------------------------------ test/tasks/test_bloomberg.py | 39 -------------------- 2 files changed, 109 deletions(-) delete mode 100644 metrik/tasks/bloomberg.py delete mode 100644 test/tasks/test_bloomberg.py diff --git a/metrik/tasks/bloomberg.py b/metrik/tasks/bloomberg.py deleted file mode 100644 index ab901c8..0000000 --- a/metrik/tasks/bloomberg.py +++ /dev/null @@ -1,70 +0,0 @@ -from luigi import Task, Parameter -from pyquery import PyQuery as pq -import logging -# noinspection PyUnresolvedReferences -from six.moves.urllib.parse import quote_plus -# noinspection PyUnresolvedReferences -from six.moves.html_parser import HTMLParser -from six import PY2 - - -class BloombergEquityInfo(Task): - bbg_code = Parameter() - user_agent = Parameter() - - @staticmethod - def retrieve_info(bbg_code, user_agent): - class EquityInfoParser(HTMLParser): - def __init__(self, keys): - if PY2: - HTMLParser.__init__(self) - else: - super(EquityInfoParser, self).__init__() - self.keys = keys - self.records = {k: None for k in keys} - self.do_record = {k: False for k in keys} - - def handle_data(self, data): - stripped = data.strip() - # Ignore blank lines - if not stripped: - return - for k, v in self.do_record.items(): - if v: - self.records[k] = stripped - self.do_record[k] = False - - if stripped in self.keys: - self.do_record[stripped] = True - - def get_records(self): - return self.records - - url = 'http://www.bloomberg.com/quote/{}'.format( - quote_plus(bbg_code)) - logging.info('Visiting "{}" with agent "{}'.format(url, user_agent)) - html = pq(url, {'User-Agent': user_agent}).html() - - keys = ['Sector', 'Industry', 'Sub-Industry'] - eq_info = EquityInfoParser(keys) - eq_info.feed(html) - records = eq_info.get_records() - - return [records[k] for k in keys] - - -class BloombergFXPrice(Task): - bbg_code = Parameter() - user_agent = Parameter() - - @staticmethod - def retrieve_price(bbg_code, user_agent): - url = 'http://www.bloomberg.com/quote/{}'.format( - quote_plus(bbg_code) - ) - logging.info('Visiting "{}" with agent "{}"'.format(url, user_agent)) - html = pq(url, {'User-Agent': user_agent}) - - price = float(html('.price').text()) - logging.info('Found FX price {}: {}'.format(bbg_code, price)) - return price diff --git a/test/tasks/test_bloomberg.py b/test/tasks/test_bloomberg.py deleted file mode 100644 index 30a3b22..0000000 --- a/test/tasks/test_bloomberg.py +++ /dev/null @@ -1,39 +0,0 @@ -from unittest import TestCase - -from metrik.conf import USER_AGENT -from metrik.tasks.bloomberg import BloombergEquityInfo -from metrik.tasks.bloomberg import BloombergFXPrice - - -class BloombergTest(TestCase): - def test_correct_info_apple(self): - sector, industry, sub_industry = \ - BloombergEquityInfo.retrieve_info("AAPL:US", USER_AGENT) - - assert sector == 'Technology' - assert industry == 'Hardware' - assert sub_industry == 'Communications Equipment' - - def test_correct_info_kcg(self): - sector, industry, sub_industry = \ - BloombergEquityInfo.retrieve_info("KCG:US", USER_AGENT) - - assert sector == 'Financials' - assert industry == 'Institutional Financial Svcs' - assert sub_industry == 'Institutional Brokerage' - - def test_fx_triangle_euj(self): - eur_usd = BloombergFXPrice.retrieve_price('EURUSD:CUR', USER_AGENT) - usd_jpy = BloombergFXPrice.retrieve_price('USDJPY:CUR', USER_AGENT) - eur_jpy = BloombergFXPrice.retrieve_price('EURJPY:CUR', USER_AGENT) - - diff = abs(eur_usd * usd_jpy - eur_jpy) - assert diff < .05 - - def test_fx_triangle_ghc(self): - gbp_hkd = BloombergFXPrice.retrieve_price('GBPHKD:CUR', USER_AGENT) - hkd_inr = BloombergFXPrice.retrieve_price('HKDCAD:CUR', USER_AGENT) - gbp_inr = BloombergFXPrice.retrieve_price('GBPCAD:CUR', USER_AGENT) - - diff = abs(gbp_hkd * hkd_inr - gbp_inr) - assert diff < .05 \ No newline at end of file