-
Notifications
You must be signed in to change notification settings - Fork 2
/
YahooTWStock.py
executable file
·68 lines (53 loc) · 1.84 KB
/
YahooTWStock.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from getStockFromYahoo import getStockFromYahoo
# from getStockFromTWSE import getStockFromTWSE
import csv
class YahooTWStock(object):
def __init__(self):
self._id = [] # str
self._price = [] # float
self._name = [] # str
self._size = 0
def addStockId(self, stockId):
self._id.append(stockId) # str
self._price.append(0.0) # float
self._name.append('') # str
self._size += 1
def addStockIdList(self, stockIdList):
for stockId in stockIdList:
self._id.append(stockId) # str
self._price.append(0.0) # float
self._name.append('') # str
self._size += 1
def remove(self, stockId):
# TODO
pass
def refresh(self, i):
self._request(i)
def id(self, i):
return self._id[i]
def price(self, i):
return self._price[i]
def name(self, i):
return self._name[i]
@property
def size(self):
return self._size
def loadFromStockCsv(self, filename):
with open(filename) as csvfile:
# rows = csv.reader(csvfile, delimiter=',')
rows = csv.DictReader(csvfile, delimiter=',')
# get column value
for row in rows:
self.addStockId(row['stock id'])
def saveToStockCsv(self, filename):
with open(filename, 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['stock id', 'stock name', 'price'])
for i in range(self.size):
writer.writerow([self.yahoo.id(i), self.yahoo.name(i), self.yahoo.price(i)])
def _request(self, i):
self._price[i], self._name[i] = getStockFromYahoo(self._id[i])
# backup
# self._price[i], self._name[i] = getStockFromTWSE(self._id[i])