PyDeepLX icon indicating copy to clipboard operation
PyDeepLX copied to clipboard

POST https://www2.deepl.com/jsonrpc ~ 429 error.

Open ahopelesshadow opened this issue 9 months ago • 2 comments

After making the proposed change from https://github.com/OwO-Network/PyDeepLX/issues/22, the lib works again, however every translation I attempt returns a 429 too many requests, with a message saying PyDeepLX Error: Too many requests, your IP has been blocked by DeepL temporarily, please don't request it frequently in a short time. However this does not appear to be temporary, I have tried it with a vpn, without a vpn, and at all hours of the day, even with days in between. I have no idea if they just started denying the IOS headers OwO-Network was using, but I went ahead and made the following changes to mimic the browser calls. In the PyDeepLX.py file you want to make the following changes...

update the headers dictionary to

headers = {
    "Content-Type": "application/json",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
    "Accept": "*/*",
    "Accept-Language": "en-US,en;q=0.9",
    "Origin": "https://www.deepl.com",
    "Referer": "https://www.deepl.com/",
    "X-Requested-With": "XMLHttpRequest",
}

in the translate function of that file, update the postData to...

postData = {
    "jsonrpc": "2.0",
    "method": "LMT_handle_jobs",
    "id": id,
    "params": {
        "jobs": [
            {
                "kind": "default",
                "raw_en_sentence": text,
                "raw_en_context_before": [],
                "raw_en_context_after": [],
                "preferred_num_beams": numberAlternative + 1,
            }
        ],
        "lang": {
            "source_lang_user_selected": sourceLang,
            "target_lang": targetLang,
        },
        "priority": 1,
        "commonJobParams": {},
        "timestamp": getTimestamp(iCount),
    },
}

remove or comment out the replace formula to update the method.

# if (id + 5) % 29 == 0 or (id + 3) % 13 == 0:
# postDataStr = postDataStr.replace('"method":"', '"method" : "', -1)
# else:
# postDataStr = postDataStr.replace('"method":"', '"method": "', -1)

Finally, update the dictionary locations of the translated text for return...

if numberAlternative <= 1:
    targetText = respJson["result"]["translations"][0]["beams"][0]["postprocessed_sentence"]
    if printResult:
        print(targetText)
    return targetText

targetTextArray = []
for item in respJson["result"]["translations"][0]["beams"]:
    targetTextArray.append(item["postprocessed_sentence"])
        if printResult:
            print(item["text"])

return targetTextArray 

Optionally

If you like, you can update the TooManyRequestsException class as below to update the grammar.

class TooManyRequestsException(Exception):
    "Raised when there is a 429 error"

    def __str__(self):
        return "PyDeepLX Error: Too many requests... Your IP has temporarily been blocked by DeepL, please try again later."

and turn the default httpx logging off with

import logging
logging.basicConfig(level=logging.INFO)
httpx_logger = logging.getLogger("httpx")
httpx_logger.setLevel(logging.WARNING)

This should get us back up and running until they block it again.

ahopelesshadow avatar Apr 22 '25 23:04 ahopelesshadow

pleasse can you explain how to use it, to translate .srt subtitles files? an example maybe? Or is there is a possibility to use it in proxy mode with subtitle edit, should be more easier, but i dont' understand how to.

Please can you help me ?

I did it every your suggestion in this thread.

RobertusIT avatar May 15 '25 10:05 RobertusIT

pleasse can you explain how to use it, to translate .srt subtitles files? an example maybe? Or is there is a possibility to use it in proxy mode with subtitle edit, should be more easier, but i dont' understand how to.

Please can you help me ?

I did it every your suggestion in this thread.

The existing module has no functionality to directly translate an external file such as srt. That is something you would need to create your own project to do, possibly using this module for the translation, however i would caution against that for several reasons. First being an AI subtitle track is a terrible idea given you are gambling on the translation being 100% accurate and including context in speach. Second in that you would need to parse that file strip out all the tags to actually get text, then deal with language identification and potentially unicode characters, then reformat your translation to a working subtitle file. I won't be able to help you with this as I dont agree with the reasoning behind the project.

As far as using this module, you can have a look at the included readme, none of the function syntax was changed in my patches, i also see there is a PR using a newer ios agent, while i have not tested that, it is another option. I can tell you i have tested my changes as of the timestamp of this post and confirm they are still working.

ahopelesshadow avatar May 15 '25 11:05 ahopelesshadow