From a0d30541ee9349b8c62200c9aa6d4dbb2873fccb Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Thu, 23 Nov 2023 19:32:52 -0800 Subject: [PATCH] Replies --- paperdoorknob.py | 8 ++++++++ paperdoorknob_test.py | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/paperdoorknob.py b/paperdoorknob.py index b88c02d..0c6ec85 100644 --- a/paperdoorknob.py +++ b/paperdoorknob.py @@ -7,6 +7,9 @@ from argparse import ArgumentParser import os.path + +from typing import Iterable + from bs4 import BeautifulSoup from bs4.element import Tag import requests @@ -25,6 +28,11 @@ class Post: assert isinstance(text, Tag) return text + def replies(self) -> Iterable[Tag]: + replies = self._html.find_all("div", class_="post-reply") + assert all(isinstance(r, Tag) for r in replies) + return replies + def command_line_parser() -> ArgumentParser: parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic') diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index 83cf76a..7c48607 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -33,6 +33,14 @@ class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler):
This is glowfic
+
+
+ You sure? +
+
+ Pretty sure. +
+
''' self.send_response(self._response_code()) @@ -92,6 +100,8 @@ class TestFetch(unittest.TestCase): post = paperdoorknob.Post(paperdoorknob.fetch( f"http://localhost:{self._port()}", s, TIMEOUT)) self.assertEqual(post.text().text.strip(), "This is glowfic") + self.assertEqual([r.text.strip() for r in post.replies()], + ["You sure?", "Pretty sure."]) def testFetchErrors(self) -> None: with requests.session() as s: -- 2.44.1