Strongbad Email Collector

Miscellaneous Forums/Blitz Showcase/Strongbad Email Collector

Hi all,

I present to you, in one Large-messy-code production:

Strong Bad Email Collector. Does what it says in the box, and (hopefully) stays in the background and lets you know when a new one comes out :P

Now, I wrote it in B3D, then got a hand converting it to Blitz+. Since I don't Own Blitz+, i cant help with any B+ related issues with the source.

Lemme know if there are any problems!

-------------------------
BLITZ+ Code
-------------------------
Global http$="http:"+Chr$(47)+Chr$(47)
AppTitle "New StrongBad Email"
Global wc
TCPTimeouts 2000,2000
Global website$="www.homestarrunner.com"
Global bnksize=8*1024
Global bank1=CreateBank(bnksize)
updated$=""
Global minfilesize=1024*1
Global  downloadroot$="emails"
If FileType("emails")<>2 Then CreateDir("emails")
If FileType("getsbad.ini")=0 Then fs=WriteFile("getsbad.ini"):WriteLine fs,"1":CloseFile fs
fs=ReadFile("getsbad.ini")
counter=ReadLine(fs)
CloseFile fs

For oo=1 To 1024
file$="sbemail"+Str$(oo)+".swf"
If FileType(downloadroot$+file$)=1 Then
If FileSize(downloadroot$+file$)<minfilesize Then DeleteFile downloadroot$+file$
EndIf
Next

.again
For oo=counter To 1024
counter=oo
gfile$="sbemail"+Str$(oo)+".html"
file$=getfilename(gfile$)
If file$="404error.swf" Then file$=""
If file$<>"" Then
OUTPUT "found filename; "+file$
If FileType(downloadroot$+file$)=1 Then DeleteFile downloadroot$+file$
If FileType(downloadroot$+file$)=0 Then

tcp = HttpGet(website$,"/"+file$)
If tcp = False RuntimeError "unable to connect to address"
OUTPUT "Getting "+file$
fs=WriteFile(downloadroot$+file$)
found=0
Repeat
n$=ReadLine$(tcp)
If Left$(n$,16)="Content-Length: " Then size=Mid$(n$,17)
OUTPUT n$
If Len(n$)<3 Then found=1
wait:Until Eof(tcp)=True Or found=1 Or KeyDown(1)
If found=0 Then OUTPUT "not found":CloseFile fs:DeleteFile downloadroot$+file$
If KeyDown(1) Then found=0
If found=0 Or size=0 Then Goto Endbit
count=0
done=0


Repeat
ms=MilliSecs():Repeat:av=ReadAvail(tcp):wait:Until av>0 Or (MilliSecs()-ms)>1000;:av=av-1
;If av>bnksize Then av=bnksize
;av=1
;If av=0 And MilliSecs()-lc>500 Then done=1;If count>=size-bnksize Then done=1
If count>=size Then done=1
If av<>0 Then

	count=count+av
 	ReadBytes (bank1,tcp,0,av)
	WriteBytes(bank1, fs,0,av)
EndIf
If KeyHit(1) Then found=0
wait:Until done=1 Or Eof(tcp) Or KeyDown(1)
If KeyDown(1) Then found=0
.Endbit

OUTPUT "Ending"
CloseFile fs
If FileSize(downloadroot$+file$)>minfilesize And found=1 Then fcnt=0:counter=counter+1:If updated="" Then updated=file$
If FileSize(downloadroot$+file$)<minfilesize Or found=0 Then DeleteFile downloadroot$+file$:fcnt=fcnt+1:OUTPUT "Invalid file"

EndIf

Else fcnt=fcnt+1

EndIf

If fcnt>4 Then oo=99999:
If found=1 And fcnt=0 Then updatecounter(counter)
Next
CloseTCPStream(tcp)


If updated<>"" Then 

result=Confirm("There is a new Strong Bad Email available."+Chr$(13)+Chr$(10)+"Would you like to view it?")
If result=1 Then  ExecFile "iexplore.exe "+CurrentDir$()+downloadroot$+updated


EndIf
For nmt=0 To 1000
Delay 60*60
Next
Goto again



Function getfilename$(webfile$)
Local tcp = HttpGet(website$,"/"+webfile$)
Local found,n$,ret$
Local find$=" <PARAM NAME=movie VALUE="+Chr$(34)
If tcp = False RuntimeError "unable To connect To address"
found=0
ms=MilliSecs()
Repeat
n$=ReadLine$(tcp)
If Instr(n$,find$) Then
	ret$=Mid$(n$,Instr(n$,find$)+Len(find$))
	ret$=Mid$(ret$,1,Instr(ret$,Chr$(34))-1)
	found=1
EndIf
If Left$(n$,16)="Content-Length: " Then size=Mid$(n$,17)
wait:Until Eof(tcp)=True Or found=1 Or KeyDown(1) Or (MilliSecs()-ms)>1000
If KeyDown(1) Then End
Return ret$
End Function

;example of using a proxy server
tcp = HttpGet("www.blitzbasic.com","/Home/_index_.php",80,"planet1.scs.cs.nyu.edu",3128)
If tcp = False RuntimeError "unable to connect to address"
While Eof(tcp) = False
If KeyDown(1) Then End
	DebugLog ReadLine$(tcp)
Wend
CloseTCPStream(tcp)


;function
Function HttpGet(server$,path$,port=80,proxy$="",proxyport=0)
	Local www
	If Len(proxy$) = 0 proxy$ = server$
	If proxyport = 0 proxyport = port
	www = OpenTCPStream(proxy$,proxyport)
	If www = False Return False
	WriteLine www,"GET "+http$+ server$ + ":" + port + path$ + " HTTP/1.1" + Chr$(13)+Chr$(10) + "Host: " + server$ + Chr$(13)+Chr$(10) + "User-Agent: blitzbasic" + Chr$(13)+Chr$(10) + "Accept: */*" + Chr$(13)+Chr$(10)
	Return www
	CloseTCPStream(tcp)
End Function
Function wait()
wc=(wc+1) Mod 20:If wc=0 Then Delay 2
End Function
Function OUTPUT(N$)
DebugLog n$
End Function

Function updatecounter(counter)
Local fs=WriteFile("getsbad.ini")
WriteLine fs,Str$(counter)
CloseFile fs
End Function


--------------------
Blitz2D/3D code
--------------------
Global http$="http:"+Chr$(47)+Chr$(47)
AppTitle "New StrongBad Email"
Global wc
TCPTimeouts 2000,2000
Global website$="www.homestarrunner.com"
Global bnksize=8*1024
Global bank1=CreateBank(bnksize)
updated$=""
Global minfilesize=1024*1
Global  downloadroot$="emails"
If FileType("emails")<>2 Then CreateDir("emails")
If FileType("getsbad.ini")=0 Then fs=WriteFile("getsbad.ini"):WriteLine fs,"1":CloseFile fs
fs=ReadFile("getsbad.ini")
counter=ReadLine(fs)
CloseFile fs

For oo=1 To 1024
file$="sbemail"+Str$(oo)+".swf"
If FileType(downloadroot$+file$)=1 Then
If FileSize(downloadroot$+file$)<minfilesize Then DeleteFile downloadroot$+file$
EndIf
Next

.again
For oo=counter To 1024
counter=oo
gfile$="sbemail"+Str$(oo)+".html"
file$=getfilename(gfile$)
If file$="404error.swf" Then file$=""
If file$<>"" Then
OUTPUT "found filename; "+file$
If FileType(downloadroot$+file$)=1 Then DeleteFile downloadroot$+file$
If FileType(downloadroot$+file$)=0 Then

tcp = HttpGet(website$,"/"+file$)
If tcp = False RuntimeError "unable to connect to address"
OUTPUT "Getting "+file$
fs=WriteFile(downloadroot$+file$)
found=0
Repeat
n$=ReadLine$(tcp)
If Left$(n$,16)="Content-Length: " Then size=Mid$(n$,17)
OUTPUT n$
If Len(n$)<3 Then found=1
wait:Until Eof(tcp)=True Or found=1 Or KeyDown(1)
If found=0 Then OUTPUT "not found":CloseFile fs:DeleteFile downloadroot$+file$
If KeyDown(1) Then found=0
If found=0 Or size=0 Then Goto Endbit
count=0
done=0


Repeat
ms=MilliSecs():Repeat:av=ReadAvail(tcp):wait:Until av>0 Or (MilliSecs()-ms)>1000;:av=av-1
;If av>bnksize Then av=bnksize
;av=1
;If av=0 And MilliSecs()-lc>500 Then done=1;If count>=size-bnksize Then done=1
If count>=size Then done=1
If av<>0 Then

	count=count+av
 	ReadBytes (bank1,tcp,0,av)
	WriteBytes(bank1, fs,0,av)
EndIf
If KeyHit(1) Then found=0
wait:Until done=1 Or Eof(tcp) Or KeyDown(1)
If KeyDown(1) Then found=0
.Endbit

OUTPUT "Ending"
CloseFile fs
If FileSize(downloadroot$+file$)>minfilesize And found=1 Then fcnt=0:counter=counter+1:If updated="" Then updated=file$
If FileSize(downloadroot$+file$)<minfilesize Or found=0 Then DeleteFile downloadroot$+file$:fcnt=fcnt+1:OUTPUT "Invalid file"

EndIf

Else fcnt=fcnt+1

EndIf

If fcnt>4 Then oo=99999:
If found=1 And fcnt=0 Then updatecounter(counter)
Next
CloseTCPStream(tcp)


If updated<>"" Then 

result=1;Confirm("There is a new Strong Bad Email available."+Chr$(13)+Chr$(10)+"Would you like to view it?")
If result=1 Then  ExecFile "iexplore.exe "+CurrentDir$()+downloadroot$+updated


EndIf
For nmt=0 To 1000
Delay 60*60
Next
Goto again



Function getfilename$(webfile$)
Local tcp = HttpGet(website$,"/"+webfile$)
Local found,n$,ret$
Local find$=" <PARAM NAME=movie VALUE="+Chr$(34)
If tcp = False RuntimeError "unable To connect To address"
found=0
ms=MilliSecs()
Repeat
n$=ReadLine$(tcp)
If Instr(n$,find$) Then
	ret$=Mid$(n$,Instr(n$,find$)+Len(find$))
	ret$=Mid$(ret$,1,Instr(ret$,Chr$(34))-1)
	found=1
EndIf
If Left$(n$,16)="Content-Length: " Then size=Mid$(n$,17)
wait:Until Eof(tcp)=True Or found=1 Or KeyDown(1) Or (MilliSecs()-ms)>1000
If KeyDown(1) Then End
Return ret$
End Function

;example of using a proxy server
tcp = HttpGet("www.blitzbasic.com","/Home/_index_.php",80,"planet1.scs.cs.nyu.edu",3128)
If tcp = False RuntimeError "unable to connect to address"
While Eof(tcp) = False
If KeyDown(1) Then End
	DebugLog ReadLine$(tcp)
Wend
CloseTCPStream(tcp)


;function
Function HttpGet(server$,path$,port=80,proxy$="",proxyport=0)
	Local www
	If Len(proxy$) = 0 proxy$ = server$
	If proxyport = 0 proxyport = port
	www = OpenTCPStream(proxy$,proxyport)
	If www = False Return False
	WriteLine www,"GET "+http$+ server$ + ":" + port + path$ + " HTTP/1.1" + Chr$(13)+Chr$(10) + "Host: " + server$ + Chr$(13)+Chr$(10) + "User-Agent: blitzbasic" + Chr$(13)+Chr$(10) + "Accept: */*" + Chr$(13)+Chr$(10)
	Return www
	CloseTCPStream(tcp)
End Function
Function wait()
wc=(wc+1) Mod 20:If wc=0 Then Delay 2
End Function
Function OUTPUT(N$)
DebugLog n$
Print n$
End Function

Function updatecounter(counter)
Local fs=WriteFile("getsbad.ini")
WriteLine fs,Str$(counter)
CloseFile fs
End Function


Nice one man. I love strongbad! I'll give it a try later :D

Does what it says in the box,
Apparantly I can't read. What does a strong bad email collector do?

WolRon vist Strongbad Email and enjoy.

Oh, I get it, it's a file reamer for those animations, a bit like my gamebase64 collector.

Now, I wrote it in B3D, then got a hand converting it to Blitz+. Since I don't Own Blitz+...


Not having any experience with B+, am I to understand this is B+ code? As written, is doesn't work in B3D... so I hope you say "Why yes, it is".

If you do say that though, then why - especially since you don't have B+ yourself? Would you like to post the B3D code?

Apart from all that, cool idea - Strongbad rocks. :o)

I liked the idea that B+ could hide the windows so it could hide in the background.

I could go pull out my b3D code if you want :)

Given I don't have B+ and you didn't provide an executable, and I lurve Strongbad's emails...

...yes, that would be chipper, if you'd be so kind. :o)

Given the proportion of B3D:B+ users, I'm surprised you didn't in the first place. Astounded, if you will.

hehe...

Sorry guys, I simply thought itd be cool for it to hide.

I will provide the orignial source when i return home later. :)

[edit]

Um, i edited it in notepad, i think this should work:

Global http$="http:"+Chr$(47)+Chr$(47)
AppTitle "New StrongBad Email"
Global wc
TCPTimeouts 2000,2000
Global website$="www.homestarrunner.com"
Global bnksize=8*1024
Global bank1=CreateBank(bnksize)
updated$=""
Global minfilesize=1024*1
Global  downloadroot$="emails"
If FileType("emails")<>2 Then CreateDir("emails")
If FileType("getsbad.ini")=0 Then fs=WriteFile("getsbad.ini"):WriteLine fs,"1":CloseFile fs
fs=ReadFile("getsbad.ini")
counter=ReadLine(fs)
CloseFile fs

For oo=1 To 1024
file$="sbemail"+Str$(oo)+".swf"
If FileType(downloadroot$+file$)=1 Then
If FileSize(downloadroot$+file$)<minfilesize Then DeleteFile downloadroot$+file$
EndIf
Next

.again
For oo=counter To 1024
counter=oo
gfile$="sbemail"+Str$(oo)+".html"
file$=getfilename(gfile$)
If file$="404error.swf" Then file$=""
If file$<>"" Then
OUTPUT "found filename; "+file$
If FileType(downloadroot$+file$)=1 Then DeleteFile downloadroot$+file$
If FileType(downloadroot$+file$)=0 Then

tcp = HttpGet(website$,"/"+file$)
If tcp = False RuntimeError "unable to connect to address"
OUTPUT "Getting "+file$
fs=WriteFile(downloadroot$+file$)
found=0
Repeat
n$=ReadLine$(tcp)
If Left$(n$,16)="Content-Length: " Then size=Mid$(n$,17)
OUTPUT n$
If Len(n$)<3 Then found=1
wait:Until Eof(tcp)=True Or found=1 Or KeyDown(1)
If found=0 Then OUTPUT "not found":CloseFile fs:DeleteFile downloadroot$+file$
If KeyDown(1) Then found=0
If found=0 Or size=0 Then Goto Endbit
count=0
done=0


Repeat
ms=MilliSecs():Repeat:av=ReadAvail(tcp):wait:Until av>0 Or (MilliSecs()-ms)>1000;:av=av-1
;If av>bnksize Then av=bnksize
;av=1
;If av=0 And MilliSecs()-lc>500 Then done=1;If count>=size-bnksize Then done=1
If count>=size Then done=1
If av<>0 Then

	count=count+av
 	ReadBytes (bank1,tcp,0,av)
	WriteBytes(bank1, fs,0,av)
EndIf
If KeyHit(1) Then found=0
wait:Until done=1 Or Eof(tcp) Or KeyDown(1)
If KeyDown(1) Then found=0
.Endbit

OUTPUT "Ending"
CloseFile fs
If FileSize(downloadroot$+file$)>minfilesize And found=1 Then fcnt=0:counter=counter+1:If updated="" Then updated=file$
If FileSize(downloadroot$+file$)<minfilesize Or found=0 Then DeleteFile downloadroot$+file$:fcnt=fcnt+1:OUTPUT "Invalid file"

EndIf

Else fcnt=fcnt+1

EndIf

If fcnt>4 Then oo=99999:
If found=1 And fcnt=0 Then updatecounter(counter)
Next
CloseTCPStream(tcp)


If updated<>"" Then 

result=1;Confirm("There is a new Strong Bad Email available."+Chr$(13)+Chr$(10)+"Would you like to view it?")
If result=1 Then  ExecFile "iexplore.exe "+CurrentDir$()+downloadroot$+updated


EndIf
For nmt=0 To 1000
Delay 60*60
Next
Goto again



Function getfilename$(webfile$)
Local tcp = HttpGet(website$,"/"+webfile$)
Local found,n$,ret$
Local find$=" <PARAM NAME=movie VALUE="+Chr$(34)
If tcp = False RuntimeError "unable To connect To address"
found=0
ms=MilliSecs()
Repeat
n$=ReadLine$(tcp)
If Instr(n$,find$) Then
	ret$=Mid$(n$,Instr(n$,find$)+Len(find$))
	ret$=Mid$(ret$,1,Instr(ret$,Chr$(34))-1)
	found=1
EndIf
If Left$(n$,16)="Content-Length: " Then size=Mid$(n$,17)
wait:Until Eof(tcp)=True Or found=1 Or KeyDown(1) Or (MilliSecs()-ms)>1000
If KeyDown(1) Then End
Return ret$
End Function

;example of using a proxy server
tcp = HttpGet("www.blitzbasic.com","/Home/_index_.php",80,"planet1.scs.cs.nyu.edu",3128)
If tcp = False RuntimeError "unable to connect to address"
While Eof(tcp) = False
If KeyDown(1) Then End
	DebugLog ReadLine$(tcp)
Wend
CloseTCPStream(tcp)


;function
Function HttpGet(server$,path$,port=80,proxy$="",proxyport=0)
	Local www
	If Len(proxy$) = 0 proxy$ = server$
	If proxyport = 0 proxyport = port
	www = OpenTCPStream(proxy$,proxyport)
	If www = False Return False
	WriteLine www,"GET "+http$+ server$ + ":" + port + path$ + " HTTP/1.1" + Chr$(13)+Chr$(10) + "Host: " + server$ + Chr$(13)+Chr$(10) + "User-Agent: blitzbasic" + Chr$(13)+Chr$(10) + "Accept: */*" + Chr$(13)+Chr$(10)
	Return www
	CloseTCPStream(tcp)
End Function
Function wait()
wc=(wc+1) Mod 20:If wc=0 Then Delay 2
End Function
Function OUTPUT(N$)
DebugLog n$
Print n$
End Function

Function updatecounter(counter)
Local fs=WriteFile("getsbad.ini")
WriteLine fs,Str$(counter)
CloseFile fs
End Function



Can't manage to get it to run past this line:
	WriteLine www,"GET <a href="http://"" target="_blank">"</a> + server$ + ":" + port + path$ + " HTTP/1.1" + Chr$(13)+Chr$(10) + "Host: " + server$ + Chr$(13)+Chr$(10) + "User-Agent: blitzbasic" + Chr$(13)+Chr$(10) + "Accept: */*" + Chr$(13)+Chr$(10)


Bombs out at compile time with "Error: Expecting 'End Function'"

I guess you need to dereference one of the sets of quotes, but I've not needed to do that before. Any clues?

heh... i have no idea.. It certainly works here, so perhaps i @#!*ed up copying it..... Gimme a sec :)

By the way that said C O C K ed.

Hmmm..

The forum tags have converted your "http:///" to a link, Cyg.

I know. Not much i can do about it.

Oh wait......


I just had a brainwave and the above selections of code should now work.
I added "http" to a global string and used Chr$() to add the "//". Hahaha :)

Cha-CHING!

Works a treat. Thankyou for indulging me enough to persist. :o)

And thanks for persisting. I had no idea how badly it worked ^.^ Even had to hack the code to get it to work on the forums... Wierd.

Great! For those HS/SB fans there is also a homestarrunner wiki site, that tracks everything tree-like style, every cartoon by date, etc. It also provides all the direct links. Also I'll be back home from iraq in a couple days. Long year. sucked.