fetch.bac 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. '
  2. ' Small utility to get all BaCon programs over HTTP from the BaCon website
  3. '
  4. ' Dec 2010 - PvE.
  5. ' Mar 2011: added size check - PvE
  6. ' Jul 2013: various improvements, network handling more robust - PvE
  7. '-----------------------------------------------------------------------------------
  8. CONST Sep$ = CHR$(13) & NL$ & CHR$(13) & NL$
  9. ' Define website
  10. website$ = "www.basic-converter.org"
  11. ' Get the list with files
  12. OPEN website$ & ":80" FOR NETWORK AS mynet
  13. SEND "GET /files.txt HTTP/1.1\r\nHost: " & website$ & Sep$ TO mynet
  14. REPEAT
  15. RECEIVE dat$ FROM mynet
  16. IF LEN(dat$) > 0 THEN total$ = total$ & dat$
  17. UNTIL ISFALSE(WAIT(mynet, 500))
  18. CLOSE NETWORK mynet
  19. ' Split the text into separate filenames
  20. FOR file$ IN MID$(total$, INSTR(total$, Sep$)+4) STEP NL$
  21. PRINT "Fetching ", file$, "... ";
  22. attempt = 0
  23. REPEAT
  24. ' We create a new connection for each individual file
  25. OPEN website$ & ":80" FOR NETWORK AS mynet
  26. ' Get the current file
  27. SEND "GET /" & file$ & " HTTP/1.1\r\nHost: " & website$ & Sep$ TO mynet
  28. total$ = ""
  29. ' Prevent endless waiting for server
  30. IF NOT(WAIT(mynet, 250)) THEN
  31. COLOR FG TO CYAN
  32. PRINT "Timeout on server! Retrying... ";
  33. SLEEP 500
  34. COLOR RESET
  35. CLOSE NETWORK mynet
  36. INCR attempt
  37. IF attempt = 4 THEN
  38. COLOR FG TO RED
  39. PRINT "Could not download ", file$, " - skipping."
  40. SLEEP 500
  41. COLOR RESET
  42. BREAK
  43. END IF
  44. CONTINUE
  45. ELSE
  46. ' So there is data
  47. RECEIVE dat$ FROM mynet
  48. ' Get the filesize from the HTTP header
  49. dat$ = MID$(dat$, INSTR(dat$, "Content-Length:")+15)
  50. length = VAL(LEFT$(dat$, INSTR(dat$, NL$)))
  51. ' As long as there is data, get it
  52. WHILE WAIT(mynet, 500) AND LEN(dat$) > 0
  53. total$ = total$ & dat$
  54. RECEIVE dat$ FROM mynet
  55. WEND
  56. total$ = total$ & dat$
  57. ' Write to file
  58. OPEN file$ FOR WRITING AS baconfile
  59. WRITELN MID$(total$, INSTR(total$, Sep$)+4) TO baconfile
  60. CLOSE FILE baconfile
  61. ' Closing the connection again
  62. CLOSE NETWORK mynet
  63. END IF
  64. ' If the size of the file is not correct
  65. IF FILELEN(file$) <> length+1 THEN
  66. ' Keep track of attempts
  67. INCR attempt
  68. SELECT attempt
  69. CASE 2;
  70. CASE 3
  71. COLOR FG TO CYAN
  72. PRINT "Failed! Retrying... ";
  73. SLEEP 500
  74. COLOR RESET
  75. CASE 4
  76. COLOR FG TO RED
  77. PRINT "Could not download ", file$, " - skipping."
  78. SLEEP 500
  79. COLOR RESET
  80. IF FILEEXISTS(file$) THEN DELETE FILE file$
  81. BREAK
  82. END SELECT
  83. END IF
  84. ' Make sure filelength equals size mentioned in HTTP header +1 for last newline
  85. UNTIL LEN(total$) > 0 AND FILELEN(file$) = length+1
  86. IF attempt < 4 THEN
  87. COLOR FG TO GREEN
  88. PRINT "done."
  89. COLOR RESET
  90. END IF
  91. ' Give the remote server some time
  92. SLEEP 250
  93. NEXT