smallbasic で学ぶ twitter

先日の .NET ラボ勉強会で、木沢さんが、smallbasic を紹介していました。

smallbasic

http://msdn.microsoft.com/en-us/devlabs/cc950524.aspx

公式サイト(ver.0.8)からダウンロードすることができます。かつ、このバージョンからメニューなどが日本語で表示されます。

20100426_01

見てわかる通り、非常にきれいな画面です。.NET Framework 3.5を要求しますが、smallbasicの言語のためというより、WPFのために必要なのです。

中身は、変則basicです。ifに対応するのがendifだったり、forに対応するのがendforだったりしますが、まあ、basic です。

クラスや関数などややこしいものはありません。関数はなくてサブルーチンです。サブルーチンなので、引数が扱えません、という感じですが、まぁ、昔のN88Basicを使っていた人は、その頃に気分を戻すということで。

面白いのは、smallbasic で即実行ができるところです。HSPみたいに、さくっと作って動かすってのが可能です。

一応、Kids向けなのですが、squeakのような子供向けという訳ではないようです。インテリセンスが親切なのと、オブジェクトのメソッドが制限されているので、プログラミング初心者にはやりやすそうです。

なつかしいタートルグラフィックスもできます。

というわけで、

Twitter のログを smallbasic で読み込んでみました。

name = "moonmile"
url = "http://twitter.com/" + name
f = Network.DownloadFile( url )
'ファイル読み込み
For i = 1 To 1000
line[i] = File.ReadLine(f,i)
If line[i] = "</html>" Then
max = i
i = 1000
EndIf
EndFor
'ファイルを消す
File.DeleteFile( f )

GraphicsWindow.Width = 300
GraphicsWindow.Height = 400
GraphicsWindow.Title = name
GraphicsWindow.BackgroundColor = "darkgray"
GraphicsWindow.FontSize = 12
GraphicsWindow.Show()

y = 0
For i = 1 To max
l = line[i]
n = Text.GetIndexOf(l,"entry-content")
if n > 0 then
l = Text.GetSubTextToEnd( l, n + 15 )
l = Text.GetSubText( l, 1 , Text.GetLength(l)-7 )

'TextWindow.WriteLine( "- " + l )
txt = l
CutTag()
'GraphicsWindow.DrawText(1,y*20,txt)
GraphicsWindow.BrushColor = "darkslategray"
GraphicsWindow.FillRectangle(5,y*16*6,GraphicsWindow.Width,16*6-5)
GraphicsWindow.BrushColor = "white"
GraphicsWindow.DrawBoundText(5,y*16*6, GraphicsWindow.Width-10,txt )
y = y + 1
EndIf
EndFor

Sub CutTag

nn = 1
While nn > 0
s1 = Text.GetIndexOf(txt,"<")
If s1 > 0 then
s2 = Text.GetIndexOf(txt,">")
If s2 > 0 Then
txt = Text.GetSubText(txt,1,s1-1) + Text.GetSubTextToEnd(txt,s2+1)
EndIf
EndIf
If s1 = 0 or s2 = 0 Then
nn = 0
endif
EndWhile
EndSub

Twitter APIではなくて、httpの直読みw

20100426_02

ああ、そうそう、これってWEBで共有できて、Silverlightで確認できるんですが、このプログラムはローカルファイルを読み込んでいるので動きません。。。う~ん、サンプルとしてはいまいちか。

マウスイベントも取れるので、オセロとか作ると面白そうです。

カテゴリー: 開発 パーマリンク