October 17, 2021, 10:35:22

Author Topic: [bb] Download file by bytecode77 [ 1+ years ago ]  (Read 1696 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Download file by bytecode77 [ 1+ years ago ]
« on: June 29, 2017, 00:28:43 »
Title : Download file
Author : bytecode77
Posted : 1+ years ago

Description : Unlike BlitzGet Deluxe this download routine is faster, because it downloads 4 KB blocks instead of byte-per-byte.
It also supports automatic redirects as well as chunked transfer encoding, which is required to download php/css files.
0 byte sized files will not be threaded like they don't exist.


Code :
Code: BlitzBasic
  1. Graphics 640, 480, 32, 2
  2. SetBuffer BackBuffer()
  3.  
  4. ;Download a normal file (easy)
  5. Print Download("http://www.google.com/intl/en_us/images/logo.gif")
  6.  
  7. ;Download a php file, which is chunked!
  8. Print Download("http://www.blitzbasic.com/index.php", "", "blitzbasic.com.index.html")
  9.  
  10. ;A download which will be redirected, which is also chunked!
  11. Print Download("http://example.com/index.php", "", "index.html")
  12.  
  13. Print ""
  14. Print "Done."
  15. Print "What the download function returned is printed on the screen."
  16. Print "0 = download failed"
  17. Print "1 = download successful"
  18. WaitKey()
  19. End
  20.  
  21. ;=============================================================================================
  22.  
  23.  
  24. ;link$      - The link. You may enter the link just like you enter it
  25. ;             in your browser. Very tolerant. No http:// required.
  26. ;savepath$  - The path where the file should be saved
  27. ;savefile$  - The filename of the saved file. When given "", it will
  28. ;             be named like the file in the link$.
  29.  
  30. Function Download(link$, savepath$ = "", savefile$ = "")
  31. ;Strip protocol and return false if not "http"
  32. inst = Instr(link$, "://")
  33. If inst Then
  34.         If Lower(Trim(Left(link$, inst - 1))) <> "http" Then Return False
  35.         link$ = Right(link$, Len(link$) - inst - 2)
  36. EndIf
  37.  
  38. ;Seperate host from link
  39. inst = Instr(link$, "/")
  40. If inst = 0 Then Return False
  41. host$ = Trim(Left(link$, inst - 1))
  42. link$ = Trim(Right(link$, Len(link$) - inst + 1))
  43.  
  44. ;Seperate path and file from the link
  45. For i = Len(link$) To 1 Step -1
  46.         If Mid(link$, i, 1) = "/" Then
  47.                 link_path$ = Trim(Left(link$, i))
  48.                 link_file$ = Right(link$, Len(link$) - i)
  49.                 Exit
  50.         EndIf
  51. Next
  52. If link_file$ = "" Then Return False
  53. If savefile$ = "" Then savefile$ = link_file$
  54.  
  55. ;Open TCP stream
  56. tcp = OpenTCPStream(host$, 80)
  57. If tcp = 0 Then Return False
  58. WriteLine tcp, "GET " + link_path$ + link_file$ + " HTTP/1.1" + Chr(13) + Chr(10) + "Host: " + host$ + Chr(13) + Chr(10) + "User-Agent: Download_Function_By_bytecode77" + Chr(13) + Chr(10)
  59.  
  60. ;Download file
  61. l$ = ReadLine(tcp)
  62. inst1 = Instr(l$, " ")
  63. inst2 = Instr(l$, " ", inst1 + 1)
  64. num = Mid(l$, inst1, inst2 - inst1)
  65. Select num
  66.         Case 200
  67.                 conlen = -1
  68.                 chunk = False
  69.                
  70.                 Repeat
  71.                         l$ = Trim(ReadLine(tcp))
  72.                         If l$ = "" Then Exit
  73.                        
  74.                         inst = Instr(l$, ":")
  75.                         l1$ = Trim(Left(l$, inst - 1))
  76.                         l2$ = Trim(Right(l$, Len(l$) - inst))
  77.                         Select Lower(l1$)
  78.                                 Case "content-length"
  79.                                         conlen = l2$
  80.                                 Case "transfer-encoding"
  81.                                         If Lower(l2$) = "chunked" Then chunk = True
  82.                         End Select
  83.                 Forever
  84.                
  85.                 If conlen = 0 Then
  86.                         file = WriteFile(savepath$ + savefile$)
  87.                         CloseFile file
  88.                         CloseTCPStream tcp
  89.                         Return True
  90.                 ElseIf conlen > 0 Then
  91.                         file = WriteFile(savepath$ + savefile$)
  92.                         bnk = CreateBank(4096)
  93.                         pos = 0
  94.                         Repeat
  95.                                 avail = conlen - pos
  96.                                 If avail > 4096 Then
  97.                                         ReadBytes bnk, tcp, 0, 4096
  98.                                         WriteBytes bnk, file, 0, 4096
  99.                                         pos = pos + 4096
  100.                                 Else
  101.                                         ReadBytes bnk, tcp, 0, avail
  102.                                         WriteBytes bnk, file, 0, avail
  103.                                         Exit
  104.                                 EndIf
  105.                         Forever
  106.                         FreeBank bnk
  107.                         CloseFile file
  108.                         CloseTCPStream tcp
  109.                         Return True
  110.                 ElseIf chunk Then
  111.                         file = WriteFile(savepath$ + savefile$)
  112.                         bnk = CreateBank(4096)
  113.                        
  114.                         Repeat
  115.                                 l$ = Trim(Upper(ReadLine(tcp)))
  116.                                 ln = 0
  117.                                 For i = 1 To Len(l$)
  118.                                         ln = 16 * ln + Instr("123456789ABCDEF", Mid$(l$, i, 1))
  119.                                 Next
  120.                                 If ln = 0 Then Exit
  121.                                
  122.                                 If BankSize(bnk) < ln Then ResizeBank bnk, ln
  123.                                 ReadBytes bnk, tcp, 0, ln
  124.                                 WriteBytes bnk, file, 0, ln
  125.                                 ReadShort(tcp)
  126.                         Forever
  127.                        
  128.                         FreeBank bnk
  129.                         CloseFile file
  130.                         CloseTCPStream tcp
  131.                         Return True
  132.                 Else
  133.                         CloseTCPStream tcp
  134.                         Return False
  135.                 EndIf
  136.         Case 301, 302
  137.                 Repeat
  138.                         l$ = Trim(ReadLine(tcp))
  139.                         If l$ = "" Then Exit
  140.                        
  141.                         inst = Instr(l$, ":")
  142.                         l1$ = Trim(Left(l$, inst - 1))
  143.                         l2$ = Trim(Right(l$, Len(l$) - inst))
  144.                         Select Lower(l1$)
  145.                                 Case "location"
  146.                                         CloseTCPStream tcp
  147.                                         Return Download(l2$, savepath$, savefile$)
  148.                         End Select
  149.                 Forever
  150.         Default
  151.                 CloseTCPStream tcp
  152.                 Return False
  153. End Select
  154. End Function


Comments :


Wings(Posted 1+ years ago)

 Hey this stuff is usefull..i added some extra thing just in case somthing hapends while downloading file..Function checkstream(stream)   If Eof(stream)<=-1         RuntimeError "Cowabonga lost connection.. download failed"      End   End If   End Function


Chasson629(Posted 1+ years ago)

 Going from intermediate coder to expert.  Many directions. marginal gains possible with two of my projects, however at this stage they were on the back-burner for 1 year.  persistent error.  Even just sharing that i have a problem may help my attitude.  So, this looked like the best place to start.  Happy Easter!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal