guptachetan1997/Episodes

API key needed ?

julienbalas opened this issue · 9 comments

Hi,
I can't make Episodes work.
Every add_search ends-up in a blank page.

Should every user apply for an API key on the tvdb website ?

Is there any API KEY modification to apply in the tvdb_api_wrap.py file ?
Adding some logs in the search_series_list , the json_r variable contains {'Error': 'Not authorized'}

Best regards.

For mass usage you may apply for your own API key, but for personal usage the given works fine for me and others who have used this.

Ok. I use my own key now but i think there is a problem in the way the token is save to disk.
The file is always empty (but my token is not according to my log)
If i put my token manually into the file and comment the part of get_next_token which save the token into the file the application is working fine.
I will try to investigate later this day.

Best regards

Some other user faced this error of the token not going in the file also. I could not find any solution to this problem as this was working on my system. So to make this work for that user I created the production branch, wherein I commented out the code to save the token and instead directly got the token from the API every time.

Try shifting to the production branch, that might surely work.
Also if you could find a solution to this, all suggestions are welcome.

It's quite strange, the python code get a 401-notauthorized response
But if i use the swagger test pages for the API and fill in the apikey&co, i get a token.

To my eyes, the 2 query seems identical

Python code (excerpt from tvdb_api_wrapper)
r = requests.post('https://api.thetvdb.com/login', data={"username": "gupta.chetan1997", "userkey": "217AEB727734271F", "apikey": "DA10DC72930575CA"}, headers={"Content-Type":"application/json","Accept": "application/json", "User-agent": "Mozilla/5.0"})
print (r.status_code)
print (r.text)

curl from swagger
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
"apikey": "DA10DC72930575CA",
"userkey": "217AEB727734271F",
"username": "gupta.chetan1997"
}' 'https://api.thetvdb.com/login'

Well... I was missing a "json.dumps(data)" in the python code, now the python code also get a valid token in my test code
r = requests.post('https://api.thetvdb.com/login', data=json.dumps({"apikey": "DA10DC72930575CA", "userkey": "217AEB727734271F","username": "gupta.chetan1997"}), headers={"Content-Type":"application/json","Accept": "application/json", "User-agent": "Mozilla/5.0"})

and it's also working in the episodes code...
https://gist.github.com/julienbalas/cfe4b3d4ddc77f6def11274093366548
the only modification I've made is to save the token in the "try" portion of code

The thing was that getting the token from file does not work for everyone. It works on my system, but many said otherwise. Can you suggest some other method of storing the auth token.
One method which can be done is saving it in a database table, that way checking the time interval would also become easy. All suggestions are welcome :)

the01 commented

You don't actually need the json.dumps, requests can do that for you:

requests.post(.., json={..}, ..)

;-)

The issue is that if you clone the code, the token file (Episodes/token.datas) gets a timestamp from the time when you cloned the repository. This means the code will assume the token is valid and use it for requests (rather than retrieving a new token).

If you download a .zip/.tar.gz containing the code instead, the timestamp points to the time the last commit to the repository was (seems to be what GitHub sets it to for all files in generated archives). This means it will work as expected in this case (either the token is still valid, or the file timestamp is correctly detected as too old and a new token retrieved).

So, in summary: it will never work if you clone the git repository with the current code.

I suggest modifying the code to handle Episodes/token.datas not existing at all by requesting a new token and saving it (right now it just crashes). Then remove the file from the repository.

@tobbez Thank you for your insights. I did realise that it was wrong to use the modified time of the file to update the token. I have removed that part.