parafoxia / opentriviadb

An asynchronous wrapper for the Open Trivia DB API.
https://parafoxia.github.io/opentriviadb/
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

Multiple issues encountered trying to get this package to work! #1

Open apitofme opened 1 month ago

apitofme commented 1 month ago

Please take this as "being brief" rather than being blunt...

  1. Documentation is sparse and seemingly inaccurate. e.g. looking at client.py on line #196 you start the example with from opentriviadb import Category -- this alone appears to have two issues when I attempted to copy this example:

i. The package name is incorrect:

Traceback (most recent call last):
  File "<python-input-20>", line 1, in <module>
    from opentriviadb import Category
ModuleNotFoundError: No module named 'opentriviadb'

-- "opentdb" however does work!

ii. The module name is inccorrect:

Traceback (most recent call last):
  File "<python-input-21>", line 1, in <module>
    from opentdb import Category
ImportError: cannot import name 'Category' from 'opentdb' (--snip--/lib/python3.13/site-packages/opentdb/__init__.py). Did you mean: 'categories'?

-- "categories" works!

  1. The code of the locally installed package client.py file does not match the one (linked above) here on GitHub. Whilst trying to confirm the package was installed and available I opened a Python interactive shell prompt and ran help('modules opentdb') to confirm the package was available. I then ran just help('opentdb') which resulted in an error, (to simplify) the important part of which is:
    from .client import Client
    File "--snip--/lib/python3.13/site-packages/opentdb/client.py", line 5, in <module>
    from HTMLParser import HTMLParser
    ModuleNotFoundError: No module named 'HTMLParser'

    -- Opening the file and correcting line #5 to read from html.parser import HTMLParser fixed that issue!

However later in the file you have the line unescape = HTMLParser().unescape which also causes an error, since HTMLParser does not have an attribute called 'unescape' (or a method for that matter), the base html module has the unescape method on it. (Perhaps a Python version issue?)

  1. The examples in client.py (including the one previously mentioned above) seem to all be using the async syntax, as does the main usage example on the homepage of this project, e.g. async with Client() as client: but this causes another error message: async with Client() as client: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: 'async with' outside async function -- I am not familiar with the asyncio library so I could attribute this to "rookie error", but if so then perhaps the example should be more comprehensive so as to demonstrate it's usage in a way that works when copied 'verbatim' as it were.

As such, I am so far unable to get this package to function in any meaningful way. Even when creating a Client instance without using async I am unable to yield any questions following (and adapting) the examples given...

  1. Using an interactive Python prompt I import 'Client' and crete a client object instance as per the example on the home page. Then inspecting this object with dir() I can see: ['_Client__API_BASEURL', '_Client__API_ENDPOINT', '_Client__API_TOKEN', '_Client__API_TOKEN_ENDPOINT', '_Client__apiRequest', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'getCategories', 'getQuestions', 'getToken', 'printToken']

I am confused because the available public methods (listed last) do not match up to the code here on GitHub, e.g. the request_token method.

Note: I installed this package from PyPI using pip, which reports the same version number as here on GitHub.

parafoxia commented 1 month ago

Just to confirm, what command did you run to install this? You've installed opentdb, a completely different package, by mistake. Whether you've typo'd or whether something really weird has happened I'm not sure, but either way, it's not opentriviadb you have on your system.

parafoxia commented 1 month ago

This is what I get when doing it on 3.13 (I haven't declared it supports 3.13 yet):

❯ pip install opentriviadb
ERROR: Ignored the following versions that require a different python version: 0.1.0 Requires-Python >=3.7.0,<3.13
ERROR: Could not find a version that satisfies the requirement opentriviadb (from versions: none)
ERROR: No matching distribution found for opentriviadb

And on 3.12:

❯ pip install opentriviadb
...
Installing collected packages: propcache, multidict, idna, frozenlist, attrs, aiohappyeyeballs, yarl, aiosignal, aiohttp, opentriviadb
Successfully installed aiohappyeyeballs-2.4.3 aiohttp-3.10.10 aiosignal-1.3.1 attrs-24.2.0 frozenlist-1.4.1 idna-3.10 multidict-6.1.0 opentriviadb-0.1.0 propcache-0.2.0 yarl-1.16.0

❯ py -c "from opentriviadb import Category; print(Category)"
<enum 'Category'>
apitofme commented 1 month ago

Oh dear, my sincere apologies you are absolutely correct. As you could see I was referring to the PyPI page for this project ... that explains my confusion when comparing the code-bases.

I think I might have had a local file with the same name when I tried to install the package, because I had started off trying to write my own OTDB interface before deciding to search for an existing solution ... but even so I can't see how that would have caused this. -- I must have miss typed it and not realised. I am so sorry, I usually just copy and paste the install commands from PyPI.

Strange thing is I remember seeing this:

Installing collected packages: propcache, multidict, idna, frozenlist, attrs, aiohappyeyeballs, yarl, aiosignal, aiohttp, opentriviadb
Successfully installed aiohappyeyeballs-2.4.3 aiohttp-3.10.10 aiosignal-1.3.1 attrs-24.2.0 frozenlist-1.4.1 idna-3.10 multidict-6.1.0 opentriviadb-0.1.0 propcache-0.2.0 yarl-1.16.0

...because I looked up each package to see what it was and what it did...


Ahh right, I think maybe I just figured it out: The first install was on Python 3.12, and the installation did work, but my local file must have clashed during import name resolution, so it didn't work when I tried to use it. Then (for some reason) I decided to run update on my python environment (built with micromamaba), which is how I ended up with Python 3.13 ... though I must have still made the typo at some point while trying to reinstall this module, so I ended up with opentdb (as you said) by mistake ... and from there the confusion only got worse!

Well, on the bright side at least there's nothing wrong after all! I have removed the incorrect package, rebuilt my environment and it locked to Python 3.12, reinstalled the opentriviadb package and have managed to retrieve questions.

My only note is that I had to wrap the async with Client() as client: and await client.request_token() lines within an async function definition, which I then had to call with asyncio.run() (adding this module to my imports) ... I haven't used any kind of asynchronous libraries and code before so I'm assuming this is correct? It just wasn't clear.

Thanks, and again so sorry.