lushan88a / google_trans_new

A free and unlimited python API for google translate.
MIT License
394 stars 171 forks source link

support for proxies? #11

Open noman00910 opened 3 years ago

noman00910 commented 3 years ago

is there proxy support for this library as Google may block for too many requests?

lushan88a commented 3 years ago

You can use proxy like the following code: Attention! Google only support https proxy!

from google_trans_new import google_translator  

# If you want use proxy, you can set proxies like proxies={'http':'xxx.xxx.xxx.xxx:xxxx','https':'xxx.xxx.xxx.xxx:xxxx'}
translator = google_translator(url_suffix="com",timeout=5,proxies={'http':'xxx.xxx.xxx.xxx:xxxx','https':'xxx.xxx.xxx.xxx:xxxx'})  
# <Translator url_suffix=cn timeout=5 proxies={'http':'xxx.xxx.xxx.xxx:xxxx','https':'xxx.xxx.xxx.xxx:xxxx'}>  
#  default parameter : url_suffix="cn" timeout=5 proxies={}
translate_text = translator.translate('สวัสดีจีน',lang_tgt='zh')  
# <Translate text=สวัสดีจีน lang_tgt=th lang_src=zh>  
#  default parameter : lang_src=auto lang_tgt=auto 
#  API can automatically identify the src translation language, so you don’t need to set lang_src
print(translate_text)
-> 你好中国
noman00910 commented 3 years ago

What if I want to add dedicated proxies with a username and password? Will it support that sort of method?

lushan88a commented 3 years ago

You can put your username and password into the proxy url. It depends on your proxy agent. My proxy is used like this {"https": "https://%(user)s:%(pwd)s@%(proxy)s/" % {'user': username, 'pwd': password, 'proxy': 'xxx.xxx.xxx.xxx:xxxx'}

noman00910 commented 3 years ago

Ok, I will try it. But how do I know my request is going through my proxy. I have tried the exact format, it gave me the correct transalation. I added the wrong proxy and wrong format as well, it again gave me the correct result. So, how can I know my proxy is working.

lushan88a commented 3 years ago

Google only accept the https proxy, so you can add {"https" : "XXXX"}. If you only added http proxy, whatever right or wrong it will not have any effect.

This is a very difficult thing doing in this lib as for how do you know your proxy is working. =。=

noman00910 commented 3 years ago

Google only accept the https proxy, so you can add {"https" : "XXXX"}. If you only added http proxy, whatever right or wrong it will not have any effect.

This is a very difficult thing doing in this lib as for how do you know your proxy is working. =。=

This format does not return any result. I used it as {"https" : "user:pwd@proxy:port"} @lushan88a

bjquinniii commented 3 years ago

In your proxy format:

translator = google_translator(url_suffix="com",timeout=5,proxies={'http':'xxx.xxx.xxx.xxx:xxxx','https':'xxx.xxx.xxx.xxx:xxxx'})

what is going to happen under the covers? Is it always going to try the first proxy, and if that fails, move on to the second one? The reason I ask is if this is the behavior, it won't really solve the problem, just delay it. If, on the other hand, the translator call were to automatically rotate through the proxies (first call => first proxy, second call => second proxy), this would have the effect of reducing the number of calls within a certain period of time from each proxy (which is what they base the blocks on). That being said, having proxy as a parameter is a huge benefit. Could always just control which proxy is called externally... something like this:

proxy0 = 'xxx.xxx.xxx.xxx:xxxx' proxy1 = 'xxx.xxx.xxx.xxx:xxxx' proxy2 = 'xxx.xxx.xxx.xxx:xxxx'

proxyCall = 0 while TRUE: proxyCall += 1 if proxyCall%3 == 0: myProxy = proxy0 elif proxyCall%3 == 1: myProxy = proxy1 elif proxyCall%3 == 2: myProxy = proxy2 translator = google_translator(url_suffix="com",timeout=5,proxies={'http':myProxy})