Wednesday, June 22, 2011

Yelp API Business Review Searches

Going along with the whole DataContractSerializer thing in .NET I thought I would share the class I use to deserialize business review searches with the Yelp API. As an added bonus, the Yelp API doesn't return any URL for a business so that you can easily navigate to the page to write a review for that business. It's easy to figure out though. The review URL uh, key, for a lack of better terms is just the business ID.

So here it goes, some VB.NET code to get businesses that match some common terms within a 1 mile(?) radius of a given latitude and longitude.



Public Sub getNearbyLocations(ByVal myLatitude As Long, ByVal myLongitude As Long)
If gps_allowed = True Then
myThrobber.Visibility = Visibility.Visible
myWebClient = New WebClient()
myWebClient.OpenReadAsync(New Uri("http://api.yelp.com/business_review_search?term=food%20bars%20pizza%20bar%20diner%20bar&lat=" & myLatitude & "&long=" & myLongitude & "&radius=1&limit=20&ywsid=YOURYELPAPIKEY"))
AddHandler myWebClient.OpenReadCompleted, AddressOf gotJson
Else
MessageBox.Show("You need to allow this application to use the GPS in the settings panel to use this function!")
End If
End Sub

Private Sub gotJson(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs)
For Each businessObject As yelpBusiness In placesMainPanel.Children
businessObject = Nothing
Next

placesMainPanel.Children.Clear()

Dim sr As New StreamReader(e.Result)
jsonString = sr.ReadToEnd
Dim jsonThing As New jsonStuff(jsonString)
For Each returnedBusiness As yelpResponse.business In jsonThing.myYelpResponse.businesses
Dim myNewBusiness As New yelpBusiness
myNewBusiness.lblBusinessName.Text = returnedBusiness.name
myNewBusiness.lblAddress1.Text = returnedBusiness.address1
myNewBusiness.lblAddress2.Text = returnedBusiness.city & ", " & returnedBusiness.state_code & " " & returnedBusiness.zip
myNewBusiness.lblReviewCount.Text = returnedBusiness.review_count & " Reviews"
myNewBusiness.imgRatingStars.Source = New BitmapImage(New Uri(returnedBusiness.rating_img_url))
myNewBusiness.imgPlacePhoto.Source = New BitmapImage(New Uri(returnedBusiness.photo_url))
myNewBusiness.lblReadReviews.NavigateUri = New Uri(returnedBusiness.url, UriKind.Absolute)
myNewBusiness.lblReadReviews.TargetName = "_blank"
myNewBusiness.btnLeaveReview.NavigateUri = New Uri("http://www.yelp.com/writeareview/biz/" & returnedBusiness.id)
myNewBusiness.btnLeaveReview.TargetName = "_blank"
placesMainPanel.Children.Add(myNewBusiness)
Next

myThrobber.Visibility = Visibility.Collapsed
End Sub




Imports System.Runtime.Serialization.Json
Imports System.Runtime.Serialization

<DataContract()> _
Partial Public Class yelpResponse

<DataMember()> _
Public Property message As yelpMessage

<DataMember()> _
Public Property businesses As List(Of business)

Public Class yelpMessage

Public Property text As String
Public Property code As Integer
Public Property version As String
End Class

Public Class business
Public Property rating_img_url As String
Public Property country_code As String
Public Property id As String
Public Property is_closed As Boolean
Public Property city As String
Public Property mobile_url As String
Public Property review_count As Integer
Public Property zip As String
Public Property state As String
Public Property latitude As Double
Public Property rating_img_url_small As String
Public Property address1 As String
Public Property address2 As String
Public Property address3 As String
Public Property phone As String
Public Property state_code As String
Public Property categories As List(Of category)
Public Property photo_url As String
Public Property distance As Double
Public Property name As String
Public Property neighborhoods As List(Of neighborhood)
Public Property url As String
Public Property country As String
Public Property avg_rating As Double
Public Property longitude As Double
Public Property nearby_url As String
Public Property reviews As List(Of review)
Public Property photo_url_small As String

End Class

Public Class category
Public Property category_filter As String
Public Property search_url As String
Public Property name As String

End Class

Public Class neighborhood
Public Property url As String
Public Property name As String


End Class

Public Class review
Public Property rating_img_url_small As String
Public Property user_photo_url_small As String
Public Property rating_img_url As String
Public Property rating As Integer
Public Property mobile_uri As String
Public Property url As String
Public Property user_url As String
Public Property text_excerpt As String
Public Property user_photo_url As String
Public Property date1 As String
Public Property user_name As String
Public Property id As String

End Class
End Class




1 comment: