Exemple #1
0
def buildVisualNovelReply(searchText, isExpanded):
    """ Builds an VN reply from VNDB """
    try:
        cache = AniDB('vn')

        entry = cache.get(searchText)
        if entry and (datetime.now() -
                      cache.string_to_date(entry['last_update'])).days < 7:
            logger.info('Cache HIT ' + searchText + ' ' + entry['last_update'])
            return entry['info']

        vndb = VNDB()

        result = vndb.getVisualNovelDetails(searchText)

        vndb.close()

        if result:
            info = CommentBuilder.buildVisualNovelComment(isExpanded, result)
            cache.set(list({searchText}), info)
            logger.info('Cache UPSERT ' + searchText)
            return info
        else:
            print('No result found for ' + searchText)
            return None

    except Exception:
        traceback.print_exc()
        return None
Exemple #2
0
    async def get_app_oauth_info(self, **kwargs):
        """ get app oauth status
        :param kwargs: app_name
               customer_id
        :return: {'status': status, 'is_first_install': is_first_install}
        """

        customer_id = kwargs.get('customer_id')
        app_name = kwargs.get('app_name')

        if not customer_id or not app_name:
            logger.error('request param not found')
            return self.write_error_msg(PARAM_NOT_FOUND)

        if OAUTH_APPS.get(app_name) is None:
            logger.error('app %s not found' % app_name)
            return self.write_error_msg(PARAM_INVALID)

        try:
            request_data = await self._get_app_oauth_info(
                app_name, customer_id)
            logger.info(f'oauth info is : {request_data}')
            return self.write_json(request_data)
        except Exception as e:
            logger.error(f'get base info error:{e}')
            return self.write_error_msg(SERVER_ERROR)
Exemple #3
0
def getJSON(galleryNumber):
    galleryNumber = str(galleryNumber)
    entry = cache.get(galleryNumber)

    if entry and (datetime.now() -
                  cache.string_to_date(entry['last_update'])).days < 7:
        logger.info('Cache HIT ' + galleryNumber + ' ' + entry['last_update'])
        return entry['info']

    # request = getRequest(galleryNumber)  # ['tags'] #
    request = requests.get(API_URL_NHENTAI + galleryNumber)
    if request is None:
        return entry['info'] if entry else []
    if request.status_code == 404:
        return entry['info'] if entry else [404]
    # nhentaiTags = json.loads(re.search(r'(?<=N.gallery\().*(?=\))', request.text).group(0))
    print(request.text)
    print('\n\n')
    print(request.json())
    nhentaiTags = request.json()
    if "error" in nhentaiTags:
        return []
    else:
        cache.set(galleryNumber, nhentaiTags)
        logger.info('Cache UPSERT ' + galleryNumber)
        return nhentaiTags
Exemple #4
0
    async def get_token(self, app_id, customer_id):
        """refresh access_token
        :param app_id:
        :param customer_id:
        :return: access_token
        """
        if not app_id or not customer_id:
            logger.error('app_id or customer_id not exist')
            return None

        access_token_key = f'{app_id}-{customer_id}'
        try:
            access_token = self.redis_conn.get(access_token_key)
            if access_token:
                logger.info(f'get token from redis:{access_token}')
                return str(access_token, encoding='utf-8')

            app_token = await self.app_token.async_get_token_by_cid_aid(
                app_id, customer_id)
            if not app_token:
                return None
            if not self._is_expired(app_token):
                logger.info('get token from mongo ' +
                            app_token['access_token'])
                return app_token['access_token']
        except Exception as e:
            logger.error('get data from mongo error :%s' % e)
            return None

        refresh_url = f"{self.oauth_host}/oauth/refresh_token?app_id={app_id}&grant_type=refresh_token" \
            f"&refresh_token={app_token['refresh_token']}"

        logger.info('refresh_url: {refresh_url}')
        request = HTTPRequest(refresh_url,
                              method="POST",
                              allow_nonstandard_methods=True)

        try:
            http_response = await AsyncHTTPClient().fetch(request)
            result = json.loads(str(http_response.body, encoding='utf-8'))
            logger.info('remote result is: {result}')

            if result.get('code') != 0:
                logger.error(f'get access token error: {result}')
                return None

            app_token['access_token'] = result['result']['access_token']
            app_token['access_token_ctime'] = int(time())
            await self.app_token.async_update(app_id, customer_id, app_token)

            self.redis_conn.set(access_token_key, app_token['access_token'],
                                self.token_keep_time)
            logger.info(f'get token from mongodb: {app_token["access_token"]}')
            return app_token['access_token']

        except Exception as e:
            logger.error(f"get refresh token error: {e}")
            return None
Exemple #5
0
def getHTML(galleryNumber):
    entry = cache.get(galleryNumber)

    if entry and (datetime.now() - cache.string_to_date(entry['last_update'])).days < 7:
        logger.info('Cache HIT ' + str(galleryNumber) + ' ' + entry['last_update'])
        return entry['info']

    response = requests.get(API_URL_TSUMINO + str(galleryNumber))
    if response.status_code == 200:
        cache.set(galleryNumber, response.text)
        logger.info('Cache UPSERT ' + str(galleryNumber))
        return response.text
Exemple #6
0
 async def _get_app_oauth_info(self, app_name, customer_id):
     status = 0
     is_first_install = 0
     app_token = await self.app_token.async_get_token_by_cid_aid(
         OAUTH_APPS[app_name]['app_id'], customer_id)
     if app_token:
         is_first_install = app_token['is_first_install']
         refresh_token_ctime = app_token['refresh_token_ctime']
         logger.info(
             f'refresh token has used {int((time() - refresh_token_ctime)) / self.day:{0.2}} days'
         )
         if not self._is_need_refreshed(app_token):
             status = 1
     return {'status': status, 'is_first_install': is_first_install}
Exemple #7
0
    async def get_oauth_url(self, **kwargs):
        """get app oauth information and connect url
        :param kwargs: app_name
                       customer_id
                       xspe
                       fe_url
        :return: {'status': status, 'is_first_install': is_first_install, 'oauth_url': oauth_url}
        """

        fe_url = kwargs.get('fe_url')
        xspe = kwargs.get('xspe')
        customer_id = kwargs.get('customer_id')
        app_name = kwargs.get('app_name')
        if not customer_id or not fe_url or not xspe or not app_name:
            logger.error('request param not found')
            return self.write_error_msg(PARAM_NOT_FOUND)

        if not OAUTH_APPS.get(app_name):
            logger.error('app %s not found' % app_name)
            return self.write_error_msg(PARAM_INVALID)

        try:
            oauth_info = await self._get_app_oauth_info(app_name, customer_id)
            app_info = OAUTH_APPS[app_name]
            state_value = {
                'app_id': app_info['app_id'],
                'customer_id': customer_id,
                'app_name': app_name,
                'fe_url': fe_url,
                'is_first_install': oauth_info['is_first_install']
            }
            state = get_uuid()
            self.redis_conn.set(state, str(state_value), 60)

            oauth_url = f'{self.oauth_host}/oauth/connect?app_id={app_info["app_id"]}' \
                        f'&response_type=code' \
                        f'&scope=install_login' \
                        f'&state={state}' \
                        f'&xspe={xspe}' \
                        f'&customer_id={customer_id}' \
                        f'&redirect_uri={self.app_host}{app_info["redirect_uri"]}'

            oauth_info.update({'oauth_url': oauth_url})
            logger.info(f'oauth info is: {oauth_info}')
            return self.write_json(oauth_info)
        except Exception as e:
            logger.error(f'get oauth url error:{e}')
            return self.write_error_msg(SERVER_ERROR)
Exemple #8
0
def getJSON(galleryNumberAndToken):
    galleryID = galleryNumberAndToken[0]
    galleryToken = galleryNumberAndToken[1]
    entry = cache.get(galleryID)

    if entry and (datetime.now() -
                  cache.string_to_date(entry['last_update'])).days < 7:
        logger.info('Cache HIT ' + galleryID + ' ' + entry['last_update'])
        return entry['info']
    requestString = '{"method": "gdata","gidlist": [[' + str(
        galleryID) + ',' + '"' + galleryToken + '"]],"namespace": 1}'
    ehentaiResponse = requests.post(API_URL_EHENTAI,
                                    json=json.loads(requestString))
    if ehentaiResponse.status_code == 200:
        cache.set(galleryID, ehentaiResponse.json(), galleryToken)
        logger.info('Cache UPSERT ' + galleryID + ', ' + galleryToken)
    return ehentaiResponse.json()
Exemple #9
0
    async def get_callback(self, **kwargs):
        """
        :param kwargs:
                 code
                 state
        :return: redirect to fe_url
        """
        re_state = kwargs.get("state")
        code = kwargs.get("code")

        try:
            state_value = self.redis_conn.get(re_state)
            logger.info('state value is %s:' % state_value)
        except Exception as e:
            logger.error(f'get state from redis error: {e}')
            return self.write_error_msg(SERVER_ERROR)

        self.redis_conn.delete(re_state)
        if not state_value:
            return self.write_error_msg(PARAM_INVALID)

        dict_state_value = eval(state_value)

        app_id = dict_state_value['app_id']
        app_info = OAUTH_APPS[dict_state_value['app_name']]

        url = f"{self.oauth_host}/oauth/access_token?app_id={app_id}&secret={app_info['secret']}&code={code}"
        logger.info(f'begin: {url}')
        request = HTTPRequest(url,
                              method="POST",
                              allow_nonstandard_methods=True)
        try:
            http_response = await AsyncHTTPClient().fetch(request)
        except Exception as e:
            logger.error(f'ger refresh token error: {e}')
            return self.write_error_msg(REMOTE_SERVER_ERROR)

        token_result = json.loads(str(http_response.body, encoding='utf-8'))
        if token_result.get('code') != 0:
            return self.write_json(None,
                                   status_code=token_result.get('code', ''),
                                   msg=token_result.get('msg', ''))
        logger.info(f'remote result is: {token_result}')

        customer_id = dict_state_value['customer_id']
        try:
            app_token = token_result.get('result')
            app_token['app_id'] = app_id
            app_token['customer_id'] = customer_id
            app_token['is_first_install'] = dict_state_value[
                'is_first_install']
            # app status used for customer uninstall, app 0: install 1:uninstall
            app_token['status'] = APP_STATUS_NORMAL
            app_token['access_token_ctime'] = int(time())
            app_token['refresh_token_ctime'] = int(time())

            db_app_token = await self.app_token.async_get_token_by_cid_aid(
                app_id=app_id, customer_id=customer_id)
            logger.info(f'db_app_token: {db_app_token}')

            try:
                await self._save_app_token(dict_state_value, app_token,
                                           db_app_token)
            except Exception as e:
                logger.error(f'save app_token error :{e}')
                return self.write_error_msg(SERVER_ERROR)

            return self.redirect(dict_state_value['fe_url'])
        except Exception as e:
            logger.error('get token error: %s' % str(e))
            return self.write_error_msg(SERVER_ERROR)
Exemple #10
0
def buildMangaReply(searchText, isExpanded):
    """ Builds a manga reply from multiple sources """
    try:
        cache = AniDB('manga')

        entry = cache.get(searchText)
        if entry and (datetime.now() -
                      cache.string_to_date(entry['last_update'])).days < 7:
            logger.info('Cache HIT ' + searchText + ' ' + entry['last_update'])
            return entry['info']

        ani = {
            'search_function': Anilist.getMangaDetails,
            'title_function': Anilist.getTitles,
            'synonym_function': Anilist.getSynonyms,
            'checked_synonyms': [],
            'result': None
        }
        kit = {
            'search_function': Kitsu.search_manga,
            'synonym_function': Kitsu.get_synonyms,
            'title_function': Kitsu.get_titles,
            'checked_synonyms': [],
            'result': None
        }
        mu = {'search_function': MU.getMangaURL, 'result': None}
        ap = {'search_function': AniP.getMangaURL, 'result': None}

        data_sources = [ani, kit]

        synonyms = set([searchText])
        titles = set()

        for x in range(len(data_sources)):
            for source in data_sources:
                if source['result']:
                    break
                else:
                    for title in titles:
                        if title in source['checked_synonyms']:
                            break

                        if source['result']:
                            break

                        source['result'] = source['search_function'](title)
                        source['checked_synonyms'].append(title)

                        if source['result']:
                            break

                    for synonym in synonyms:
                        if synonym in source['checked_synonyms']:
                            break

                        if source['result']:
                            break

                        search_function = source['search_function']
                        source['result'] = search_function(synonym)
                        source['checked_synonyms'].append(synonym)

                        if source['result']:
                            break

                if source['result']:
                    result = source['result']
                    synonym_function = source['synonym_function']
                    title_function = source['title_function']
                    synonyms.update(
                        [s.lower() for s in synonym_function(result)])
                    for t in title_function(result):
                        if t is not None:
                            titles.update(t.lower())

        if ani['result'] or kit['result']:
            info = CommentBuilder.buildMangaComment(isExpanded=isExpanded,
                                                    ani=ani['result'],
                                                    kit=kit['result'])
            cache.set(list(synonyms), info)
            logger.info('Cache UPSERT ' + searchText)
            return info
        else:
            print('No result found for ' + searchText)
            return None

    except Exception:
        traceback.print_exc()
        return None
Exemple #11
0
 def log(self, event):
     logger.info('USER {} {} CHANNEL {}'.format(self.current_user,
                                                 event, self.chnl))