def create_information():
    lifeeasy.change_working_dir(filecenter.get_correct_path(destination_dir))
    print(destination_dir)
    lifeeasy.sleep(3)
    information_file = open('cleaning_information.txt', 'w+')
    information_file.write('Here are the information about this folder.\n')
    information_file.write('\n')
    information_file.write('Cleaned the: {date} {time}\n'.format(
        date=lifeeasy.today(), time=lifeeasy.current_time()))
    information_file.write('\n')
    information_file.write('\n')
    information_file.write('\n')
    information_file.write('Cleaned folder: {}\n'.format(cleaning_dir))
    information_file.write('Result folder: {}\n'.format(destination_dir))
    information_file.write('\n')
    information_file.write(
        'Number of items in given folder: {}\n'.format(number_of_items))
    information_file.write(
        'Number of moved items: {}\n'.format(number_of_moved_items))
    information_file.write('\n')
    information_file.write('\n')
    information_file.write('Generated by Folder Cleaner\n')
    information_file.write('©Anime no Sekai - 2020\n')
    information_file.close()
def translate(text, destination_language, source_language="auto", cache=False, debug=False):
    """
    Translates the given text into the chosen language by scraping Google Translate with Selenium.

    Returns a string with the text translated.\n
    Returns "An error occured while translating: translation not found." if the translation was not found in the webpage. This might come from a mistyped language code.
    """
    from .internal.domain import gt_domain
    global last_translation
    if debug:
        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Starting Translation...\n', append=True)
    if debug:
        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Searching Caches...\n', append=True)
    cache_result = search_translation_cache(source_language=source_language, destination_language=destination_language, source=text)
    if not cache_result is None:
        if debug:
            line_number = cache_result['line_number']
            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Translation found in Caches (line {line_number})\n', append=True)
        return cache_result['result']
    else:
        if driver is None:
            if debug:
                write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - No driver selected\n', append=True)
            raise BrowserError("Browser is not set yet.\n Please set it with browser()")
        if not connected:
            if debug:
                write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Driver disconnected, last driver: {driver_name}\n', append=True)
            raise BrowserError(f'You disconnected the last browser in use ({driver_name}).\n Please reconnect one with browser()')
        else:
            if debug:
                write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - First attempt url is: https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}\n', append=True)
            driver.get(f"https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}")
            try:
                if debug:
                    write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Getting DOM Element by Class Name (tlid-translation)\n', append=True)
                result = driver.find_element_by_class_name("tlid-translation")
                if result.text == last_translation or result.text == str(last_translation + '...'):
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Translation not finished detected... Refreshing page before new attempt...\n', append=True)
                    driver.refresh()
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Getting DOM Element by Class Name (tlid-translation)\n', append=True)
                    result = driver.find_element_by_class_name("tlid-translation")
                if debug:
                    write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Setting last_translation global variable to new translation...\n', append=True)
                last_translation = str(result.text)
                if cache:
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Adding result to cache...\n', append=True)
                    add_translation_cache(source_language=source_language, destination_language=destination_language, source=text, result=str(result.text))
                if debug:
                    write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Returning value... {result.text}\n', append=True)
                return str(result.text)
            except NoSuchElementException:
                if debug:
                    write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Element not found on page...\n', append=True)
                try:
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] New attempt...\n', append=True)
                    driver.get(f"https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}")
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Getting DOM Element by Class Name (tlid-translation)\n', append=True)
                    result = driver.find_element_by_class_name("tlid-translation")
                    if result.text == last_translation or result.text == str(last_translation + '...'):
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Translation not finished detected... Refreshing page before new attempt...\n', append=True)
                        driver.refresh()
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Getting DOM Element by Class Name (tlid-translation)\n', append=True)
                        result = driver.find_element_by_class_name("tlid-translation")
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Setting last_translation global variable to new translation...\n', append=True)
                    last_translation = str(result.text)
                    if cache:
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Adding result to cache...\n', append=True)
                        add_translation_cache(source_language=source_language, destination_language=destination_language, source=text, result=str(result.text))
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Returning value... {result.text}\n', append=True)
                    return str(result.text)
                except NoSuchElementException:
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Element not found on page...\n', append=True)
                    try:
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] New attempt...\n', append=True)
                        driver.get(f"https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}")
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Translation not finished detected... Refreshing page before new attempt...\n', append=True)
                        driver.refresh()
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Getting DOM Element by Class Name (tlid-translation)\n', append=True)
                        result = driver.find_element_by_class_name("tlid-translation")
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Setting last_translation global variable to new translation...\n', append=True)
                        last_translation = str(result.text)
                        if cache:
                            if debug:
                                write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Adding result to cache...\n', append=True)
                            add_translation_cache(source_language=source_language, destination_language=destination_language, source=text, result=str(result.text))
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Returning value... {result.text}\n', append=True)
                        return str(result.text)
                    except NoSuchElementException:
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Element not found, aborting...\n', append=True)
                        return "An error occured while translating: translation not found."
                    except Exception as e:
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Unknown error\n', append=True)
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Error details: {str(e)}\n', append=True)
                        return "An error occured while translating: unknown error."
                except Exception as e:
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Unknown error\n', append=True)
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Error details: {str(e)}\n', append=True)
                    return "An error occured while translating: unknown error."
            except Exception as e:
                if debug:
                    write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Unknown error\n', append=True)
                    write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Error details: {str(e)}\n', append=True)
                try:
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] New attempt...\n', append=True)
                    driver.get(f"https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}")
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Getting DOM Element by Class Name (tlid-translation)\n', append=True)
                    result = driver.find_element_by_class_name("tlid-translation")
                    if result.text == last_translation or result.text == str(last_translation + '...'):
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Translation not finished detected... Refreshing page before new attempt...\n', append=True)
                        driver.refresh()
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Getting DOM Element by Class Name (tlid-translation)\n', append=True)
                        result = driver.find_element_by_class_name("tlid-translation")
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Setting last_translation global variable to new translation...\n', append=True)
                    last_translation = str(result.text)
                    if cache:
                        if debug:
                            write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Adding result to cache...\n', append=True)
                        add_translation_cache(source_language=source_language, destination_language=destination_language, source=text, result=str(result.text))
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Returning value... {result.text}\n', append=True)
                    return str(result.text)
                except Exception as e:
                    if debug:
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Unknown error\n', append=True)
                        write_file('logs.txt', today() + ' ' + current_time() + f'   text={text}|sl={source_language}|dl={destination_language} - Error details: {str(e)}\n', append=True)
                    return "An error occured while translating: unknown error."
lifeeasy.clear()

print("Disks Infos: " + str(lifeeasy.disks_infos()))
print('')
input('Press [enter] to continue...')
lifeeasy.clear()

print("IP Address: " + lifeeasy.ip_address())
print("Number of network interfaces: " +
      str(lifeeasy.number_of_network_interfaces()))
print('')
input('Press [enter] to continue...')
lifeeasy.clear()

lifeeasy.display_title('Real Time System Monitoring')
time = lifeeasy.today() + ' ' + lifeeasy.current_time()
cpu_freq = "CPU Freq.: " + str(lifeeasy.cpu_current_frequency()) + "Mhz"
cpu_usage = "CPU Usage: " + str(lifeeasy.cpu_usage()) + "%"
ram_usage = "RAM Usage: " + str(lifeeasy.used_ram()) + '/' + str(
    lifeeasy.total_ram()) + ' (' + str(lifeeasy.used_ram_percentage()) + '%)'
swap_usage = 'SWAP Usage: ' + str(lifeeasy.used_swap_memory()) + '/' + str(
    lifeeasy.total_swap_memory()) + ' (' + str(
        lifeeasy.used_swap_memory_percentage()) + '%)'
disk_read = "Disk Total Read: " + lifeeasy.disk_total_read()
disk_write = "Disk Total Write: " + lifeeasy.disk_total_write()
net_receive = "Net. Total Received: " + lifeeasy.net_total_received()
net_sent = "Net. Total Sent: " + lifeeasy.net_total_sent()

lifeeasy.display_body([
    time, '', cpu_freq, cpu_usage, ram_usage, swap_usage, disk_read,
    disk_write, net_receive, net_sent
Exemple #4
0
def search_iqdb(image_hash, image_url='', file_io=None):
    """
    Searches and caches IQDB for anime/manga related images.

    Erina Project - 2020\n
    © Anime no Sekai
    """

    erina_log.logcaches(f'Searching for IQDB Data...', 'iqdb', str(image_hash))
    StatsAppend(ExternalStats.iqdbCalls, "New Call")
    results = {}

    ### If a file is given, send the file to iqdb.
    if file_io is not None:
        response = requests.post('https://iqdb.org/',
                                 files={'file': ('image_to_search', file_io)})
    else:
        if image_url == '':
            erina_log.logerror('[ErinaCaches] [IQDB] No file or URL provided')
            return {'error': 'no file or url provided'}
        else:
            response = request(f'https://iqdb.org/?url={image_url}')

    ### If the image format is not supported by IQDB
    if 'Not an image or image format not supported' in response.text:
        print('Format not supported.')
        erina_log.logerror('[ErinaCaches] [IQDB] Format not supported')
        return {'error': 'format not supported'}

###### IQDB SCRAPING
    iqdb = BeautifulSoup(response.text, 'html.parser')

    ##### Search for the IQDB result
    try:
        tables = iqdb.find_all('table')
        search_result = tables[1].findChildren("th")[0].get_text()
    except Exception as e:
        erina_log.logerror(
            f'[ErinaCaches] [IQDB] Client Error, Error details: {str(e)}')
        return {'error': 'client error', 'error_details': e}

##### Verify if the result is relevant or not
    iqdb_tags = []
    if search_result == 'No relevant matches':
        erina_log.logerror('[ErinaCaches] [IQDB] No relevant matches found')
        return {'error': 'not found'}
    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 = ''

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

    #### 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 = ''

    #### Adding the results to the main result variable
    results['iqdb_tags'] = iqdb_tags
    results['url'] = url
    results['size'] = size
    results['is_safe'] = is_safe
    results['similarity'] = similarity

    ############ FUNCTION DEFINITION FOR RESULTS SCRAPING
    if url.find('gelbooru.') != -1:
        results['database'] = 'gelbooru'
        results['gelbooru_results'] = search_gelbooru(url)

    elif url.find('danbooru.') != -1:
        results['database'] = 'danbooru'
        results['danbooru_results'] = search_danbooru(url)

    elif url.find('zerochan.') != -1:
        results['database'] = 'zerochan'
        results['zerochan_results'] = search_zerochan(url)

    elif url.find('konachan.') != -1:
        results['database'] = 'konachan'
        results['konachan_results'] = search_konachan(url)

    elif url.find('yande.re') != -1:
        results['database'] = 'yandere'
        results['yandere_results'] = search_yandere(url)

    elif url.find('anime-pictures.') != -1:
        results['database'] = 'anime_pictures'
        results['anime_pictures_results'] = search_animepictures(url)

    elif url.find('e-shuushuu') != -1:
        results['database'] = 'e_shuushuu'
        results['e_shuushuu_results'] = search_eshuushuu(url)

#################### CACHING ##########

    new_cache_content = []
    new_cache_content.append('   --- IQDB CACHE ---   ')
    new_cache_content.append('')

    new_cache_content.append('IQDB Tags: ' +
                             create_erina_list(results['iqdb_tags']))
    new_cache_content.append('URL: ' + results['url'])
    new_cache_content.append('Size: ' + results['size'])
    new_cache_content.append('isSafe: ' + str(results['is_safe']))
    new_cache_content.append('Similarity: ' + results['similarity'])
    new_cache_content.append('Database: ' + results['database'])
    new_cache_content.append('')

    if results['database'] == 'gelbooru':

        new_cache_content.append(
            'Gelbooru Characters: ' +
            create_erina_list(results['gelbooru_results']['characters']))
        new_cache_content.append(
            'Gelbooru Copyrights: ' +
            create_erina_list(results['gelbooru_results']['copyrights']))
        new_cache_content.append(
            'Gelbooru Metadatas: ' +
            create_erina_list(results['gelbooru_results']['metadatas']))
        new_cache_content.append(
            'Gelbooru Tags: ' +
            create_erina_list(results['gelbooru_results']['tags']))

        new_cache_content.append('Gelbooru ID: ' +
                                 results['gelbooru_results']['id'])
        new_cache_content.append('Gelbooru Size: ' +
                                 results['gelbooru_results']['size'])
        new_cache_content.append('Gelbooru Source: ' +
                                 results['gelbooru_results']['source'])
        new_cache_content.append('Gelbooru Rating: ' +
                                 results['gelbooru_results']['rating'])
        new_cache_content.append('Gelbooru Date: ' +
                                 results['gelbooru_results']['date'])
        new_cache_content.append('Gelbooru Uploader: ' +
                                 results['gelbooru_results']['uploader'])
        new_cache_content.append('Gelbooru Score: ' +
                                 results['gelbooru_results']['score'])

    elif results['database'] == 'danbooru':

        new_cache_content.append(
            'Danbooru Artists: ' +
            create_erina_list(results['danbooru_results']['artists']))
        new_cache_content.append(
            'Danbooru Characters: ' +
            create_erina_list(results['danbooru_results']['characters']))
        new_cache_content.append(
            'Danbooru Copyrights: ' +
            create_erina_list(results['danbooru_results']['copyrights']))
        new_cache_content.append(
            'Danbooru Metadatas: ' +
            create_erina_list(results['danbooru_results']['metadatas']))
        new_cache_content.append(
            'Danbooru Tags: ' +
            create_erina_list(results['danbooru_results']['tags']))

        new_cache_content.append('Danbooru ID: ' +
                                 results['danbooru_results']['id'])
        new_cache_content.append('Danbooru Uploader: ' +
                                 results['danbooru_results']['uploader'])
        new_cache_content.append('Danbooru Date: ' +
                                 results['danbooru_results']['date'])
        new_cache_content.append('Danbooru Content Size: ' +
                                 results['danbooru_results']['content_size'])
        new_cache_content.append('Danbooru Format: ' +
                                 results['danbooru_results']['format'])
        new_cache_content.append('Danbooru Size: ' +
                                 results['danbooru_results']['size'])
        new_cache_content.append('Danbooru Source: ' +
                                 results['danbooru_results']['source'])
        new_cache_content.append('Danbooru Rating: ' +
                                 results['danbooru_results']['rating'])
        new_cache_content.append('Danbooru Score: ' +
                                 results['danbooru_results']['score'])
        new_cache_content.append('Danbooru Favorites: ' +
                                 results['danbooru_results']['favorites'])
        new_cache_content.append('Danbooru Status: ' +
                                 results['danbooru_results']['status'])

    elif results['database'] == 'zerochan':
        new_cache_content.append('Zerochan ID: ' +
                                 results['zerochan_results']['id'])
        new_cache_content.append('Zerochan Uploader: ' +
                                 results['zerochan_results']['uploader'])
        new_cache_content.append('Zerochan Content URL: ' +
                                 results['zerochan_results']['content_url'])
        new_cache_content.append('Zerochan Thumbnail: ' +
                                 results['zerochan_results']['thumbnail'])
        new_cache_content.append('Zerochan Format: ' +
                                 results['zerochan_results']['format'])
        new_cache_content.append('Zerochan Post Date: ' +
                                 results['zerochan_results']['post_date'])
        new_cache_content.append('Zerochan Name: ' +
                                 results['zerochan_results']['name'])
        new_cache_content.append('Zerochan Width: ' +
                                 results['zerochan_results']['width'])
        new_cache_content.append('Zerochan Height: ' +
                                 results['zerochan_results']['height'])
        new_cache_content.append('Zerochan Content Size: ' +
                                 results['zerochan_results']['content_size'])
        new_cache_content.append('Zerochan Mangaka: ' +
                                 results['zerochan_results']['mangaka'])
        new_cache_content.append('Zerochan Series: ' +
                                 results['zerochan_results']['series'])
        new_cache_content.append('Zerochan Character: ' +
                                 results['zerochan_results']['character'])
        new_cache_content.append('Zerochan Source: ' +
                                 results['zerochan_results']['source'])

    elif results['database'] == 'konachan':

        new_cache_content.append(
            'Konachan Copyrights: ' +
            create_erina_list(results['konachan_results']['copyrights']))
        new_cache_content.append(
            'Konachan Styles: ' +
            create_erina_list(results['konachan_results']['styles']))
        new_cache_content.append(
            'Konachan Artists: ' +
            create_erina_list(results['konachan_results']['artists']))
        new_cache_content.append(
            'Konachan Characters: ' +
            create_erina_list(results['konachan_results']['characters']))
        new_cache_content.append(
            'Konachan Tags: ' +
            create_erina_list(results['konachan_results']['tags']))
        new_cache_content.append(
            'Konachan Favorited By: ' +
            create_erina_list(results['konachan_results']['favorited_by']))

        new_cache_content.append('Konachan ID: ' +
                                 results['konachan_results']['id'])
        new_cache_content.append('Konachan Size: ' +
                                 results['konachan_results']['size'])
        new_cache_content.append('Konachan Source: ' +
                                 results['konachan_results']['source'])
        new_cache_content.append('Konachan Rating: ' +
                                 results['konachan_results']['rating'])
        new_cache_content.append('Konachan Date: ' +
                                 results['konachan_results']['date'])
        new_cache_content.append('Konachan Uploader: ' +
                                 results['konachan_results']['uploader'])
        new_cache_content.append('Konachan Score: ' +
                                 results['konachan_results']['score'])

    elif results['database'] == 'yandere':

        new_cache_content.append(
            'Yandere Copyrights: ' +
            create_erina_list(results['yandere_results']['copyrights']))
        new_cache_content.append(
            'Yandere Styles: ' +
            create_erina_list(results['yandere_results']['styles']))
        new_cache_content.append(
            'Yandere Artists: ' +
            create_erina_list(results['yandere_results']['artists']))
        new_cache_content.append(
            'Yandere Characters: ' +
            create_erina_list(results['yandere_results']['characters']))
        new_cache_content.append(
            'Yandere Tags: ' +
            create_erina_list(results['yandere_results']['tags']))
        new_cache_content.append(
            'Yandere Favorited By: ' +
            create_erina_list(results['yandere_results']['favorited_by']))

        new_cache_content.append('Yandere ID: ' +
                                 results['yandere_results']['id'])
        new_cache_content.append('Yandere Size: ' +
                                 results['yandere_results']['size'])
        new_cache_content.append('Yandere Source: ' +
                                 results['yandere_results']['source'])
        new_cache_content.append('Yandere Rating: ' +
                                 results['yandere_results']['rating'])
        new_cache_content.append('Yandere Date: ' +
                                 results['yandere_results']['date'])
        new_cache_content.append('Yandere Uploader: ' +
                                 results['yandere_results']['uploader'])
        new_cache_content.append('Yandere Score: ' +
                                 results['yandere_results']['score'])

    elif results['database'] == 'anime_pictures':

        new_cache_content.append('Anime-Pictures ID: ' +
                                 results['anime_pictures_results']['id'])
        new_cache_content.append('Anime-Pictures Uploader: ' +
                                 results['anime_pictures_results']['uploader'])
        new_cache_content.append(
            'Anime-Pictures Last Editing User: '******'anime_pictures_results']['last_editing_user'])
        new_cache_content.append(
            'Anime-Pictures Post Date: ' +
            results['anime_pictures_results']['post_date'])
        new_cache_content.append(
            'Anime-Pictures Published Date: ' +
            results['anime_pictures_results']['published_date'])
        new_cache_content.append(
            'Anime-Pictures Download Count: ' +
            results['anime_pictures_results']['download_count'])
        new_cache_content.append('Anime-Pictures Size: ' +
                                 results['anime_pictures_results']['size'])
        new_cache_content.append(
            'Anime-Pictures Aspect Ratio: ' +
            results['anime_pictures_results']['aspect_ratio'])
        new_cache_content.append(
            'Anime-Pictures Content Size: ' +
            results['anime_pictures_results']['content_size'])
        new_cache_content.append(
            'Anime-Pictures Artefacts Degree: ' +
            results['anime_pictures_results']['artefacts_degree'])
        new_cache_content.append(
            'Anime-Pictures Smooth Degree: ' +
            results['anime_pictures_results']['smoothness_degree'])
        new_cache_content.append(
            'Anime-Pictures Complexity: ' +
            results['anime_pictures_results']['complexity'])
        new_cache_content.append(
            'Anime-Pictures Copyright: ' +
            results['anime_pictures_results']['copyright'])
        new_cache_content.append('Anime-Pictures Artist: ' +
                                 results['anime_pictures_results']['artist'])
        new_cache_content.append(
            'Anime-Pictures Average Color: ' + create_erina_list(
                results['anime_pictures_results']['average_color']))
        new_cache_content.append(
            'Anime-Pictures References: ' +
            create_erina_list(results['anime_pictures_results']['references']))
        new_cache_content.append(
            'Anime-Pictures Objects: ' +
            create_erina_list(results['anime_pictures_results']['objects']))
        new_cache_content.append(
            'Anime-Pictures Similar Images: ' + create_erina_list(
                results['anime_pictures_results']['similar_images_id']))
        new_cache_content.append(
            'Anime-Pictures Artist Links: ' + create_erina_list(
                results['anime_pictures_results']['artist_links']))

    elif results['database'] == 'e_shuushuu':

        new_cache_content.append('E-Shuushuu Posting Uploader: ' +
                                 results['e_shuushuu_results']['uploader'])
        new_cache_content.append('E-Shuushuu Posting Post Date: ' +
                                 results['e_shuushuu_results']['post_date'])
        new_cache_content.append('E-Shuushuu Posting Filename: ' +
                                 results['e_shuushuu_results']['filename'])
        new_cache_content.append(
            'E-Shuushuu Posting Original Filename: ' +
            results['e_shuushuu_results']['original_filename'])
        new_cache_content.append('E-Shuushuu Posting Content Size: ' +
                                 results['e_shuushuu_results']['content_size'])
        new_cache_content.append('E-Shuushuu Posting Size: ' +
                                 results['e_shuushuu_results']['size'])
        new_cache_content.append('E-Shuushuu Posting Favorites: ' +
                                 results['e_shuushuu_results']['favorites'])
        new_cache_content.append('E-Shuushuu Posting Image Rating: ' +
                                 results['e_shuushuu_results']['image_rating'])

        new_cache_content.append(
            'E-Shuushuu Tags: ' +
            create_erina_list(results['e_shuushuu_results']['tags']))
        new_cache_content.append(
            'E-Shuushuu Sources: ' +
            create_erina_list(results['e_shuushuu_results']['sources']))
        new_cache_content.append(
            'E-Shuushuu Characters: ' +
            create_erina_list(results['e_shuushuu_results']['characters']))
        new_cache_content.append(
            'E-Shuushuu Artists: ' +
            create_erina_list(results['e_shuushuu_results']['artists']))

    new_cache_content.append('')
    new_cache_content.append('Cache Timestamp: ' +
                             str(datetime.timestamp(datetime.today())))
    new_cache_content.append('Cache Timestamp (formatted): ' + today() +
                             ' at ' + current_time())

    new_cache_destination = env_information.erina_dir + '/ErinaCaches/IQDB_Cache/'
    new_cache_filename = str(image_hash) + '.erina'
    erina_log.logcaches(f'Caching IQDB and {results["database"]} data...')
    write_file(file_title=new_cache_filename,
               text=new_cache_content,
               destination=new_cache_destination)
    return results
Exemple #5
0
async def _translate(text,
                     destination_language,
                     source_language="auto",
                     cache=False,
                     debug=False):
    """
    Internal Function to translate
    """
    from .domain import gt_domain
    global last_translation
    if debug:
        write_file(
            'logs.txt',
            today() + ' ' + current_time() +
            f'   text={text}|sl={source_language}|dl={destination_language} - Starting Translation...\n',
            append=True)
    if debug:
        write_file(
            'logs.txt',
            today() + ' ' + current_time() +
            f'   text={text}|sl={source_language}|dl={destination_language} - Searching Caches...\n',
            append=True)
    cache_result = search_translation_cache(
        source_language=source_language,
        destination_language=destination_language,
        source=text)
    if not cache_result is None:
        if debug:
            line_number = cache_result['line_number']
            write_file(
                'logs.txt',
                today() + ' ' + current_time() +
                f'   text={text}|sl={source_language}|dl={destination_language} - Translation found in Caches (line {line_number})\n',
                append=True)
        return cache_result['result']
    else:
        if debug:
            write_file(
                'logs.txt',
                today() + ' ' + current_time() +
                f'   text={text}|sl={source_language}|dl={destination_language} - First attempt url is: https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}\n',
                append=True)
        await page.goto(
            f"https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}"
        )
        try:
            if debug:
                write_file(
                    'logs.txt',
                    today() + ' ' + current_time() +
                    f'   text={text}|sl={source_language}|dl={destination_language} - Getting DOM Element by Class Name (tlid-translation)\n',
                    append=True)
            result = await page.evaluate(
                "document.getElementsByClassName(\"tlid-translation\")[0].innerText"
            )
            if result == last_translation or result == str(last_translation +
                                                           '...'):
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - Translation not finished detected... Refreshing page before new attempt...\n',
                        append=True)
                await page.reload()
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - Getting DOM Element by Class Name (tlid-translation)\n',
                        append=True)
                result = await page.evaluate(
                    "document.getElementsByClassName(\"tlid-translation\")[0].innerText"
                )
            if debug:
                write_file(
                    'logs.txt',
                    today() + ' ' + current_time() +
                    f'   text={text}|sl={source_language}|dl={destination_language} - Setting last_translation global variable to new translation...\n',
                    append=True)
            last_translation = str(result)
            if cache:
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - Adding result to cache...\n',
                        append=True)
                add_translation_cache(
                    source_language=source_language,
                    destination_language=destination_language,
                    source=text,
                    result=str(result))
            if debug:
                write_file(
                    'logs.txt',
                    today() + ' ' + current_time() +
                    f'   text={text}|sl={source_language}|dl={destination_language} - Returning value... {result}\n',
                    append=True)
            return str(result)
        except ElementHandleError:
            if debug:
                write_file(
                    'logs.txt',
                    today() + ' ' + current_time() +
                    f'   text={text}|sl={source_language}|dl={destination_language} - Element not found on page...\n',
                    append=True)
            try:
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] New attempt...\n',
                        append=True)
                await page.goto(
                    f"https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}"
                )
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Getting DOM Element by Class Name (tlid-translation)\n',
                        append=True)
                result = await page.evaluate(
                    "document.getElementsByClassName(\"tlid-translation\")[0].innerText"
                )
                if result == last_translation or result == str(
                        last_translation + '...'):
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Translation not finished detected... Refreshing page before new attempt...\n',
                            append=True)
                    await page.reload()
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Getting DOM Element by Class Name (tlid-translation)\n',
                            append=True)
                    result = await page.evaluate(
                        "document.getElementsByClassName(\"tlid-translation\")[0].innerText"
                    )
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Setting last_translation global variable to new translation...\n',
                        append=True)
                last_translation = str(result)
                if cache:
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - Adding result to cache...\n',
                            append=True)
                    add_translation_cache(
                        source_language=source_language,
                        destination_language=destination_language,
                        source=text,
                        result=str(result))
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Returning value... {result}\n',
                        append=True)
                return str(result)
            except ElementHandleError:
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Element not found on page...\n',
                        append=True)
                try:
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] New attempt...\n',
                            append=True)
                    await page.goto(
                        f"https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}"
                    )
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Translation not finished detected... Refreshing page before new attempt...\n',
                            append=True)
                    await page.reload()
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Getting DOM Element by Class Name (tlid-translation)\n',
                            append=True)
                    result = await page.evaluate(
                        "document.getElementsByClassName(\"tlid-translation\")[0].innerText"
                    )
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Setting last_translation global variable to new translation...\n',
                            append=True)
                    last_translation = str(result)
                    if cache:
                        if debug:
                            write_file(
                                'logs.txt',
                                today() + ' ' + current_time() +
                                f'   text={text}|sl={source_language}|dl={destination_language} - Adding result to cache...\n',
                                append=True)
                        add_translation_cache(
                            source_language=source_language,
                            destination_language=destination_language,
                            source=text,
                            result=str(result))
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Returning value... {result}\n',
                            append=True)
                    return str(result)
                except ElementHandleError:
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Element not found, aborting...\n',
                            append=True)
                    return "An error occured while translating: translation not found."
                except Exception as e:
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 3] Unknown error\n',
                            append=True)
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - Error details: {str(e)}\n',
                            append=True)
                    return "An error occured while translating: unknown error."
            except Exception as e:
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Unknown error\n',
                        append=True)
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - Error details: {str(e)}\n',
                        append=True)
                return "An error occured while translating: unknown error."
        except Exception as e:
            if debug:
                write_file(
                    'logs.txt',
                    today() + ' ' + current_time() +
                    f'   text={text}|sl={source_language}|dl={destination_language} - Unknown error\n',
                    append=True)
                write_file(
                    'logs.txt',
                    today() + ' ' + current_time() +
                    f'   text={text}|sl={source_language}|dl={destination_language} - Error details: {str(e)}\n',
                    append=True)
            try:
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] New attempt...\n',
                        append=True)
                await page.goto(
                    f"https://{gt_domain}/?hl=en#view=home&op=translate&sl={verify_language_code(source_language)}&tl={verify_language_code(destination_language)}&text={str(text)}"
                )
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Getting DOM Element by Class Name (tlid-translation)\n',
                        append=True)
                result = await page.evaluate(
                    "document.getElementsByClassName(\"tlid-translation\")[0].innerText"
                )
                if result == last_translation or result == str(
                        last_translation + '...'):
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Translation not finished detected... Refreshing page before new attempt...\n',
                            append=True)
                    await page.reload()
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Getting DOM Element by Class Name (tlid-translation)\n',
                            append=True)
                    result = await page.evaluate(
                        "document.getElementsByClassName(\"tlid-translation\")[0].innerText"
                    )
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Setting last_translation global variable to new translation...\n',
                        append=True)
                last_translation = str(result)
                if cache:
                    if debug:
                        write_file(
                            'logs.txt',
                            today() + ' ' + current_time() +
                            f'   text={text}|sl={source_language}|dl={destination_language} - Adding result to cache...\n',
                            append=True)
                    add_translation_cache(
                        source_language=source_language,
                        destination_language=destination_language,
                        source=text,
                        result=str(result))
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - [Attempt 2] Returning value... {result}\n',
                        append=True)
                return str(result)
            except Exception as e:
                if debug:
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - Unknown error\n',
                        append=True)
                    write_file(
                        'logs.txt',
                        today() + ' ' + current_time() +
                        f'   text={text}|sl={source_language}|dl={destination_language} - Error details: {str(e)}\n',
                        append=True)
                return "An error occured while translating: unknown error."