Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parsing Microsoft MVP Page and Uploading Photo to Sky Biometry
#r "../packages/FSharp.Data.2.0.15/lib/net40/FSharp.Data.dll"
open System
open System.IO
open System.Net
open System.Text
open FSharp.Data
open System.Threading
open System.Text.RegularExpressions
//private/publicprofile/photo?mvpid=4034937">
//http://mvp.microsoft.com/en-us/search-mvp.aspx?lo=United+States&sl=0&browse=False&sc=s&ps=36&pn=1
let getPageContents(pageNumber:int) =
let uri = new Uri("http://mvp.microsoft.com/en-us/search-mvp.aspx?lo=United+States&sl=0&browse=False&sc=s&ps=36&pn=" + pageNumber.ToString())
let request = WebRequest.Create(uri)
request.Method <- "GET"
let response = request.GetResponse()
let stream = response.GetResponseStream()
let reader = new StreamReader(stream)
reader.ReadToEnd()
//http://cs.hubfs.net/topic/None/58296
let getMVPIdsFromPageContents(pageContents:string) =
let pattern = "mvpid=\d+"
let matchCollection = Regex.Matches(pageContents, pattern)
matchCollection
|> Seq.cast
|> Seq.map(fun (m:Match) -> m.Value)
|> Seq.map(fun s -> s.Split('='))
|> Seq.map(fun a -> a.[1])
let getGetMVPIds(pageNumber: int) =
let pageContents = getPageContents(pageNumber)
getMVPIdsFromPageContents pageContents
let pageList = [1..17]
let mvpIds = pageList
|>Seq.collect(fun i -> getGetMVPIds(i))
let getMvpImageUri(mvpId: int) =
new Uri("http://mvp.microsoft.com/private/en-us/PublicProfile/Photo/" + mvpId.ToString())
//let getMvpImage(mvpId: int) =
// let uri = getMvpImageUri(mvpId)
// let request = WebRequest.Create(uri)
// request.Method <- "GET"
// let response = request.GetResponse()
// let stream = response.GetResponseStream()
// Image.FromStream(stream)
let skyBiometryUri = "http://api.skybiometry.com"
let skyBiometryApiKey = "12345"
let skyBiometryApiSecret = "12345"
type skybiometryFaceDetection = JsonProvider<".\SkyBiometryImageJson\FaceDetection.json">
type skybiometryAddTags = JsonProvider<".\SkyBiometryImageJson\AddTags.json">
type skybiometryFaceTraining = JsonProvider<".\SkyBiometryImageJson\FaceTraining.json">
let detectFace (imageUri:string) =
let stringBuilder = new StringBuilder()
stringBuilder.Append(skyBiometryUri) |> ignore
stringBuilder.Append("/fc/faces/detect.json?urls=") |> ignore
stringBuilder.Append(imageUri) |> ignore
stringBuilder.Append("&api_key=") |> ignore
stringBuilder.Append(skyBiometryApiKey) |> ignore
stringBuilder.Append("&api_secret=") |> ignore
stringBuilder.Append(skyBiometryApiSecret) |> ignore
try
let faceDetection = skybiometryFaceDetection.Load(stringBuilder.ToString())
if faceDetection.Photos.[0].Tags.Length > 0 then
Some faceDetection.Photos.[0].Tags.[0].Tid
else
None
with | :? System.Exception -> None
let saveTag(uid:string, tid:string)=
let stringBuilder = new StringBuilder()
stringBuilder.Append(skyBiometryUri) |> ignore
stringBuilder.Append("/fc/tags/save.json?uid=") |> ignore
stringBuilder.Append(uid) |> ignore
stringBuilder.Append("&tids=") |> ignore
stringBuilder.Append(tid) |> ignore
stringBuilder.Append("&api_key=") |> ignore
stringBuilder.Append(skyBiometryApiKey) |> ignore
stringBuilder.Append("&api_secret=") |> ignore
stringBuilder.Append(skyBiometryApiSecret) |> ignore
let tags = skybiometryAddTags.Load(stringBuilder.ToString())
tags.Status
let trainFace(uid:string)=
let stringBuilder = new StringBuilder()
stringBuilder.Append(skyBiometryUri) |> ignore
stringBuilder.Append("/fc/faces/train.json?uids=") |> ignore
stringBuilder.Append(uid) |> ignore
stringBuilder.Append("&api_key=") |> ignore
stringBuilder.Append(skyBiometryApiKey) |> ignore
stringBuilder.Append("&api_secret=") |> ignore
stringBuilder.Append(skyBiometryApiSecret) |> ignore
let training = skybiometryFaceTraining.Load(stringBuilder.ToString())
training.Status
let saveToSkyBiometry(mvpId:string, imageUri:string) =
let tid = detectFace(imageUri)
match tid with
| Some x -> saveTag(mvpId + "@terminatorChicken",x) |> ignore
trainFace(mvpId + "@terminatorChicken")
| None -> "Failure"
let results = mvpIds
|> Seq.map(fun mvpId -> mvpId, getMvpImageUri(Int32.Parse(mvpId)))
for (mvpId,uri) in results do
let result= saveToSkyBiometry(mvpId, uri.ToString())
printfn "%s" result
Thread.Sleep(TimeSpan.FromMinutes(1.))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment