Exemplo n.º 1
0
def buildAnimeReply(searchText, isExpanded, baseComment, blockTracking=False):
    try:
        mal = {
            'search_function': MAL.getAnimeDetails,
            'synonym_function': MAL.getSynonyms,
            'checked_synonyms': [],
            'result': None
        }
        kit = {
            'search_function': Kitsu.search_anime,
            'synonym_function': Kitsu.get_synonyms,
            'checked_synonyms': [],
            'result': None
        }
        ani = {
            'search_function': Anilist.getAnimeDetails,
            'synonym_function': Anilist.getSynonyms,
            'checked_synonyms': [],
            'result': None
        }
        ap = {'search_function': AniP.getAnimeURL, 'result': None}
        adb = {'search_function': AniDB.getAnimeURL, 'result': None}

        try:
            sqlCur.execute(
                'SELECT dbLinks FROM synonyms WHERE type = "Anime" 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']

                kitsyn = None
                if 'kit' in synonym and synonym['kit']:
                    kitsyn = synonym['kit']

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

                apsyn = None
                if 'ap' in synonym and synonym['ap']:
                    apsyn = synonym['ap']

                adbsyn = None
                if 'adb' in synonym and synonym['adb']:
                    adbsyn = synonym['adb']

                mal['result'] = MAL.getAnimeDetails(
                    malsyn[0], malsyn[1]) if malsyn else None
                kit['result'] = Kitsu.get_anime(kitsyn) if kitsyn else None
                ani['result'] = Anilist.getAnimeDetailsById(
                    anisyn) if anisyn else None
                ap['result'] = AniP.getAnimeURLById(apsyn) if apsyn else None
                adb['result'] = AniDB.getAnimeURLById(
                    adbsyn) if adbsyn else None

        else:
            data_sources = [ani, kit, mal]
            aux_sources = [ap, adb]

            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'] = 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'] = source['search_function'](synonym)

                    if source['result']:
                        break

        if ani['result'] or mal['result'] or kit['result']:
            try:
                titleToAdd = ''
                if ani['result']:
                    if 'title_romaji' in ani['result']:
                        titleToAdd = ani['result']['title_romaji']
                elif mal['result']:
                    if 'title' in mal['result']:
                        titleToAdd = mal['result']['title']
                elif kit['result']:
                    if 'title_romaji' in kit['result']:
                        titleToAdd = kit['result']['title_romaji']

                if (str(baseComment.subreddit).lower is not 'nihilate') and (
                        str(baseComment.subreddit).lower
                        is not 'roboragi') and not blockTracking:
                    DatabaseHandler.addRequest(titleToAdd, 'Anime',
                                               baseComment.author.name,
                                               baseComment.subreddit)
            except:
                traceback.print_exc()
                pass

        return CommentBuilder.buildAnimeComment(isExpanded, mal['result'],
                                                ani['result'], ap['result'],
                                                adb['result'], kit['result'])

    except Exception as e:
        traceback.print_exc()
        return None
Exemplo n.º 2
0
def buildAnimeReply(searchText, isExpanded, baseComment, blockTracking=False):
    try:

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

        alternateLinks = sqlCur.fetchone()

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

            if (synonym['mal']):
                mal = MAL.getAnimeDetails(synonym['mal'])
            if (synonym['hb']):
                hb = Hummingbird.getAnimeDetails(synonym['hb'])
            if (synonym['ani']):
                ani = Anilist.getAnimeDetails(synonym['ani'])
            if (synonym['ap']):
                ap = AniP.getAnimeURL(synonym['ap'])
        else:
            #Basic breakdown:
            #If Anilist finds something, use it to find the HB version.
            #If we can't find it, try with HB and use it to try and "refind" Anilist
            #If we hit HB, we don't need to look for MAL, since we can get the MAL ID from within HB. If we don't hit HB, find MAL on its own.
            #If, at the end, we have something from Anilist, get the full set of Anilist data
            #If it hits anything, add it to the request-tracking DB.
            
            ani = Anilist.getAnimeDetails(searchText)
            
            if (ani is not None):
                hb = Hummingbird.getAnimeDetails(ani['title_romaji'])

                if (hb is None):
                    for synonym in ani['synonyms']:
                        hb = Hummingbird.getAnimeDetails(synonym)
                        if hb is not None:
                            break
                    hb = Hummingbird.getAnimeDetails(ani['title_english'])
            else:
                hb = Hummingbird.getAnimeDetails(searchText)
                if (hb is not None):
                   ani = Anilist.getAnimeDetails(hb['title'])

            #Doing MAL stuff
            if not mal:
                if hb:
                    mal = MAL.getAnimeDetails(hb['title'])

                    if not mal and hb['alternate_title']:
                        if (hb['alternate_title']):
                            mal = MAL.getAnimeDetails(hb['alternate_title'])
                        
                if ani and not mal:
                    mal = MAL.getAnimeDetails(ani['title_romaji'])

                    if not mal:
                        mal = MAL.getAnimeDetails(ani['title_english'])

                    if not mal and ani['synonyms']:
                        for synonym in ani['synonyms']:
                            if mal:
                                break
                            mal = MAL.getAnimeDetails(synonym)

                if not mal:
                    mal = MAL.getAnimeDetails(searchText)

                if mal and not hb:
                    hb = Hummingbird.getAnimeDetails(mal['title'])
                    if not hb:
                        hb = Hummingbird.getAnimeDetails(mal['english'])

                if mal and not ani:
                    ani = Anilist.getAnimeDetails(mal['title'])
                    if not ani:
                        ani = Anilist.getAnimeDetails(mal['english'])

        #----- Finally... -----#
        try:
            if ani is not None:
                aniFull = Anilist.getFullAnimeDetails(ani['id'])
                if aniFull is not None:
                    ani = aniFull
        except:
            pass

        if (ani is not None) or (hb is not None) or (mal is not None):
            try:
                titleToAdd = ''
                if mal:
                    titleToAdd = mal['title']
                if hb:
                    titleToAdd = hb['title']
                if ani:
                    titleToAdd = ani['title_romaji']

                #Do Anime-Planet stuff
                if mal and not ap:
                    if mal['title'] and not ap:
                        ap = AniP.getAnimeURL(mal['title'])
                    if mal['english'] and not ap:
                        ap = AniP.getAnimeURL(mal['english'])
                    if mal['synonyms'] and not ap:
                        for synonym in mal['synonyms']:
                            if ap:
                                break
                            ap = AniP.getAnimeURL(synonym)

                if hb and not ap:
                    if hb['title'] and not ap:
                        ap = AniP.getAnimeURL(hb['title'])
                    if hb['alternate_title'] and not ap:
                        ap = AniP.getAnimeURL(hb['alternate_title'])
                
                if ani and not ap:
                    if ani['title_english'] and not ap:
                        ap = AniP.getAnimeURL(ani['title_english'])
                    if ani['title_romaji'] and not ap:
                        ap = AniP.getAnimeURL(ani['title_romaji'])
                    if ani['synonyms'] and not ap:
                        for synonym in ani['synonyms']:
                            if ap:
                                break
                            ap = AniP.getAnimeURL(synonym)

                if (str(baseComment.subreddit).lower is not 'nihilate') and (str(baseComment.subreddit).lower is not 'roboragi') and not blockTracking:
                    DatabaseHandler.addRequest(titleToAdd, 'Anime', baseComment.author.name, baseComment.subreddit)
            except:
                traceback.print_exc()
                pass

        if ani is not None:
            if ani['adult'] is True:
                print("NSFW ENTRY")
                mal = None
                hb = None
                ani = None
                ap = None
        
        return CommentBuilder.buildAnimeComment(isExpanded, mal, hb, ani, ap)

    except Exception as e:
        traceback.print_exc()
        return None
Exemplo n.º 3
0
def buildAnimeReply(searchText, isExpanded, baseComment, blockTracking=False):
    """ Builds an anime reply from multiple sources """
    try:
        kit = {
            'search_function': Kitsu.search_anime,
            'synonym_function': Kitsu.get_synonyms,
            'title_function': Kitsu.get_titles,
            'checked_synonyms': [],
            'result': None
        }
        ani = {
            'search_function': Anilist.getAnimeDetails,
            'synonym_function': Anilist.getSynonyms,
            'title_function': Anilist.getTitles,
            'checked_synonyms': [],
            'result': None
        }
        ap = {'search_function': AniP.getAnimeURL, 'result': None}

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

        alternateLinks = sqlCur.fetchone()

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

            if synonym:
                kitsyn = None
                if 'kit' in synonym and synonym['kit']:
                    kitsyn = synonym['kit']

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

                apsyn = None
                if 'ap' in synonym and synonym['ap']:
                    apsyn = synonym['ap']

                kit['result'] = Kitsu.get_anime(kitsyn) if kitsyn else None

                if anisyn:
                    ani['result'] = Anilist.getAnimeDetailsById(anisyn)
                else:
                    ani['result'] = None

                ap['result'] = AniP.getAnimeURLById(apsyn) if apsyn else None

        else:
            data_sources = [ani, kit]
            aux_sources = [ap]

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

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

                            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)])
                        titles.update(
                            [t.lower() for t in title_function(result)])

            for source in aux_sources:
                for title in titles:
                    source['result'] = source['search_function'](synonym)

                    if source['result']:
                        break

                if not source['result']:
                    for synonym in synonyms:
                        source['result'] = source['search_function'](synonym)

                        if source['result']:
                            break

        if ani['result'] or kit['result']:
            try:
                titleToAdd = ''
                if ani['result']:
                    if 'title_romaji' in ani['result']:
                        titleToAdd = ani['result']['title_romaji']
                elif kit['result']:
                    if 'title_romaji' in kit['result']:
                        titleToAdd = kit['result']['title_romaji']

                subreddit = str(baseComment.subreddit).lower
                ignored_subreddits = ('nihilate', 'roboragi')
                if subreddit not in ignored_subreddits and not blockTracking:
                    DatabaseHandler.addRequest(
                        name=titleToAdd,
                        rType='Anime',
                        requester=baseComment.author.name,
                        subreddit=baseComment.subreddit)
            except Exception:
                traceback.print_exc()
                pass

        if ani['result'] or kit['result']:
            return CommentBuilder.buildAnimeComment(isExpanded=isExpanded,
                                                    ani=ani['result'],
                                                    ap=ap['result'],
                                                    kit=kit['result'])
        else:
            print('No result found for ' + searchText)
            return None

    except Exception:
        traceback.print_exc()
        return None
Exemplo n.º 4
0
async def buildAnimeReply(searchText,
                          message,
                          isExpanded,
                          canEmbed,
                          blockTracking=False):
    try:
        mal = {
            'search_function': MAL.getAnimeDetails,
            'synonym_function': MAL.getSynonyms,
            'checked_synonyms': [],
            'result': None
        }
        ani = {
            'search_function': Anilist.getAnimeDetails,
            'synonym_function': Anilist.getSynonyms,
            'checked_synonyms': [],
            'result': None
        }
        ap = {'search_function': AniP.getAnimeURL, 'result': None}
        adb = {'search_function': AniDB.getAnimeURL, 'result': None}

        try:
            sqlCur.execute(
                'SELECT dbLinks FROM synonyms WHERE type = "Anime" 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']

                apsyn = None
                if 'ap' in synonym and synonym['ap']:
                    apsyn = synonym['ap']

                adbsyn = None
                if 'adb' in synonym and synonym['adb']:
                    adbsyn = synonym['adb']

                mal['result'] = await MAL.getAnimeDetails(
                    malsyn[0], malsyn[1]) if malsyn else None
                ani['result'] = await Anilist.getAnimeDetailsById(
                    anisyn) if anisyn else None
                ap['result'] = AniP.getAnimeURLById(apsyn) if apsyn else None
                adb['result'] = AniDB.getAnimeURLById(
                    adbsyn) if adbsyn else None
                print(ani['result'])

        else:
            data_sources = [ani, mal]
            aux_sources = [ap, adb]
            #aux_sources = [ap]

            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']:
                    if 'title' in mal['result']:
                        titleToAdd = mal['result']['title']
                '''if hb['result']:
                    if 'title' in hb['result']:
                        titleToAdd = hb['result']['title']'''
                if ani['result']:
                    if 'title_romaji' in ani['result']:
                        titleToAdd = ani['result']['title_romaji']

                if not blockTracking:
                    DatabaseHandler.addRequest(titleToAdd, 'Anime',
                                               message.author.id,
                                               message.server.id)
            except:
                traceback.print_exc()
                pass
        if mal['result']:
            print('trying to add an anime to cache')
            try:
                DatabaseHandler.addMalEntry('malanime', mal['result'])
            except:
                traceback.print_exc()
                pass
        if ani:
            try:
                DatabaseHandler.addAniEntry('anilistanime', ani['result'])
            except:
                traceback.print_exc()
                pass
        if not canEmbed:
            return CommentBuilder.buildAnimeComment(isExpanded, mal['result'],
                                                    ani['result'],
                                                    ap['result'],
                                                    adb['result'])
        else:
            return CommentBuilder.buildAnimeEmbed(isExpanded, mal['result'],
                                                  ani['result'], ap['result'],
                                                  adb['result'])

    except Exception as e:
        traceback.print_exc()
        return None
Exemplo n.º 5
0
def buildAnimeReply(searchText, isExpanded, baseComment, blockTracking=False):
    try:

        mal = {'search_function': MAL.getAnimeDetails,
                'synonym_function': MAL.getSynonyms,
                'checked_synonyms': [],
                'result': None}
        hb = {'search_function': Hummingbird.getAnimeDetails,
                'synonym_function': Hummingbird.getSynonyms,
                'checked_synonyms': [],
                'result': None}
        ani = {'search_function': Anilist.getAnimeDetails,
                'synonym_function': Anilist.getSynonyms,
                'checked_synonyms': [],
                'result': None}
        ap = {'search_function': AniP.getAnimeURL,
                'result': None}
        adb = {'search_function': AniDB.getAnimeURL,
                'result': None}
        
        try:
            sqlCur.execute('SELECT dbLinks FROM synonyms WHERE type = "Anime" 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']

                hbsyn = None
                if 'hb' in synonym and synonym['hb']:
                    hbsyn = synonym['hb']

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

                apsyn = None
                if 'ap' in synonym and synonym['ap']:
                    apsyn = synonym['ap']

                adbsyn = None
                if 'adb' in synonym and synonym['adb']:
                    adbsyn = synonym['adb']

                mal['result'] = MAL.getAnimeDetails(malsyn[0],malsyn[1]) if malsyn else None
                hb['result'] = Hummingbird.getAnimeDetailsById(hbsyn) if hbsyn else None
                ani['result'] = Anilist.getAnimeDetailsById(anisyn) if anisyn else None
                ap['result'] = AniP.getAnimeURLById(apsyn) if apsyn else None
                adb['result'] = AniDB.getAnimeURLById(adbsyn) if adbsyn else None
                
        else:
            data_sources = [ani, hb, mal]
            aux_sources = [ap, adb]

            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'] = 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'] = source['search_function'](synonym)

                    if source['result']:
                        break

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

                if (str(baseComment.subreddit).lower is not 'nihilate') and (str(baseComment.subreddit).lower is not 'roboragi') and not blockTracking:
                    DatabaseHandler.addRequest(titleToAdd, 'Anime', baseComment.author.name, baseComment.subreddit)
            except:
                traceback.print_exc()
                pass
        
        return CommentBuilder.buildAnimeComment(isExpanded, mal['result'], hb['result'], ani['result'], ap['result'], adb['result'])

    except Exception as e:
        traceback.print_exc()
        return None
Exemplo n.º 6
0
def buildAnimeReply(searchText, isExpanded, baseComment):
    try:
        #Basic breakdown:
        #If Anilist finds something, use it to find the HB version.
        #If we can't find it, try with HB and use it to try and "refind" Anilist
        #If we hit HB, we don't need to look for MAL, since we can get the MAL ID from within HB. If we don't hit HB, find MAL on its own.
        #If, at the end, we have something from Anilist, get the full set of Anilist data
        #If it hits anything, add it to the request-tracking DB.
        
        ani = Anilist.getAnimeDetails(searchText)
        hb = None
        mal = None
        
        if (ani is not None):
            hb = Hummingbird.getAnimeDetails(ani['title_romaji'])

            if (hb is None):
                for synonym in ani['synonyms']:
                    hb = Hummingbird.getAnimeDetails(synonym)
                    if hb is not None:
                        break
                hb = Hummingbird.getAnimeDetails(ani['title_english'])
        else:
            hb = Hummingbird.getAnimeDetails(searchText)
            if (hb is not None):
               ani = Anilist.getAnimeDetails(hb['title'])

        if (hb is None):
            mal = MAL.getAnimeDetails(searchText)
            if (mal is not None):
                hb = Hummingbird.getAnimeDetails(mal['title'])
                if (hb is None):
                    hb = Hummingbird.getAnimeDetails(mal['english'])

                if (ani is None):
                    ani = Anilist.getAnimeDetails(mal['title'])
                    if (ani is None):
                        ani = Anilist.getAnimeDetails(mal['english'])

        try:
            if ani is not None:
                aniFull = Anilist.getFullAnimeDetails(ani['id'])
                if aniFull is not None:
                    ani = aniFull
        except:
            pass

        if (ani is not None) or (hb is not None) or (mal is not None):
            try:
                titleToAdd = ''
                if ani is None:
                    titleToAdd = hb['title']
                elif hb is not None:
                    titleToAdd = ani['title_romaji']
                else:
                    titleToAdd = mal['title']

                if (str(baseComment.subreddit).lower is not 'nihilate') and (str(baseComment.subreddit).lower is not 'roboragi'):
                    DatabaseHandler.addRequest(titleToAdd, 'Anime', baseComment.author.name, baseComment.subreddit)
            except:
                traceback.print_exc()
                pass

            if ani is not None:
                if ani['adult'] is True:
                    print("NSFW ENTRY")
                    mal = None
                    hb = None
                    ani = None
            
            return CommentBuilder.buildAnimeComment(isExpanded, mal, hb, ani)

    except Exception as e:
        traceback.print_exc()
        return None