-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock.py
70 lines (59 loc) · 2.29 KB
/
stock.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
from nsetools import Nse
import csv
import sys
import os
import pathlib
import datetime
import requests
from bs4 import BeautifulSoup
fname = pathlib.Path('stockData.csv')
mtime = datetime.datetime.fromtimestamp(fname.stat().st_mtime).date()
today = datetime.date.today()
if today != mtime:
nse = Nse()
def listToString(value):
name = ""
return (name.join(value))
print("Fetching Data...")
stocks = []
with open('stocks.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
stocks.append(row)
listSize = len(stocks)
counter = 0
with open('stockDataStore.csv', 'w', newline='') as file:
writer = csv.writer(file)
"""header = ['Ticker', 'Price']
writer.writerow(header)"""
for entry in stocks:
counter += 1
print(entry,counter,"/",listSize)
if entry == ['NIFTYBEES']:
URL = 'https://www.tickertape.in/etfs/nippon-india-nifty-50-bees-etf-NBES'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
price_div = soup.find(class_='current-price')
price = price_div.get_text()
outPut = ['NIFTYBEES',float(price)]
elif entry == ['ICICIB22']:
ticker = nse.get_index_quote('NIFTY 50')
outPut = ['ICICIB22',float(ticker['lastPrice']/377.87)]
elif entry == ['SETFNIF50']:
URL = 'https://www.tickertape.in/etfs/sbi-nifty-50-etf-SBFP'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
price_div = soup.find(class_='current-price')
price = price_div.get_text()
outPut = ['SETFNIF50',float(price)]
elif entry == ['INDINFO']:
outPut = ['INDINFO',1.46]
else:
ticker = nse.get_quote(listToString(entry))
outPut = [ticker['symbol'],ticker['lastPrice']]
writer.writerow(outPut)
with open('stockDataStore.csv', 'r', newline='') as file, open('stockData.csv', 'w', newline='') as data:
for ticker in file:
data.write(ticker)
print("Writing Complete!!")
os.remove("stockDataStore.csv")