Dilbert.com daily comic

Miscellaneous Forums/Blitz Showcase/Dilbert.com daily comic

I wanted to be able to read my two favourite comic strips every day with ease (translation: the only ones that are consitently funny, like comics are meant to be but aren't). So, I signed up for the free dilbert.com comic mailing service. Turns out I have to pay money to get it on Sundays, I am sent an image of the entire web page rather than just the comic, and thus I may as well just bookmark the web site or something. Consequently, I decided to have a shot at coding it myself; so I took a peak at the code archives, borrowed an image loading function, did a bit of confusing string stuff, and it worked first time with absolutely no errors at all!
Sadly, I couldn't get Calvin & Hobbes... But Dilbert is great, so I'm happy :D

AppTitle "Dilbert.com daily comic By Mr. Picklesworth"

Graphics 800,600,0,2
Const BGr=200,BGg=200,BGb=200
ClsColor BGr,BGg,BGb 
Color 0,0,0

; -----------------------------------------------------------------------------
; Load an image from the web, straight into our game!
; -----------------------------------------------------------------------------
Cls
Text 20, 20, "Downloading file - Please wait..."
Flip
comic = LoadWebImage (DilbertImagePath())

; -----------------------------------------------------------------------------
; Check for failure
; -----------------------------------------------------------------------------
If comic = 0
	RuntimeError "Failed to load web image!": End	
EndIf

MaskImage comic,BGr,BGg,BGb ;Am I forced to mask the image?!
Cls
DrawImage comic,10,10

WaitKey
End

Function DilbertImagePath$()
	If BlitzGet("http://www.dilbert.com/index.html",CurrentDir(),"temp_web_page.txt")
		page = OpenFile("temp_web_page.txt")
		Repeat
			inLine$=ReadLine(page)
			tag$ = "<IMG SRC="+Chr(34)
			check = Instr(inLine$,"<IMG SRC="+Chr(34)+"/comics/dilbert/archive/images/")
			If check
				PathStart=check+Len(tag$)
				PathEnd=Instr(inLine$,Chr(34),PathStart)
				Path$ = Mid(inLine$,PathStart,PathEnd-PathStart)
				CloseFile page
				DeleteFile "temp_web_page.txt"
				Return "http://www.dilbert.com"+Path$
			EndIf
		Until Eof(page)
		DeleteFile "temp_web_page.txt"
	EndIf	
End Function

; -----------------------------------------------------------------------------
; LoadWebImage -- uses BlitzGet Deluxe, based on Mark Sibly's HTTPGet
; -----------------------------------------------------------------------------
; james@...
; -----------------------------------------------------------------------------

Function LoadWebImage (webFile$)
	If BlitzGet (webFile$, CurrentDir (), "temp_web_image.bmp")
		Image = LoadImage ("temp_web_image.bmp")
		DeleteFile "temp_web_image.bmp"
	EndIf
	Return Image
End Function

Function BlitzGet (webFile$, saveDir$, saveFile$)

	; -------------------------------------------------------------------------
	; Strip "http://" if provided
	; -------------------------------------------------------------------------
	If Left (webFile$, 7) = "http://" Then webFile$ = Right (webFile$, Len (webFile$) - 7)

	; -------------------------------------------------------------------------
	; Split into hostname and path/filename to download
	; -------------------------------------------------------------------------
	slash = Instr (webFile$, "/")
	If slash
		webHost$ = Left (webFile$, slash - 1)
		webFile$ = Right (webFile$, Len (webFile$) - slash + 1)
	Else
		webHost$ = webFile$
		webFile$ = "/"
	EndIf
		
	; -------------------------------------------------------------------------
	; Add trailing slash to download dir if not given
	; -------------------------------------------------------------------------
	If Right (saveDir$, 1) <> "" Then saveDir$ = saveDir$ + ""

	; -------------------------------------------------------------------------
	; Save filename -- get from webFile$ if not provided
	; -------------------------------------------------------------------------
	If saveFile$ = ""
		If webFile = "/"
			saveFile$ = "Unknown file.txt"
		Else
			For findSlash = Len (webFile$) To 1 Step - 1
				testForSlash$ = Mid (webFile$, findSlash, 1)
				If testForSlash$ = "/"
					saveFile$ = Right (webFile$, Len (webFile$) - findSlash)
					Exit
				EndIf
			Next
			If saveFile$ = "" Then saveFile$ = "Unknown file.txt"
		EndIf
	EndIf

	; DEBUG
	; RuntimeError "Web host: " + webHost$ + Chr (10) + "Web file: " + webFile$ + Chr (10) + "Save dir: " + saveDir$ + Chr (10) + "Save file: " + saveFile$

	www = OpenTCPStream (webHost$, 80)

	If www
	
		WriteLine www, "GET " + webFile$ + " HTTP/1.1" ; GET / gets default page...
		WriteLine www, "Host: " + webHost$
		WriteLine www, "User-Agent: BlitzGet Deluxe"
		WriteLine www, "Accept: */*"
		WriteLine www, ""
		
		; ---------------------------------------------------------------------
		; Find blank line after header data, where the action begins...
		; ---------------------------------------------------------------------
				
		Repeat
			header$ = ReadLine (www)
			If Left (header$, 16) = "Content-Length: "	; Number of bytes to read
				bytesToRead = Right (header$, Len (header$) - 16)
			EndIf
		Until header$ = "" Or (Eof (www))
		
		If bytesToRead = 0 Then Goto skipDownLoad
		
		; ---------------------------------------------------------------------
		; Create new file to write downloaded bytes into
		; ---------------------------------------------------------------------
		save = WriteFile (saveDir$ + saveFile$)
		If Not save Then Goto skipDownload

		; ---------------------------------------------------------------------
		; Incredibly complex download-to-file routine...
		; ---------------------------------------------------------------------
				
		For readWebFile = 1 To bytesToRead
		
			If Not Eof (www) Then WriteByte save, ReadByte (www)
			
			; Call BytesReceived with position and size every 100 bytes (slows down a LOT with smaller updates)
			
			;tReadWebFile = readWebFile			
			;If tReadWebFile Mod 1000 = 0 Then BytesReceived (readWebFile, bytesToRead)

		Next

		CloseFile save
		
		; Fully downloaded?
		If (readWebFile - 1) = bytesToRead
			success = 1
		EndIf
		
		; Final update (so it's not rounded to nearest 100 bytes!)
		;BytesReceived (bytesToRead, bytesToRead)

		.skipDownload
		CloseTCPStream www
		
	Else
	
		RuntimeError "Failed to connect"
		
	EndIf
	
	Return success
	
End Function

; -----------------------------------------------------------------------------
; User-defined update function, called every 100 bytes of download -- alter to suit!
; -----------------------------------------------------------------------------
; TIP: Pass a user-defined type instead, with all data (this stuff plus URL, local filename, etc)
; -----------------------------------------------------------------------------
Function BytesReceived (posByte, totalBytes)
	; Example update code...
	Cls
	Text 20, 20, "Downloading file -- please wait..."
	Text 20, 40, "Received: " + posByte + "/" + totalBytes + " bytes (" + Percent (posByte, totalBytes) + "%)"
	Flip
End Function

; -----------------------------------------------------------------------------
; Handy percentage function
; -----------------------------------------------------------------------------
Function Percent (part#, total#)
	Return Int (100 * (part / total))
End Function


Two questions for you now:
Why is it so slow to use Blitz to load a web site? Can I speed it up without any effort on my part?
How do I tell an application to just freeze? Right now I have a very silly delay in there that repeats itself endlessly.

How do I tell an application to just freeze?


With the WaitKey() command, perhaps?

Ryan

change your repeat forever loop to:
While Not KeyDown(1)
Wend


*EDIT*
you might want to change the following:
			If tReadWebFile Mod 100 = 0 Then BytesReceived (readWebFile, bytesToRead)

to:
			If tReadWebFile Mod 1000 = 0 Then BytesReceived (readWebFile, bytesToRead)

it takes way too long to download otherwise (on broadband at least).

Ah, I didn't know the download speed problem was that simple :)
Thanks Perturbio!
Updated


My repeat forever loop is not closing on keydown 1 because people usually press the x at the top of the window :D
However, come to think of it, the program is simple enough to just get away with WaitKey.

Heh, I made something like this a while back, except for
my stocks! :)

So... Because I LOVVVVEEE comics, I have to use your thingie, and I also made one for daily Garfield, and soon, Get Fuzzy! :)

Actually, the code above only works on Sundays, because the images are Gifs every other day of the week.
Just make it open up the windows picture viewer (or just call execfile and hope for the best, like I did) with the path for the temporary image. Unless you feel like putting in a Gif loader :)
AppTitle "Dilbert.com daily comic By Dylan McCall"

Graphics 800,600,0,2
Const BGr=200,BGg=200,BGb=200
ClsColor BGr,BGg,BGb 
Color 0,0,0

; -----------------------------------------------------------------------------
; Load an image from the web, straight into our game!
; -----------------------------------------------------------------------------
Cls
Text 20, 20, "Downloading file - Please wait..."
Flip
comic = LoadWebImage (DilbertImagePath())

; -----------------------------------------------------------------------------
; Check for failure
; -----------------------------------------------------------------------------
If Not comic
	RuntimeError "Failed to load web image!" : End	
EndIf

MaskImage comic,BGr,BGg,BGb ;Am I forced to mask the image?!
Cls
DrawImage comic,10,10

WaitKey
End

Function DilbertImagePath$()
	If BlitzGet("http://www.dilbert.com/index.html",CurrentDir(),"temp_web_page.txt")
		page = OpenFile("temp_web_page.txt")
		Repeat
			inLine$=Lower(ReadLine(page))
			tag$ = "<img src="+Chr(34)
			check = Instr(inLine$,tag$+"/comics/dilbert/archive/images/")
			If check
				PathStart=check+Len(tag$)
				PathEnd=Instr(inLine$,Chr(34),PathStart)
				Path$ = Mid(inLine$,PathStart,PathEnd-PathStart)
				CloseFile page
				DeleteFile "temp_web_page.txt"
				Return "http://www.dilbert.com"+Path$
			EndIf
		Until Eof(page)
		DeleteFile "temp_web_page.txt"
	EndIf	
End Function

; -----------------------------------------------------------------------------
; LoadWebImage -- uses BlitzGet Deluxe, based on Mark Sibly's HTTPGet
; -----------------------------------------------------------------------------
; james@...
; -----------------------------------------------------------------------------

Function LoadWebImage (webFile$)
	If BlitzGet (webFile$, CurrentDir(), "temp_web_image.bmp")		
		Repeat
			If FileType("temp_web_image.bmp")=1
				;Img = LoadImage ("temp_web_image.bmp")
				ExecFile "temp_web_image.bmp"
				End
				;Return Img
			EndIf		
		Forever
	EndIf
End Function

Function BlitzGet (webFile$, saveDir$, saveFile$,DEBUG=False)

	; -------------------------------------------------------------------------
	; Strip "http://" if provided
	; -------------------------------------------------------------------------
	If Left (webFile$, 7) = "http://" Then webFile$ = Right (webFile$, Len (webFile$) - 7)

	; -------------------------------------------------------------------------
	; Split into hostname and path/filename to download
	; -------------------------------------------------------------------------
	slash = Instr (webFile$, "/")
	If slash
		webHost$ = Left (webFile$, slash - 1)
		webFile$ = Right (webFile$, Len (webFile$) - slash + 1)
	Else
		webHost$ = webFile$
		webFile$ = "/"
	EndIf
		
	; -------------------------------------------------------------------------
	; Add trailing slash to download dir if not given
	; -------------------------------------------------------------------------
	If Right (saveDir$, 1) <> "" Then saveDir$ = saveDir$ + ""

	; -------------------------------------------------------------------------
	; Save filename -- get from webFile$ if not provided
	; -------------------------------------------------------------------------
	If saveFile$ = ""
		If webFile = "/"
			saveFile$ = "Unknown file.txt"
		Else
			For findSlash = Len (webFile$) To 1 Step - 1
				testForSlash$ = Mid (webFile$, findSlash, 1)
				If testForSlash$ = "/"
					saveFile$ = Right (webFile$, Len (webFile$) - findSlash)
					Exit
				EndIf
			Next
			If saveFile$ = "" Then saveFile$ = "Unknown file.txt"
		EndIf
	EndIf

	; DEBUG
	If DEBUG Then RuntimeError "Web host: " + webHost$ + Chr (10) + "Web file: " + webFile$ + Chr (10) + "Save dir: " + saveDir$ + Chr (10) + "Save file: " + saveFile$

	www = OpenTCPStream (webHost$, 80)
	If www
	
		WriteLine www, "GET " + webFile$ + " HTTP/1.1" ; GET / gets default page...
		WriteLine www, "Host: " + webHost$
		WriteLine www, "User-Agent: BlitzGet Deluxe"
		WriteLine www, "Accept: */*"
		WriteLine www, ""
		
		; ---------------------------------------------------------------------
		; Find blank line after header data, where the action begins...
		; ---------------------------------------------------------------------
				
		Repeat
			header$ = ReadLine (www)
			If Left (header$, 16) = "Content-Length: "	; Number of bytes to read
				bytesToRead = Right (header$, Len (header$) - 16)
			EndIf
		Until header$ = "" Or (Eof (www))
		
		If bytesToRead = 0 Then Goto skipDownLoad
		
		; ---------------------------------------------------------------------
		; Create new file to write downloaded bytes into
		; ---------------------------------------------------------------------
		save = WriteFile (saveDir$ + saveFile$)
		If Not save Then Goto skipDownload

		; ---------------------------------------------------------------------
		; Incredibly complex download-to-file routine...
		; ---------------------------------------------------------------------
				
		For readWebFile = 1 To bytesToRead
		
			If Not Eof (www) Then WriteByte save, ReadByte (www)
			
			; Call BytesReceived with position and size every 100 bytes (slows down a LOT with smaller updates)
			
			;tReadWebFile = readWebFile			
			;If tReadWebFile Mod 1000 = 0 Then BytesReceived (readWebFile, bytesToRead)

		Next

		CloseFile save
		
		; Fully downloaded?
		If (readWebFile - 1) = bytesToRead
			success = 1
		EndIf
		
		; Final update (so it's not rounded to nearest 100 bytes!)
		;BytesReceived (bytesToRead, bytesToRead)

		.skipDownload
		CloseTCPStream www
		
	Else
	
		RuntimeError "Failed to connect"
		
	EndIf
	
	Return success
	
End Function

; -----------------------------------------------------------------------------
; User-defined update function, called every 100 bytes of download -- alter to suit!
; -----------------------------------------------------------------------------
; TIP: Pass a user-defined type instead, with all data (this stuff plus URL, local filename, etc)
; -----------------------------------------------------------------------------
Function BytesReceived (posByte, totalBytes)
	; Example update code...
	Cls
	Text 20, 20, "Downloading file -- please wait..."
	Text 20, 40, "Received: " + posByte + "/" + totalBytes + " bytes (" + Percent (posByte, totalBytes) + "%)"
	Flip
End Function

; -----------------------------------------------------------------------------
; Handy percentage function
; -----------------------------------------------------------------------------
Function Percent (part#, total#)
	Return Int (100 * (part / total))
End Function