Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bhav copy feature functions, exceptions & tests
- Loading branch information
Showing
7 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
import io | ||
import csv | ||
import requests | ||
import tempfile | ||
import datetime | ||
from zipfile import ZipFile | ||
from bsedata.exceptions import BhavCopyNotFound | ||
from bsedata.helpers import COMMON_REQUEST_HEADERS | ||
|
||
|
||
def loadBhavCopyData(statsDate: datetime.date) -> list: | ||
tempDir = os.path.join(tempfile.gettempdir(), "bsedata") | ||
zipfileName = f"EQ{statsDate.strftime('%d%m%y')}_CSV.ZIP" | ||
r = requests.get( | ||
f"https://www.bseindia.com/download/BhavCopy/Equity/{zipfileName}", | ||
headers=COMMON_REQUEST_HEADERS, | ||
) | ||
|
||
if r.status_code != 200: | ||
raise BhavCopyNotFound() | ||
|
||
try: | ||
os.makedirs(tempDir) | ||
except FileExistsError: | ||
pass | ||
|
||
f_zip = open(os.path.join(tempDir, zipfileName), "wb+") | ||
f_zip.write(r.content) | ||
f_zip.close() | ||
|
||
output = [] | ||
|
||
with ZipFile(os.path.join(tempDir, zipfileName)) as bhavCopyZip: | ||
with bhavCopyZip.open(f"EQ{statsDate.strftime('%d%m%y')}.CSV") as bhavCopyFile: | ||
reader = csv.DictReader(io.TextIOWrapper(bhavCopyFile)) | ||
for row in reader: | ||
output.append(mapBhavCopyRowToDict(row)) | ||
|
||
return output | ||
|
||
|
||
def mapBhavCopyRowToDict(row: dict) -> dict: | ||
SC_TYPE_MAP = {"B": "bond", "Q": "equity", "D": "debenture", "P": "preference"} | ||
return { | ||
"scrip_code": row["SC_CODE"], | ||
"open": row["OPEN"], | ||
"high": row["HIGH"], | ||
"low": row["LOW"], | ||
"close": row["CLOSE"], | ||
"last": row["LAST"], | ||
"prev_close": row["PREVCLOSE"], | ||
"total_trades": row["NO_TRADES"], | ||
"total_shares_traded": row["NO_OF_SHRS"], | ||
"net_turnover": row["NET_TURNOV"], | ||
"scrip_type": SC_TYPE_MAP[row["SC_TYPE"]], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
COMMON_REQUEST_HEADERS = { | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 Edg/83.0.478.45' | ||
} | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 Edg/83.0.478.45" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters