fetch_rumsey.cmd 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. @if (@CodeSection == @Batch) @then
  2. @echo off & setlocal
  3. set "DSTDIR=%~1"
  4. if not defined DSTDIR set "DSTDIR=%~dp0"
  5. if not exist "%DSTDIR%" mkdir "%DSTDIR%"
  6. set "NAME=%~n1"
  7. set "PAGES=%~2"
  8. if not defined PAGES set "PAGES=1,1,1"
  9. rem SETLOCAL ENABLEDELAYEDEXPANSION
  10. set PROCS="%~dp0procs.cmd"
  11. set USERAGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
  12. for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
  13. rem set "url=https://gallica.bnf.fr/iiif/ark:/12148/btv1b8447838j"
  14. rem set "url=https://gallica.bnf.fr/iiif/ark:/12148/btv1b23006724"
  15. set "url=https://media.davidrumsey.com/MediaManager/srvr?mediafile=/JP2K/RUMSEY~8~1/179/10130087.jp2"
  16. set "testfnw=%Temp%/test_tile_w.jpg"
  17. set "testfnh=%Temp%/test_tile_h.jpg"
  18. @echo.Fetching %NAME% files from: %url%, pages: %PAGES%
  19. setlocal ENABLEDELAYEDEXPANSION
  20. for /L %%G in (%PAGES%) do (
  21. if not exist "%DSTDIR%\%%G" mkdir "%DSTDIR%\%%G"
  22. @echo."%DSTDIR%\%%G"
  23. set "purl=%url%"
  24. rem set /a WT=5888
  25. rem set /a HT=8448
  26. set /a WT=62000
  27. set /a HT=62000
  28. set /a TD=750
  29. set /a TDI=!TD!
  30. set /a TDJ=!TD!
  31. @echo.Determine width
  32. curl -fsS -A !USERAGENT! -o !testfnw! "!purl!&x=!WT!&y=0&width=!TD!&height=!TD!&level=0" || (
  33. set /a WT = !WT! - !TD!
  34. curl -fsS -A !USERAGENT! -o !testfnw! "!purl!&x=!WT!&y=0&width=!TD!&height=!TD!&level=0" || (
  35. set /a WT = !WT! - !TD!
  36. curl -fsS -A !USERAGENT! -o !testfnw! "!purl!&x=!WT!&y=0&width=!TD!&height=!TD!&level=0" || (
  37. set /a WT = !WT! - !TD!
  38. curl -fsS -A !USERAGENT! -o !testfnw! "!purl!&x=!WT!&y=0&width=!TD!&height=!TD!&level=0" || @echo.Process failed. Error:!ERRORLEVEL!
  39. )
  40. )
  41. )
  42. @echo.Requested last tile at pos !WT!,0 of !TD!x!TD!
  43. for /f "tokens=3,4 delims=x+ " %%D in ( 'gm identify !testfnw!' ) do (
  44. set /a dimx=%%D
  45. set /a dimy=%%E
  46. @echo.Received tile of size !dimx!x!dimy!
  47. if !dimy! GTR !TD! (
  48. set /a "TDI=!TDJ!*1000/!dimy!*!dimx!/1000"
  49. ) else (
  50. set /a "TDI=!dimx!"
  51. )
  52. set /a "W=!WT!+!TDI!"
  53. set /a "WTC=!W!/!TD!+1"
  54. @echo.Result image width !W! ^(!WTC! tiles^)
  55. )
  56. @echo.Determine height
  57. curl -fsS -A !USERAGENT! -o !testfnh! "!purl!&x=0&y=!HT!&width=!TD!&height=!TD!&level=0" || (
  58. set /a HT = !HT! - !TD!
  59. curl -fsS -A !USERAGENT! -o !testfnh! "!purl!&x=0&y=!HT!&width=!TD!&height=!TD!&level=0" || (
  60. set /a HT = !HT! - !TD!
  61. curl -fsS -A !USERAGENT! -o !testfnh! "!purl&x=0&y=!HT!&width=!TD!&height=!TD!&level=0" || (
  62. set /a HT = !HT! - !TD!
  63. curl -fsS -A !USERAGENT! -o !testfnh! "!purl!&x=0&y=!HT!&width=!TD!&height=!TD!&level=0" || @echo.Process failed. Error:!ERRORLEVEL!
  64. )
  65. )
  66. )
  67. @echo.Requested last tile at pos 0,!HT! of !TD!x!TD!
  68. for /f "tokens=3,4 delims=x+ " %%D in ( 'gm identify !testfnh!' ) do (
  69. set /a dimx=%%D
  70. set /a dimy=%%E
  71. @echo.Received tile of size !dimx!x!dimy!
  72. if !dimy! GTR !TD! (
  73. set /a "TDJ=!dimy!*1000/!dimx!*!TDI!"/1000"
  74. ) else (
  75. set /a "TDJ=!dimy!"
  76. )
  77. set /a "H=!HT!+!TDJ!"
  78. set /a "HTC=!H!/!TD!+1"
  79. @echo.Result image dimensions !W!x!H! ^(!WTC!x!HTC! tiles^)
  80. )
  81. set /a TC=!HTC!*!WTC!
  82. set /a COUNTER=0
  83. for /f "tokens=1,2,3 delims= " %%I in ('cscript /nologo /e:JScript "%~f0" "!purl!" "!W!" "!H!" "!TD!"') do (
  84. set /a COUNTER+=1
  85. set /a PTS=100*!COUNTER!/!TC!
  86. echo.
  87. @echo !COUNTER!.%%I %%J!CR!
  88. if not exist "%DSTDIR%/%%G/%%I" (
  89. curl -fsS -A %USERAGENT% -o "%DSTDIR%/%%G/%%I" "%%J" || @echo.Process failed. Error:!ERRORLEVEL!
  90. )
  91. @echo.!PTS!^%% done.!CR!
  92. )
  93. for /f %%a in ( '2^>nul dir "%DSTDIR%/%%G" /a-d/b/-o/-p/s^|find /v /c ""') do set /a n=%%a
  94. @echo.Assembling !n! files. Please wait...
  95. gm montage -mode concatenate -tile !WTC!x!HTC! "%DSTDIR%/%%G/*.jp2" "%DSTDIR%/%NAME% - %%G.jpg" || @echo.Process failed. Error:!ERRORLEVEL!
  96. )
  97. goto :EOF
  98. @end // end batch / begin JScript hybrid code
  99. var w=Number(WSH.Arguments(1));
  100. var h=Number(WSH.Arguments(2));
  101. var td=Number(WSH.Arguments(3));
  102. var wtc=Math.ceil(w/td);
  103. var htc=Math.ceil(h/td);
  104. var tdi=td;
  105. var tdj=td;
  106. for (i = 0; i < wtc; i++) {
  107. for (j = 0; j < htc; j++) {
  108. if ( i == wtc-1 & w % td > 0 ) { tdi = w % td; } else { tdi = td };
  109. if ( j == htc-1 & h % td > 0 ) { tdj = h % td; } else { tdj = td };
  110. WSH.Echo(('00000' + j*td).slice(-6)+"_"+('00000' + i*td).slice(-6)+".jp2 "+WSH.Arguments(0)+"&x="+Number(i*td)+"&y="+Number(j*td)+"&width="+tdi+"&height="+tdj+"&level=0");
  111. }
  112. }