Permalink
Browse files

Add memcache in goristock.

  • Loading branch information...
toomore committed Nov 24, 2010
1 parent 112e830 commit 7fa001b7ccb7aa398de8c65992e84b4a9f375df5
Showing with 31 additions and 9 deletions.
  1. +4 −1 app.yaml
  2. +22 −5 goristock.py
  3. +5 −3 main.py
View
@@ -1,5 +1,5 @@
application: goristock
version: alpha
version: mem
runtime: python
api_version: 1
@@ -47,6 +47,9 @@ inbound_services:
- xmpp_message
- mail
builtins:
- appstats: on
derived_file_type:
- python_precompiled
View
@@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from google.appengine.api import memcache
from datetime import datetime, timedelta
import urllib2, logging, csv, re
@@ -58,14 +59,14 @@ def __init__(self, stock_no, data_num = 75, debug=0):
try:
while len(self.raw_data) < data_num:
# start fetch data.
self.csv_read = self.fetch_data(stock_no, datetime.today() - timedelta(days = 30 * starttime))
self.csv_read = self.fetch_data(stock_no, datetime.today() - timedelta(days = 30 * starttime), starttime)
try:
result = self.list_data(self.csv_read)
except:
# In first day of months will fetch no data.
if starttime == 0:
starttime += 1
self.csv_read = self.fetch_data(stock_no, datetime.today() - timedelta(days = 30 * starttime))
self.csv_read = self.fetch_data(stock_no, datetime.today() - timedelta(days = 30 * starttime), starttime)
result = self.list_data(self.csv_read)
logging.info('In first day of months %s' % stock_no)
@@ -125,18 +126,34 @@ def goback(self,days = 1):
self.stock_vol.pop()
##### main def #####
def fetch_data(self, stock_no, nowdatetime):
def fetch_data(self, stock_no, nowdatetime, firsttime = 1):
""" Fetch data from twse.com.tw
return list.
"""
url = 'http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/STOCK_DAY_print.php?genpage=genpage/Report%(year)d%(mon)02d/%(year)d%(mon)02d_F3_1_8_%(stock)s.php&type=csv' % {'year': nowdatetime.year, 'mon': nowdatetime.month,'stock': stock_no}
self.debug_print(url)
logging.info(url)
cc = urllib2.urlopen(url)
#print cc.info().headers
csv_read = csv.reader(cc)
# set memcache expire
if firsttime == 0:
expire = 60
else:
expire = 120
## get memcache
stkm = memcache.get('%(stock)s%(year)d%(mon)02d' % {'year': nowdatetime.year, 'mon': nowdatetime.month,'stock': stock_no})
if stkm:
csv_read = csv.reader(stkm)
else:
cc = urllib2.urlopen(url)
cc_read = cc.readlines()
csv_read = csv.reader(cc_read)
memcache.set('%(stock)s%(year)d%(mon)02d' % {'year': nowdatetime.year, 'mon': nowdatetime.month,'stock': stock_no}, cc_read, expire)
return csv_read
def list_data(self, csv_read):
""" Put the data into the 'self.raw_data' and other stock info.
View
@@ -96,10 +96,12 @@ def get(self):
print 'GoRiStock'
print a.raw_data
print a.num_data
print a.stock_no,a.stock_name
print a.MA(5),a.MAC(5),a.MA(20),a.MAC(20),a.MA(60),a.MAC(60)
print a.data_date
#print a.stock_no,a.stock_name
print '%s' % memcache.get_stats()
#print a.MA(5),a.MAC(5),a.MA(20),a.MAC(20),a.MA(60),a.MAC(60)
print '='*40
print a.display(3,6,18)
#print a.display(3,6,18)
############## webapp Models ###################
class xmpp_invite(webapp.RequestHandler):

0 comments on commit 7fa001b

Please sign in to comment.