Skip to content

Commit e959d2a

Browse files
author
renaud gaudin
committed
Add access to the target of a redirect
Added `ReadArticle.get_redirect_article()` to access a redirect's target. Raises `RuntimeError` on non-redirect articles.
1 parent dff16ac commit e959d2a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

libzim/wrapper.pyx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,18 @@ cdef class ReadArticle:
322322
"""Get if the article is a redirect"""
323323
return self.c_article.isRedirect()
324324

325+
def get_redirect_article(self) -> ReadArticle:
326+
""" Target ReadArticle of this one """
327+
if not self.is_redirect:
328+
raise RuntimeError("Article is not a redirect")
329+
330+
cdef wrapper.Article art = self.c_article.getRedirectArticle()
331+
if not art.good():
332+
raise RuntimeError("Redirect article not found")
333+
334+
article = ReadArticle.from_read_article(art)
335+
return article
336+
325337
def __repr__(self):
326338
return f"{self.__class__.__name__}(url={self.longurl}, title=)"
327339

tests/test_libzim_file_reader.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,16 @@ def test_get_wrong_article(reader):
114114
reader.get_article_by_id(reader.article_count + 100)
115115
with pytest.raises(RuntimeError):
116116
reader.get_article("A/I_do_not_exists")
117+
118+
119+
def test_redirects(reader):
120+
# we can access target article from a redirect one
121+
abundante = reader.get_article("A/Abundante")
122+
assert abundante.is_redirect
123+
target = abundante.get_redirect_article()
124+
assert target.longurl != abundante.longurl
125+
126+
# we can't access a target on non-redirect articles
127+
assert target.is_redirect is False
128+
with pytest.raises(RuntimeError):
129+
target.get_redirect_article()

0 commit comments

Comments
 (0)