Tuesday, March 25, 2014

Dilbert Me in F#

This is the Dilbert download tool in F#, just because I like playing with F# and making up reasons to do so.

I switched to WebClient here instead of WebRequest. It makes things a little easier and I clearly should have been using it all along.

I wrote it in Visual Studio 2013 as a F# script file. I selected all then hit 'alt+enter' to run it.

FsLight was what I was led to when googling F# code formatters. It was easy to use and seems to have done a good job.

1
2
3
4
5
6
7
8
let webClient = new System.Net.WebClient()
let url = "http://www.dilbert.com"
let dilbert = webClient.DownloadString(url)
let innerUrl = "http://www.dilbert.com/" + System.Text.RegularExpressions.Regex.Match(dilbert, "src=.*strip.gif").Value.Substring(5)
innerUrl
let innerTempFile = System.IO.Path.GetTempFileName() + ".gif"
webClient.DownloadFile(innerUrl, innerTempFile)
System.Diagnostics.Process.Start(innerTempFile)
Created with FsLight

Monday, March 17, 2014

Dilbert Me 2

The previous powershell script for dilbert broke. It seems the cartoons aren't sequential. This is try #2. 

$url = "http://www.dilbert.com" 
$httpRequest = [Net.WebRequest]::create($url)

$response = $httpRequest.GetResponse()
$stream = $response.GetResponseStream()
$buffer = @()
$binaryReader = New-Object System.IO.BinaryReader($stream)

$tempBuffer = $binaryReader.ReadBytes(1024)
while($tempBuffer.Length -eq 1024) { 
    $buffer += $tempBuffer
    $tempBuffer = $binaryReader.ReadBytes(1024)
}

$dilbert = [System.Text.ASCIIEncoding]::UTF8.GetString($buffer)

$url = "http://www.dilbert.com/" + [System.Text.RegularExpressions.Regex]::Match($dilbert, "src=.*strip.gif").Value.Substring(5)

$url 

$httpRequest = [Net.WebRequest]::create($url)

$response = $httpRequest.GetResponse()
$stream = $response.GetResponseStream()
$buffer = @()
$binaryReader = New-Object System.IO.BinaryReader($stream)

$tempBuffer = $binaryReader.ReadBytes(1024)
while($tempBuffer.Length -eq 1024) { 
    $buffer += $tempBuffer
    $tempBuffer = $binaryReader.ReadBytes(1024)
}

$buffer += $tempBuffer
$tempFile = [System.IO.Path]::GetTempFileName() + ".gif"
[System.IO.File]::WriteAllBytes($tempFile, $buffer)
.$tempFile

Tuesday, March 11, 2014

Dilbert Me!

My RSS reader (feedly) doesn't handle Dilbert well. Probably because they want me to view all the advertising surrounding the daily Dilbert cartoon. So it opens up an external browser and forwards me to Dilbert.com from the RSS reader. This is annoying. This powershell script opens up today's Dilbert and shows just the strip. I should wrap this in it's own RSS feed... eventually

$startDate = New-Object DateTime(1425, 9, 14)
$days = [DateTime]::Now.Subtract($startDate).Days
$hundredThousands = [Math]::Floor( $days / 100000)
$tenThousands = [Math]::Floor( ($days % 100000)/10000)
$thousands = [Math]::Floor( ($days % 10000)/1000)
$hundreds = [Math]::Floor( ($days %1000) /100)
$remainder = [Math]::Floor( ($days %100))
#http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/200000/10000/4000/900/214940/214940.strip.gif
$url = "http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/" + $hundredThousands + "00000/" + $tenThousands + "0000/" + $thousands + "000/" + $hundreds + "00/" + $days + "/" + $days + ".strip.gif"

$httpRequest = [Net.WebRequest]::create($url)

$response = $httpRequest.GetResponse()
$stream = $response.GetResponseStream()
$buffer = @()
$binaryReader = New-Object System.IO.BinaryReader($stream)

$tempBuffer = $binaryReader.ReadBytes(1024)
while($tempBuffer.Length -eq 1024) { 
 $buffer += $tempBuffer
 $tempBuffer = $binaryReader.ReadBytes(1024)
}

$buffer += $tempBuffer
$tempFile = [System.IO.Path]::GetTempFileName() + ".gif"
[System.IO.File]::WriteAllBytes($tempFile, $buffer)
.$tempFile

Wednesday, March 5, 2014

Drawing in a canvas.

I created an html 5 canvas object.
Mouse down to draw in it.

Sadly, this only works if it is the only post viewed.
JSFiddle was very useful in debugging this and getting it to work. I did the development on a PC, and tested the results immediately in a browser on a tablet and on my phone.