Skip to content

Commit

Permalink
Add update_codes and script to cli (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlouielu committed Oct 14, 2018
1 parent 9542a13 commit 43cc049
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions twstock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from twstock import realtime

from twstock.analytics import BestFourPoint
from twstock.codes import twse, tpex, codes
from twstock.codes import __update_codes, twse, tpex, codes
from twstock.stock import Stock


__version__ = '1.1.1'
__version__ = '1.1.2'
6 changes: 6 additions & 0 deletions twstock/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import argparse
from twstock.codes import __update_codes
from twstock.cli import best_four_point
from twstock.cli import stock
from twstock.cli import realtime
Expand All @@ -12,6 +13,7 @@ def run():
parser.add_argument('-b', '--bfp', nargs='+')
parser.add_argument('-s', '--stock', nargs='+')
parser.add_argument('-r', '--realtime', nargs='+')
parser.add_argument('-U', '--upgrade-codes', action='store_true', help='Update entites codes')
args = parser.parse_args()

if args.bfp:
Expand All @@ -20,3 +22,7 @@ def run():
stock.run(args.stock)
elif args.realtime:
realtime.run(args.realtime)
elif args.upgrade_codes:
print('Start to update codes')
__update_codes()
print('Done!')
2 changes: 2 additions & 0 deletions twstock/codes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .fetch import __update_codes
from .codes import codes, tpex, twse
4 changes: 2 additions & 2 deletions twstock/codes.py → twstock/codes/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
ROW = namedtuple('StockCodeInfo', ['type', 'code', 'name', 'ISIN', 'start',
'market', 'group', 'CFI'])
PACKAGE_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
TPEX_EQUITIES_CSV_PATH = os.path.join(PACKAGE_DIRECTORY, 'codes', 'tpex_equities.csv')
TWSE_EQUITIES_CSV_PATH = os.path.join(PACKAGE_DIRECTORY, 'codes', 'twse_equities.csv')
TPEX_EQUITIES_CSV_PATH = os.path.join(PACKAGE_DIRECTORY, 'tpex_equities.csv')
TWSE_EQUITIES_CSV_PATH = os.path.join(PACKAGE_DIRECTORY, 'twse_equities.csv')

codes = {}
tpex = {}
Expand Down
8 changes: 8 additions & 0 deletions twstock/codes/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# TPEx equities = 上櫃證券
#

import os
import csv
from collections import namedtuple

Expand Down Expand Up @@ -52,6 +53,13 @@ def to_csv(url, path):
writer.writerow([_ for _ in d])


def __update_codes():
def get_directory():
return os.path.dirname(os.path.abspath(__file__))
to_csv(TWSE_EQUITIES_URL, os.path.join(get_directory(), 'twse_equities.csv'))
to_csv(TPEX_EQUITIES_URL, os.path.join(get_directory(), 'tpex_equities.csv'))


if __name__ == '__main__':
to_csv(TWSE_EQUITIES_URL, 'twse_equities.csv')
to_csv(TPEX_EQUITIES_URL, 'tpex_equities.csv')

0 comments on commit 43cc049

Please sign in to comment.