async def delete_user(request):
    """
    Delte users(administrator can do it)
    :param request:
    :return:
        :   -1  administrator's session expire, need to login again
        :   0   not delete
        :   1   delete successfully
    """
    user = request['session'].get('user', None)
    role = request['session'].get('role', None)
    data = parse_qs(str(request.body, encoding='utf-8'))
    motor_db = motor_base.get_db()
    # check whether user is administrator
    if user and role == "Admin":
        if data.get('user_name', None):
            user_name_delete = data.get('user_name', None)[0]
        else:
            user_name_delete = data.get('user_name', '')
        try:
            await motor_db.user.delete_one({'user': user_name_delete})
            LOGGER.info('You have deleted the user')
            return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
async def delete_bookmark(request):
    """
    Delete bookmark
    :param request:
    :return:
        :   -1  user's session expire, need to login again
        :   0   not delete
        :   1   delete successfully
    """
    user = request['session'].get('user', None)
    data = parse_qs(str(request.body, encoding='utf-8'))
    bookmark_url = data.get('bookmarkurl', '')
    if user and bookmark_url:
        bookmark = unquote(bookmark_url[0])
        try:
            motor_db = motor_base.get_db()
            await motor_db.user_message.update_one({'user': user},
                                                   {'$pull': {'bookmarks': {"bookmark": bookmark}}})
            LOGGER.info('You have already delete one bookmark')
            return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
async def delete_bookshelf(request):
    """
    Delete bookshelf
    :param request:
    :return:
        :   -1  user's session expire, need to login again
        :   0   not delete
        :   1   delete successfully
    """
    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 = motor_base.get_db()
            await motor_db.user_message.update_one({'user': user},
                                                   {'$pull': {'books_url': {"book_url": unquote(book_url)}}})
            LOGGER.info('You have deleted bookshelf')
            return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
async def add_bookshelf(request):
    """
    Add bookshelf (any user can)
    :param request:
    :return:
        :   -1  user's session expire, need to login again
        :   0   not add
        :   1   add successfully
    """
    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 = motor_base.get_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('You have added this page successfully in your bookshelf!')
                return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
async def add_bookmark(request):
    """
    Add bookmark (any user can)
    :param request:
    :return:
        :   -1  user's session expire, need to login again
        :   0   not add
        :   1   add successfully
    """
    user = request['session'].get('user', None)
    data = parse_qs(str(request.body, encoding='utf-8'))
    # get the bookmark url
    bookmark_url = data.get('bookmark_url', '')
    if user and bookmark_url:
        url = unquote(bookmark_url[0])
        time = get_time()
        try:
            motor_db = motor_base.get_db()
            res = await motor_db.user_message.update_one({'user': user}, {'$set': {'last_update_time': time}},
                                                         upsert=True)
            if res:
                # store the bookmark data in the mongodb, user_message collection
                await motor_db.user_message.update_one(
                    {'user': user, 'bookmarks.bookmark': {'$ne': url}},
                    {'$push': {'bookmarks': {'bookmark': url, 'add_time': time}}})
                LOGGER.info('bookmark has been added')
                return json({'status': 1})
        except Exception as e:
            LOGGER.exception(e)
            return json({'status': 0})
    else:
        return json({'status': -1})
 async def fetch_url(self, url, params, headers):
     with async_timeout.timeout(15):
         try:
             # Asynchronous HTTP client-To get something from the web:.ß
             #  reference: https://github.com/aio-libs/aiohttp
             async with aiohttp.ClientSession() as client:
                 async with client.get(url, params=params,
                                       headers=headers) as response:
                     assert response.status == 200
                     LOGGER.info('Task url: {}'.format(response.url))
                     try:
                         # get the source code of search result in search engine
                         text = await response.text()
                     except:
                         text = await response.read()
                     return text
         except Exception as e:
             LOGGER.exception(e)
             return None
Example #7
0
async def add_session_to_request(request):
    # before each request initialize a session
    # using the client's request
    host = request.headers.get('host', None)
    user_agent = request.headers.get('user-agent', None)
    if user_agent:
        user_ip = request.headers.get('X-Forwarded-For')
        LOGGER.info('user ip is: {}'.format(user_ip))
        if user_ip in CONFIG.FORBIDDEN:
            return html("<h3>The website is under maintenance</h3>")
        if CONFIG.VAL_HOST == 'true':
            if not host or host not in CONFIG.HOST:
                return redirect('http://www.quickreading.net')
        if CONFIG.WEBSITE['IS_RUNNING']:
            await app.session_interface.open(request)
        else:
            return html("<h3>The website is under maintenance</h3>")
    else:
        return html("<h3>The website is under maintenance</h3>")
Example #8
0
async def target_fetch(url, headers, timeout=15):
    """
    :param url: target url
    :return: text
    """
    with async_timeout.timeout(timeout):
        try:
            async with aiohttp.ClientSession() as client:
                async with client.get(url, headers=headers) as response:
                    assert response.status == 200
                    LOGGER.info('Task url: {}'.format(response.url))
                    try:
                        text = await response.text()
                    except:
                        try:
                            text = await response.read()
                        except aiohttp.ServerDisconnectedError as e:
                            LOGGER.exception(e)
                            text = None
                    return text
        except Exception as e:
            LOGGER.exception(str(e))
            return None
Example #9
0
def init_cache(app, loop):
    LOGGER.info("Starting Aiocache : Asyncio Cache Manager For Redis")
    app.config.from_object(CONFIG)
    REDIS_DICT = CONFIG.REDIS_DICT
    # configuration: use aiocache to asyncio manager redis(the port)
    # reference https://github.com/argaen/aiocache
    aiocache.settings.set_defaults(
        class_="aiocache.RedisCache",
        endpoint=REDIS_DICT.get('REDIS_ENDPOINT', 'localhost'),
        port=REDIS_DICT.get('REDIS_PORT', 6379),
        db=REDIS_DICT.get('CACHE_DB', 0),
        password=REDIS_DICT.get('REDIS_PASSWORD', None),
        loop=loop,
    )
    LOGGER.info("Starting Redis")
    # start redis
    redis_session = RedisSession()
    # redis instance for this app
    app.get_redis_pool = redis_session.get_redis_pool
    # pass the getter method for the connection pool into the session
    app.session_interface = RedisSessionInterface(
        app.get_redis_pool,
        cookie_name="quickReading_cookie",
        expiry=30 * 24 * 60 * 60)