Exemple #1
0
async def buildLightNovelReply(searchText,
                               isExpanded,
                               message,
                               canEmbed,
                               blockTracking=False):
    try:
        mal = {
            'search_function': MAL.getLightNovelDetails,
            'synonym_function': MAL.getSynonyms,
            'checked_synonyms': [],
            'result': None
        }
        ani = {
            'search_function': Anilist.getLightNovelDetails,
            'synonym_function': Anilist.getSynonyms,
            'checked_synonyms': [],
            'result': None
        }
        nu = {'search_function': NU.getLightNovelURL, 'result': None}
        lndb = {'search_function': LNDB.getLightNovelURL, 'result': None}

        try:
            sqlCur.execute(
                'SELECT dbLinks FROM synonyms WHERE type = "LN" and lower(name) = ?',
                [searchText.lower()])
        except sqlite3.Error as e:
            print(e)

        alternateLinks = sqlCur.fetchone()

        if (alternateLinks):
            synonym = json.loads(alternateLinks[0])

            if synonym:
                malsyn = None
                if 'mal' in synonym and synonym['mal']:
                    malsyn = synonym['mal']

                anisyn = None
                if 'ani' in synonym and synonym['ani']:
                    anisyn = synonym['ani']

                nusyn = None
                if 'nu' in synonym and synonym['nu']:
                    nusyn = synonym['nu']

                lndbsyn = None
                if 'lndb' in synonym and synonym['lndb']:
                    lndbsyn = synonym['lndb']

                mal['result'] = await MAL.getLightNovelDetails(
                    malsyn[0], malsyn[1]) if malsyn else None
                ani['result'] = await Anilist.getMangaDetailsById(
                    anisyn) if anisyn else None
                nu['result'] = NU.getLightNovelById(nusyn) if nusyn else None
                lndb['result'] = LNDB.getLightNovelById(
                    lndbsyn) if lndbsyn else None

        else:
            data_sources = [ani, mal]
            aux_sources = [nu, lndb]

            synonyms = set([searchText])

            for x in range(len(data_sources)):
                for source in data_sources:
                    if source['result']:
                        break
                    else:
                        for synonym in synonyms:
                            if synonym in source['checked_synonyms']:
                                continue

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

                            if source['result']:
                                break

                    if source['result']:
                        synonyms.update([
                            synonym.lower() for synonym in
                            source['synonym_function'](source['result'])
                        ])

            for source in aux_sources:
                for synonym in synonyms:
                    source['result'] = await source['search_function'](synonym)

                    if source['result']:
                        break

        if ani['result'] or mal['result']:
            try:
                titleToAdd = ''
                if mal['result']:
                    titleToAdd = mal['result']['title']
                if ani['result']:
                    try:
                        titleToAdd = ani['result']['title_romaji']
                    except:
                        titleToAdd = ani['result']['title_english']

                if (str(message.server).lower is not 'nihilate') and (
                        str(message.server).lower
                        is not 'roboragi') and not blockTracking:
                    DatabaseHandler.addRequest(titleToAdd, 'LN',
                                               message.author.id,
                                               message.server.id)
            except:
                traceback.print_exc()
                pass
        if mal['result']:
            try:
                DatabaseHandler.addMalEntry('malmanga', mal['result'])
            except:
                traceback.print_exc()
                pass
        if ani['result']:
            try:
                DatabaseHandler.addAniEntry('anilistmanga', ani['result'])
            except:
                traceback.print_exc()
                pass
        if not canEmbed:
            return CommentBuilder.buildLightNovelComment(
                isExpanded, mal['result'], ani['result'], nu['result'],
                lndb['result'])
        else:
            return CommentBuilder.buildLightNovelEmbed(isExpanded,
                                                       mal['result'],
                                                       ani['result'],
                                                       nu['result'],
                                                       lndb['result'])
    except Exception as e:
        traceback.print_exc()
        return None