hodgesmr / mastodon_digest

A Python script that aggregates recent popular posts from your Mastodon timeline
BSD 3-Clause "New" or "Revised" License
439 stars 58 forks source link

InverseFollowerWeight crashes when the number of followers is hidden #27

Closed cnotin closed 1 year ago

cnotin commented 1 year ago

Using the default options I noticed this error:

Traceback (most recent call last):
  File "/opt/mastodon_digest/run.py", line 190, in <module>
    run(
  File "/opt/mastodon_digest/run.py", line 70, in run
    threshold_posts = threshold.posts_meeting_criteria(posts, scorer)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/mastodon_digest/thresholds.py", line 26, in posts_meeting_criteria
    all_post_scores = [p.get_score(scorer) for p in posts]
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/mastodon_digest/thresholds.py", line 26, in <listcomp>
    all_post_scores = [p.get_score(scorer) for p in posts]
                       ^^^^^^^^^^^^^^^^^^^
  File "/opt/mastodon_digest/models.py", line 21, in get_score
    return scorer.score(self)
           ^^^^^^^^^^^^^^^^^^
  File "/opt/mastodon_digest/scorers.py", line 75, in score
    return super().score(scored_post) * super().weight(scored_post)
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/mastodon_digest/scorers.py", line 38, in weight
    weight = 1 / sqrt(scored_post.info["account"]["followers_count"])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: math domain error

Bug is on this line: https://github.com/hodgesmr/mastodon_digest/blob/f70a01896e9b69e05803f6be1cd863e1ffc630b8/scorers.py#L36

By adding debug info to scored_post.info["account"]["followers_count"] I noticed that indeed I'm following an account which has hidden its number of followers: image

Therefore, followers_count=-1 and the sqrt fails

I haven't read the algorithm yet to suggest what's the reasonable thing to do in this case. Perhaps someone will be quicker than me :)