From adced9d215545558ec2bba1f6da1283a8c8bac50 Mon Sep 17 00:00:00 2001 From: Megamichi Date: Thu, 18 Jul 2024 12:36:39 +0100 Subject: [PATCH] init --- main.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..dc32cfa --- /dev/null +++ b/main.py @@ -0,0 +1,62 @@ + +import requests +import xmltodict +import json +import os +from tqdm import tqdm + + +# Laden der XML-Daten +#bookshelf_xml = requests.get("https://magpi.raspberrypi.com/bookshelf.xml") +#bookshelf_dict = xmltodict.parse(bookshelf_xml.content) +#bookshelf_json = json.dumps(magpi_dict, indent=4) +bookshelf_json = json.dumps(open("magpi.json","r").read()) + +for item in bookshelf_dict["PUBS"]["MAGPI"]["ITEM"]: + title = item["TITLE"] + pdf_url = item["PDF"] + + print(f"Downloading MagPi:{title}... ")#, end="", flush=True) + + # Herunterladen der PDF-Datei mit Fortschrittsanzeige + response = requests.get(pdf_url, stream=True) + total_size = int(response.headers.get('content-length', 0)) + block_size = 1024 # 1 Kibibyte + + with open(f"magpis/{title}.pdf", "wb") as pdf: + for data in tqdm(response.iter_content(block_size), total=total_size//block_size, unit='KiB', unit_scale=True): + pdf.write(data)# Herunterladen der Magazine + + + +for item in bookshelf_dict["PUBS"]["HACKSPACE"]["ITEM"]: + title = item["TITLE"] + pdf_url = item["PDF"] + + print(f"Downloading Hackspace:{title}... ")#, end="", flush=True) + + # Herunterladen der PDF-Datei mit Fortschrittsanzeige + response = requests.get(pdf_url, stream=True) + total_size = int(response.headers.get('content-length', 0)) + block_size = 1024 # 1 Kibibyte + + with open(f"hackspaces/{title}.pdf", "wb") as pdf: + for data in tqdm(response.iter_content(block_size), total=total_size//block_size, unit='KiB', unit_scale=True): + pdf.write(data) + + + +for item in bookshelf_dict["PUBS"]["BOOKS"]["ITEM"]: + title = item["TITLE"] + pdf_url = item["PDF"] + + print(f"Downloading Book:{title}... ")#, end="", flush=True) + + # Herunterladen der PDF-Datei mit Fortschrittsanzeige + response = requests.get(pdf_url, stream=True) + total_size = int(response.headers.get('content-length', 0)) + block_size = 1024 # 1 Kibibyte + + with open(f"books/{title}.pdf", "wb") as pdf: + for data in tqdm(response.iter_content(block_size), total=total_size//block_size, unit='KiB', unit_scale=True): + pdf.write(data)