class StockRealTime: TOKEN = '8d4afb5c6c3540a66845f8f54a6f7aa1' def __init__(self): self.retry_codes = [] self.loader = MysqlLoaderInsert('history') @classmethod def format(cls, code): if code.startswith('6'): code = 'sh%s' % code else: code = 'sz%s' % code return code def fetch(self, code): try: url = 'http://a.apix.cn/apixmoney/stockdata/stock?stockid={code}'.format(code=self.format(code)) headers = { 'accept': "application/json", 'content-type': "application/json", 'apix-key': self.TOKEN } req = urllib2.Request(url, headers=headers) res = json.loads(urllib2.urlopen(req).read()) stock = res['data']['stockinfo'] return map(lambda x: "'%s'" % x, [code, stock['date'], stock['OpenningPrice'], stock['hPrice'], stock['currentPrice'], stock['lPrice'], stock['totalNumber'], stock['turnover']]) except: pass def update(self, code): values = self.fetch(code) if values: rows = ["(%s)" % ','.join(values)] self.loader.load(','.join(rows)) print code def general(self): for code in [obj.code for obj in get_session().query(Code)]: if not self.update(code): self.retry_codes.append(code) def retry(self): for code in self.retry_codes: if self.update(code): self.retry_codes.remove(code) else: print 'Not data: %s' % code def main(self): self.general() self.retry()
def load(cls): codes = set() page = 1 loader = MysqlLoaderInsert(table_name='code') while 1: part_codes = re.findall(CODE_CONF['pattern'], urllib2.urlopen(CODE_CONF['url'].format(page=page)).read()) if not part_codes: break if CODE_CONF['is_300']: codes.update(part_codes) else: codes.update([code for code in part_codes if not code.startswith('300')]) print page, len(part_codes), len(codes) page += 1 values = ','.join(["('%s')" % code for code in set(codes)]) loader.load(values) return codes
def __init__(self): self.retry_codes = [] self.loader = MysqlLoaderInsert('history')