Skip to content

Example Use Case: Find Cafes

dirtyvagabond edited this page May 31, 2012 · 2 revisions

Let's create a function that finds cafes near a lat/lng:

(defn nearby-cafes
  "Returns up to 12 cafes within 5000 meters of the specified location."
  [lat lon]
  (fact/fetch {:table :places
                      :q "cafe"
                      :filters {:category {:$eq "Food & Beverage"}}
                      :geo {:$circle {:$center [lat lon]
                                      :$meters 5000}}
                      :include_count true
                      :limit 12}))

Using our function to get some cafes:

(def cafes (nearby-cafes 34.06018 -118.41835))

Let's peek at the metadata:

(meta cafes)
{:total_row_count 26, :included_rows 12, :version 3, :status "ok"}

We got back the full limit of 12 results, and we can see there's a total of 26 cafes near us. Let's take a look at a few of the cafes we got back:

(map :name (take 3 cafes))
("Aroma Cafe" "Cafe Connection" "Panini Cafe")

That first one, "Aroma Cafe", sounds interesting. Let's see the details:

(clojure.contrib.pprint/pprint (first cafes))

{:status "1",
 :country "US",
 :longitude -118.423421,
 :factual_id "eb67e10b-b103-41be-8bb5-e077855b7ae7",
 :name "Aroma Cafe",
 :postcode "90064",
 :locality "Los Angeles",
 :latitude 34.039792,
 :region "CA",
 :address "2530 Overland Ave",
 :website "http://aromacafe-la.com/",
 :tel "(310) 836-2919",
 :category "Food & Beverage"}

Now let's use Crosswalk to find out what Yelp has to say about this place. Note that we use Aroma Cafe's :factual_id from the above results...

(fact/crosswalk :factual_id "eb67e10b-b103-41be-8bb5-e077855b7ae7" :only "yelp")
  ({:factual_id "eb67e10b-b103-41be-8bb5-e077855b7ae7",
    :namespace :yelp,
    :namespace_id "AmtMwS2wCbr3l-_S0d9AoQ",
    :url "http://www.yelp.com/biz/aroma-cafe-los-angeles"})

That gives me the yelp URL for the Aroma Cafe, so I can read up on it on Yelp.com.

Of course, Factual supports other Crosswalked sources besides Yelp. If you look at each row returned by the crosswalk function, you'll see there's a :namespace in each one. Let's find out what namespaces are available for the Aroma Cafe:

(map :namespace (fact/crosswalk :factual_id "eb67e10b-b103-41be-8bb5-e077855b7ae7"))
  (:merchantcircle :urbanspoon :yahoolocal :foursquare :yelp ... )

Let's create a function that takes a :factual_id and returns a hashmap of each valid namespace associated with its Crosswalk URL:

(defn namespaces->urls [factid]
    (into {} (map #(do {(:namespace %) (:url %)})
      (fact/crosswalk :factual_id factid))))

Now we can do this:

    > (namespaces->urls "eb67e10b-b103-41be-8bb5-e077855b7ae7")
    {:merchantcircle   "http://www.merchantcircle.com/business/Aroma.Cafe.310-836-2919",
     :urbanspoon       "http://www.urbanspoon.com/r/5/60984/restaurant/West-Los-Angeles/Bali-Place-LA",
     :yahoolocal       "http://local.yahoo.com/info-20400708-aroma-cafe-los-angeles",
     :foursquare       "https://foursquare.com/venue/46f53d65f964a520f04a1fe3",
     :yelp             "http://www.yelp.com/biz/aroma-cafe-los-angeles",
     ... }
Clone this wiki locally