feat: only start from the beginning if --first is set

This commit is contained in:
missytake 2025-11-24 21:44:06 +01:00
parent 95b07cd802
commit 5b342f4667
Signed by: missytake
GPG key ID: 04CC6658320518DF

19
main.py
View file

@ -52,20 +52,21 @@ def fetch_page(
def fetch_comic(args): def fetch_comic(args):
"""Download the requested comic pages to a local directory.""" """Download the requested comic pages to a local directory."""
home_page = requests.get(args.domain) page = requests.get(args.domain)
home_s = BeautifulSoup(home_page.text, "html.parser") soup = BeautifulSoup(page.text, "html.parser")
out_dir = Path(os.getcwd()).joinpath(args.output) out_dir = Path(os.getcwd()).joinpath(args.output)
first_url = home_s.find("a", rel=args.first).get("href") if args.first:
first_url = soup.find("a", rel=args.first).get("href")
if not first_url.startswith("https://"): if not first_url.startswith("https://"):
first_url = args.domain + first_url first_url = args.domain + first_url
next_page = requests.get(first_url) page = requests.get(first_url)
next_soup = BeautifulSoup(next_page.text, "html.parser") soup = BeautifulSoup(page.text, "html.parser")
try: try:
next_url = next_soup.find("a", rel=args.next).get("href") next_url = soup.find("a", rel=args.next).get("href")
except AttributeError: except AttributeError:
print(next_soup.find("a")) print(soup.find("a"))
i = 1 i = 1
while i < args.end: while i < args.end:
begin = args.begin if args.begin else 1 begin = args.begin if args.begin else 1
@ -94,10 +95,10 @@ def cli():
"--img", default=None, help="html 'id' tag of the comic's img element(s)" "--img", default=None, help="html 'id' tag of the comic's img element(s)"
) )
parser.add_argument( parser.add_argument(
"--first", default=None, help="html 'id' tag of the 'first comic' button" "--first", default=None, help="html 'rel' tag of the 'first comic' button"
) )
parser.add_argument( parser.add_argument(
"--next", default=None, help="html 'id' tag of the 'next comic' button" "--next", default=None, help="html 'rel' tag of the 'next comic' button"
) )
parser.add_argument( parser.add_argument(