]> git.scottworley.com Git - paperdoorknob/commitdiff
Fetch URLs
authorScott Worley <scottworley@scottworley.com>
Thu, 23 Nov 2023 19:56:31 +0000 (11:56 -0800)
committerScott Worley <scottworley@scottworley.com>
Wed, 20 Dec 2023 01:36:53 +0000 (17:36 -0800)
README.md
default.nix
paperdoorknob.py
paperdoorknob_test.py [new file with mode: 0644]

index 7746516245f77a835ccdd221e3c80ffe673ba4d0..9782d630466c1246e8fb7a8aea9f9f53ac245775 100644 (file)
--- a/README.md
+++ b/README.md
@@ -2,6 +2,7 @@
 
 A polite glowfic → printable book scraper.
 
+* Retrieves glowfic
 
 
 ### Alternatives
index f73efb59f4bf6c6e8cd558be53f76550a5eda24c..affb0683816190d9f9cfcd52f405a048db0c27d2 100644 (file)
@@ -1,11 +1,13 @@
 { pkgs ? import <nixpkgs> { }, 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 = {
index 8f1d840d4a4f0bc9155e6e645b19535442cb2914..cd93e4ef69c24b84679432ac0a5d6435aff6e813 100644 (file)
@@ -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 (file)
index 0000000..c19e4db
--- /dev/null
@@ -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'<html><body>This is glowfic</body></html>'
+        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()