From: Scott Worley Date: Thu, 28 Dec 2023 23:19:37 +0000 (-0800) Subject: Use view=flat to get whole threads at once X-Git-Url: http://git.scottworley.com/paperdoorknob/commitdiff_plain/1452f8d33eb6ffe6812698768f1e6bb975d9c4cf?ds=sidebyside;hp=ae7b6283ed5ead577d1a8f263d37d01717e2bb33 Use view=flat to get whole threads at once --- diff --git a/glowfic.py b/glowfic.py index d1966ca..5730508 100644 --- a/glowfic.py +++ b/glowfic.py @@ -8,6 +8,7 @@ from abc import ABC, abstractmethod from dataclasses import dataclass import itertools +from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse from typing import Iterable @@ -18,6 +19,23 @@ from images import ImageStore from texify import Texifier +def _removeViewFromURL(url: str) -> str: + u = urlparse(url) + old_qs = parse_qsl(u.query) + new_qs = [(k, v) for k, v in old_qs if k != 'view'] + return urlunparse(u._replace(query=urlencode(new_qs))) + + +def nonFlatURL(url: str) -> str: + return _removeViewFromURL(url) + + +def flatURL(url: str) -> str: + u = urlparse(_removeViewFromURL(url)) + qs = parse_qsl(u.query) + [('view', 'flat')] + return urlunparse(u._replace(query=urlencode(qs))) + + @dataclass(frozen=True) class Chunk: icon: str | None diff --git a/paperdoorknob.py b/paperdoorknob.py index 5e5747c..4ee4dbc 100644 --- a/paperdoorknob.py +++ b/paperdoorknob.py @@ -8,7 +8,7 @@ from bs4 import BeautifulSoup from args import spec_from_commandline_args -from glowfic import chunkDOMs, makeChunk +from glowfic import chunkDOMs, flatURL, makeChunk from spec import Spec @@ -27,7 +27,8 @@ def process(spec: Spec) -> None: spec.geometry.encode('UTF-8') + b']{geometry}\n') spec.texout.write(b'\\begin{document}\n') - html = parse(spec.htmlfilter(spec.fetcher.fetch(spec.url))) + url = flatURL(spec.url) + html = parse(spec.htmlfilter(spec.fetcher.fetch(url))) for r in chunkDOMs(html): spec.domfilter(r) chunk = makeChunk(r, spec.images) diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index 4711fcd..2787287 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -112,7 +112,7 @@ class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase): def fetcher(self) -> Fetcher: with open('testdata/this-is-glowfic.html', 'rb') as f: - return FakeFetcher({'fic': f.read(9999)}) + return FakeFetcher({'fic?view=flat': f.read(9999)}) if __name__ == '__main__':