-- PolyMac : "Archivage de page PressReader"
-- 2025 Edition
-- Documents
-- macosxautomation.com/applescript/sbrt/sbrt-07.html
-- satimage.fr/software/en/smile/computing/as_types/list_create.html
-- daringfireball.net/2003/01/safari_source_in_bbedit
-- preserve.mactech.com/articles/mactech/Vol.24/24.08/ASBBEdit/index.html
-- developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide
-- XHR : GetPageKeys
-- https://ingress.pressreader.com/services/IssueInfo/GetPageKeys?accessToken=d1CFc--kgMs9nsidhKAMlqvZ-5jxSPEogfnk1KoczxkmZ5wYMf-PCyZoGAvPHcj1Q1zl_B94-fP1gKJ_rBNFUhR_SpPr5ZpjcwIQIEkiWdQ!&issue=f1652023011400000000001001&pageNumber=0&preview=true
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- most great common scale = LeTemps.ch Supplement
set Scale to "229"
set pageKeysURL to display dialog "GetPageKeys URL ?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
set pageKeysURL to text returned of pageKeysURL
if pageKeysURL does not contain "ingress.pressreader.com/services/IssueInfo/GetPageKeys?" or pageKeysURL does not contain "&pageNumber=0&preview=true" then
display dialog "Invalid URL" with icon stop buttons {"Cancel"} default button "Cancel"
return
end if
-- references extraction
set issueHead to (offset of "issue=" in pageKeysURL) + 6
set issueTail to (offset of "&pageNumber=" in pageKeysURL) - 1
set issueRef to text issueHead thru issueTail of pageKeysURL
set issueTitle to text 1 thru -23 of issueRef
set issueDate to text 5 thru -15 of issueRef
tell application "BBEdit"
activate
-- The front-end document should contain a preview of the Ingress "GetPageKeys" XHR request
tell front document
remove line breaks
replace " " using "" options {starting at top:true}
-- remove header & footer (but preserve lines)
replace "{\"PageKeys\":[" using "}," options {starting at top:true}
replace "}]}" using "\\n" options {starting at top:true}
-- text wrap
replace "},{\"ConfirmationRequired\":true," using "\\n" options {starting at top:true}
-- display dialog "Columns permutation"
replace "(\"Key\":\".{23}=\"),(\"PageNumber\":\\d+)" using "\\2,\\1" options {search mode:grep, starting at top:true}
-- display dialog "URLs construction"
replace "\"PageNumber\":" using "https://i.prcdn.co/img?file=" & issueRef & "&page=" options {starting at top:true}
replace ",\"Key\":\"" using "&scale=" & Scale & "&ticket=" options {starting at top:true}
replace "=\"\\n" using "=\\n" options {starting at top:true}
-- display dialog "HTML file generation"
add prefix prefix "<img src=\""
add suffix suffix "\" style=\"width:auto;\"></br>"
set first line to "<!DOCTYPE html><html><body>"
set last line to "</body></html>"
set theNewFilePath to choose file name default name issueTitle & "-" & issueDate & ".html" with prompt "Save the document as:"
save document to theNewFilePath
end tell
end tell