atykhonov / google-translate

Emacs interface to Google Translate
370 stars 48 forks source link

Remove "tk" parameter for API request #146

Closed zonuexe closed 2 years ago

zonuexe commented 2 years ago

Perhaps Google Translation API is no longer validating the tk parameter.

https://github.com/atykhonov/google-translate/issues/52 suggests that google-translate--search-tkk function returns a hard-coded pair of b-d1, but at least today It seems to work with any value.

zonuexe commented 2 years ago

Maybe google-translate-tk.el can be removed.

zonuexe commented 2 years ago

I was checking the API with the intention of submitting the following patch, but in the process I found that it was okay to encode tk with the wrong b-d1 parameter.

patch ```patch diff --git a/google-translate-core.el b/google-translate-core.el index c414450..a64a421 100644 --- a/google-translate-core.el +++ b/google-translate-core.el @@ -87,6 +87,13 @@ :group 'google-translate-core :type 'string) +(defcustom google-translate-cached-b-d1 + nil + "A pair of b and d1 parameters needed to generate an API request token (tk)." + :group 'google-translate-core + :type `(choice (const nil :tag "Disable cached b-d1") + (string :tag "A string consisting of the \"b.d1\" format. e.g. \"430675.2721866130\""))) + (defvar google-translate-host-language (if current-iso639-language (symbol-name current-iso639-language) @@ -177,7 +184,7 @@ QUERY-PARAMS must be an alist of field-value pairs." ("textlen" . ,(number-to-string (length text))) ("client" . "gtx") ("prev" . "input") - ("tk" . ,(google-translate--gen-tk text))))) + ("tk" . ,(google-translate--gen-tk text google-translate-cached-b-d1))))) (defun google-translate-format-listen-urls (text language) "Split TEXT with `google-translate--split-text', then format @@ -275,7 +282,7 @@ translate TEXT from SOURCE-LANGUAGE to TARGET-LANGUAGE." ("srcrom" . "1") ("ssel" . "0") ("tsel" . "0") - ("tk" . ,(google-translate--gen-tk text)))) + ("tk" . ,(google-translate--gen-tk text google-translate-cached-b-d1)))) for-test-purposes)) (defun google-translate-json-text-phonetic (json) diff --git a/google-translate-tk.el b/google-translate-tk.el index 196ba6b..67f133c 100644 --- a/google-translate-tk.el +++ b/google-translate-tk.el @@ -161,8 +161,10 @@ D is an integer." a) (defun google-translate--gen-tk (text &optional b-d1) - (setq b-d1 (or b-d1 (google-translate--get-b-d1))) - (let* ((b (cl-first b-d1)) + (let* ((b-d1 (cond ((null b-d1) (google-translate--get-b-d1)) + ((stringp b-d1) (mapcar #'string-to-number (split-string b-d1 "\\."))) + (t b-d1))) + (b (cl-first b-d1)) (d1 (cl-second b-d1)) (ub "+-3^+b+-f") (vb "+-a^+6") ```
zonuexe commented 2 years ago

@atykhonov @stardiviner Hi, can you please review this PR in your spare time? This PR resolves #52. Simply remove the code for token generation that is no longer needed.

stardiviner commented 2 years ago

@zonuexe Does it work on your machine by appling this patch? We might need more test to confirm. I currently don't have Emacs on my hand. Can't verify this.

zonuexe commented 2 years ago

@stardiviner Yes, compilation and execution are successful in my environment.

stardiviner commented 2 years ago

Ok, I will test this PR in later. Thanks for confirming.

zonuexe commented 2 years ago

@stardiviner I missed some mistakes and pushed a commit to fix them.

And I rebuilt the CI for this project in another PR with GitHub Actions instead of the terminated TravisCI. https://github.com/atykhonov/google-translate/pull/151

stardiviner commented 2 years ago

@stardiviner I missed some mistakes and pushed a commit to fix them.

And I rebuilt the CI for this project in another PR with GitHub Actions instead of the terminated TravisCI. #151

Thanks for upating. I pulled your branch update and try to test. But my china network blocked translate.google.com. Can't access the service now. I need to find a way to fix this problem.

image image
stardiviner commented 1 year ago

Finally got time to testing this network issue, confirmed this patch works. And this PR seems already merged. Done.