/
Getdata.py
171 lines (137 loc) · 7.06 KB
/
Getdata.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# GetData.py
# http://sites.google.com/site/pocketsense/
# retrieve statements, stock and fund data
# Intial version: rlc: Feb-2010
# Revisions
# ---------
# 11-Mar-2010*rlc
# - Added "interactive" mode
# - Download all statements and quotes before beginning upload to Money
# - Allow stock quotes to be sent to Money before statements (option defined in sites.dat)
# 09-May-2010*rlc
# - Download files in the order that they will be sent to Money so that file timestamps are in the same order
# - Send data to Money using the os.system() call rather than os.startfile(), as this seems
# to help force the order when sending files to Money (FIFO)
# - Added logic to catch failed connections and server timeouts
# - Added "About" title and version to start
# 05-Sep-2010*rlc
# - Updated to support spaces in SiteName values in sites.dat
# - Don't auto-close command window if any error is detected during download operations
# 04-Jan-2011*rlc
# - Display quotes.htm after download if "ShowQuoteHTM: Yes" defined in sites.dat
# - Ask to display quotes.htm after download if "ShowQuoteHTM: Yes" defined in sites.dat (overrides ShowQuoteHTM)
# 18-Jan-2011*rlc
# - Added 0.5 s delay between "file starts", which sends an OFX file to Money
# 23Aug2012*rlc
# - Added user option to change default download interval at runtime
# - Added support for combineOFX
# 28Aug2013*rlc
# - Added support for forceQuotes option
# 21Oct2013*rlc
# - Modified forceQuote option to prompt for statement accept in Money before continuing
# 25Feb2014*rlc
# - Bug fix for forceQuote option when the quote feature isn't being used
import os, sys, glob, time
import ofx, quotes, site_cfg
from control2 import *
from rlib1 import *
if __name__=="__main__":
stat1 = True #overall status flag across all operations (true == no errors getting data)
print AboutTitle + ", Ver: " + AboutVersion + "\n"
if Debug: print "***Running in DEBUG mode. See Control2.py to disable***\n"
doit = raw_input("Download transactions? (Y/N/I=Interactive) [Y] ").upper()
if len(doit) > 1: doit = doit[:1] #keep first letter
if doit == '': doit = 'Y'
if doit in "YI":
userdat = site_cfg.site_cfg()
#get download interval, if promptInterval=Yes in sites.dat
interval = userdat.defaultInterval
if userdat.promptInterval:
try:
p = int2(raw_input("Download interval (days) [" + str(interval) + "]: "))
if p>0: interval = p
except:
print "Invalid entry. Using defaultInterval=" + str(interval)
#get account info
#AcctArray = [['SiteName', 'Account#', 'AcctType', 'UserName', 'PassWord'], ...]
pwkey, getquotes, AcctArray = get_cfg()
ofxList = []
quoteFile1, quoteFile2, htmFileName = '','',''
if len(AcctArray) > 0 and pwkey <> '':
#if accounts are encrypted... decrypt them
pwkey=decrypt_pw(pwkey)
AcctArray = acctDecrypt(AcctArray, pwkey)
#delete old data files
ofxfiles = xfrdir+'*.ofx'
if glob.glob(ofxfiles) <> []:
os.system("del "+ofxfiles)
print "Download interval= {0} days".format(interval)
#create process Queue in the right order
Queue = ['Accts']
if userdat.savetickersfirst:
Queue.insert(0,'Quotes')
else:
Queue.append('Quotes')
for QEntry in Queue:
if QEntry == 'Accts':
if len(AcctArray) == 0:
print "No accounts have been configured. Run SETUP.PY to add accounts"
#process accounts
for acct in AcctArray:
status, ofxFile = ofx.getOFX(acct, interval)
#status == False if ofxFile doesn't exist
stat1 = stat1 and status
if status:
ofxList.append([acct[0], acct[1], ofxFile])
print ""
#get stock/fund quotes
if QEntry == 'Quotes' and getquotes:
status, quoteFile1, quoteFile2, htmFileName = quotes.getQuotes()
z = ['Stock/Fund Quotes','',quoteFile1]
stat1 = stat1 and status
if glob.glob(quoteFile1) <> []:
ofxList.append(z)
print ""
# display the HTML file after download if requested to always do so
if status and userdat.showquotehtm: os.startfile(htmFileName)
if len(ofxList) > 0:
print '\nFinished downloading data\n'
verify = False
gogo = 'Y'
if userdat.combineofx and gogo <> 'V':
cfile=combineOfx(ofxList) #create combined file
if doit == 'I' or Debug:
gogo = raw_input('Upload online data to Money? (Y/N/V=Verify) [Y] ').upper()
if len(gogo) > 1: gogo = gogo[:1] #keep first letter
if gogo == '': gogo = 'Y'
if gogo in 'YV':
if glob.glob(quoteFile2) <> []:
if Debug: print "Importing ForceQuotes statement: " + quoteFile2
runFile(quoteFile2) #force transactions for MoneyUK
raw_input('ForceQuote statement loaded. Accept in Money and press <Enter> to continue.')
print '\nSending statement(s) to Money...'
if userdat.combineofx and cfile and gogo <> 'V':
runFile(cfile)
else:
for file in ofxList:
upload = True
if gogo == 'V':
#file[0] = site, file[1] = accnt#, file[2] = ofxFile
upload = (raw_input('Upload ' + file[0] + ' : ' + file[1] + ' (Y/N) ').upper() == 'Y')
if upload:
if Debug: print "Importing " + file[2]
runFile(file[2])
time.sleep(0.5) #slight delay, to force load order in Money
#ask to show quotes.htm if defined in sites.dat
if userdat.askquotehtm:
ask = raw_input('Open <Quotes.htm> in the default browser (y/n)?').upper()
if ask=='Y': os.startfile(htmFileName) #don't wait for browser close
else:
if len(AcctArray)>0 or (getquotes and len(userdat.stocks)>0):
print "\nNo files were downloaded. Verify network connection and try again later."
raw_input("Press <Enter> to continue...")
if Debug:
raw_input("DEBUG END: Press <Enter> to continue...")
elif not stat1:
print "\nOne or more accounts (or quotes) may not have downloaded correctly."
raw_input("Review and press <Enter> to continue...")