import requests
import urllib.request
import xml.etree.ElementTree as ET
import subprocess
from pathlib import Path
import os

maxpages = 396
ebooknr = 584

contentdir = "./pages"							# ./pages													dir
pagedirtemplate = contentdir + "/{}"			# ./pages/1				pagenr								dir
subdirtemplate 	= pagedirtemplate + "/{}"		# ./pages/1/img 		pagenr, "img"						dir

svgsavetemplate = pagedirtemplate + "/{}.{}"	# ./pages/1/1.svg 		pagenr, pagenr, "svg"				file
imgsavetemplate = subdirtemplate + "/{}.{}"		# ./pages/1/img/1.png 	pagenr, "img", imgnr, "png"			file

# https://a.trauner-digibox.com/ebook/584/1/1.svg
# ebook number, page number, page number, sxtension
svgurltemplate = "https://a.trauner-digibox.com/ebook/{}/{}/{}.{}"

# https://a.trauner-digibox.com/ebook/584/1/img/1.png
# ebook number, page number, img kind, img number, extension
imgurltemplate = "https://a.trauner-digibox.com/ebook/{}/{}/{}/{}.{}"


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



ebpb="220831512%2c584%2c201dohn3vdp9%2c18390%2c0%2c0%2c%2c48+%7b983+1572032732+32D669F6A2D0D3447F9B730529C1F4A33C0883EB%7d";


for pagenr in range(1, maxpages + 1):
	cookies = {
		'digi4s': '18390%2c1571906836+%7b575+0+7D8ED830D87F0B2B8E7F0F98F4DC2E03C6BF66E8%7d',
		'ebpb' : '220831512%2c584%2c201dohn3vdp9%2c18390%2c0%2c0%2c%2c48+%7b983+1572032732+32D669F6A2D0D3447F9B730529C1F4A33C0883EB%7d'
	}

	headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 5.1.1; SM-G928X Build/LMY47X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.83 Mobile Safari/537.36'}

	print(pagenr)

	if not os.path.exists(pagedirtemplate.format(pagenr)):
		os.makedirs(pagedirtemplate.format(pagenr))
	if not os.path.exists(subdirtemplate.format(pagenr, "img")):
		os.makedirs(subdirtemplate.format(pagenr, "img"))
	if not os.path.exists(subdirtemplate.format(pagenr, "shade")):
		os.makedirs(subdirtemplate.format(pagenr, "shade"))

	response = requests.get(svgurltemplate.format(ebooknr, pagenr, pagenr, "svg"), headers=headers, cookies=cookies)   
	if (response.status_code != 404):
		with open(svgsavetemplate.format(pagenr, pagenr, "svg"), 'wb') as f:
			f.write(response.content)

	imagecount = 0

	with open(svgsavetemplate.format(pagenr, pagenr, "svg")) as f:
	    contents = f.read()
	    imagecount = contents.count("image")

	for imgnr in range(0, imagecount + 1):
		print("+{}".format(imgnr))

		response = requests.get(imgurltemplate.format(ebooknr, pagenr, "img", imgnr, "png"), headers=headers, cookies=cookies)   	
		if (response.status_code != 404):
			with open(imgsavetemplate.format(pagenr, "img", imgnr, "png"), 'wb') as f:
				f.write(response.content)

		response = requests.get(imgurltemplate.format(ebooknr, pagenr, "shade", imgnr, "png"), headers=headers, cookies=cookies)   	
		if (response.status_code != 404):
			with open(imgsavetemplate.format(pagenr, "shade", imgnr, "png"), 'wb') as f:
				f.write(response.content)

	subprocess.call(["rsvg-convert", "-f", "pdf", "-o", "{}.pdf".format(pagenr), svgsavetemplate.format(pagenr, pagenr, "svg")])