Skip to content

Commit

Permalink
🔀 Merge pull request #32 from davep/fix-job-crash
Browse files Browse the repository at this point in the history
Fix crash with `null` story data
  • Loading branch information
davep authored Jul 24, 2024
2 parents 547641c + 27d7a8b commit 1fef972
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ repos:
hooks:
- id: isort
name: isort (python)
language_version: '3.10'
language_version: '3.12'
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
language_version: python3.10
language_version: python3.12
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# OSHit ChangeLog

## v0.12.3

**Released: 2024-07-24**

- Fixed a crash when the HackerNews API returns `null` for an otherwise
valid and available story.

## v0.12.2

**Released: 2024-06-23**
Expand Down
2 changes: 1 addition & 1 deletion oshit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__credits__ = ["Dave Pearson"]
__maintainer__ = "Dave Pearson"
__email__ = "davep@davep.org"
__version__ = "0.12.2"
__version__ = "0.12.3"
__licence__ = "GPLv3+"

##############################################################################
Expand Down
1 change: 1 addition & 0 deletions oshit/app/widgets/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def _redisplay(self) -> None:
[
HackerNewsArticle(item, self.compact, number if self.numbered else None)
for number, item in enumerate(self._items)
if item.looks_valid
]
)
display.highlighted = remember
Expand Down
7 changes: 6 additions & 1 deletion oshit/hn/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ async def item(self, item_type: type[ItemType], item_id: int) -> ItemType:
Returns:
The item.
"""
if isinstance(item := Loader.load(await self._raw_item(item_id)), item_type):
# If we can get the item but it comes back with no data at all...
if not (data := await self._raw_item(item_id)):
# ...as https://hacker-news.firebaseio.com/v0/item/41050801.json
# does for some reason, just make an empty version of the item.
return item_type()
if isinstance(item := Loader.load(data), item_type):
return item
raise ValueError(
f"The item of ID '{item_id}' is of type '{item.item_type}', not {item_type.__name__}"
Expand Down
5 changes: 5 additions & 0 deletions oshit/hn/item/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def has_text(self) -> bool:
"""Does the item have any text?"""
return bool(self.text.strip())

@property
def looks_valid(self) -> bool:
"""Does the item look valid?"""
return bool(self.item_id) and bool(self.item_type)

def __contains__(self, search_for: str) -> bool:
return (
search_for.casefold() in self.by.casefold()
Expand Down

0 comments on commit 1fef972

Please sign in to comment.