2021-09-25 16:24:03 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
'''
|
|
|
|
This is an experimental Python version of libgourou.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# pyright: reportUndefinedVariable=false
|
|
|
|
|
2021-10-03 10:30:45 +02:00
|
|
|
import sys, os, time, shutil
|
2021-09-27 17:06:05 +02:00
|
|
|
|
2021-09-25 16:24:03 +02:00
|
|
|
import zipfile
|
|
|
|
from lxml import etree
|
|
|
|
|
2021-10-03 10:30:45 +02:00
|
|
|
from libadobe import sendHTTPRequest_DL2FILE
|
2021-09-25 16:24:03 +02:00
|
|
|
from libadobeFulfill import buildRights, fulfill
|
2021-10-03 10:30:45 +02:00
|
|
|
from libpdf import patch_drm_into_pdf
|
2021-09-25 16:24:03 +02:00
|
|
|
|
|
|
|
FILE_DEVICEKEY = "devicesalt"
|
|
|
|
FILE_DEVICEXML = "device.xml"
|
|
|
|
FILE_ACTIVATIONXML = "activation.xml"
|
|
|
|
|
|
|
|
#######################################################################
|
|
|
|
|
|
|
|
|
2021-09-27 17:06:05 +02:00
|
|
|
def download(replyData):
|
|
|
|
# replyData: str
|
2021-09-25 16:24:03 +02:00
|
|
|
adobe_fulfill_response = etree.fromstring(replyData)
|
|
|
|
NSMAP = { "adept" : "http://ns.adobe.com/adept" }
|
|
|
|
adNS = lambda tag: '{%s}%s' % ('http://ns.adobe.com/adept', tag)
|
|
|
|
adDC = lambda tag: '{%s}%s' % ('http://purl.org/dc/elements/1.1/', tag)
|
|
|
|
|
2021-09-28 18:43:14 +02:00
|
|
|
print (replyData)
|
|
|
|
|
2021-09-25 16:24:03 +02:00
|
|
|
|
|
|
|
download_url = adobe_fulfill_response.find("./%s/%s/%s" % (adNS("fulfillmentResult"), adNS("resourceItemInfo"), adNS("src"))).text
|
|
|
|
resource_id = adobe_fulfill_response.find("./%s/%s/%s" % (adNS("fulfillmentResult"), adNS("resourceItemInfo"), adNS("resource"))).text
|
|
|
|
license_token_node = adobe_fulfill_response.find("./%s/%s/%s" % (adNS("fulfillmentResult"), adNS("resourceItemInfo"), adNS("licenseToken")))
|
|
|
|
|
|
|
|
rights_xml_str = buildRights(license_token_node)
|
|
|
|
|
|
|
|
if (rights_xml_str is None):
|
|
|
|
print("Building rights.xml failed!")
|
2022-04-28 16:50:55 +02:00
|
|
|
return False
|
2021-09-25 16:24:03 +02:00
|
|
|
|
|
|
|
book_name = None
|
2021-10-03 10:30:45 +02:00
|
|
|
|
2021-09-25 16:24:03 +02:00
|
|
|
try:
|
2021-12-15 10:09:52 +01:00
|
|
|
metadata_node = adobe_fulfill_response.find("./%s/%s/%s" % (adNS("fulfillmentResult"), adNS("resourceItemInfo"), adNS("metadata")))
|
2021-09-25 16:24:03 +02:00
|
|
|
book_name = metadata_node.find("./%s" % (adDC("title"))).text
|
|
|
|
except:
|
|
|
|
book_name = "Book"
|
2021-09-28 18:43:14 +02:00
|
|
|
|
2021-09-25 16:24:03 +02:00
|
|
|
|
|
|
|
# Download eBook:
|
|
|
|
|
2021-09-28 18:43:14 +02:00
|
|
|
print(download_url)
|
|
|
|
|
2021-10-03 10:30:45 +02:00
|
|
|
filename_tmp = book_name + ".tmp"
|
|
|
|
|
|
|
|
dl_start_time = int(time.time() * 1000)
|
|
|
|
ret = sendHTTPRequest_DL2FILE(download_url, filename_tmp)
|
|
|
|
dl_end_time = int(time.time() * 1000)
|
|
|
|
print("Download took %d milliseconds" % (dl_end_time - dl_start_time))
|
|
|
|
|
|
|
|
if (ret != 200):
|
|
|
|
print("Download failed with error %d" % (ret))
|
2022-04-28 16:50:55 +02:00
|
|
|
return False
|
2021-10-03 10:30:45 +02:00
|
|
|
|
|
|
|
with open(filename_tmp, "rb") as f:
|
|
|
|
book_content = f.read(10)
|
|
|
|
|
2021-09-25 16:24:03 +02:00
|
|
|
filetype = ".bin"
|
|
|
|
|
|
|
|
if (book_content.startswith(b"PK")):
|
|
|
|
print("That's a ZIP file -> EPUB")
|
|
|
|
filetype = ".epub"
|
|
|
|
elif (book_content.startswith(b"%PDF")):
|
|
|
|
print("That's a PDF file")
|
|
|
|
filetype = ".pdf"
|
|
|
|
|
|
|
|
filename = book_name + filetype
|
2021-10-03 10:30:45 +02:00
|
|
|
shutil.move(filename_tmp, filename)
|
2021-09-25 16:24:03 +02:00
|
|
|
|
|
|
|
if filetype == ".epub":
|
|
|
|
# Store EPUB rights / encryption stuff
|
|
|
|
zf = zipfile.ZipFile(filename, "a")
|
|
|
|
zf.writestr("META-INF/rights.xml", rights_xml_str)
|
|
|
|
zf.close()
|
|
|
|
|
2021-09-28 18:43:14 +02:00
|
|
|
print("File successfully fulfilled to " + filename)
|
2022-04-28 16:50:55 +02:00
|
|
|
return True
|
2021-09-28 18:43:14 +02:00
|
|
|
|
2021-09-25 16:24:03 +02:00
|
|
|
elif filetype == ".pdf":
|
2021-09-28 18:43:14 +02:00
|
|
|
print("Successfully downloaded PDF, patching encryption ...")
|
2021-10-03 10:30:45 +02:00
|
|
|
|
|
|
|
adobe_fulfill_response = etree.fromstring(rights_xml_str)
|
|
|
|
NSMAP = { "adept" : "http://ns.adobe.com/adept" }
|
|
|
|
adNS = lambda tag: '{%s}%s' % ('http://ns.adobe.com/adept', tag)
|
|
|
|
resource = adobe_fulfill_response.find("./%s/%s" % (adNS("licenseToken"), adNS("resource"))).text
|
2021-09-28 18:43:14 +02:00
|
|
|
|
|
|
|
os.rename(filename, "tmp_" + filename)
|
2021-10-03 10:30:45 +02:00
|
|
|
ret = patch_drm_into_pdf("tmp_" + filename, rights_xml_str, filename, resource)
|
2021-09-28 18:43:14 +02:00
|
|
|
os.remove("tmp_" + filename)
|
2021-10-03 10:30:45 +02:00
|
|
|
if (ret):
|
|
|
|
print("File successfully fulfilled to " + filename)
|
2022-04-28 16:50:55 +02:00
|
|
|
return True
|
2021-10-03 10:30:45 +02:00
|
|
|
else:
|
|
|
|
print("Errors occurred while patching " + filename)
|
2022-04-28 16:50:55 +02:00
|
|
|
return False
|
|
|
|
|
2021-09-25 16:24:03 +02:00
|
|
|
else:
|
|
|
|
print("Error: Weird filetype")
|
2022-04-28 16:50:55 +02:00
|
|
|
return False
|
2021-09-25 16:24:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2022-04-28 16:50:55 +02:00
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
files = [ "URLLink.acsm" ]
|
2021-09-25 16:24:03 +02:00
|
|
|
else:
|
2022-04-28 16:50:55 +02:00
|
|
|
files = sys.argv[1:]
|
|
|
|
|
|
|
|
for file in files:
|
|
|
|
|
|
|
|
print("Fulfilling book '" + file + "' ...")
|
|
|
|
success, replyData = fulfill(file)
|
|
|
|
if (success is False):
|
|
|
|
print("Hey, that didn't work!")
|
|
|
|
print(replyData)
|
|
|
|
else:
|
|
|
|
print("Downloading book '" + file + "' ...")
|
|
|
|
success = download(replyData)
|
|
|
|
if (success is False):
|
|
|
|
print("That didn't work!")
|
|
|
|
|
2021-09-25 16:24:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|