UlionTse / translators

🌏🌍🌎Translators🌎🌍🌏 is a library that aims to bring free, multiple, enjoyable translations to individuals and students in Python. Translators是一个旨在用Python为个人和学生带来免费、多样、愉快翻译的库。
https://pypi.org/project/translators/
GNU General Public License v3.0
1.62k stars 189 forks source link

[Bug]: KeyError: 'target' #159

Closed kekwak closed 3 weeks ago

kekwak commented 4 months ago

Debug Tips

What happened?

i tried to print data in module before bug happens

print(data)
return data if is_detail_result else '\n'.join([self.decrypt(item) for item in data['target']])

so i got {'http_code': 200, 'message': 'Token expired', 'err_code': None, 'details': None, 'auth_code': -3}

i use caiyun: out = ts.translate_text(text, translator='caiyun', from_language='en', to_language='ru')

APP Version

5.9.1

Python Version

3.11

Runtime Environment

Windows 11

Country/Region

Russia

Relevant log output

File "C:\Основная папка\Рабочий стол\KABYTE\Git Tasks\KABYTE_2024\08_Final_Project\misc\pipe.py", line 123, in translate
    out = ts.translate_text(text[start:end], translator=translator, from_language=src_lang, to_language=dest_lang)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Основная папка\Рабочий стол\KABYTE\Git Tasks\KABYTE_2024\08_Final_Project\.venv\Lib\site-packages\translators\server.py", line 5470, in translate_text
    return self.translators_dict[translator](query_text=query_text, from_language=from_language, to_language=to_language, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Основная папка\Рабочий стол\KABYTE\Git Tasks\KABYTE_2024\08_Final_Project\.venv\Lib\site-packages\translators\server.py", line 117, in _wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "c:\Основная папка\Рабочий стол\KABYTE\Git Tasks\KABYTE_2024\08_Final_Project\.venv\Lib\site-packages\translators\server.py", line 288, in _wrapper
    return func(*args, **{**kwargs, **{'query_text': query_text}})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Основная папка\Рабочий стол\KABYTE\Git Tasks\KABYTE_2024\08_Final_Project\.venv\Lib\site-packages\translators\server.py", line 2019, in caiyun_api
    return data if is_detail_result else '\n'.join([self.decrypt(item) for item in data['target']])
                                                                                   ~~~~^^^^^^^^^^
KeyError: 'target'

Screenshots

No response

Code of Conduct

UlionTse commented 4 months ago

@kekwak Bro, look this: https://github.com/UlionTse/translators/issues/157#issuecomment-2061506773

The same problem, one, is that it is not clear what the input text is. Second, whether this bug can be reproduced. Third, through the parameters is_detail_result=True, see what the server returned.

translators_issues159
UlionTse commented 4 months ago

{'http_code': 200, 'message': 'Token expired', 'err_code': None, 'details': None, 'auth_code': -3}, Noting this, it is recommended that you use parameters update_session_after_freq and update_session_after_seconds to update the session and other cached values early.

kekwak commented 4 months ago

I tried to use update_session_after_freq and update_session_after_seconds, but still got this error

out = ts.server.caiyun(
    textz, translator='caiyun', from_language='en', to_language='ru',
    update_session_after_freq=1, update_session_after_seconds=5,
)

with is_detail_result=True I've got this: {'http_code': 200, 'message': 'Token expired', 'err_code': None, 'details': None, 'auth_code': -3}

text, that I've used: 'As the hours pass and the days unfold from the mysterious image that captivates with its futuristic and abstract allure, a series of events begin to unravel in a mesmerizing dance of technology and spirituality.\n\nThe central figure, exuding an aura of profound significance akin to a deity or spiritual being, seems to emanate a powerful energy that pulses through the surroundings. The symbols and objects surrounding this enigmatic figure come to life, shimmering with an otherworldly glow as if unlocking hidden secrets of the universe.\n\nThe silhouette of a person, glowing with a celestial blue light, stands at the forefront, their presence commanding attention amidst the intricate network of symbols and shapes. A large circular object beams with a mesmerizing blue light, casting a soft, enchanting glow that intertwines with the complex patterns scattered throughout the background.\n\nAs time unfolds, the deep shades of purple blend with the ethereal blues and pinks, creating a dreamlike ambiance that hints at revelations beyond comprehension. The eyes, triangles, and geometric shapes pulse with a rhythmic energy, as if propelling the narrative towards a climactic revelation.\n\nWithin this enigmatic setting, a subtle tension builds, hinting at a convergence of forces beyond human understanding. The interplay of light and shadow, of ancient symbols and futuristic motifs, sets the stage for a profound awakening, a moment where the boundaries between the physical and the metaphysical blur into a harmonious symphony of existence.\n\nAs the days stretch ahead, the image remains a portal to a realm where technology and spirituality converge in a dance of cosmic proportions. Each symbol, each object holds a key to a deeper truth waiting to be unlocked, promising a journey of enlightenment and transformation for those brave enough to venture into its enigmatic depths.'

Speaking about bug reproducing, try to hold you prog for a while, I use it in telegram bot and after some time this issue appears. It this way I'm not able use it, that's why I have to restart my bot, then it starts working.

UlionTse commented 4 months ago

@kekwak Bro Once it is identified by anti-crawlers due to frequent requests, the impact will be directly on the IP address level. Therefore, any parameter you change within a short period of time will be invalid. The two parameters I suggest you use are not that the smaller the value, the better. If it is too small, it will obviously be regarded as a crawler. If it is too large, it will not meet the browser session time. It is necessary to set an appropriate size.