else:
            idResult += str(random.choice(string.ascii_letters))
    return idResult


if currentSalt is None:
    salts = TextFile(erina_dir +
                     "/ErinaServer/Erina/auth/salt.erina").readlines()
    currentSalt = createRandomID(8)
    while currentSalt in salts:
        currentSalt = createRandomID(8)
    TextFile(erina_dir +
             "/ErinaServer/Erina/auth/salt.erina").append(currentSalt + "\n")
    lastTokenFile = TextFile(erina_dir +
                             "/ErinaServer/Erina/auth/lastToken.erina")
    if lastTokenFile.read().replace(" ", "").replace("\n", "") != "":
        currentToken = lastTokenFile.read().replace(" ", "").replace("\n", "")
    lastTokenFile.write("")


def createToken(lengthWithoutSalt):
    global currentToken
    global expiredTokens
    lastTokenFile.write("")
    tokenResult = str(currentSalt) + createRandomID(lengthWithoutSalt)
    if tokenResult in expiredTokens:
        tokenResult = createToken(lengthWithoutSalt)
    expiredTokens.append(currentToken)
    currentToken = tokenResult
    return tokenResult
Esempio n. 2
0
def iqdb_caching(image_hash):
    """
    Searches and caches IQDB for anime/manga related images.

    Erina Project - 2020\n
    © Anime no Sekai
    """
    try:
        log("ErinaCaches", 'Searching for IQDB Data...')
        
        ### If a file is given, send the file to iqdb.
        try:
            if image_hash.has_url:
                IQDBresponse = requests.get(f'https://iqdb.org/?url={image_hash.url}')
                StatsAppend(ExternalStats.iqdbCalls, "New Call")
            else:
                IQDBresponse = requests.post('https://iqdb.org/', files={'file': ('image_to_search',  image_hash.ImageIO) })
                StatsAppend(ExternalStats.iqdbCalls, "New Call")
        except:
            return CachingError("IQDB_RESPONSE", "An error occured while retrieving IQDB Data")

        ### If the image format is not supported by IQDB
        if 'Not an image or image format not supported' in IQDBresponse.text:
            return CachingError("IQDB_FORMAT_NOT_SUPPORTED", "The given image's format is not supported by IQDB")


    ###### IQDB SCRAPING
        try:
            iqdb = BeautifulSoup(IQDBresponse.text, 'html.parser')

        ##### Search for the IQDB result
            try:
                tables = iqdb.find_all('table')
                search_result = tables[1].findChildren("th")[0].get_text()
            except:
                return CachingError("IQDB_CLIENT_ERROR", f"An error occured while searching for the results: {exc_info()[0]}")

        ##### Verify if the result is relevant or not
            iqdb_tags = []
            if search_result == 'No relevant matches':
                return CachingError("IQDB_NO_RELEVANT_MATCH", "No relevant matches was found with IQDB", no_log=True)
            else:
                try:
                    ### Getting the tags from IQDB
                    alt_string = tables[1].findChildren("img")[0]['alt']
                    iqdb_tags = alt_string.split('Tags: ')[1].split(' ')
                except:
                    iqdb_tags = []
            
            #### Getting the Database URL from IQDB
            try:
                url = tables[1].find_all('td', attrs={'class': 'image'})[0].findChildren('a')[0]['href']
                url = 'https://' + url.split('//')[1]
            except:
                url = 'No URL'

            #### Getting the result image size
            try:
                size = tables[1].find_all('tr')[3].get_text().split(' [')[0]
            except:
                size = 'Unknown'

            #### Getting the image rating (if it is NSFW or not) 
            if tables[1].find_all('tr')[3].get_text().split()[1].replace('[', '').replace(']', '').replace(' ', '') == 'Safe':
                is_safe = True
            else:
                is_safe = False

            #### Getting the similarity
            try:
                similarity = tables[1].find_all('tr')[4].get_text().replace('% similarity', '')
            except:
                similarity = '0'


        ############ FUNCTION DEFINITION FOR RESULTS SCRAPING
            database = "Unknown"
            if url.find('gelbooru.') != -1:
                database = 'Gelbooru'
            
            elif url.find('danbooru.') != -1:
                database = 'Danbooru'

            elif url.find('zerochan.') != -1:
                database = 'Zerochan'

            elif url.find('konachan.') != -1:
                database = 'Konachan'

            elif url.find('yande.re') != -1:
                database = 'Yande.re'

            elif url.find('anime-pictures.') != -1:
                database = 'Anime-Pictures'

            elif url.find('e-shuushuu') != -1:
                database = 'E-Shuushuu'

            title = "Unknown"
            try:
                databaseWebsiteData = requests.get(url).text
                databaseWebsite = BeautifulSoup(databaseWebsiteData.text, 'html.parser')
                title = databaseWebsite.find("title").get_text()
            except:
                title = "Unkown"
        except:
            return CachingError("IQDB_PARSING", "An error occured while parsing the data from IQDB")

        try:
            #### Adding the results to the main result variable
            newCacheFile = TextFile(erina_dir + "/ErinaCaches/IQDB_Cache/" + str(image_hash) + ".erina")
            newCacheFile.append("   --- IQDB CACHE ---   \n")
            newCacheFile.append('\n')
            
            newCacheFile.append('IQDB Tags: ' + ":::".join(iqdb_tags) + "\n")
            newCacheFile.append('URL: ' + str(url) + "\n")
            newCacheFile.append('Title: ' + str(title) + "\n")
            newCacheFile.append('Size: ' + str(size)  + "\n")
            newCacheFile.append('isSafe: ' + str(is_safe) + "\n")
            newCacheFile.append('Similarity: ' + str(similarity) + "\n")
            newCacheFile.append('Database: ' + str(database) + "\n")
            return iqdb_parser.IQDBCache(newCacheFile.read())
        except:
            return CachingError("FILE_WRITE", f"An error occured while writing out the cache data to a file")
    except:
        return CachingError("UNKNOWN", "An unknown error occured while caching IQDB Data")