-
Notifications
You must be signed in to change notification settings - Fork 0
/
automaticUpdate.py
52 lines (42 loc) · 1.48 KB
/
automaticUpdate.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 30 00:47:16 2014
@author: Pranav
"""
import urllib2
import time
import datetime
StocksToPull = 'ITC.NS','BHEL.NS','idfc.NS'
def pullData(stock):
try:
print 'currently puling', stock
print str(datetime.datetime.fromtimestamp(time.time()))
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10d/csv'
#alt = 'localhost/ITC.csv'
saveFileLine = stock+'.txt'
try:
readExistingData = open(saveFileLine,'r').read()
splitExisting = readExistingData.split('\n')
mostRecentLine = splitExisting[-2]
lastUnix = mostRecentLine.split(',')[0]
except Exception,e:
print str(e)
lastUnix = 0
saveFile = open(saveFileLine,'a')
SourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = SourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine) == 6:
if splitLine[0] > lastUnix:
if 'values' not in eachLine:
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
saveFile.close()
print 'pulled', stock
print 'sleeping'
time.sleep(5)
except Exception,e:
print "main loop fail "
for eachStock in StocksToPull:
pullData(eachStock);