def find_more(): """ 首页分类发现更多 """ params = request.args.get('params', HOT_TYPE) page_no = int(request.args.get("page_no", 1)) num = int(request.args.get("num", 20)) sex = request.args.get('sex', 1, int) key = 'find_more_%s_%s_%s_%s' % (params, page_no, num, sex) redis_data = redis_utils.get_cache(key, refresh_expires=False) if redis_data: return json.dumps({'code': 0, 'data': json.loads(redis_data)}) book_shelfs = BookShelf.query.order_by(BookShelf.ranking.desc()).filter( BookShelf.name == params, BookShelf.showed == True, or_(BookShelf.sex == sex, BookShelf.sex == 0))[(page_no - 1) * num:page_no * num] #pagination = BookShelf.query.filter_by(name=params).paginate(page_no, per_page=num, error_out=False) #book_shelfs = pagination.items book_list = [] for book in book_shelfs: b = Book.query.filter_by(book_id=book.book_id).first() book_dict = b.to_dict() category = BookCategory.query.filter_by(cate_id=b.cate_id).first() book_dict['cate_name'] = category.cate_name book_list.append(book_dict) data = {"book_list": book_list, "page_no": page_no, "num": num, "sex": sex} redis_utils.set_cache(key, json.dumps(data), 3600) return json.dumps({'code': 0, 'data': data})
def get_type(): ''' 获取类型列表 ''' platform = request.args.get('platform', 'android') v = request.args.get('v') m_id = request.args.get('m_id', 0, int) key = 'get_type_%s' % platform redis_data = redis_utils.get_cache(key, refresh_expires=False) if redis_data: json.dumps({'code': 0, 'data': json.loads(redis_data)}) parent_list = {1: u'男生', 2: u'出版', 3: u'女生'} type_list = {} for parent in parent_list: category_list = [] book_category = BookCategory.query.filter_by(parent_id=parent, showed=True).all() for c in book_category: if v == '1.0.0' and int(c.cate_id) in [3014, 2011] and not m_id: pass else: category_list.append(c.to_dict()) type_list[parent] = category_list data = {'parent_list': parent_list, 'type_list': type_list} redis_utils.set_cache(key, json.dumps(data), 600) return json.dumps({'code': 0, 'data': data})
def get_free_book(): """ 获取免费书籍 """ sex = request.args.get("sex", 1, int) page_no = request.args.get("page_no", 1, int) num = request.args.get("num", 20, int) platform = request.args.get("platform", "android") mian_banner = 'http://ov2eyt2uw.bkt.clouddn.com/mianfei.jpg' if platform == 'android': mian_banner = 'http://ov2eyt2uw.bkt.clouddn.com/free_banner_1023.jpg' key = 'get_free_book_%s_%s_%s' % (page_no, num, sex) redis_data = redis_utils.get_cache(key, refresh_expires=False) if redis_data: return json.dumps({'code': 0, 'data': json.loads(redis_data)}) pagination = Book.query.filter_by(showed=True, free_collect=True, channel_type=sex).paginate( page_no, per_page=num, error_out=False) books = pagination.items book_list = [] for book in books: book_dict = book.to_dict() category = BookCategory.query.filter_by(cate_id=book.cate_id).first() book_dict['cate_name'] = category.cate_name book_list.append(book_dict) data = {'mian_banner': mian_banner, 'book_list': book_list} redis_utils.set_cache(key, json.dumps(data), 300) return json.dumps({'code': 0, 'data': data})
def update_act_times(key, action='default'): times = redis_utils.get_cache(key, refresh_expires=False) if not times: times = 0 times = int(times) if action == 'reset_success': if times > 0: times = 2 else: times += 1 redis_utils.set_cache(key, times, 1800)
def get_book_first_pay_chapter_id(book_id): ''' 返回开始付费章节ID ''' key = "book_first_pay_chapter_id_%s" % book_id redis_data = redis_utils.get_cache(key, refresh_expires=False) if redis_data: return redis_data chapter = BookChapters.query.order_by( BookChapters.chapter_id.asc()).filter( BookChapters.book_id == book_id)[PAY_NUM:PAY_NUM + 1] if not chapter: return 0 pay_num = int(chapter[0].chapter_id) redis_utils.set_cache(key, pay_num, 86400) return pay_num
def book_associate(): ''' 书籍查询联想 ''' params = request.args.get('params', '') if not params: return json.dumps({'code': -1, 'msg': u'请输入搜索条件'}) book_name_list = [] author_name_list = [] key = 'book_associate_%s' % params if not is_sup_free_book(): key = 'nofreebook_associate_%s' % params redis_data = redis_utils.get_cache(key, refresh_expires=False) if redis_data: return json.dumps({'code': 0, 'data': json.loads(redis_data)}) if is_sup_free_book(): book_names = Book.query.filter(Book.book_name.like('%' + params + '%'), Book.showed == True).limit(5) author_names = Book.query.filter( Book.author_name.like('%' + params + '%'), Book.showed == True).limit(5) else: book_names = Book.query.filter(Book.book_name.like('%' + params + '%'), Book.showed == True, Book.free_collect == 0).limit(5) author_names = Book.query.filter( Book.author_name.like('%' + params + '%'), Book.showed == True, Book.free_collect == 0).limit(5) for book_name in book_names: names = { 'book_name': book_name.book_name, 'book_id': book_name.book_id, } book_name_list.append(names) for author_name in author_names: names = { 'author_name': author_name.author_name, 'book_id': author_name.book_id, } author_name_list.append(names) data = { 'book_name_list': book_name_list, 'author_name_list': author_name_list } redis_utils.set_cache(key, json.dumps(data), 86400) return json.dumps({'code': 0, 'data': data})
def pull_book(): """拉取最新书籍信息""" book_list = get_book_list() a_num = 0 finish_list = [] key = 'error_pull_book_anzhi' redis_utils.set_cache(key, json.dumps([]), 86400) #key = 'pull_book_anzhi' #redis_data = redis_utils.get_cache(key, refresh_expires=False) #redis_data = '' #if redis_data: # redis_book_list = json.loads(redis_data) # for i, book in enumerate(redis_book_list): # update_volume_chapter(book['loca_book_id'], book['book_id'], book['max_cid'], i) # update_book(book) #else: # 取本地书籍对应最新章节id 卷id 更新时间 for book in book_list: print book['book_name'] print '--------------------------------' my_book = Book.query.filter_by(channel_book_id=gen_channel_bind_id(book['book_id'])).first() channel_book_id = gen_channel_bind_id(book['book_id']) book['channel_book_id'] = channel_book_id if my_book: book_m = my_book ch = BookChapters.query.order_by(BookChapters.chapter_id.desc()).filter(BookChapters.book_id==book_m.book_id).first() if ch: code = update_volume_chapter(book_m.book_id, book['book_id'], ch.chapter_id) else: code = update_volume_chapter(book_m.book_id, book['book_id'], 0) if code: update_book(book) else: book_m = Book(book) db.session.add(book_m) db.session.commit() update_volume_chapter(book_m.book_id, book['book_id'], 0) if book['status'] == 2: finish_list.append({'book_id':book['book_id'], 'book_name':book['book_name']}) db.session.commit() redis_data = redis_utils.get_cache(key, refresh_expires=False) if redis_data: print 'error data----------------------'+ redis_data print 'lianzaizhong++++++++++++'+ str(finish_list)
def update_volume_chapter(loca_book_id, book_id, max_cid): """更新卷章节""" chapter_list = get_chapter_list(book_id) if not chapter_list: return 0 now = datetime.datetime.now() # data = json.loads(redis_data) # if max_cid < int(chapter_list[len(chapter_list)-1]['chapter_id']) and data: # data[index]['max_cid'] = int(chapter_list[len(chapter_list)-1]['chapter_id']) # redis_utils.set_cache(key, json.dumps(data), 86400) if max_cid >= int(chapter_list[len(chapter_list)-1]['chapter_id']): return 0 for chapter in chapter_list: # 增加章节 print chapter['chapter_name'] chapter_id = int(chapter['chapter_id']) if chapter_id <= max_cid: continue try: chapter_content = get_chapter_content(book_id, chapter_id) except: key = 'error_pull_book_anzhi' redis_data = redis_utils.get_cache(key, refresh_expires=False) if redis_data: data = json.loads(redis_data) data.append({'book_id': book_id, 'chapter_id': chapter_id}) redis_utils.set_cache(key, json.dumps(data), 86400) else: redis_utils.set_cache(key, json.dumps([{'book_id':book_id, 'chapter_id':apter_id}]), 86400) return book_id, chapter_id db.session.add(BookChapterContent(dict(book_id=loca_book_id, volume_id=0, chapter_id=chapter_id, content=chapter_content['content']))) db.session.add(BookChapters(dict( book_id=loca_book_id, volume_id=0, chapter_id=chapter_id, chapter_name=chapter['chapter_name'], create_time=now, update_time=now, word_count=cal_word_count(chapter_content['content']), ))) return 1
def landing_page(): book_id = request.args.get('book_id', '') if not book_id: return json.dumps({'code': -1, 'msg': u'信息不足'}) print book_id key = 'landing_page_%s' % book_id redis_data = redis_utils.get_cache(key, refresh_expires=False) if redis_data: return json.dumps({'code': 0, 'data': json.loads(redis_data)}) book = Book.query.filter_by(book_id=book_id).first() chapter = BookChapters.query.order_by( BookChapters.id.asc()).filter(BookChapters.book_id == book_id)[:2] chapter_list = [] for ch in chapter: data = {'chapter_name': ch.chapter_name} chapter_list.append(data) print chapter content = BookChapterContent.query.filter_by( book_id=book_id, volume_id=chapter[0].volume_id, chapter_id=chapter[0].chapter_id).first() book_content_list = [] data = {'content': content.content} book_content_list.append(data) last_data = { 'book_info': book.to_dict(), 'chapter_list': chapter_list, 'book_content_list': book_content_list } redis_utils.set_cache(key, json.dumps(last_data), 3600) return json.dumps({'code': 0, 'data': last_data})
def get_ranking(): """ 获取对应大分类排行榜 """ big_place = request.args.get('big_place', 1, int) key = 'get_ranking_%s' % big_place redis_data = redis_utils.get_cache(key, refresh_expires=False) if redis_data: return json.dumps({'code': 0, 'data': json.loads(redis_data)}) ranking_list = { 1: { 1: { 'name': u'男生人气榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/nanren.png' }, 2: { 'name': u'男生新书榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/nanxin.png' }, 3: { 'name': u'男生完结榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/nanwan.png' }, 4: { 'name': u'出版畅销榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/chuchang.png' } }, 2: { 1: { 'name': u'女生人气榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/nvren.png' }, 2: { 'name': u'女生新书榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/nvxin.png' }, 3: { 'name': u'女生完结榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/nvwan.png' }, 4: { 'name': u'出版畅销榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/chuchang.png' } }, 3: { 1: { 'name': u'出版人气榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/churen.png' }, 2: { 'name': u'出版新书榜', 'url': 'http://ov2eyt2uw.bkt.clouddn.com/chuxin.png' } } } ranking = ranking_list.get(big_place) if not ranking: return json.dumps({'code': -1, 'msg': u'请选择条件查询'}) big_list = {} for r in ranking: buy_ranking_list = get_ranking_list(big_place, r, 1, 3) big_list[r] = buy_ranking_list data = {'ranking': ranking, 'big_list': big_list, 'big_place': big_place} redis_utils.set_cache(key, json.dumps(data), 3600) return json.dumps({'code': 0, 'data': data})
def detail(): ''' 获取书籍详情 ''' book_id = request.args.get('book_id', 0, int) if not book_id: return json.dumps({'code': -2, 'msg': u'参数错误'}) is_sup_free = is_sup_free_book() key = 'detail_%s' % book_id if not is_sup_free: key = 'nofreedetail_%s' % book_id redis_data = redis_utils.get_cache(key, refresh_expires=False) #redis_data = "" if redis_data: data = json.loads(redis_data) if current_user.is_authenticated: collect = BookShelf.query.filter_by(name=MYSELF_TYPE, user_id=current_user.id, book_id=book_id).count() if collect == 1: data['is_myself'] = 1 else: data['is_myself'] = 0 return json.dumps({'code': 0, 'data': data}) book = Book.query.filter_by(book_id=book_id).first() if not book: return json.dumps({'code': -1, 'msg': u'无此书籍信息'}) book_volume = BookVolume.query.filter_by(book_id=book_id).first() if book_volume: book_chapters = BookChapters.query.filter_by( book_id=book_id, volume_id=book_volume.volume_id)[:20] else: book_chapters = BookChapters.query.filter_by(book_id=book_id)[:20] #获取分类 category = BookCategory.query.filter_by(cate_id=book.cate_id).first() like_key = 'like_book_%s' % book.cate_id if not is_sup_free: like_key = 'nofreelike_book_%s' % book.cate_id like_book = redis_utils.get_cache(like_key, refresh_expires=False) #like_book_list = [] if like_book: like_book_list = json.loads(like_book) else: like_book_list = [] query = Book.query.filter_by(cate_id=book.cate_id, showed=1) if not is_sup_free: query = query.filter_by(free_collect=0) if query.count() < 6: like_book_query = query.all() else: count = random.randint(1, query.count()) count = count if count >= 6 else 6 like_book_query = query[count - 5:count] for l in like_book_query: like_book_list.append(l.to_dict()) redis_utils.set_cache(like_key, json.dumps(like_book_list), 180) chapter_list = [] for chapter in book_chapters: chapter_list.append(chapter.to_dict()) book_dict = book.to_dict() # 外链小说 free_source = request.args.get('free_source', '') if book.free_collect: source_item = get_free_source(book_id, free_source) if not source_item: return json.dumps({'code': -1, 'msg': u'无此书籍信息'}) book_dict.update(source_item.to_dict()) book_dict['cate_name'] = category.cate_name if not book_dict.get('chapter_num'): book_dict['chapter_num'] = BookChapters.query.filter_by( book_id=book_id).count() data = { 'book_detail': book_dict, 'book_chapters': chapter_list, 'like_book_list': like_book_list } redis_utils.set_cache(key, json.dumps(data), 1800) if current_user.is_authenticated: collect = BookShelf.query.filter_by(name=MYSELF_TYPE, user_id=current_user.id, book_id=book_id).count() if collect == 1: data['is_myself'] = 1 else: data['is_myself'] = 0 return json.dumps({'code': 0, 'data': data})
def index(): ''' 首页 ''' platform = request.args.get('platform') v = request.args.get('v') sex = request.args.get('sex', 0) m_id = request.args.get('m_id', 0, int) key = 'index_%s_%s_%s_%s' % (platform, sex, v, m_id) redis_data = redis_utils.get_cache(key, refresh_expires=False) #redis_data = "" if redis_data: data = json.loads(redis_data) if is_ios_special(): data['top_list'] = data['top_list'][:-1] return json.dumps({'code': 0, 'data': data}) data = {} if v == '1.0.0': hot_list = BookShelf.query.order_by(BookShelf.ranking.desc()).filter( BookShelf.name == HOT_TYPE, BookShelf.showed == True, or_(BookShelf.sex == sex, BookShelf.sex == 0))[:3] else: if not m_id: hot_list = BookShelf.query.order_by( BookShelf.ranking.desc()).filter( BookShelf.name == HOT_TYPE, BookShelf.showed == True, or_(BookShelf.sex == sex, BookShelf.sex == 0))[:6] else: hot_list = BookShelf.query.filter( BookShelf.name == HOT_TYPE, BookShelf.showed == True, or_(BookShelf.sex == sex, BookShelf.sex == 0))[:9] hot_data = [] for hot in hot_list: book = Book.query.filter_by(book_id=hot.book_id).first() if book.showed: hot_data.append(book.to_dict()) data['hot_data'] = hot_data if v == '1.0.0': new_list = BookShelf.query.order_by(BookShelf.ranking.desc()).filter( BookShelf.name == NEW_TYPE, BookShelf.showed == True, or_(BookShelf.sex == sex, BookShelf.sex == 0))[:3] else: if not m_id: new_list = BookShelf.query.order_by( BookShelf.ranking.desc()).filter( BookShelf.name == NEW_TYPE, BookShelf.showed == True, or_(BookShelf.sex == sex, BookShelf.sex == 0))[:6] else: new_list = BookShelf.query.filter( BookShelf.name == NEW_TYPE, BookShelf.showed == True, or_(BookShelf.sex == sex, BookShelf.sex == 0))[:3] new_data = [] for new in new_list: book = Book.query.filter_by(book_id=new.book_id).first() if book.showed: new_data.append(book.to_dict()) if not m_id: finish_list = BookShelf.query.order_by( BookShelf.ranking.desc()).filter( BookShelf.name == FINISH_TYPE, BookShelf.showed == True, or_(BookShelf.sex == sex, BookShelf.sex == 0))[:6] else: finish_list = BookShelf.query.filter( BookShelf.name == FINISH_TYPE, BookShelf.showed == True, or_(BookShelf.sex == sex, BookShelf.sex == 0))[:3] finish_data = [] for finish in finish_list: book = Book.query.filter_by(book_id=finish.book_id).first() if book.showed: finish_data.append(book.to_dict()) free_data = [] free_list = Book.query.filter( Book.showed == True, Book.free_collect == True, or_(Book.channel_type == sex, Book.channel_type == 0))[:6] for free in free_list: free_data.append(free.to_dict()) data['finish_data'] = finish_data data['new_data'] = new_data data['banner_list'] = get_banner(m_id, sex, platform) data['hot_type'] = get_hot_type() data['top_list'] = get_top_list(v) data['free_data'] = free_data redis_utils.set_cache(key, json.dumps(data), 180) if is_ios_special(): data['top_list'] = data['top_list'][:-1] return json.dumps({'code': 0, 'data': data})
def get_act_times(key): times = redis_utils.get_cache(key, refresh_expires=False) print 'get act times', times if not times: times = 0 return int(times)