A polite glowfic → printable book scraper.
+* Retrieves glowfic
### Alternatives
{ 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 = {
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__':
--- /dev/null
+# 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()