Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeepL] Implement web API fallback #428

Merged
merged 1 commit into from
May 23, 2024
Merged

Conversation

papjul
Copy link
Contributor

@papjul papjul commented May 8, 2024

This adds a fallback to the DeepL engine, when no API key has been defined.

It emulates a request made through a browser, as seen in similar translation projects/libraries.

The downside is that it requires a reasonable delay between requests, otherwise you get a soft ban. It works fine when you disable automatic translation, but I didn't find a way to force users to have it (probably also needs to disable simultaneous translation?) when API key is not set.

Language("es", "Spanish"),
Language("sv", "Swedish"),
Language("tr", "Turkish"),
Language("uk", "Ukrainian")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did you extract these languages from? The website? It'd be nice to add a comment here so that future people know where to look when updating the supported languages.

"Sec-Fetch-Dest: empty",
"Sec-Fetch-Mode: cors",
"Sec-Fetch-Site: none",
"User-Agent: DeepLBrowserExtension/1.18.0 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the user agent string to a constant, it will probably change in the future and thus we should place it in a different variable for ease of updating.

"Sec-Fetch-Site: none",
"User-Agent: DeepLBrowserExtension/1.18.0 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
)
@POST("jsonrpc?client=chrome-extension,1.18.0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the version (1.18.0) to a constant and re-use for the user agent as commented above.

"\"method\":\"",
// The random ID determines the spacing to use, do NOT change it
if ((id + 3) % 13 == 0 || (id + 5) % 29 == 0) {
"\"method\" : \""
} else "\"method\": \""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the source / reasoning for this magic stuff here?

@papjul
Copy link
Contributor Author

papjul commented May 10, 2024

Your feedback was taken into account and I rebased/merged commits.

Note that it's usually not necessary to bump extension version/user agent, as you can see in other projects they have older versions, because old versions still work and bumping would require a review of other stuff than just changing the user agent, to be sure that nothing else changed in new versions that would prevent the API from working.

Also, it would be best to disable automatic translation when the web version is used to avoid soft bans. This would result in the same situation of today where it doesn't work without an API key, but the whole point of this PR is to have a working anonymous (although less reliable) method :)

An alternative would be to implement the web page API but it is more complex and more subject to client-side changes than the Chrome extension that is versioned ;) Some projects implement the two methods and the user can choose, but I will leave the second method to other contributors if interested ;)

Copy link
Member

@Bnyro Bnyro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

My most recent commit did just set the apiKeyState to optional so that users selecting DeepL in the settings know that the engine might work without API key.

@Bnyro Bnyro merged commit fe5f7c7 into you-apps:master May 23, 2024
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants