-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_and_store_bhavcopy.py
85 lines (71 loc) · 2.38 KB
/
fetch_and_store_bhavcopy.py
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
import datetime
import os
import urllib.request
import zipfile
import redis
import json
def fetch_and_store_bhavcopy():
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
conn = redis.Redis(host='localhost', port=6379, db=0)
today = datetime.datetime.now().date()
yesterday = today - datetime.timedelta(1)
if yesterday.weekday() in [5,6]:
# Weekends holidays
print("Returning because yesterday is a weekend")
return
year = str(yesterday.year)[2:]
month = yesterday.month
if month < 10:
month = "0%s" % month
day = yesterday.day
if day < 10:
day = "0%s" % day
url = "https://www.bseindia.com/download/BhavCopy/Equity/EQ%s%s%s_CSV.ZIP" % (
day, month, year)
# Fetch file
downloaded_zip_files_location = os.path.join(BASE_DIR, "bhavcopy_zip")
if not os.path.isdir(downloaded_zip_files_location):
os.mkdir(downloaded_zip_files_location)
file_name = "EQ%s%s%s_CSV.ZIP" % (day, month, year)
file_url = os.path.join(downloaded_zip_files_location, file_name)
try:
urllib.request.urlretrieve(url, file_url)
except HTTPError:
# File doesnt exist either due to wrong url or holiday
print("Returning because yesterday is either a holiday or wrong url")
return
# Extract file from zip
csv_files_location = os.path.join(BASE_DIR, "bhavcopy_csv")
if not os.path.isdir(csv_files_location):
os.mkdir(csv_files_location)
zip_ref = zipfile.ZipFile(file_url, 'r')
zip_ref.extractall(csv_files_location)
zip_ref.close()
# Read csv file and store
csv_file_url = os.path.join(csv_files_location, "EQ%s%s%s.CSV" % (day, month, year))
csv_file = open(csv_file_url)
csv_file_text = csv_file.read()
csv_file.close()
csv_file_lines = csv_file_text.split("\n")[1:]
data = {}
for row_text in csv_file_lines:
if not row_text:
break
row = row_text.split(",")
code = row[0]
name = row[1]
_open = row[4]
high = row[5]
low = row[6]
close = row[7]
data[code] = {
'code': code,
'name': name,
'open': _open,
'high': high,
'low': low,
'close': close
}
key = "equity-bhavcopy-%s%s%s" % (day, month, year)
conn.set(key, json.dumps(data))
fetch_and_store_bhavcopy()