From: Scott Worley Date: Thu, 23 Nov 2023 19:56:31 +0000 (-0800) Subject: Fetch URLs X-Git-Url: http://git.scottworley.com/paperdoorknob/commitdiff_plain/b25a2f90428a1e7fda03c77fcee8ed0fa3b22555?ds=sidebyside Fetch URLs --- diff --git a/README.md b/README.md index 7746516..9782d63 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ A polite glowfic → printable book scraper. +* Retrieves glowfic ### Alternatives diff --git a/default.nix b/default.nix index f73efb5..affb068 100644 --- a/default.nix +++ b/default.nix @@ -1,11 +1,13 @@ { pkgs ? import { }, lint ? false }: pkgs.python3Packages.callPackage -({ lib, buildPythonPackage, autopep8, mypy, pylint, }: +({ lib, buildPythonPackage, autopep8, mypy, pylint, requests, types-requests }: buildPythonPackage rec { pname = "paperdoorknob"; version = "0.0.1"; src = lib.cleanSource ./.; - nativeCheckInputs = [ mypy ] ++ lib.optionals lint [ autopep8 pylint ]; + propagatedBuildInputs = [ requests ]; + nativeCheckInputs = [ mypy types-requests ] + ++ lib.optionals lint [ autopep8 pylint ]; doCheck = true; checkPhase = "./test.sh"; meta = { diff --git a/paperdoorknob.py b/paperdoorknob.py index 8f1d840..cd93e4e 100644 --- a/paperdoorknob.py +++ b/paperdoorknob.py @@ -6,15 +6,27 @@ from argparse import ArgumentParser +import requests def command_line_parser() -> ArgumentParser: parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic') + parser.add_argument( + '--timeout', + help='How long to wait for HTTP requests, in seconds', + default=30) + parser.add_argument('url', help='URL to retrieve') return parser +def fetch(url: str, timeout: int) -> None: + r = requests.get(url, timeout=timeout) + r.raise_for_status() + + def main() -> None: - command_line_parser().parse_args() + args = command_line_parser().parse_args() + fetch(args.url, args.timeout) if __name__ == '__main__': diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py new file mode 100644 index 0000000..c19e4db --- /dev/null +++ b/paperdoorknob_test.py @@ -0,0 +1,42 @@ +# paperdoorknob: Print glowfic +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation, version 3. + + +import unittest +import threading +from http.server import BaseHTTPRequestHandler, HTTPServer +import paperdoorknob + +TEST_PORT = 8080 +TIMEOUT = 8 + + +class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler): + + def do_GET(self) -> None: + body = b'This is glowfic' + self.send_response(200) + self.send_header("Content-type", "text/html") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + +class TestFetch(unittest.TestCase): + def setUp(self) -> None: + web_server = HTTPServer(('', TEST_PORT), FakeGlowficHTTPRequestHandler) + threading.Thread(target=web_server.serve_forever).start() + self._stop_server = web_server.shutdown + + def tearDown(self) -> None: + self._stop_server() + + def testFetch(self) -> None: + paperdoorknob.fetch(f"http://localhost:{TEST_PORT}", TIMEOUT) + + +if __name__ == '__main__': + unittest.main()