eshapard / AnkiHabitica

Anki 2.0/2.1 add-on for use with HabitRPG. Automatically scores habits when you reach the end of your Anki timebox and when you review all cards in a deck.
GNU General Public License v3.0
195 stars 30 forks source link

Says not connected to internet #47

Open kabutar111 opened 7 years ago

kabutar111 commented 7 years ago

AnkiHabitica is not scoring my cards giving same error as it does when not connected to internet but on clicking score habitica points from menu it shows me that it has score points but even then nothing gets scored on Habitica .

adoette commented 7 years ago

Would like to second this one. I tried renewing the login information just in case there was an error there and it had no effect. (And thus we reach the limits of my troubleshooting with these!)

steelwolf commented 7 years ago

I'm wondering if something changed with the Habitica API because scoring suddenly stopped working for me as well. Automatic scoring fails with a "try again later" message, while manually scoring the backlog produces the following error message:

An error occurred in an add-on. Please post on the add-on forum: https://anki.tenderapp.com/discussions/add-ons

Unhandled exception in thread started by <function mark_daily_complete at 0x106205f0> Traceback (most recent call last): File "./daily_habit.py", line 47, in mark_daily_complete if ah.habitica.api.perform_task(habitID, "up"): File ".habitica_api.py", line 172, in perform_task out = self.v3_request("post", url) File ".habitica_api.py", line 76, in v3_request response = json.load(urllib2.urlopen(req)) File "urllib2.pyc", line 127, in urlopen File "urllib2.pyc", line 410, in open File "urllib2.pyc", line 523, in http_response File "urllib2.pyc", line 448, in error File "urllib2.pyc", line 382, in _call_chain File "urllib2.pyc", line 531, in http_error_default urllib2.HTTPError: HTTP Error 411: Length Required

eshapard commented 7 years ago

My guess is intermittent problems with the api today. I saw this a few times myself today

On Jul 17, 2017 4:23 PM, "Michael" notifications@github.com wrote:

I'm wondering if something changed with the Habitica API because scoring suddenly stopped working for me as well.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/eshapard/AnkiHabitica/issues/47#issuecomment-315913329, or mute the thread https://github.com/notifications/unsubscribe-auth/AF5-Ku0jk4vj48NvEM33iLHfuVXs6Dwmks5sO-0DgaJpZM4OZ0o8 .

steelwolf commented 7 years ago

That made sense to me but a few days later it's still going on. I'm not sure how to check the API status in a different way - perhaps it's been down or they're having some problems keeping up with traffic?

a-pankhurst commented 7 years ago

It looks like the Anki Points habit is being updated but not clicked. According to the data display tool, mine was last clicked at 2017-07-15 22:02:27 (UTC+9), but scoresincedate is currently 1500868302 (2017-05-24 12:51:42 (UTC+9)), and it has changed a few times. (I haven't checked whether it's every time AnkiHabitica scores manually or every time it tries to score automatically.) Perhaps a change was made to the API since that last click?

Would a logfile be useful? I'm assuming it's no problem if I edit it lightly to remove irrelevant and private information.

AlexApostolov commented 7 years ago

In the Aspiring Comrades guild/forum Alys wrote the following which might be of help:

Everyone, changes have happened to the servers that Habitica uses which have resulted in the API's score route now needing a body parameter, which can be an empty string. For example, it's necessary to add -d "" as shown below. Ideally, we'll try to make it so that the new requirement stops being necessary since it's a breaking change, but I don't know at the moment if that will be possible.

a-pankhurst commented 7 years ago

When the addon starts working again (either because the API is fixed or the addon is changed to include the new -d "" requirement), will changing scoresincedate to a Unix time just after the last recorded click allow all cards studied since then to be scored correctly, albeit late? I thought about trying it out locally, but the code is way beyond my very, very low level.

eshapard commented 7 years ago

OK, I think I found the issue. It looks like the API isn't as fast as it used to be, so the addon kept thinking it was down. By giving the API 10 seconds to respond (instead of 2), the issues seems to be resolved. Download the version on github and see if it works for you.

eshapard commented 7 years ago

Code also changed to add dummy data to post, put, and delete requests... strangely, my ubuntu platform worked without this change, but my windows seems to need it... not sure why, but this will hopefully fix the recent API issues. Download the new code from github and let me know if its working.

eshapard commented 7 years ago

Download the files in the v2.0 folder and put them in your Anki addons folder. I don't know where that is on a Mac.

On Wed, Jul 26, 2017 at 11:21 AM, kabutar111 notifications@github.com wrote:

how do you install github version on mac

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eshapard/AnkiHabitica/issues/47#issuecomment-318139847, or mute the thread https://github.com/notifications/unsubscribe-auth/AF5-Kpca4ODadl0O_MevlH2a8eQiOrfmks5sR4PFgaJpZM4OZ0o8 .

levischooley commented 7 years ago

Hello, I first want to say "Thank you" for this fantastic plugin and for being here to maintain it.

The lack of scoring was irritating me, so I've been looking into the problem. I think I've found the reason: python's urllib2 does not consistently add the "Content-Length" HTTP header for null data. Changing the code in habitica_api.py from

        if method == "post":
           req.add_header('Content-Type', 'application/json') #Important!
           req.get_method = lambda:"POST" #Needed for no-data posts

to

        if method == "post":
           req.add_header('Content-Type', 'application/json') #Important!
           if not data: req.add_header('Content-Length', '0')
           req.get_method = lambda:"POST" #Needed for no-data posts

fixed the problem for me.

This is strange, because my installed version of python seems to add the "Content-Length: 0" header in my manual tests, but Anki's python does not add that header. I had to manually capture and compare the HTTP requests to discover the difference.

eshapard commented 7 years ago

@levischooley Thanks! That could explain why this problem doesn't show up on all platforms. I think I've fixed the problem by sending dummy data when the API doesn't require real data to be sent over, but I might as well add your fix too.

Oh, and the reason we use urllib and urllib2 is because those come with the version of python that ships with Anki.... it took me quite a while to figure out how to make them work for this purpose.

levischooley commented 7 years ago

@eshapard Yeah, I think urllib2 is fine, but it isn't well documented--I never did find a way to get it to write the HTTP request to a file from within Anki for debug purposes (very frustrating!).

I'm not sure what recently changed on Habitica's end to suddenly make this problem evident, but I've read in some places that MS IIS requires the "Content-Length" header to be present for POST requests, even if there is no data sent. Adding dummy data is certainly a solution, since it basically "forces" urllib2 to calculate and include a "Content-Length" header, and I'm assuming the Habitica server will safely discard it. But just so you are aware, I'm quite certain that the problem is the absence of the "Content-Length" header, and not the absence of the data itself.

However, if urllib2 is behaving inconsistently on different platforms, adding dummy data may be the best solution. I'd have to test to be sure, but there is a small possibility that my solution may result in two "Content-Length" headers on some platforms, which may cause other problems. Don't know...

steelwolf commented 7 years ago

Based simply on the error message I'm receiving, I'd hazard it's the "Content-Length" header that's needed rather than dummy content. I unfortunately don't have enough platforms to A-B test both ways on multiple systems though.

On Wed, Jul 26, 2017, 8:39 PM levischooley notifications@github.com wrote:

@eshapard https://github.com/eshapard Yeah, I think urllib2 is fine, but it isn't well documented--I never did find a way to get it to write the HTTP request to a file from within Anki for debug purposes (very frustrating!).

I'm not sure what recently changed on Habitica's end to suddenly make this problem evident, but I've read in some places that MS IIS requires the "Content-Length" header to be present for POST requests, even if there is no data sent. Adding dummy data is certainly a solution, since it basically "forces" urllib2 to calculate and include a "Content-Length" header, and I'm assuming the Habitica server will safely discard it. But just so you are aware, I'm quite certain that the problem is the absence of the "Content-Length" header, and not the absence of the data itself.

However, if urllib2 is behaving inconsistently on different platforms, adding dummy data may be the best solution. I'd have to test to be sure, but there is a small possibility that my solution may result in two "Content-Length" headers on some platforms, which may cause other problems. Don't know...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eshapard/AnkiHabitica/issues/47#issuecomment-318241872, or mute the thread https://github.com/notifications/unsubscribe-auth/AAwVPgKTaKt6Pauy52z5e4H-wab3vSMSks5sR_hFgaJpZM4OZ0o8 .

--

eshapard commented 7 years ago

@a-pankhurst, yes, changing the scoresincedate will do what you expect.

a-pankhurst commented 7 years ago

@eshapard Thanks for the confirmation. I'll have to wait until we get a quest started to score the backlog ;)

eshapard commented 7 years ago

I finally got around to updating the addon on ankiweb. So you should be able to just install it the usual way to fix this problem: https://ankiweb.net/shared/info/954979168

guglusharma commented 3 years ago

@eshapard Hi, thanks for creating this add on. I am facing an issue. Whenever I open Anki, I get the following error message:

Debug info: Anki 2.1.40 (cf446733) Python 3.8.6 Qt 5.14.2 PyQt 5.14.2 Platform: Windows 10 Flags: frz=True ao=True sv=1 Add-ons, last update check: 2021-04-08 22:52:41

Caught exception: Traceback (most recent call last): File "aqt\progress.py", line 52, in handler File "aqt\main.py", line 138, in on_window_init File "aqt\main.py", line 186, in setupProfileAfterWebviewsLoaded File "aqt\main.py", line 227, in setupProfile File "aqt\main.py", line 406, in loadProfile File "aqt\hooks_gen.py", line 2146, in call File "anki\hooks.py", line 34, in runHook File "C:\Users\Umesh-PC\AppData\Roaming\Anki2\addons21\1758045507__init__.py", line 751, in grab_profile check_unsynced_score() File "C:\Users\Umesh-PC\AppData\Roaming\Anki2\addons21\1758045507__init__.py", line 707, in check_unsynced_score if not (be_ready() and ah.user_settings["check_db_on_profile_load"] and ah.habitica.grab_scorecounter() and compare_score_to_db()): File "C:\Users\Umesh-PC\AppData\Roaming\Anki2\addons21\1758045507\habitica_class.py", line 367, in grab_scorecounter response = self.api.task(self.habit_id) File "C:\Users\Umesh-PC\AppData\Roaming\Anki2\addons21\1758045507\habitica_api.py", line 106, in task out = self.v3_request("get", "/tasks/%s" % str(task_id)) File "C:\Users\Umesh-PC\AppData\Roaming\Anki2\addons21\1758045507\habitica_api.py", line 75, in v3_request response = json.load(opener.open(req, timeout=timeout)) File "urllib\request.py", line 531, in open File "urllib\request.py", line 640, in http_response File "urllib\request.py", line 569, in error File "urllib\request.py", line 502, in _call_chain File "urllib\request.py", line 649, in http_error_default urllib.error.HTTPError: HTTP Error 401: Unauthorized

Is there anything I can try? Or will Uninstalling and Reinstalling be a simpler solution?

Regards