Imports System.Net
Imports System.IO
Imports System.Threading
Imports System.Web.Script.Serialization 'Reference needed!
Class FacebookAPI
Private URL As String = "http://graph.facebook.com/"
Public Event Completed(ByVal sender As Object, ByVal e As FacebookEventArgs)
Public Event [Error](ByVal sender As Object, ByVal ex As Exception)
Public Sub FetchData(ByVal ID As String)
Try
Using WC As New WebClient
WC.DownloadStringAsync(New Uri(URL & ID & "?fields=id,username,name,first_name,last_name,gender,picture,cover"))
AddHandler WC.DownloadStringCompleted, AddressOf DeserializeData
End Using
Catch ex As Exception
RaiseEvent Error(Me, ex)
End Try
End Sub
Private Sub DeserializeData(sender As Object, e As DownloadStringCompletedEventArgs)
Try
If e.Cancelled Then Return
If e.Error IsNot Nothing Then Return
Dim t As New Thread(Sub()
Dim args As New FacebookEventArgs
For Each node As KeyValuePair(Of String, Object) In New JavaScriptSerializer().Deserialize(Of Dictionary(Of String, Object))(e.Result)
Dim str As String = node.Value.ToString
Select Case node.Key
Case "id" : args.ID = str
Case "username" : args.Username = str
Case "name" : args.Name = str
Case "first_name" : args.FirstName = str
Case "last_name" : args.LastName = str
Case "gender" : args.Gender = str
Case "picture"
Dim picJSON As Object = Nothing
Dim picURL As Object = Nothing
If DirectCast(node.Value, Dictionary(Of String, Object)).TryGetValue("data", picJSON) Then
If DirectCast(picJSON, Dictionary(Of String, Object)).TryGetValue("url", picURL) Then
args.PictureURL = picURL.ToString
args.Picture = ConvertImage(picURL.ToString)
End If
End If
Case "cover"
Dim coverDict As Dictionary(Of String, Object) = DirectCast(node.Value, Dictionary(Of String, Object))
If coverDict.ContainsKey("offset_y") Then args.CoverOffset = Integer.Parse(coverDict("offset_y").ToString)
If coverDict.ContainsKey("source") Then
args.CoverURL = coverDict("source").ToString
args.Cover = ConvertImage("https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/" & args.CoverURL.Substring(55, args.CoverURL.Length - 55).ToString)
End If
End Select
Next
RaiseEvent Completed(Me, args)
End Sub)
t.Start()
Catch ex As Exception
RaiseEvent Error(Me, ex)
End Try
End Sub
Private Function ConvertImage(ByVal URL As String) As Image
Try
Using WC As New WebClient()
Using MS As New MemoryStream(WC.DownloadData(URL))
Using B As New Bitmap(MS)
Return B
End Using
End Using
End Using
Catch ex As Exception
RaiseEvent Error(Me, ex)
Return Nothing
End Try
End Function
Public Class FacebookEventArgs
Inherits EventArgs
Public ID As String
Public Username As String
Public Name As String
Public FirstName As String
Public LastName As String
Public Gender As String
Public PictureURL As String
Public CoverURL As String
Public CoverOffset As Integer
Public Picture As Image
Public Cover As Image
End Class
End Class