def announcement_feed(): def bjdate(d): from datetime import timedelta return (d + timedelta(hours=8)).strftime('%Y年%m月%d日') type_ = request.args.get('type', '') typecn = type_to_cn(type_) exchange = request.args.get('exchange', '') cond = {} feedtitle = '邮币卡公告聚合' if type_: cond['type_'] = type_ feedtitle += ' - {}'.format(typecn) if exchange: cond['exchange'] = exchange feedtitle += ' - {}'.format(exchange) feed = AtomFeed(feedtitle, feed_url=request.url, url=request.url_root) announcements = list( Announcement.query(cond, sort=[('updated_at', -1)], limit=20)) for a in announcements: feed.add('{} {}'.format(bjdate(a.published_at), a.title.strip()), '更多内容请点击标题连接', content_type='text', author=a.exchange, url=a.url, updated=a.updated_at, published=a.published_at) return feed.get_response()
def announcement(): locals()['type_to_cn'] = type_to_cn nav = 'announcement' tab = 'raw' type_ = request.args.get('type', '') typecn = type_to_cn(type_) exchange = request.args.get('exchange', '') page = int(request.args.get('page', 1) or 1) limit = 50 skip = limit * (page - 1) cond = {} if type_: cond['type_'] = type_ if exchange: cond['exchange'] = exchange total = Announcement.count(cond) pagination = Pagination(page, limit, total) exchanges = list(sorted(list(e.abbr for e in Exchange.query()))) types = ['offer', 'result', 'stock'] announcements = list( Announcement.query(cond, sort=[('updated_at', -1)], skip=skip, limit=limit)) for a in announcements: a.typecn = type_to_cn(a.type_) ex = Exchange.query_one(sort=[('updated_at', -1)]) updated_at = None if not ex else ex.updated_at return render_template('frontend/announcement.html', **locals())
def index(): nav = 'index' exchanges = CONFS for e in exchanges: urls = e['offer']['index'] if isinstance(urls, list): e['offer']['index_url'] = urls[0] else: e['offer']['index_url'] = urls urls = e['result']['index'] if isinstance(urls, list): e['result']['index_url'] = urls[0] else: e['result']['index_url'] = urls announcements = [a for a in Announcement.query( sort=[('published_at', -1)], limit=len(exchanges))] for a in announcements: a.type_ = { 'offer': '申购', 'result': '中签', 'stock': '托管', }.get(a.type_, '托管') return render_template('frontend/index.html', **locals())
def parse(site): rabbrs = {v: k for k, v in ABBRS.items()} abbr = rabbrs[site] parser = importlib.__import__('ybk.parsers.{}'.format(site), fromlist=['Parser']).Parser() log.info('解析交易所 {}'.format(abbr)) num_parsed = 0 num_failed = 0 for a in Announcement.query({'exchange': abbr, 'parsed': {'$ne': True}}): log.info('parsing {}'.format(a.url)) try: for c in parser.parse(a.type_, a.html): c['from_url'] = a.url Collection(c).upsert() a.update({'$set': {'parsed': True}}) num_parsed += 1 except Exception as e: num_failed += 1 if not isinstance(e, NotImplementedError): log.exception('解析错误') continue log.info('解析完毕, {}个成功, {}个失败'.format(num_parsed, num_failed))