0

I am trying to download a dataset using the following code from a publicly available platform that was used before for the same purpose. However, I am not sure why I get this error, that is, whether because of an error in code or some changes in the website's (hatebase) API. Any suggestions would be very helpful. Thank you.

import json 
import requests
import pandas as pd
from hatebase import HatebaseAPI

key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA" # this is the key
filepath = "/dictionary.csv"  

hatebase = HatebaseAPI({"key": key})
filters = {"language": "eng"}
format = "json"
# initialize list for all vocabulary entry dictionaries
eng_vocab = []
response = hatebase.getVocabulary(filters=filters, format=format)
pages = response["number_of_pages"]
# fill the vocabulary list with all entries of all pages
# this might take some time...
for page in range(1, pages+1):
    filters["page"] = str(page) 
    response = hatebase.getVocabulary(filters=filters, format=format)
    eng_vocab.append(response["result"])

# create empty pandas df for all vocabulary entries
df_eng_vocab = pd.DataFrame()
# fill df
for elem in eng_vocab:
    df_eng_vocab = df_eng_vocab.append(elem)
# reset the df index
df_eng_vocab.reset_index(drop=True, inplace=True) 
# saving the file to csv
df_eng_vocab.to_csv(filepath)

The error I get is the following

Please check your API-Key, Authentication did nod respond with a token.
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-60-de9970a761e5> in <module>
      7 #filepath = "/dictionary.csv" 
      8 
----> 9 hatebase = HatebaseAPI({"key": key})
     10 filters = {"language": "eng"}
     11 format = "json"

~\Anaconda3\lib\site-packages\hatebase\__init__.py in __init__(self, settings)
     37             self.debug = settings["debug"]
     38 
---> 39         self.authenticate()
     40 
     41     def authenticate(self):

~\Anaconda3\lib\site-packages\hatebase\__init__.py in authenticate(self)
     57             print("Please check your API-Key, Authentication did nod respond with a token.")
     58 
---> 59         if token is not None:
     60             self.token = response.json()["result"]["token"]
     61         else:

UnboundLocalError: local variable 'token' referenced before assignment
1
  • it seems module has bug - author forgot to create variable token with default value None. OR maybe it couldn't get token from server and it didn't set default value token = None. Porbably you will have to digg in source code to check token. OR if this module has GitHub then check on GitHub if it has some fix. Commented Jul 16, 2021 at 2:09

1 Answer 1

1

EDIT:

After checking source code I found that you can set current version to resolve problem

hatebase = HatebaseAPI({"key": key, "version": "4-4"})

ORIGINAL VERSION:

I registerd on Hatebase.org to get API Key but I didn't select price plan.


I took source code and added print() in function authenticate() to see `JSON data from server

    print(response.json())

    try:
        token = response.json()["result"]["token"]
    except KeyError as e:
        print("Please check your API-Key, Authentication did nod respond with a token.")

and it shows this text somewhere in JSON

'The version of the API is now retired; 
 please update your queries to resume accessing the API'

Source code has line version = '4-2' but documentation shows that current version is 4.4 so I changed into version = '4-4' and it stopped showing this error.

Because I didn't select price plan so now I get

'You must assign a plan to your account to access the API'

I will not select price plan so I don't know if it may need other changes.


You can copy source code with class HatebaseAPI to your file and change version = '4-4' and maybe it will works for you.


I sent this problem to author of the module as issues on GitHub: newer API version - 4.4.
Maybe it will correct module (or not).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. This is definitely the issue. I have now found a direct solution here: gist.github.com/noahgh221/68ae96c3f6dc41c9596fed704fdc454d which worked
after checking source code I found that you can run HatebaseAPI({"key": key, 'version': '4-4'})
Not actually. It still shows the same error.
it can show the same error with token but it may have different problem - and only text in JSON data could explain it . In my answer I had the same error with token but first because I had wrong version in code, and later because I didn't select price plan. Bot problems gives the same error but they need different changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.