flagist0 / reverso_context_api

Simple Python API for Reverso Context
MIT License
57 stars 6 forks source link

kill request if there are no [more] available translation samples #3

Open paulorssalves opened 2 years ago

paulorssalves commented 2 years ago

Ой, братишка! Greetings from Brazil. Your API has been of great use to me. I'm using it to create material to study Russian on Anki. There was a little problem where if you made a request for translation samples for an entry that is not available on Reverso Context it would never stop doing the request, and then the server would inevitably throw a 429 exception (and if this kept happening your IP would be temporarily blocked from making requests, which is awful). Same thing would happen if you asked for a number of samples for a given word, but the number of available samples on Reverso Context for that word is smaller than the number you requested.

Anyway, my solution is not the most elegant but I think it solves the problem.

flagist0 commented 2 years ago

Паоло, привет!) Thank you for the pull request, I haven't bumped into this problem before. Also it's nice to hear that this package was of some value to you, thanks) I think the good place for this change is in _translations_pager as it's responsible for iteration of results. You could stop the pager by just 'raise StopIteration()' to avoid changing the result type of get_translation_samples

paulorssalves commented 2 years ago

Hello, Alexander! I have done what you suggested. I put a break statement in the _translations_pager instead of using StopIteration(), however. I tried doing it the way you proposed, but then it throws a runtime exception if StopIteration() is explicitly stated. The generator never actually gets to the end because it keeps returning empty lists (which the other function keeps scanning for content that is never found), so it seemed to me to be necessary to kill it from outside. The runtime exception could be handled, but I figured it would become too bulky to do it that way.