forked from prstcsnpr/qmagicformula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_index.py
119 lines (96 loc) · 4.24 KB
/
stock_index.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
# -*- coding: utf-8 -*-
import datetime
import logging
from google.appengine.api.labs import taskqueue
from google.appengine.api import mail
from google.appengine.ext import db
from google.appengine.api import urlfetch
from google.appengine.api.urlfetch_errors import DownloadError
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class StockIndex(db.Model):
ss_000001 = db.StringProperty(indexed=False)
sz_399001 = db.StringProperty(indexed=False)
ss_000300 = db.StringProperty(indexed=False)
sz_399300 = db.StringProperty(indexed=False)
sz_399005 = db.StringProperty(indexed=False)
sz_399006 = db.StringProperty(indexed=False)
index_date = db.DateProperty(indexed=False)
def get():
entry = StockIndex.get_or_insert('stock_index')
return entry
def put(entry):
entry.put()
class ShowStockIndexHandler(webapp.RequestHandler):
def get(self):
try:
#entry = get()
#if entry.index_date == datetime.date.today():
taskqueue.add(url='/tasks/magicformula',
queue_name='formula',
method='GET')
taskqueue.add(url='/tasks/grahamformula',
queue_name='formula',
method='GET')
taskqueue.add(url='/tasks/netcurrentassetapproach',
queue_name='formula',
method='GET')
except Exception as e:
logging.exception(e)
taskqueue.add(url='/tasks/showstockindex',
queue_name='showstockindex',
method='GET')
class UpdateStockIndexHandler(webapp.RequestHandler):
def __get_stock_index(self, symbol):
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=l1' % (symbol)
result = urlfetch.fetch(url=url)
if result.status_code == 200:
return result.content.strip()
def __get_stock_indexes(self):
entry = StockIndex(key_name='stock_index')
entry.ss_000001 = self.__get_stock_index('000001.ss')
entry.sz_399001 = self.__get_stock_index('399001.sz')
entry.ss_000300 = self.__get_stock_index('000300.ss')
entry.sz_399300 = self.__get_stock_index('399300.sz')
entry.sz_399005 = self.__get_stock_index('399005.sz')
entry.sz_399006 = self.__get_stock_index('399006.sz')
entry.index_date = datetime.date.today()
return entry
def __equal(self, old_index, new_index):
if (old_index.ss_000001 == new_index.ss_000001
and old_index.sz_399001 == new_index.sz_399001
and old_index.ss_000300 == new_index.ss_000300
and old_index.sz_399300 == new_index.sz_399300
and old_index.sz_399005 == new_index.sz_399005
and old_index.sz_399006 == new_index.sz_399006):
return True
else:
return False
def get(self):
try:
entry_old = get()
entry_new = self.__get_stock_indexes()
if not self.__equal(entry_old, entry_new):
logging.info('It is on the exchange today')
put(entry_new)
taskqueue.add(url='/tasks/updatestockinfo',
queue_name='updatestockinfo',
method='GET')
else:
logging.info('It is not on the exchange today')
mail.send_mail(sender="prstcsnpr@gmail.com",
to="prstcsnpr@gmail.com",
subject="神奇公式",
body='It is not on the exchange today')
except Exception as e:
logging.exception(e)
taskqueue.add(url='/tasks/updatestockindex',
queue_name='updatestockindex',
method='GET')
application = webapp.WSGIApplication([('/tasks/updatestockindex', UpdateStockIndexHandler),
('/tasks/showstockindex', ShowStockIndexHandler)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()