#-*- coding: UTF- 8 -*-
'' '
Created on 2015 年 3 月 2 日
@author : Casey
'' '
import time
import threading
'' '
上證編碼: '600001' .. '602100'
深圳編碼: '000001' .. '001999'
中小板: '002001' .. '002999'
創(chuàng)業(yè)板: '300001' .. '300400'
'' '
import urllib2
from datetime import date
from db import *
from setting import *
class StockTencent(object):
#數(shù)據(jù)庫表
__stockTables = { 'cash' : 'stock_cash_tencent' , 'quotation' : 'stock_quotation_tencent' }
'' '初始化' ''
def __init__(self):
self.__logger = LoggerFactory.getLogger( 'StockTencent' )
self.__dbOperator = DBOperator()
def main(self):
self.__dbOperator.connDB()
threading.Thread(target = self.getStockCash()).start()
threading.Thread(target = self.getStockQuotation()).start()
self.__dbOperator.closeDB()
'' '查找指定日期股票流量' ''
def __isStockExitsInDate(self, table, stock, date):
sql = "select * from " + table + " where code = '%s' and date='%s'" % (stock, date)
n = self.__dbOperator.execute(sql)
if n >= 1 :
return True
'' '獲取股票資金流明細(xì)' ''
def __getStockCashDetail(self, dataUrl):
#讀取數(shù)據(jù)
tempData = self.__getDataFromUrl(dataUrl)
if tempData == None:
time.sleep( 10 )
tempData = self.__getDataFromUrl(dataUrl)
return False
#解析資金流向數(shù)據(jù)
stockCash = {}
stockInfo = tempData.split( '~' )
if len(stockInfo) < 13 : return
if len(stockInfo) != 0 and stockInfo[ 0 ].find( 'pv_none' ) == - 1 :
table = self.__stockTables[ 'cash' ]
code = stockInfo[ 0 ].split( '=' )[ 1 ][ 2 :]
date = stockInfo[ 13 ]
if not self.__isStockExitsInDate(table, code, date):
stockCash[ 'code' ] = stockInfo[ 0 ].split( '=' )[ 1 ][ 2 :]
stockCash[ 'main_in_cash' ] = stockInfo[ 1 ]
stockCash[ 'main_out_cash' ] = stockInfo[ 2 ]
stockCash[ 'main_net_cash' ] = stockInfo[ 3 ]
stockCash[ 'main_net_rate' ] = stockInfo[ 4 ]
stockCash[ 'private_in_cash' ] = stockInfo[ 5 ]
stockCash[ 'private_out_cash' ] = stockInfo[ 6 ]
stockCash[ 'private_net_cash' ] = stockInfo[ 7 ]
stockCash[ 'private_net_rate' ] = stockInfo[ 8 ]
stockCash[ 'total_cash' ] = stockInfo[ 9 ]
stockCash[ 'name' ] = stockInfo[ 12 ].decode( 'utf8' )
stockCash[ 'date' ] = stockInfo[ 13 ]
#插入數(shù)據(jù)庫
self.__dbOperator.insertIntoDB(table, stockCash)
'' '獲取股票交易信息明細(xì)' ''
def getStockQuotationDetail(self, dataUrl):
tempData = self.__getDataFromUrl(dataUrl)
if tempData == None:
time.sleep( 10 )
tempData = self.__getDataFromUrl(dataUrl)
return False
stockQuotation = {}
stockInfo = tempData.split( '~' )
if len(stockInfo) < 45 : return
if len(stockInfo) != 0 and stockInfo[ 0 ].find( 'pv_none' ) ==- 1 and stockInfo[ 3 ].find( '0.00' ) == - 1 :
table = self.__stockTables[ 'quotation' ]
code = stockInfo[ 2 ]
date = stockInfo[ 30 ]
if not self.__isStockExitsInDate(table, code, date):
stockQuotation[ 'code' ] = stockInfo[ 2 ]
stockQuotation[ 'name' ] = stockInfo[ 1 ].decode( 'utf8' )
stockQuotation[ 'price' ] = stockInfo[ 3 ]
stockQuotation[ 'yesterday_close' ] = stockInfo[ 4 ]
stockQuotation[ 'today_open' ] = stockInfo[ 5 ]
stockQuotation[ 'volume' ] = stockInfo[ 6 ]
stockQuotation[ 'outer_sell' ] = stockInfo[ 7 ]
stockQuotation[ 'inner_buy' ] = stockInfo[ 8 ]
stockQuotation[ 'buy_one' ] = stockInfo[ 9 ]
stockQuotation[ 'buy_one_volume' ] = stockInfo[ 10 ]
stockQuotation[ 'buy_two' ] = stockInfo[ 11 ]
stockQuotation[ 'buy_two_volume' ] = stockInfo[ 12 ]
stockQuotation[ 'buy_three' ] = stockInfo[ 13 ]
stockQuotation[ 'buy_three_volume' ] = stockInfo[ 14 ]
stockQuotation[ 'buy_four' ] = stockInfo[ 15 ]
stockQuotation[ 'buy_four_volume' ] = stockInfo[ 16 ]
stockQuotation[ 'buy_five' ] = stockInfo[ 17 ]
stockQuotation[ 'buy_five_volume' ] = stockInfo[ 18 ]
stockQuotation[ 'sell_one' ] = stockInfo[ 19 ]
stockQuotation[ 'sell_one_volume' ] = stockInfo[ 20 ]
stockQuotation[ 'sell_two' ] = stockInfo[ 22 ]
stockQuotation[ 'sell_two_volume' ] = stockInfo[ 22 ]
stockQuotation[ 'sell_three' ] = stockInfo[ 23 ]
stockQuotation[ 'sell_three_volume' ] = stockInfo[ 24 ]
stockQuotation[ 'sell_four' ] = stockInfo[ 25 ]
stockQuotation[ 'sell_four_volume' ] = stockInfo[ 26 ]
stockQuotation[ 'sell_five' ] = stockInfo[ 27 ]
stockQuotation[ 'sell_five_volume' ] = stockInfo[ 28 ]
stockQuotation[ 'datetime' ] = stockInfo[ 30 ]
stockQuotation[ 'updown' ] = stockInfo[ 31 ]
stockQuotation[ 'updown_rate' ] = stockInfo[ 32 ]
stockQuotation[ 'heighest_price' ] = stockInfo[ 33 ]
stockQuotation[ 'lowest_price' ] = stockInfo[ 34 ]
stockQuotation[ 'volume_amout' ] = stockInfo[ 35 ].split( '/' )[ 2 ]
stockQuotation[ 'turnover_rate' ] = stockInfo[ 38 ]
stockQuotation[ 'pe_rate' ] = stockInfo[ 39 ]
stockQuotation[ 'viberation_rate' ] = stockInfo[ 42 ]
stockQuotation[ 'circulated_stock' ] = stockInfo[ 43 ]
stockQuotation[ 'total_stock' ] = stockInfo[ 44 ]
stockQuotation[ 'pb_rate' ] = stockInfo[ 45 ]
self.__dbOperator.insertIntoDB(table, stockQuotation)
'' '讀取信息' ''
def __getDataFromUrl(self, dataUrl):
r = urllib2.Request(dataUrl)
try :
stdout = urllib2.urlopen(r, data=None, timeout= 3 )
except Exception,e:
self.__logger.error( ">>>>>> Exception: " +str(e))
return None
stdoutInfo = stdout.read().decode(params.codingtype).encode( 'utf-8' )
tempData = stdoutInfo.replace( '"' , '' )
self.__logger.debug(tempData)
return tempData
'' '獲取股票現(xiàn)金流量' ''
def getStockCash(self):
self.__logger.debug( "開始:收集股票現(xiàn)金流信息" )
try :
#滬市股票
for code in range( 600001 , 602100 ):
self.__getStockCashDetail(dataUrl)
#深市股票
for code in range( 1 , 1999 ):
self.__getStockCashDetail(dataUrl)
#中小板股票
for code in range( 2001 , 2999 ):
self.__getStockCashDetail(dataUrl)
# '300001' .. '300400'
#創(chuàng)業(yè)板股票
for code in range( 300001 , 300400 ):
self.__getStockCashDetail(dataUrl)
except Exception as err:
self.__logger.error( ">>>>>> Exception: " +str(code) + " " + str(err))
finally :
None
self.__logger.debug( "結(jié)束:股票現(xiàn)金流收集" )
'' '獲取股票交易行情數(shù)據(jù)' ''
def getStockQuotation(self):
self.__logger.debug( "開始:收集股票交易行情數(shù)據(jù)" )
try :
#滬市股票
for code in range( 600001 , 602100 ):
self.getStockQuotationDetail(dataUrl)
#深市股票
for code in range( 1 , 1999 ):
self.getStockQuotationDetail(dataUrl)
#中小板股票
for code in range( 2001 , 2999 ):
self.getStockQuotationDetail(dataUrl)
# '300001' .. '300400'
# 創(chuàng)業(yè)板股票
for code in range( 300001 , 300400 ):
self.getStockQuotationDetail(dataUrl)
except Exception as err:
self.__logger.error( ">>>>>> Exception: " +str(code) + " " + str(err))
finally :
None
self.__logger.debug( "結(jié)束:收集股票交易行情數(shù)據(jù)" )
if __name__ == '__main__' :
StockTencent(). main()
|