-
Notifications
You must be signed in to change notification settings - Fork 0
/
top.py
executable file
·122 lines (110 loc) · 2.94 KB
/
top.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
#!/usr/bin/python
# coding: utf-8
import rlog
import rutil
import rdate
import sys
import datetime
import time
import os, sys
from rdownload import ogerdownloader
from rcontext import ogercontext
from rdate import ogerdate
from rhtmlwriter import ogerhtmlwriter
from remail import ogeremail
from rtextdump import ogertxtdump
from rutil import ogerutil
import logging
from rlog import ogerlogger
########################################### setup logging primitive
log = ogerlogger('top')
_info = log.info
_debug = log.debug
_warn = log.warning
_err = log.error
_crit = log.critical
def usage():
print '\t./stokbot.py update : update or init db listed in stok.list'
print ''
def init(stock_id):
stock_id = int(stock_id)
d = ogerdownloader(stock_id)
d.mergemonfile2db()
d.gendbfile()
c = ogercontext(stock_id)
c.load()
c.pad()
c.calextends(False)
c.store()
def update(stock_id):
stock_id = int(stock_id)
c = ogercontext(stock_id)
c.load()
c.updatemissing()
def resolveDateArg(s):
if s == 'today':
d = str(ogerdate().today())
else:
d = sys.argv[3]
return d
def update_all():
_info('starting daily update procedure..')
with open('stok.list', 'r') as f:
ids = f.readlines()
f.close()
for i in ids:
u = ogerutil(i)
### TODO, FIXME, this is bad to decide init() by check db file.
### we do immediatedly merge to db when download only one month data....
if u.isDBFileExist():
_info('updating #%d' % int(i))
update(i)
else:
_info('initialize #%d' % int(i))
_info('it may take times to download, please be patient')
_info('%s' % time.strftime('%c'))
init(int(i))
def report_all():
filename = './sample.html'
w = ogerhtmlwriter()
w.open(filename)
w.head()
with open('stok.list', 'r') as f:
ids = f.readlines()
f.close()
for i in ids:
c = ogercontext(int(i))
c.load()
w.prepare(c)
w.write(str(ogerdate().today()), 10)
w.end()
del c
_info('data %s have been written into %s' % (i.strip(), filename))
w.close()
del w
if __name__ == '__main__':
if len(sys.argv) < 2:
usage()
exit(0)
elif sys.argv[1] == 'update':
update_all()
elif sys.argv[1] == 'report':
report_all()
elif sys.argv[1] == 'email':
TO = ['peace.doggie@gmail.com'] #must be a list
e = ogeremail("", "", "", TO)
with open('./sample.html', 'rb') as f:
lines = f.read()
f.close()
e.send(lines)
elif sys.argv[1] == 'dump':
with open('stok.list', 'r') as f:
ids = f.readlines()
f.close()
for i in ids:
c = ogercontext(i)
c.load()
d = ogertxtdump(c)
d.dump(resolveDateArg('today'))
del c
del d