-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiexcloud.py
More file actions
95 lines (76 loc) · 2.92 KB
/
Copy pathiexcloud.py
File metadata and controls
95 lines (76 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from abc import ABC, abstractmethod
from audioop import add
from datetime import datetime
from io import BytesIO
import json
import os
import requests
from cache import StockCache
import exceptions as ex
from handler import LogoDataHandler
from PIL import Image
from stock import Stock
class IIEXDataReceiver(ABC):
"""Creates interface for receiving data
"""
@abstractmethod
def fetch(self) -> json:
pass
class IEXStockReceiver(IIEXDataReceiver):
"""
Raises:
ex.VersionNotFound: _description_
ex.NotFound: _description_
Returns:
JSON: returns a batch quote request from IEX Cloud in JSON format
"""
_API_BASE = "https://cloud.iexapis.com"
_SANDBOX_BASE = "https://sandbox.iexapis.com"
_TICKERS: list
_BATCHURL = "stock/market/batch?symbols="
def __init__(self, tickers: list, API_KEY, version="sandbox"):
self._API_KEY = API_KEY
if version not in (["stable", "v1", "beta", "sandbox"]):
raise ex.VersionNotFound(
"Version must be one of following: stable, v1, sandbox or beta"
)
self._version = version
self._TICKERS = tickers
def fetch(self) -> json:
sep_tickers = ",".join([ticker for ticker in self._TICKERS])
batch_request = f"{self._API_BASE}/{self._version}/{self._BATCHURL}{sep_tickers}&types=quote&token={self._API_KEY}"
if self._version == "sandbox":
batch_request = f"{self._SANDBOX_BASE}/stable/{self._BATCHURL}{sep_tickers}&types=quote&token={self._API_KEY}"
try:
print('Receiving quotes')
request = requests.get(batch_request)
request.raise_for_status()
return request.json()
except requests.exceptions.HTTPError:
raise ex.NotFound("Not found")
class IEXLogoReceiver(IIEXDataReceiver):
def __init__(self, tickers: list):
self._TICKERS = tickers
def fetch(self) -> dict:
base_url = "https://storage.googleapis.com/iex/api/logos/"
images = {}
for ticker in self._TICKERS:
try:
print(f'Receiving image for {ticker}.')
request = requests.get(base_url + ticker.upper() + ".png",
stream=True)
print(request.status_code)
request.raise_for_status()
if request.status_code == 200:
image = Image.open(BytesIO(request.content))
images[ticker] = image
except requests.exceptions.HTTPError as e:
print(f"{ticker} logo does not exist:")
continue
print(images)
return images
class IEXDataFactory():
def stock_receiver(self, tickers, API_KEY, version) -> IIEXDataReceiver:
return IEXStockReceiver(tickers, API_KEY, version)
def logo_receiver(self, tickers) -> IIEXDataReceiver:
return IEXLogoReceiver(tickers)