import requests import os import sys import shutil from jinja2 import Template template = Template('''--- title: "{{ name }}" date: {{createdAt}}T00:00:01+00:00 draft: false categories: ["Images"] description: "{{ name }}" startpage: false lang: img gallery: "{{imagePath}}/{{mainImage}}" cc: "by-nc-sa" --- # Gallery {% raw %}{{{% endraw %}< gallery dir="{{imagePath}}" caption-position="none" caption-effect="fade" />}} {% raw %}{{{% endraw %}< load-photoswipe >}} ''') r = requests.get("https://www.kuvia.cloud/kekskurse.json?page=1&limit=100") data = r.json() for d in data["data"]["galleries"]: x = d["created"].split("-") if os.path.isdir("static/img/"+x[0]+"/"+x[1]+"/"+d["url"]["name"]): print("TODO: Refresh folder") else: os.makedirs("static/img/"+x[0]+"/"+x[1]+"/"+d["url"]["name"]) mainImageName = "" #Download all Images images = requests.get(d["url"]["json"]).json() for image in images["data"]["images"]: if image["id"] == d["mainImage"]["id"]: mainImageName = image["filename"] if os.path.isfile("static/img/"+x[0]+"/"+x[1]+"/"+d["url"]["name"]+"/"+image["filename"]) == False: print("Download") r = requests.get(image["url"]["big"], stream=True) if r.status_code == 200: with open("static/img/"+x[0]+"/"+x[1]+"/"+d["url"]["name"]+"/"+image["filename"], 'wb') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) #Create Post filename = d["created"]+"-"+d["url"]["name"]+".md" if os.path.isfile("content/posts/"+filename) == False: res = template.render(imagePath="/img/"+x[0]+"/"+x[1]+"/"+d["url"]["name"], mainImage=mainImageName, name=d["name"], createdAt=d["created"]) with open("content/posts/"+filename, "w") as f: f.write(res) f.close()