def save_stock(self, sid: str): self.stock_data = stock.Stock(sid) if self.strdate == None and self.enddate == None: self.strdate = datetime.now().strftime("%Y-%m-%d") elif self.enddate == None: year = int(self.strdate[0:4]) month = int(self.strdate[5:7]) self.stock_data.fetch(year=year, month=month) else: syear = int(self.strdate[0:4]) smonth = int(self.strdate[5:7]) eyear = int(self.enddate[0:4]) emonth = int(self.enddate[5:7]) self.stock_data.fetch_from_to(syear=syear, smonth=smonth, eyear=eyear, emonth=emonth) try: os.mkdir(self.dbdir) except: pass dbname = '%s/%s.db' % (self.dbdir, sid) conn = sqlite3.connect(dbname, detect_types=sqlite3.PARSE_DECLTYPES) cursor = conn.cursor() # Create table cursor.execute('''CREATE TABLE IF NOT EXISTS stocks (date timestamp, capacity integer, turnover text, open real, high real, low real, close real, change real, transactions integer)''') # Creat unique index cursor.execute( 'CREATE UNIQUE INDEX IF NOT EXISTS date_unique ON stocks (date)') # Insert a row of data for data in self.stock_data.data: cursor.execute( "INSERT OR IGNORE INTO stocks VALUES (?,?,?,?,?,?,?,?,?)", data) # Save (commit) the changes conn.commit() # We can also close the connection if we are done with it. # Just be sure any changes have been committed or they will be lost. conn.close()
def test_proxy(self): proxies_list = [ 'http://128.199.165.29:8888', 'http://128.199.195.200:8080', 'socks5://207.180.233.152:50775', ] self.stk = stock.Stock('6223', initial_fetch=False, proxies_list=proxies_list) responses.add( responses.GET, self.FETCH_URL, json=json.loads(TPEX_6223_TW_201505_RESPONSE), status=200 ) self.stk.fetch(2015, 5) self.assertNotEqual(self.stk.data, []) self.assertEqual(self.stk.fetcher.PROXIES_LIST, proxies_list) self.assertLess(self.stk.fetcher.proxy_counter, len(proxies_list)) self.assertGreaterEqual(self.stk.fetcher.proxy_counter, 0) self.assertEqual(self.stk.sid, '6223')
def test_proxy_raises_proxy_error(self): proxies_list = [ 'http://0.0.0.0:1234', 'http://0.0.0.0:3128', ] self.stk = stock.Stock('6223', initial_fetch=False, proxies_list=proxies_list) responses.add( responses.GET, self.FETCH_URL, body=ProxyError('Unittest Mock ProxyError!!'), status=200 ) self.stk.fetch(2015, 5) self.assertEqual(self.stk.data, []) self.assertEqual(self.stk.fetcher.PROXIES_LIST, proxies_list) self.assertLess(self.stk.fetcher.proxy_counter, len(proxies_list)) self.assertGreaterEqual(self.stk.fetcher.proxy_counter, 0) for data in self.stk.raw_data: self.assertEqual(data, {'aaData': [], 'data': []}) self.assertEqual(self.stk.sid, '6223')
def setUpClass(cls): cls.stk = stock.Stock('6223')
def setUpClass(cls): cls.stk = stock.Stock('2330')
def setUpClass(self): self.stock = stock.Stock('2330') self.stock.fetch(2015, 5) self.legacy = legacy.LegacyBestFourPoint(self.stock) self.ng = analytics.BestFourPoint(self.stock)
def setUpClass(cls): cls.stk = stock.Stock('6223', initial_fetch=False) cls.FETCH_URL = 'http://www.tpex.org.tw/web/stock/aftertrading/daily_trading_info/st43_result.php?d=104/5&stkno=6223'
def test_initial_fetch(self): self.stk = stock.Stock('2330', initial_fetch=True) self.assertNotEqual(self.stk.data, []) for data in self.stk.raw_data: self.assertEqual(data['stat'], 'OK') self.assertEqual(self.stk.sid, '2330')
def setUpClass(cls): cls.FETCH_URL = 'http://www.twse.com.tw/exchangeReport/STOCK_DAY?date=20150501&stockNo=2330' cls.stk = stock.Stock('2330', initial_fetch=False)