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.64k stars 190 forks source link

[Bug]: redundant code/web request #121

Closed JustFrederik closed 1 year ago

JustFrederik commented 1 year ago

Debug Tips

What happened?

This line is redundant

 _ = self.session.get(self.host_url, headers=self.host_headers, timeout=timeout, proxies=proxies)  # must twice, send cookies.

its on line 620. i noticed the comment, but i tried it without and it worked and there is no code that would attach the cookies, so...

APP Version

latest

Python Version

3.9 (Default)

Runtime Environment

Other

Country/Region

Germany

Relevant log output

No response

Screenshots

No response

Code of Conduct

UlionTse commented 1 year ago

Please read line 3687 of view-source:https://fanyi.baidu.com/, thanks.

JustFrederik commented 1 year ago

@UlionTse 1. It works without the second request.

  1. Cookies are usually send with the request, but since you don’t attach cookies it works without the cookies. Therefor it’s redundant. mit probably was a problem in the past, but works now
  2. It would make no sense when to make 2 requests when they log your ip. The second request would only make sense when the first failed or not contain "the cookie". They don’t do that. I just tried it with a single request and a vpn and it worked
  3. The language check itself is kinda redundant. It would make more sense to have an already fetched list of languages in the project. It wouldn’t get the language every time and the translator would work twice as fast. Or an argument like bypass langcheck
UlionTse commented 1 year ago

Thanks for your guidance, bro. The second request ensures the stability of the service and does not affect the speed of the request because the link is not used again after the session is obtained. Language checks are necessary, cost almost no time, you can test it, and that can avoid anti-cheating.

JustFrederik commented 1 year ago

@UlionTse i am implementing my own translator and needed to fetch the languages. I have gone with the attempt to fetch them before and have a csv file that maps them together. That has the benefits of having universal identifiers. I noticed that there is indeed protection, but it isn’t a cookie but a ip lookup. It isn’t used for the translation, but only the language detection. The second request doesn’t help at all for stability since it takes Baidu like half a minute to register the ip. I’m working with rust, but something like this comes back from the server: error trying to connect: dns error: failed to lookup address information: nodename nor servname provided, or not known. It’s just something I noticed while working with it.

And that there is no cost is simply wrong. At least from where I live. The translation is fetched within 2 seconds, but the language check takes 5-10seconds each.

Anyway I’m gonna close this issue. It was just something I wanted to point out, because it has no purpose.I thought the request was redundant, because it will succeed, but it is redundant because the second request will fail when the first failed