Nimbin[12]?Linux From Scratch / Packagemanager-LFS-PackageUser-System / OLD/blfs_parse_all

Packagemanager-LFS-PackageUser-System git · main

Package-user based package manager for LFS/BLFS

lfs blfs package-manager bash · first commit 2026-08-18 · last commit 2026-10-26 (8 days ago) · synced 8 hours ago · upstream: github.com/nimbin2/Packagemanager-LFS-PackageUser-System

Shell 63.7% Markdown 35.8%
git clone https://git.christianimmanuel.de/linux-from-scratch/Packagemanager-LFS-PackageUser-System.gitwget https://git.christianimmanuel.de/linux-from-scratch/Packagemanager-LFS-PackageUser-System/archive/Packagemanager-LFS-PackageUser-System.tar.gz
OLD/blfs_parse_all 3 KB · 97 lines raw
#!/bin/python3

import os
import requests
from bs4 import BeautifulSoup
import tempfile


base_url = "https://www.linuxfromscratch.org/blfs/view/12.0/"
main_index_url = f"{base_url}index.html"
base_dir = tempfile.mkdtemp()
print(base_dir)

class ChapterInfo:
    def __init__(self):
        self.chapter_name = ""
        self.chapter_link = ""
        self.chapter_pages = []

# Create the 'downloaded_pages' directory if it doesn't exist
if not os.path.exists(base_dir):
    os.makedirs(base_dir)

response = requests.get(main_index_url)
if response.status_code == 200:
    # Save the HTML content to a file
    with open(f"{base_dir}/index.html", "w", encoding="utf-8") as file:
        file.write(response.text)

    # Create a BeautifulSoup object
    soup = BeautifulSoup(response.text, 'html.parser')

    chapters = []
    for part in soup.select('div.book div.toc li.part'):
        for chapter in part.select('li.chapter'):
            chapter_info = ChapterInfo()
            h4 = chapter.find('h4')
            if h4:
                chapter_info.chapter_name = ' '.join(h4.a.get_text(strip=True).split())
                chapter_info.chapter_link = h4.a['href']

            ul = chapter.find('ul')
            if ul:
                chapter_info.chapter_pages = [(' '.join(a.get_text(strip=True).split()), a['href']) for a in ul.select('li a')]
            
            chapters.append(chapter_info)



    for chapter in chapters:
        print("Chapter Name:", chapter.chapter_name)
        print("Chapter Link:", chapter.chapter_link)
        print("Chapter Pages:")

        chapter_url = base_url + chapter.chapter_link
        chapter_response = requests.get(chapter_url)

        if chapter_response.status_code == 200:
            # Create a directory for the chapter
            chapter_file = os.path.join(base_dir, chapter.chapter_link)
            chapter_dir = os.path.dirname(chapter_file)
            print("1#################")
            print(chapter_file)
            print(chapter_dir)
            print("2#################")

            if not os.path.exists(chapter_dir):
                os.makedirs(chapter_dir)

            # Save the chapter page content
            print(f"CH: {chapter_dir}")
            with open(chapter_file, "wb") as f:
                f.write(chapter_response.content)

            # Download and save chapter pages
            for page_name, page_link in chapter.chapter_pages:
                print(f"{page_name}\t{page_link}")
                page_url = base_url + page_link
                page_response = requests.get(page_url)

                if page_response.status_code == 200:
                    page_file = os.path.join(base_dir, page_link)
                    page_dir = os.path.dirname(page_file)
                    os.makedirs(page_dir, exist_ok=True)

                    with open(page_file, "wb") as f:
                        f.write(page_response.content)


else:
    print(f"Failed to fetch the page. Status code: {response.status_code}")


print()
print(base_dir)
#shutil.rmtree(base_dir)
#print("Temporary directory deleted.")