ArmindoFlores / ao3_api

An unofficial archiveofourown.org (AO3) API for python
MIT License
163 stars 62 forks source link

Works hidden by being added to a collection cause an AttributeError if you try to initialize them. #81

Open TheElementalOfDestruction opened 1 year ago

TheElementalOfDestruction commented 1 year ago
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python39\lib\site-packages\AO3\works.py", line 37, in __init__
    self.reload(load_chapters)
  File "C:\Program Files\Python39\lib\site-packages\AO3\threadable.py", line 13, in new
    return func(*args, **kwargs)
  File "C:\Program Files\Python39\lib\site-packages\AO3\works.py", line 81, in reload
    if "Error 404" in self._soup.find("h2", {"class", "heading"}).text:
AttributeError: 'NoneType' object has no attribute 'text'

Expected result: Trying to create a Work instance using an ID of a work that has been hidden by being added to a collection (shows up on the site as a Mystery Work) should either raise a descriptive exception about it being hidden or should cause the object to be flagged in some way that the user can detect.

Actual result: An extremly unclear error is thrown because the existing error handling expects that the page shows is actually a 404 error, and treats it as such without checking.

An example of a work that will throw this exception is 20336386, and the page can be seen at https://archiveofourown.org/works/20336386

I believe that simply raising InvalidIdError, as the class already does for a work that simply couldn't be found, with a different message would be an acceptable behavior. Additionally, it looks like a <p> block with the class "notice" is something that may be unique to this page, and could be used to detect this.

EthanLeitch commented 1 year ago

Restricted works also exhibit this behavior. For example, attempting to get this restricted work results in this:

>>> AO3.Work(38952870)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Ethan/Desktop/ao3scraper/.venv/lib/python3.10/site-packages/AO3/works.py", line 37, in __init__
    self.reload(load_chapters)
  File "/Users/Ethan/Desktop/ao3scraper/.venv/lib/python3.10/site-packages/AO3/threadable.py", line 13, in new
    return func(*args, **kwargs)
  File "/Users/Ethan/Desktop/ao3scraper/.venv/lib/python3.10/site-packages/AO3/works.py", line 81, in reload
    if "Error 404" in self._soup.find("h2", {"class", "heading"}).text:
AttributeError: 'NoneType' object has no attribute 'text'