Bookshelf-downloader/main.py

66 lines
2.2 KiB
Python
Raw Permalink Normal View History

2024-07-18 13:36:39 +02:00
import requests
import xmltodict
import json
import os
from tqdm import tqdm
2024-07-18 13:42:15 +02:00
os.makedirs('magpis', exist_ok=True)
os.makedirs('hackspaces', exist_ok=True)
os.makedirs('books', exist_ok=True)
2024-07-18 13:36:39 +02:00
# 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)