Exemple #1
0
async def similar_user(request):
    user = request['session'].get('user', None)
    if user:
        motor_db = MotorBase().db
        try:
            similar_info = await motor_db.user_recommend.find_one({'user': user})
            if similar_info:
                similar_user = similar_info['similar_user']
                user_tag = similar_info['user_tag']
                updated_at = similar_info['updated_at']
                return template('similar_user.html',
                                title='与' + user + '相似的书友',
                                is_login=1,
                                is_similar=1,
                                user=user,
                                similar_user=similar_user,
                                user_tag=user_tag,
                                updated_at=updated_at)
            else:
                return template('similar_user.html',
                                title='与' + user + '相似的书友',
                                is_login=1,
                                is_similar=1,
                                user=user)
        except Exception as e:
            LOGGER.error(e)
            return redirect('/')
    else:
        return redirect('/')
Exemple #2
0
async def owllook_register(request):
    """
    用户注册 不允许重名
    :param request:
    :return:
        :   -1  用户名已存在
        :   0   用户名或密码不能为空
        :   1   注册成功
    """
    user = request.args.get('user', None)
    pwd = request.args.get('pwd', None)
    email = request.args.get('email', None)
    if user and pwd and email:
        motor_db = MotorBase().db
        is_exist = await motor_db.user.find_one({'user': user})
        if not is_exist:
            pass_first = hashlib.md5(
                (WEBSITE["TOKEN"] + pwd).encode("utf-8")).hexdigest()
            password = hashlib.md5(pass_first.encode("utf-8")).hexdigest()
            time = get_time()
            data = {
                "user": user,
                "password": password,
                "email": email,
                "register_time": time,
            }
            object_id = await motor_db.user.save(data)
            return json({'object_id': str(object_id)}) if object_id else None
        else:
            return json({'status': -1})
    else:
        return json({'status': 0})
async def owllook_delete_bookmark(request):
    """

    :param request:
    :return:
        :   -1  用户session失效  需要重新登录
        :   0   删除书签失败
        :   1   删除书签成功
    """
    user = request['session'].get('user', None)
    if user:
        bookmarkurl = request.args.get('bookmarkurl', '')
        name = request.args.get('name', '')
        chapter_url = request.args.get('chapter_url', '')
        novels_name = request.args.get('novels_name', '')
        url = bookmarkurl + "&name=" + name + "&chapter_url=" + chapter_url + "&novels_name=" + novels_name
        try:
            motor_db = MotorBase().db
            motor_db.user_message.update_one(
                {'user': user}, {'$pull': {
                    'bookmarks': {
                        "bookmark": url
                    }
                }})
            LOGGER.info('删除书签成功')
            return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
async def owllook_login(request):
    """
    用户登录
    :param request:
    :return:
        :   -1  用户名或密码不能为空
        :   0   用户名或密码错误
        :   1   登陆成功
    """
    user = request.args.get('user', None)
    pwd = request.args.get('pwd', None)
    if user and pwd:
        motor_db = MotorBase().db
        data = await motor_db.user.find_one({'user': user})
        if data:
            password = hashlib.md5(
                (WEBSITE["TOKEN"] + pwd).encode("utf-8")).hexdigest()
            if password == data.get('password'):
                request['session']['user'] = user
                # response = json({'status': 1})
                # response.cookies['user'] = user
                return json({'status': 1})
        return json({'status': -1})
    else:
        return json({'status': 0})
async def owllook_delete_book(request):
    """

    :param request:
    :return:
        :   -1  用户session失效  需要重新登录
        :   0   删除书架失败
        :   1   删除书架成功
    """
    user = request['session'].get('user', None)
    if user:
        novels_name = request.args.get('novels_name', '')
        chapter_url = request.args.get('chapter_url', '')
        url = "/chapter?url={chapter_url}&novels_name={novels_name}".format(
            chapter_url=chapter_url, novels_name=novels_name)
        try:
            motor_db = MotorBase().db
            motor_db.user_message.update_one(
                {'user': user}, {'$pull': {
                    'books_url': {
                        "book_url": url
                    }
                }})
            LOGGER.info('删除书架成功')
            return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
Exemple #6
0
async def owllook_delete_book(request):
    """

    :param request:
    :return:
        :   -1  用户session失效  需要重新登录
        :   0   删除书架失败
        :   1   删除书架成功
    """
    user = request['session'].get('user', None)
    data = parse_qs(str(request.body, encoding='utf-8'))
    if user:
        if data.get('book_url', None):
            book_url = data.get('book_url', None)[0]
        else:
            novels_name = data.get('novels_name', '')
            chapter_url = data.get('chapter_url', '')
            book_url = "/chapter?url={chapter_url}&novels_name={novels_name}".format(
                chapter_url=chapter_url[0], novels_name=novels_name[0])
        try:
            motor_db = MotorBase().db
            await motor_db.user_message.update_one(
                {'user': user},
                {'$pull': {
                    'books_url': {
                        "book_url": unquote(book_url)
                    }
                }})
            LOGGER.info('删除书架成功')
            return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
Exemple #7
0
async def owllook_delete_bookmark(request):
    """

    :param request:
    :return:
        :   -1  用户session失效  需要重新登录
        :   0   删除书签失败
        :   1   删除书签成功
    """
    user = request['session'].get('user', None)
    data = parse_qs(str(request.body, encoding='utf-8'))
    bookmarkurl = data.get('bookmarkurl', '')
    if user and bookmarkurl:
        bookmark = unquote(bookmarkurl[0])
        try:
            motor_db = MotorBase().db
            await motor_db.user_message.update_one(
                {'user': user},
                {'$pull': {
                    'bookmarks': {
                        "bookmark": bookmark
                    }
                }})
            LOGGER.info('删除书签成功')
            return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
Exemple #8
0
async def books(request):
    user = request['session'].get('user', None)
    if user:
        motor_db = MotorBase().db
        try:
            data = await motor_db.user_message.find_one({'user': user})
            if data:
                books_url = data.get('books_url', None)
                if books_url:
                    result = []
                    for i in books_url:
                        item_result = {}
                        book_url = i.get('book_url', None)
                        query = parse_qs(urlparse(book_url).query)
                        item_result['novels_name'] = query.get('novels_name', '')[0] if query.get('novels_name',
                                                                                                  '') else ''
                        item_result['book_url'] = book_url
                        item_result['add_time'] = i.get('add_time', '')
                        result.append(item_result)
                    return template('admin_books.html', title='{user}的书架 - owllook'.format(user=user),
                                    is_login=1,
                                    user=user,
                                    is_bookmark=1,
                                    result=result[::-1])
            return template('admin_books.html', title='{user}的书架 - owllook'.format(user=user),
                            is_login=1,
                            user=user,
                            is_bookmark=0)
        except Exception as e:
            LOGGER.error(e)
    else:
        return redirect('/')
Exemple #9
0
async def get_tag():
    motor_db = MotorBase().db
    novels = Novels()
    # 获取所有书架链接游标
    books_url_cursor = motor_db.user_message.find({}, {'books_url.book_url': 1, 'user': 1, '_id': 0})
    async for document in books_url_cursor:
        if document:
            books_url = document.get('books_url', None)
            if books_url:
                all_user = {}
                user = document['user']
                all_user[user + '_novels'] = []
                all_user[user + '_tag'] = []
                all_user[user + '_author'] = []
                for book_url in books_url:
                    chapter_url = book_url['book_url']
                    novels_name = parse_qs(urlparse(chapter_url).query).get('novels_name', '')[0]
                    all_user[user + '_novels'].append(novels_name)
                    novels_info = novels.search_name(novels_name)
                    if novels_info:
                        novels_type = novels_info['novels_type'].split('#')
                        author = novels_info['author']
                        all_user[user + '_author'].append(author)
                        all_user[user + '_tag'].extend(novels_type)
                data = {
                    'user_novels': all_user[user + '_novels'],
                    'user_tag': all_user[user + '_tag'],
                    'user_author': all_user[user + '_author'],
                }
                await motor_db.user_tag.update_one(
                    {"user": user},
                    {'$set': {'data': data, "updated_at": get_time()}}, upsert=True)
                pprint(data)
Exemple #10
0
async def get_the_latest_chapter(chapter_url):
    url = parse_qs(urlparse(chapter_url).query).get('url', '')
    novels_name = parse_qs(urlparse(chapter_url).query).get('novels_name', '')
    data = None
    if url and novels_name:
        url = url[0]
        novels_name = novels_name[0]
        netloc = urlparse(url).netloc
        if netloc in LATEST_RULES.keys():
            async with aiohttp.ClientSession() as client:
                html = await target_fetch(client=client, url=url)
                soup = BeautifulSoup(html, 'html5lib')
                latest_chapter_name, latest_chapter_url = None, None
                if LATEST_RULES[netloc].plan:
                    meta_value = LATEST_RULES[netloc].meta_value
                    latest_chapter_name = soup.select('meta[property="{0}"]'.format(meta_value["latest_chapter_name"]))
                    latest_chapter_name = latest_chapter_name[0].get('content', None) if latest_chapter_name else None
                    latest_chapter_url = soup.select('meta[property="{0}"]'.format(meta_value["latest_chapter_url"]))
                    latest_chapter_url = latest_chapter_url[0].get('content', None) if latest_chapter_url else None
                else:
                    selector = LATEST_RULES[netloc].selector
                    content_url = selector.get('content_url')
                    if selector.get('id', None):
                        latest_chapter_soup = soup.find_all(id=selector['id'])
                    elif selector.get('class', None):
                        latest_chapter_soup = soup.find_all(class_=selector['class'])
                    else:
                        latest_chapter_soup = soup.select(selector.get('tag'))
                    if latest_chapter_soup:
                        if content_url == '1':
                            pass
                        elif content_url == '0':
                            pass
                        else:
                            latest_chapter_url = content_url + latest_chapter_soup[0].get('href', None)
                        latest_chapter_name = latest_chapter_soup[0].get('title', None)
                if latest_chapter_name and latest_chapter_url:
                    time_current = get_time()
                    data = {
                        "latest_chapter_name": latest_chapter_name,
                        "latest_chapter_url": latest_chapter_url,
                        "owllook_chapter_url": chapter_url,
                        "owllook_content_url": "/owllook_content?url={latest_chapter_url}&name={name}&chapter_url={chapter_url}&novels_name={novels_name}".format(
                            latest_chapter_url=latest_chapter_url,
                            name=latest_chapter_name,
                            chapter_url=url,
                            novels_name=novels_name,
                        ),
                    }
                    # 存储最新章节
                    motor_db = MotorBase().db
                    await motor_db.latest_chapter.update_one(
                        {"novels_name": novels_name, 'owllook_chapter_url': chapter_url},
                        {'$set': {'data': data, "finished_at": time_current}}, upsert=True)
    return data
Exemple #11
0
async def owllook_content(request):
    url = request.args.get('url', None)
    chapter_url = request.args.get('chapter_url', None)
    novels_name = request.args.get('novels_name', None)
    name = request.args.get('name', None)
    bookmark_url = "{path}?url={url}&name={name}&chapter_url={chapter_url}&novels_name={novels_name}".format(
        path=request.url,
        url=url,
        name=name,
        chapter_url=chapter_url,
        novels_name=novels_name)
    book_url = "/chapter?url={chapter_url}&novels_name={novels_name}".format(
        chapter_url=chapter_url, novels_name=novels_name)
    netloc = urlparse(url).netloc
    if netloc not in RULES.keys():
        return redirect(url)
    content_url = RULES[netloc].content_url
    content = await cache_owllook_novels_content(url=url, netloc=netloc)
    if content:
        user = request['session'].get('user', None)
        # 破坏广告链接
        content = str(content).strip('[]Jjs,').replace('http', 'hs')
        if user:
            motor_db = MotorBase().db
            bookmark = await motor_db.user_message.find_one(
                {'bookmarks.bookmark': bookmark_url})
            book = await motor_db.user_message.find_one(
                {'books_url.book_url': book_url})
            bookmark = 1 if bookmark else 0
            book = 1 if book else 0
            return template('content.html',
                            is_login=1,
                            user=user,
                            name=name,
                            url=url,
                            bookmark=bookmark,
                            book=book,
                            content_url=content_url,
                            chapter_url=chapter_url,
                            novels_name=novels_name,
                            soup=content)
        else:
            return template('content.html',
                            is_login=0,
                            name=name,
                            url=url,
                            bookmark=0,
                            book=0,
                            content_url=content_url,
                            chapter_url=chapter_url,
                            novels_name=novels_name,
                            soup=content)
    else:
        return text('解析失败,请将失败页面反馈给本站,请重新刷新一次,或者访问源网页:{url}'.format(url=url))
Exemple #12
0
async def owllook_add_book(request):
    """

    :param request:
    :return:
        :   -1  用户session失效  需要重新登录
        :   0   添加书架失败
        :   1   添加书架成功
    """
    user = request['session'].get('user', None)
    data = parse_qs(str(request.body, encoding='utf-8'))
    novels_name = data.get('novels_name', '')
    chapter_url = data.get('chapter_url', '')
    last_read_url = data.get('last_read_url', '')
    if user and novels_name and chapter_url:
        url = "/chapter?url={chapter_url}&novels_name={novels_name}".format(
            chapter_url=chapter_url[0], novels_name=novels_name[0])
        time = get_time()
        try:
            motor_db = MotorBase().db
            res = await motor_db.user_message.update_one(
                {'user': user}, {'$set': {
                    'last_update_time': time
                }},
                upsert=True)
            if res:
                await motor_db.user_message.update_one(
                    {
                        'user': user,
                        'books_url.book_url': {
                            '$ne': url
                        }
                    }, {
                        '$push': {
                            'books_url': {
                                'book_url': url,
                                'add_time': time,
                                'last_read_url': unquote(last_read_url[0])
                            }
                        }
                    })
                LOGGER.info('书架添加成功')
                return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
Exemple #13
0
async def search_user(request):
    user = request['session'].get('user', None)
    name = request.args.get('ss', None)
    if user and name:
        motor_db = MotorBase().db
        try:
            data = await motor_db.user_message.find_one({'user': name})
            books_url = data.get('books_url', None) if data else None
            if books_url:
                result = []
                for i in books_url:
                    item_result = {}
                    book_url = i.get('book_url', None)
                    last_read_url = i.get("last_read_url", "")
                    book_query = parse_qs(urlparse(book_url).query)
                    last_read_chapter_name = parse_qs(last_read_url).get('name', ['暂无'])[0]
                    item_result['novels_name'] = book_query.get('novels_name', '')[0] if book_query.get(
                        'novels_name', '') else ''
                    item_result['book_url'] = book_url
                    latest_data = await motor_db.latest_chapter.find_one({'owllook_chapter_url': book_url})
                    if latest_data:
                        item_result['latest_chapter_name'] = latest_data['data']['latest_chapter_name']
                        item_result['owllook_content_url'] = latest_data['data']['owllook_content_url']
                    else:
                        get_latest_data = await get_the_latest_chapter(book_url) or {}
                        item_result['latest_chapter_name'] = get_latest_data.get('latest_chapter_name', '暂未获取,请反馈')
                        item_result['owllook_content_url'] = get_latest_data.get('owllook_content_url', '')
                    item_result['add_time'] = i.get('add_time', '')
                    item_result["last_read_url"] = last_read_url if last_read_url else book_url
                    item_result["last_read_chapter_name"] = last_read_chapter_name
                    result.append(item_result)
                return template('search_user.html', title='{name}的书架 - owllook'.format(name=name),
                                is_login=1,
                                user=user,
                                name=name,
                                is_bookmark=1,
                                result=result[::-1])
            else:
                return template('search_user.html', title='{name}的书架 - owllook'.format(name=name),
                                is_login=1,
                                user=user,
                                is_bookmark=0)
        except Exception as e:
            LOGGER.error(e)
            return redirect('/')
    else:
        return redirect('/')
async def owllook_add_bookmark(request):
    """

    :param request:
    :return:
        :   -1  用户session失效  需要重新登录
        :   0   添加书签失败
        :   1   添加书签成功
    """
    user = request['session'].get('user', None)
    if user:
        bookmarkurl = request.args.get('bookmarkurl', '')
        name = request.args.get('name', '')
        chapter_url = request.args.get('chapter_url', '')
        novels_name = request.args.get('novels_name', '')
        url = bookmarkurl + "&name=" + name + "&chapter_url=" + chapter_url + "&novels_name=" + novels_name
        time = get_time()
        try:
            motor_db = MotorBase().db
            res = motor_db.user_message.update_one(
                {'user': user}, {'$set': {
                    'last_update_time': time
                }},
                upsert=True)
            if res:
                motor_db.user_message.update_one(
                    {
                        'user': user,
                        'bookmarks.bookmark': {
                            '$ne': url
                        }
                    }, {
                        '$push': {
                            'bookmarks': {
                                'bookmark': url,
                                'add_time': time
                            }
                        }
                    })
                LOGGER.info('书签添加成功')
                return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
async def owllook_add_book(request):
    """

    :param request:
    :return:
        :   -1  用户session失效  需要重新登录
        :   0   添加书架失败
        :   1   添加书架成功
    """
    user = request['session'].get('user', None)
    if user:
        novels_name = request.args.get('novels_name', '')
        chapter_url = request.args.get('chapter_url', '')
        url = "/chapter?url={chapter_url}&novels_name={novels_name}".format(
            chapter_url=chapter_url, novels_name=novels_name)
        time = get_time()
        try:
            motor_db = MotorBase().db
            res = motor_db.user_message.update_one(
                {'user': user}, {'$set': {
                    'last_update_time': time
                }},
                upsert=True)
            if res:
                motor_db.user_message.update_one(
                    {
                        'user': user,
                        'books_url.book_url': {
                            '$ne': url
                        }
                    }, {
                        '$push': {
                            'books_url': {
                                'book_url': url,
                                'add_time': time
                            }
                        }
                    })
                LOGGER.info('书架添加成功')
                return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
Exemple #16
0
async def owllook_add_bookmark(request):
    """

    :param request:
    :return:
        :   -1  用户session失效  需要重新登录
        :   0   添加书签失败
        :   1   添加书签成功
    """
    user = request['session'].get('user', None)
    data = parse_qs(str(request.body, encoding='utf-8'))
    bookmarkurl = data.get('bookmarkurl', '')
    if user and bookmarkurl:
        url = unquote(bookmarkurl[0])
        time = get_time()
        try:
            motor_db = MotorBase().db
            res = await motor_db.user_message.update_one(
                {'user': user}, {'$set': {
                    'last_update_time': time
                }},
                upsert=True)
            if res:
                await motor_db.user_message.update_one(
                    {
                        'user': user,
                        'bookmarks.bookmark': {
                            '$ne': url
                        }
                    }, {
                        '$push': {
                            'bookmarks': {
                                'bookmark': url,
                                'add_time': time
                            }
                        }
                    })
                LOGGER.info('书签添加成功')
                return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
Exemple #17
0
async def get_user_tag():
    motor_db = MotorBase().db
    user_tag_cursor = motor_db.user_tag.find({}, {'data.user_tag': 1, 'user': 1, '_id': 0})
    result = {}
    async for document in user_tag_cursor:
        if document['data']['user_tag']:
            result[document['user']] = document['data']['user_tag']

    for key, value in result.items():
        result_copy = deepcopy(result)
        del result_copy[key]
        cos = CosineSimilarity(value, result_copy)
        vector = cos.create_vector()
        resultDic = cos.calculate(vector)
        pprint(resultDic)
        # pprint(type(resultList[1]))
        await motor_db.user_recommend.update_one(
            {"user": key},
            {'$set': {'similar_user': resultDic, 'user_tag': result[key], "updated_at": get_time()}}, upsert=True)
Exemple #18
0
async def owllook_search(request):
    start = time.time()
    name = request.args.get('wd', None)
    if not name:
        return redirect('/')
    else:
        novels_name = 'intitle:{name} 小说 阅读'.format(
            name=name) if ':baidu' not in name else name.split('baidu')[1]
        try:
            motor_db = MotorBase().db
            motor_db.search_records.update_one({'keyword': name},
                                               {'$inc': {
                                                   'count': 1
                                               }},
                                               upsert=True)
        except Exception as e:
            LOGGER.exception(e)
    is_web = int(request.args.get('is_web', 1))
    result = await search(novels_name, is_web)
    if result:
        parse_result = [i for i in result if i]
        result_sorted = sorted(
            parse_result, reverse=True, key=lambda res: res[
                'timestamp']) if ':baidu' not in name else parse_result
        user = request['session'].get('user', None)
        if user:
            return template('result.html',
                            is_login=1,
                            user=user,
                            name=name,
                            time='%.2f' % (time.time() - start),
                            result=result_sorted,
                            count=len(parse_result))
        else:
            return template('result.html',
                            is_login=0,
                            name=name,
                            time='%.2f' % (time.time() - start),
                            result=result_sorted,
                            count=len(parse_result))
    else:
        return html("No Result!")
Exemple #19
0
async def update_all_books():
    try:
        motor_db = MotorBase().db
        # 获取所有书架链接游标
        books_url_cursor = motor_db.user_message.find({}, {'books_url.book_url': 1, '_id': 0})
        # 已更新url集合
        already_urls = set()
        async for document in books_url_cursor:
            if document:
                books_url = document['books_url']
                # 一组书架链接列表数据
                for book_url in books_url:
                    chapter_url = book_url['book_url']
                    if chapter_url not in already_urls:
                        await get_the_latest_chapter(chapter_url)
                        already_urls.add(chapter_url)
        return True
    except Exception as e:
        LOGGER.exception(e)
        return False
Exemple #20
0
async def owllook_register(request):
    """
    用户注册 不允许重名
    :param request:
    :return:
        :   -1  用户名已存在
        :   0   用户名或密码不能为空
        :   1   注册成功
    """
    register_data = parse_qs(str(request.body, encoding='utf-8'))
    user = register_data.get('user', [None])[0]
    pwd = register_data.get('pwd', [None])[0]
    email = register_data.get('email', [None])[0]
    answer = register_data.get('answer', [None])[0]
    reg_index = request.cookies['reg_index']
    if user and pwd and email and answer and reg_index:
        motor_db = MotorBase().db
        is_exist = await motor_db.user.find_one({'user': user})
        if not is_exist:
            # 验证问题答案是否准确
            real_answer = get_real_answer(str(reg_index))
            if real_answer and real_answer == answer:
                pass_first = hashlib.md5(
                    (WEBSITE["TOKEN"] + pwd).encode("utf-8")).hexdigest()
                password = hashlib.md5(pass_first.encode("utf-8")).hexdigest()
                time = get_time()
                data = {
                    "user": user,
                    "password": password,
                    "email": email,
                    "ip": request.ip[0],
                    "register_time": time,
                }
                await motor_db.user.save(data)
                return json({'status': 1})
            else:
                return json({'status': -2})
        else:
            return json({'status': -1})
    else:
        return json({'status': 0})
Exemple #21
0
async def owllook_login(request):
    """
    用户登录
    :param request:
    :return:
        :   -1  用户名或密码不能为空
        :   0   用户名或密码错误
        :   1   登陆成功
    """
    login_data = parse_qs(str(request.body, encoding='utf-8'))
    user = login_data.get('user', [None])[0]
    pwd = login_data.get('pwd', [None])[0]
    if user and pwd:
        motor_db = MotorBase().db
        data = await motor_db.user.find_one({'user': user})
        if data:
            pass_first = hashlib.md5(
                (WEBSITE["TOKEN"] + pwd).encode("utf-8")).hexdigest()
            password = hashlib.md5(pass_first.encode("utf-8")).hexdigest()
            if password == data.get('password'):
                response = json({'status': 1})
                # 将session_id存于cokies
                date = datetime.datetime.now()
                response.cookies['owl_sid'] = request['session'].sid
                response.cookies['owl_sid'][
                    'expires'] = date + datetime.timedelta(days=30)
                response.cookies['owl_sid']['httponly'] = True
                # 此处设置存于服务器session的user值
                request['session']['user'] = user
                # response.cookies['user'] = user
                # response.cookies['user']['expires'] = date + datetime.timedelta(days=30)
                # response.cookies['user']['httponly'] = True
                # response = json({'status': 1})
                # response.cookies['user'] = user
                return response
        return json({'status': -1})
    else:
        return json({'status': 0})
async def owllook_content(request):
    """
    返回小说章节内容页
    : content_url   这决定当前U页面url的生成方式
    : url           章节内容页源url
    : chapter_url   小说目录源url
    : novels_name   小说名称
    :return: 小说章节内容页
    """
    url = request.args.get('url', None)
    chapter_url = request.args.get('chapter_url', None)
    novels_name = request.args.get('novels_name', None)
    name = request.args.get('name', None)
    # 当小说内容url不在解析规则内 跳转到原本url
    netloc = urlparse(url).netloc
    if netloc not in RULES.keys():
        return redirect(url)
    # 拼接小说书签url
    bookmark_url = "{path}?url={url}&name={name}&chapter_url={chapter_url}&novels_name={novels_name}".format(
        path=request.path,
        url=url,
        name=name,
        chapter_url=chapter_url,
        novels_name=novels_name)
    # 拼接小说目录url
    book_url = "/chapter?url={chapter_url}&novels_name={novels_name}".format(
        chapter_url=chapter_url, novels_name=novels_name)
    content_url = RULES[netloc].content_url
    content = await cache_owllook_novels_content(url=url, netloc=netloc)
    if content:
        user = request['session'].get('user', None)
        # 破坏广告链接
        content = str(content).strip('[]Jjs,').replace('http', 'hs')
        if user:
            motor_db = MotorBase().db
            bookmark = await motor_db.user_message.find_one({
                'user':
                user,
                'bookmarks.bookmark':
                bookmark_url
            })
            book = await motor_db.user_message.find_one({
                'user':
                user,
                'books_url.book_url':
                book_url
            })
            bookmark = 1 if bookmark else 0
            if book:
                # 当书架中存在该书源
                book = 1
                # 保存最后一次阅读记录
                await motor_db.user_message.update_one(
                    {
                        'user': user,
                        'books_url.book_url': book_url
                    }, {'$set': {
                        'books_url.$.last_read_url': bookmark_url
                    }})
            else:
                book = 0
            return template('content.html',
                            is_login=1,
                            user=user,
                            name=name,
                            url=url,
                            bookmark=bookmark,
                            book=book,
                            content_url=content_url,
                            chapter_url=chapter_url,
                            novels_name=novels_name,
                            soup=content)
        else:
            return template('content.html',
                            is_login=0,
                            name=name,
                            url=url,
                            bookmark=0,
                            book=0,
                            content_url=content_url,
                            chapter_url=chapter_url,
                            novels_name=novels_name,
                            soup=content)
    else:
        return text('解析失败,请将失败页面反馈给本站,请重新刷新一次,或者访问源网页:{url}'.format(url=url))
async def owllook_search(request):
    start = time.time()
    name = request.args.get('wd', None)
    motor_db = MotorBase().db
    if not name:
        return redirect('/')
    else:
        # 记录搜索小说名
        try:
            await motor_db.search_records.update_one({'keyword': name},
                                                     {'$inc': {
                                                         'count': 1
                                                     }},
                                                     upsert=True)
        except Exception as e:
            LOGGER.exception(e)
    # 通过搜索引擎获取检索结果
    parse_result = [None]
    for each_engine in ENGINE_PRIORITY:
        # for 360 so
        if each_engine == "360":
            novels_name = "{name} 小说 免费阅读".format(name=name)
            parse_result = await cache_owllook_so_novels_result(novels_name)
            if parse_result:
                break
        if each_engine == "baidu":
            # for baidu
            novels_name = 'intitle:{name} 小说 阅读'.format(
                name=name) if ':baidu' not in name else name.split('baidu')[1]
            parse_result = await cache_owllook_baidu_novels_result(novels_name)
            if parse_result:
                break
    if parse_result[0]:
        # result_sorted = sorted(
        #     parse_result, reverse=True, key=lambda res: res['timestamp']) if ':baidu' not in name else parse_result
        # 优先依靠是否解析进行排序  其次以更新时间进行排序
        result_sorted = sorted(
            parse_result,
            reverse=True,
            key=itemgetter(
                'is_parse',
                'timestamp')) if ':baidu' not in name else parse_result
        user = request['session'].get('user', None)
        if user:
            try:
                time_current = get_time()
                res = await motor_db.user_message.update_one(
                    {'user': user},
                    {'$set': {
                        'last_update_time': time_current
                    }},
                    upsert=True)
                # 此处语法操作过多  下次看一遍mongo再改
                if res:
                    is_ok = await motor_db.user_message.update_one(
                        {
                            'user': user,
                            'search_records.keyword': {
                                '$ne': name
                            }
                        },
                        {
                            '$push': {
                                'search_records': {
                                    'keyword': name,
                                    'counts': 1
                                }
                            }
                        },
                    )

                    if is_ok:
                        await motor_db.user_message.update_one(
                            {
                                'user': user,
                                'search_records.keyword': name
                            }, {'$inc': {
                                'search_records.$.counts': 1
                            }})

            except Exception as e:
                LOGGER.exception(e)
            return template('result.html',
                            is_login=1,
                            user=user,
                            name=name,
                            time='%.2f' % (time.time() - start),
                            result=result_sorted,
                            count=len(parse_result))

        else:
            return template('result.html',
                            is_login=0,
                            name=name,
                            time='%.2f' % (time.time() - start),
                            result=result_sorted,
                            count=len(parse_result))

    else:
        return html("No Result!请将小说名反馈给本站,谢谢!")