Skip to content

Commit

Permalink
Merge branch 'v0.5.3rc' of github.com:toomore/grs
Browse files Browse the repository at this point in the history
  • Loading branch information
toomore committed Apr 17, 2014
2 parents f4448e2 + 7b21b9c commit 5b9fdfd
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 24 deletions.
8 changes: 7 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ grs 台灣上市上櫃股票價格擷取
-----------------------------

:Authors: Toomore Chiang
:Version: 0.5.2 of 2014/04/12
:Version: 0.5.3 of 2014/04/17
:Python Version: Python 2.7, PyPy
:Docs: http://grs-docs.toomore.net/

Expand Down Expand Up @@ -302,6 +302,12 @@ Quick Start
Change Logs
-----------------------------

0.5.3 2014/04/17
====================================

- 修正:離線時的錯誤訊息
- 修正:`realtime` str format.

0.5.2 2014/04/12
====================================

Expand Down
8 changes: 8 additions & 0 deletions docs/source/error.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
grs Errors and Exceptions
==========================


Errors and Exceptions
---------------------------
.. automodule:: grs.error
:members:
7 changes: 6 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ grs 台灣上市上櫃股票價格擷取
-----------------------------

:Authors: Toomore Chiang
:Version: 0.5.2 of 2014/04/12
:Version: 0.5.3 of 2014/04/17
:Python Version: Python 2.7, PyPy
:Docs: http://grs-docs.toomore.net/

Expand Down Expand Up @@ -75,11 +75,16 @@ Feature
時間、倒數處理 grs.TWTime/Countdown <tw_time>
盤中即時資訊擷取 grs.RealtimeStock/RealtimeWeight <realtime>
四大買賣點判斷 grs.BestFourPoint <best_buy_or_sell>
其他錯誤訊息處理 grs.error <error>


Change Logs
-----------------------------

* 0.5.3 2014/04/17
- 修正:離線時的錯誤訊息
- 修正:`realtime` str format.

* 0.5.2 2014/04/12
- 修正:字串判斷使用 `basestring`.

Expand Down
2 changes: 1 addition & 1 deletion grs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# THE SOFTWARE.

__title__ = 'grs'
__version__ = '0.5.2'
__version__ = '0.5.3'
__author__ = 'Toomore Chiang'
__license__ = 'MIT'
__copyright__ = 'Copyright (C) 2012, 2013, 2014 Toomore Chiang'
Expand Down
34 changes: 34 additions & 0 deletions grs/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
''' grs Exception '''
# Copyright (c) 2012, 2013, 2014 Toomore Chiang, http://toomore.net/
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


class Error(Exception):
''' Exception base '''


class StockNoError(Error):
""" Exception for stock_no not in TWSE or OTC list. """


class ConnectionError(Error):
""" Exception for no connection. """

16 changes: 8 additions & 8 deletions grs/fetch_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@
import logging
import random
import urllib2
from .error import ConnectionError
from .error import StockNoError
from .twseno import OTCNo
from .twseno import TWSENo
from datetime import datetime
from dateutil.relativedelta import relativedelta


class StockNoError(Exception):
""" Exception for stock_no not in TWSE or OTC list. """
pass


class FetchData(object):
''' FetchData '''
def __init__(self):
Expand Down Expand Up @@ -456,10 +453,13 @@ def __new__(cls, stock_no, mons=3, twse=False, otc=False):
stock_proxy = type('Stock', (OTCFetch, SimpleAnalytics), {})()
twse = False
else:
raise StockNoError
raise StockNoError()

stock_proxy.__init__()
cls.__raw_data = stock_proxy.serial_fetch(stock_no, mons, twse)
stock_proxy._load_data(cls.__raw_data)
try:
cls.__raw_data = stock_proxy.serial_fetch(stock_no, mons, twse)
stock_proxy._load_data(cls.__raw_data)
except urllib2.URLError:
raise ConnectionError(), u'IN OFFLINE, NO DATA FETCH.'

return stock_proxy
32 changes: 19 additions & 13 deletions grs/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import logging
import random
import urllib2
from .error import ConnectionError


def covstr(strings):
Expand All @@ -46,10 +47,13 @@ class RealtimeStock(object):
def __init__(self, no):
assert isinstance(no, basestring), '`no` must be a string'
self.__raw = ''
page = urllib2.urlopen(
'http://mis.tse.com.tw/data/{0}.csv?r={1}'.format(
no, random.randrange(1, 10000))
)
try:
page = urllib2.urlopen(
'http://mis.tse.com.tw/data/%s.csv?r=%s' % (no,
random.randrange(1, 10000)))
except urllib2.URLError:
raise ConnectionError(), u'IN OFFLINE, NO DATA FETCH.'

logging.info('twsk no %s', no)
reader = csv.reader(page)
for i in reader:
Expand Down Expand Up @@ -99,8 +103,8 @@ def real(self):
'time': self.__raw[2], # 取得時間
'max': self.__raw[3], # 漲停價
'min': self.__raw[4], # 跌停價
'unch': '{:.2f}'.format(unch), # 昨日收盤價
'pp': '{:.2f}'.format((covstr(self.__raw[8]) - unch) / unch * 100),
'unch': '%.2f' % unch, # 昨日收盤價
'pp': '%.2f' % ((covstr(self.__raw[8]) - unch) / unch * 100),
# 漲跌幅 %
'o': self.__raw[5], # 開盤價
'h': self.__raw[6], # 當日最高價
Expand Down Expand Up @@ -131,10 +135,8 @@ def real(self):

result['crosspic'] = ("http://chart.apis.google.com/chart?" +
"chf=bg,s,ffffff&chs=20x50&cht=ls" +
"&chd=t1:0,0,0|0,{},0|0,{},0|0,{},0|0,{},0" +
"&chds={},{}&chm=F,,1,1:4,20").format(
result['h'], result['c'], result['o'], result['l'], result['l'],
result['h'])
"&chd=t1:0,0,0|0,%(h)s,0|0,%(c)s,0|0,%(o)s,0|0,%(l)s,0" +
"&chds=%(l)s,%(h)s&chm=F,,1,1:4,20") % result

result['top5buy'].sort()
result['top5sell'].sort()
Expand All @@ -154,9 +156,13 @@ def __init__(self):
代碼可以參考:http://goristock.appspot.com/API#apiweight
"""
self.__raw = {}
page = urllib2.urlopen(
'http://mis.tse.com.tw/data/TSEIndex.csv?r=%s'.format(
random.randrange(1, 10000)))
try:
page = urllib2.urlopen(
'http://mis.tse.com.tw/data/TSEIndex.csv?r=%s' % random.randrange(
1, 10000))
except urllib2.URLError:
raise ConnectionError(), u'IN OFFLINE, NO DATA FETCH.'

reader = csv.reader(page)
for i in reader:
if len(i):
Expand Down

0 comments on commit 5b9fdfd

Please sign in to comment.