Created
December 12, 2017 18:30
-
-
Save mhilbrunner/7e30f057794b68cc5e3e23dc4be0b9c7 to your computer and use it in GitHub Desktop.
Godot 3 HTTPClient video download test; execute with godot -s http_test.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends SceneTree | |
func _init(): | |
var bigVideo = true | |
var URL1 = "http://techslides.com" | |
var URL2 = "/demos/sample-videos/small.ogv" | |
var httpPort = 80 | |
var httpSSL = false | |
if bigVideo: | |
URL1 = "https://upload.wikimedia.org" | |
URL2 = "/wikipedia/commons/a/a4/Xacti-AC8EX-Sample_video-001.ogv" | |
httpPort = 443 | |
httpSSL = true | |
var err=0 | |
var http = HTTPClient.new() # Create the Client | |
err = http.connect_to_host(URL1, httpPort, httpSSL, false) # Connect to host/port | |
assert(err==OK) # Make sure connection was OK | |
# Wait until resolved and connected | |
while( http.get_status()==HTTPClient.STATUS_CONNECTING or http.get_status()==HTTPClient.STATUS_RESOLVING): | |
http.poll() | |
print("Connecting..") | |
OS.delay_msec(500) | |
print("STATUS: " + str(http.get_status())) | |
if http.get_status() == HTTPClient.STATUS_CANT_CONNECT: | |
print("CANT CONNECT") | |
assert( http.get_status() == HTTPClient.STATUS_CONNECTED ) # Could not connect | |
# Some headers | |
var headers=[ | |
"User-Agent: Pirulo/1.0 (Godot)", | |
"Accept: */*" | |
] | |
err = http.request(HTTPClient.METHOD_GET,URL2,headers) # Request a page from the site (this one was chunked..) | |
assert( err == OK ) # Make sure all is OK | |
while (http.get_status() == HTTPClient.STATUS_REQUESTING): | |
# Keep polling until the request is going on | |
http.poll() | |
print("Requesting..") | |
OS.delay_msec(500) | |
assert( http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED ) # Make sure request finished well. | |
print("response? ",http.has_response()) # Site might not have a response. | |
if (http.has_response()): | |
# If there is a response.. | |
headers = http.get_response_headers_as_dictionary() # Get response headers | |
print("code: ",http.get_response_code()) # Show response code | |
print("**headers:\n",headers) # Show headers | |
# Getting the HTTP Body | |
if (http.is_response_chunked()): | |
# Does it use chunks? | |
print("Response is Chunked!") | |
else: | |
# Or just plain Content-Length | |
var bl = http.get_response_body_length() | |
print("Response Length: ",bl) | |
# This method works for both anyway | |
var rb = PoolByteArray() # Array that will hold the data | |
while(http.get_status()==HTTPClient.STATUS_BODY): | |
# While there is body left to be read | |
http.poll() | |
var chunk = http.read_response_body_chunk() # Get a chunk | |
if (chunk.size()==0): | |
# Got nothing, wait for buffers to fill a bit | |
OS.delay_usec(1000) | |
else: | |
rb = rb + chunk # Append to read buffer | |
# Done! | |
print("bytes got: ",rb.size()) | |
var text = rb.get_string_from_ascii() | |
print("Text: ",text) | |
print("Saving") | |
var file = File.new() | |
file.open("./video.bin", file.WRITE) | |
file.store_buffer(rb) | |
file.close() | |
print("Saved") | |
quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment