fetch_gallica.cmd 4.3 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="Googlebot/2.1 (+http://www.google.com/bot.html)"
  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. set "url=https://gallica.bnf.fr/iiif/ark:/12148/btv1b23006724"
  15. set "testfnw=%Temp%/test_tile_w.jpg"
  16. set "testfnh=%Temp%/test_tile_h.jpg"
  17. @echo.Fetching %NAME% files from: %url%, pages: %PAGES%
  18. setlocal ENABLEDELAYEDEXPANSION
  19. for /L %%G in (%PAGES%) do (
  20. if not exist "%DSTDIR%\%%G" mkdir "%DSTDIR%\%%G"
  21. @echo."%DSTDIR%\%%G"
  22. set "purl=%url%/f%%G"
  23. rem set /a WT=5888
  24. rem set /a HT=8448
  25. set /a WT=1280
  26. set /a HT=1280
  27. set /a TD=256
  28. set /a TDI = !TD!
  29. set /a TDJ = !TD!
  30. @echo.Determine width
  31. curl -fsS -A !USERAGENT! -o !testfnw! "!purl!/!WT!,0,!TD!,!TD!/!TD!,/0/native.jpg" || (
  32. set /a WT = !WT! - !TD!
  33. curl -fsS -A !USERAGENT! -o !testfnw! "!purl!/!WT!,0,!TD!,!TD!/!TD!,/0/native.jpg" || (
  34. set /a WT = !WT! - !TD!
  35. curl -fsS -A !USERAGENT! -o !testfnw! "!purl!/!WT!,0,!TD!,!TD!/!TD!,/0/native.jpg" || (
  36. set /a WT = !WT! - !TD!
  37. curl -fsS -A !USERAGENT! -o !testfnw! "!purl!/!WT!,0,!TD!,!TD!/!TD!,/0/native.jpg" || @echo.Process failed. Error:!ERRORLEVEL!
  38. )
  39. )
  40. )
  41. @echo.Requested last tile at pos !WT!,0 of !TD!x!TD!
  42. for /f "tokens=3,4 delims=x+ " %%D in ( 'gm identify !testfnw!' ) do (
  43. set /a dimx=%%D
  44. set /a dimy=%%E
  45. @echo.Received tile of size !dimx!x!dimy!
  46. if !dimy! GTR !TD! (
  47. set /a "TDI=!TDJ!*1000/!dimy!*!dimx!/1000"
  48. ) else (
  49. set /a "TDI=!dimx!"
  50. )
  51. set /a "W=!WT!+!TDI!"
  52. set /a "WTC=!W!/!TD!"
  53. if !TDI! LSS !TD! ( set /a "WTC=!WTC!+1" )
  54. @echo.Result image width !W! ^(!WTC! tiles^)
  55. )
  56. @echo.Determine height
  57. curl -fsS -A !USERAGENT! -o !testfnh! "!purl!/0,!HT!,!TD!,!TD!/!TD!,/0/native.jpg" || (
  58. set /a HT = !HT! - !TD!
  59. curl -fsS -A !USERAGENT! -o !testfnh! "!purl!/0,!HT!,!TD!,!TD!/!TD!,/0/native.jpg" || (
  60. set /a HT = !HT! - !TD!
  61. curl -fsS -A !USERAGENT! -o !testfnh! "!purl!/0,!HT!,!TD!,!TD!/!TD!,/0/native.jpg" || (
  62. set /a HT = !HT! - !TD!
  63. curl -fsS -A !USERAGENT! -o !testfnh! "!purl!/0,!HT!,!TD!,!TD!/!TD!,/0/native.jpg" || @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!"
  79. if !TDJ! LSS !TD! ( set /a "HTC=!HTC!+1" )
  80. @echo.Result image dimensions !W!x!H! ^(!WTC!x!HTC! tiles^)
  81. )
  82. set /a TC=!HTC!*!WTC!
  83. set /a COUNTER=0
  84. for /f "tokens=1,2,3 delims= " %%I in ('cscript /nologo /e:JScript "%~f0" "!purl!" "!W!" "!H!"') do (
  85. set /a COUNTER+=1
  86. set /a PTS=100*!COUNTER!/!TC!
  87. echo.
  88. @echo !COUNTER!.%%I %%J!CR!
  89. if not exist "%DSTDIR%/%%G/%%I" (
  90. curl -fsS -A %USERAGENT% -o "%DSTDIR%/%%G/%%I" "%%J" || @echo.Process failed. Error:!ERRORLEVEL!
  91. )
  92. @echo.!PTS!^%% done.!CR!
  93. )
  94. for /f %%a in ( '2^>nul dir "%DSTDIR%/%%G" /a-d/b/-o/-p/s^|find /v /c ""') do set /a n=%%a
  95. @echo.Assembling !n! files. Please wait...
  96. gm montage -mode concatenate -tile !WTC!x!HTC! "%DSTDIR%/%%G/*.jpg" "%DSTDIR%/%NAME% - %%G.png" || @echo.Process failed. Error:!ERRORLEVEL!
  97. )
  98. rem pause
  99. goto :EOF
  100. @end // end batch / begin JScript hybrid code
  101. var w=WSH.Arguments(1);
  102. var h=WSH.Arguments(2);
  103. var td=256;
  104. var wtc=Math.ceil(w/td);
  105. var htc=Math.ceil(h/td);
  106. var tdi=td;
  107. var tdj=td;
  108. for (i = 0; i < wtc; i++) {
  109. for (j = 0; j < htc; j++) {
  110. if ( i == wtc-1 & w % td > 0 ) { tdi = w % td; } else { tdi = td };
  111. if ( j == htc-1 & h % td > 0 ) { tdj = h % td; } else { tdj = td };
  112. WSH.Echo(('000' + j*td).slice(-4)+"_"+('000' + i*td).slice(-4)+".jpg "+WSH.Arguments(0)+"/"+i*td+","+j*td+","+tdi+","+tdj+"/"+tdi+",/0/native.jpg");
  113. }
  114. }