Skip to content

Instantly share code, notes, and snippets.

@milesgrimshaw
Created March 16, 2015 23:07
Show Gist options
  • Save milesgrimshaw/eaae8b0691394f644f9b to your computer and use it in GitHub Desktop.
Save milesgrimshaw/eaae8b0691394f644f9b to your computer and use it in GitHub Desktop.
Script to collect data from CityGridMedia's Places API
require 'rubygems'
require 'nokogiri'
require 'pp'
require 'mechanize'
require 'open-uri'
require 'csv'
key = "## YOUR KEY HERE"
cities = [
"New York, NY Metro", "Los Angeles, CA Metro", "Chicago, IL Metro", "Houston, TX Metro", "Philadelphia, PA Metro", "Phoenix, AZ Metro",
"San Antonio, TX Metro", "San Diego, CA Metro", "Dallas, TX Metro", "San Jose, CA Metro", "Austin, TX Metro", "Jacksonville, FL Metro",
"Indianapolis, IN Metro", "San Francisco, CA Metro", "Columbus, OH Metro", "Fort Worth, TX Metro", "Charlotte, NC Metro",
"Detroit, MI Metro", "El Paso, TX Metro", "Memphis, TN Metro", "Boston, MA Metro", "Seattle, WA Metro", "Denver, CO Metro", "Washington, DC Metro",
"Nashville, TN Metro", "Baltimore, MD Metro", "Louisville, KY Metro", "Portland, OR Metro", "Oklahoma City, OK Metro", "Milwaukee, WI Metro",
"Las Vegas, NV Metro", "Albuquerque, NM Metro", "Tucson, AZ Metro", "Fresno, CA Metro", "Sacramento, CA Metro", "Long Beach, CA",
"Kansas City, MO Metro", "Mesa, AZ", "Virginia Beach, VA Metro", "Atlanta, GA Metro", "Colorado Springs, CO Metro", "Raleigh, NC Metro",
"Omaha, NE Metro", "Miami, FL Metro", "Oakland, CA", "Tulsa, OK Metro", "Minneapolis, MN", "Cleveland, OH Metro", "Wichita, KS",
"Arlington, TX", "New Orleans, LA Metro", "Bakersfield, CA", "Tampa, FL", "Honolulu, HI", "Anaheim, CA",
"Aurora, CO", "Santa Ana, CA", "St. Louis, MI", "Riverside, CA Metro", "Corpus Christi, TX Metro", "Pittsburgh, PA Metro",
"Lexington, KY Metro", "Anchorage, AK Metro", "Stockton, CA Metro", "Cincinnati, OH Metro", "Saint Paul, MN", "Toledo, OH Metro",
"Newark, NJ Metro", "Greensboro, NC Metro", "Plano, TX", "Henderson, NV", "Lincoln, NE Metro"
]
tags = [
"4168", "3476", "6214", "3995", "12550", "1184", "12606", "6235", "12603", "3462", "3463", "19560", "12604", "956", "10983", "10984", "953", "973", "360",
"524", "604", "608", "669", "690", "355", "357", "920", "972", "979", "1172", "1791", "1825", "1933", "3564", "3617", "3624", "3991", "4082"
]
categories = [
"Sports & Recreation Instruction", "Spas", "Day Spas", "Fitness", "Children's Fitness", "Fitness Activities", "Yoga", "Yoga Studios",
"Pilates", "Beauty", "Beauty Salons", "Toning Salon",
"Spinning", "Music Schools", "Adult Music", "Children's Music Lessons", "Foreign Langues Lessons", "Test Prep", "Business Consultants",
"Cable & Satellite TV", "Youth Organizations", "Counseling", "Vocational Rehabilitation", "Computer Repair", "Building Maintenance",
"Interior Cleaning", "Adult and Continuing Education", "Tutoring & Test Preparation", "Vocational Schools", "Recreational Activities",
"Alternative Medicine", "Dentists", "Appliance Repair", "Repair Shops", "Pet Grooming", "Pet Trainers", "Dance Studio", "Rifle & Pistol Ranges",
]
CSV.open("citygrid.csv", "wb") do |csv|
row = Array.new
row << "City"
for y in 0..(categories.length-1)
row << categories[y]
end
csv << row
for i in 0..(cities.length-1)
city = cities[i]
row = Array.new
row << city
for t in 0..(tags.length-1)
tag = tags[t]
url = "http://api.citygridmedia.com/content/places/v2/search/where?tag=#{tag}&where=#{city}&publisher=10000005442"
uri = URI.parse(URI.encode(url.strip))
pp uri
page = Nokogiri::XML(open(uri))
count = page.xpath("//results").attr('total_hits').value()
pp count
row << count
end
csv << row
pp row
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment