-- PolyMac : "Archivage de page PressReader"

-- 06.02.2023



-- 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



set PubScaleList to

"1003", "iNYT", "53", "81", "181", "259", ¬

"2504", "Liberation", "63", "88", "129", "185", ¬

"2900", "LeFigaroMag", "117", "161", "229", "346", ¬

"2923", "LePoint", "86", "119", "164", "235", ¬

"2923", "LePoint", "84", "116", "166", "237", ¬

"2989", "L'Obs", "90", "129", "242", "346", ¬

"2989", "L'Obs", "81", "129", "243", "347", ¬

"6532", "PME", "81", "125", "243", "347", ¬

"6533", "L'Illustre", "87", "125", "243", "347", ¬

"6543", "TV8", "87", "125", "243", "347", ¬

"8302", "NYT", "54", "84", "178", "254", ¬

"9786", "LeTempsImmo", "71", "110", "217", "310", ¬

"9786", "LeTempsEco", "53", "75", "151", "216", ¬

"9a18", "TrialMag", "77", "166", "239", "342", ¬

"9af1", "Car", "77", "167", "241", "344", ¬

"9ag3", "ClassicCarsUK", "95", "143", "240", "343", ¬

"9f47", "Macworld", "98", "147", "261", "308", ¬

"9g84", "Forbes", "99", "158", "250", "357", ¬

"9gcf", "Evo", "80", "111", "229", "327", ¬

"9gcl", "Octane", "80", "123", "229", "327", ¬

"9gvk", "ClassicSportsCar", "77", "101", "232", "331", ¬

"9j55", "Voiles", "71", "106", "224", "321", ¬

"9kvc", "SeptMook", "84", "130", "264", "376", ¬

"9lg7", "BoatHeroes", "79", "123", "242", "346", ¬

"9vgp", "MacworldUS", "98", "148", "263", "310", ¬

"9wsp", "RollingStones", "82", "118", "206", "295", ¬

"9yk5", "EchappeeBelle", "89", "142", "239", "342", ¬

"9yzu", "VWklassik", "82", "126", "242", "346", ¬

"9yzu", "VWklassik", "79", "121", "246", "351", ¬

"efct", "L'AutoAncienne", "83", "129", "242", "346", ¬

"effh", "Crown", "75", "107", "242", "346", ¬

"f040", "Inrockuptibles", "91", "146", "225", "322", ¬

"f045", "GdsReportages", "89", "127", "242", "346", ¬

"f056", "Trek", "82", "127", "241", "345", ¬

"f057", "Vertical", "85", "131", "243", "347", ¬

"f142", "L'Aviation", "83", "118", "239", "342", ¬

"f149", "MotoRevue", "83", "118", "239", "342", ¬

"f150", "MotoRevueClassic", "83", "118", "239", "342", ¬

"f151", "MotoVerte", "83", "118", "239", "342", ¬

"f161", "AutoFlash", "77", "118", "239", "342", ¬

"f165", "LeTemps", "90", "127", "183", "261", ¬

"f331", "L'Express", "85", "132", "251", "359", ¬

"f331", "L'Express", "91", "141", "267", "382", ¬

"f640", "Diplomatie", "82", "117", "220", "315", ¬

"f674", "VanLifeMag", "95", "142", "239", "342", ¬

"f693", "MotoHeroes", "79", "123", "241", "345", ¬

"f694", "AutoHeroesHS", "80", "123", "241", "344", ¬

"f696", "AutoHeroes", "79", "123", "241", "344", ¬

"f697", "Speedster", "73", "104", "241", "344", ¬

"f698", "MontreHeroes", "73", "104", "242", "346", ¬

"f705", "Gazoline", "77", "129", "239", "342"}


-- list size = max 32 lines !


set token to ¬

"d1CFc--kgMs9nsidhKAMlqvZ-5jxSPEogfnk1KoczxkmZ5wYMf-PCyZoGAvPHcj1Q1zl_B94-fP1gKJ_rBNFUhR_SpPr5ZpjcwIQIEkiWdQ!"


set scaleList to {}



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



-- grant access by swapping with a valid token

set tokenTail to offset of "issue=" in pageKeysURL

set tokenHead to tokenTail - 1


set pageKeysHead to characters 1 thru tokenHead of pageKeysURL as string

set pageKeysTail to characters tokenTail thru end of pageKeysURL as string

set pageKeysURL to pageKeysHead & "accessToken=" & token & "&" & pageKeysTail



-- 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



-- scales Lookup

repeat with titleIdx from 1 to length of PubScaleList by 6

set theCurrentListItem to item titleIdx of PubScaleList

if theCurrentListItem is equal to issueTitle then

set titleName to item (titleIdx + 1) of PubScaleList

repeat with scaleIdx from 2 to 5

set end of scaleList to item (titleIdx + scaleIdx) of PubScaleList

end repeat

exit repeat -- Title found

end if

end repeat



if scaleList is equal to {} then

display dialog "Unknown Publication" with icon stop buttons {"Cancel"} default button "Cancel"

return

else

set scale to choose from list scaleList with prompt "Select scale of «" & titleName & "» ?" default items {item 3 of scaleList}

if scale is false then return -- Cancel Button

end if



tell application "BBEdit"

activate

set shell to "curl -L -H \"Accept: application/xml\" " & "\"" & pageKeysURL & "\""

set keysChain to do shell script shell

make new text window with properties {contents:keysChain, source language:"HTML", soft wrap text:false}

format mode source_format_profile

tell front document

-- 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 titleName & "-" & issueDate & "-s" & scale & ".html" with prompt "Save the document as:"

save document to theNewFilePath

end tell

end tell